1.安装后进行用户设置
$ git config --global user.name "Your Name"
$ git config --global user.email email@examplecom
2.建立仓库并上传文件
git init 初始化一个Git仓库
git add <file> 添加文件到暂存区
git commit -m <注释> 将暂存区文件上传
3.工作区状态
git status 显示工作区状态
git diff 查看修改内容
4.版本管理
git log 查看提交历史
git reset --hard commit_id 根据版本号穿梭版本
git reflog 查看git命令历史,确定要回到未来的哪个版本
5.丢弃修改
git checkout -- file 丢弃工作区的修改
git reset HEAD <file> 将文件从暂存区退回
6.删除文件及恢复
git rm test.txt 删除文件,需要提交
git checkout -- test.txt 如已经上传数据库可一键还原
7.远程仓库
ssh-keygen 获取密钥
git remote add origin <仓库址> 关联远程库
git push -u origin master第一次推送master分支的所有内容
git push origin master 推送最新修改
git clone <仓库地址> 下载远程仓库内容
8.分支
git branch查看分支
git branch <name> 创建分支
git checkout <name>或者git switch <name> 切换分支
git checkout -b <name>或者git switch -c <name> 创建+切换分支
git merge <name> 合并某分支到 当前 分支
git merge --no-ff -m "注释" dev 准备合并dev分支,注意--no-ff参数,表示禁用fast forward(快速合并),能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并
git branch -d <name> 删除分支
git log --graph 分支合并图
git branch -D <name> 强行删除一个没有被合并过的分支
9.避免重复劳动
git stash 储藏工作现场
git stash pop 解封工作现场
git cherry-pick <commit> 复制一个特定的提交到当前分支
10.分支与远程库
git remote 查看远程库的信息
git remote -v 显示更详细的信息
git rebase 把本地未push的分叉提交历史整理成直线
本地新建的分支如果不推送到远程,对其他人就是不可见的;
11.多人协作
-
从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
-
在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
-
建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;
-
从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。
12.标签(如 v1.0)
git tag <tagname> 新建一个标签,默认为HEAD
git tag <tagname> <commit id> 为指定版本添加标签
git tag -a <tagname> -m "blablabla..." 指定标签信息
git tag 查看所有标签
git tag -d <tagname> 删除一个本地标签
- 创建的标签都只存储在本地,不会自动推送到远程
git push origin <tagname> 推送一个本地标签;
命令git push origin --tags 推送全部未推送过的本地标签;
git push origin :refs/tags/<tagname> 删除一个远程标签