-
设置账户
git config –global user.email “ your email” git config –global user.name “your name”
-
提交代码
git add test (test 提交到暂存区的文件) git commit –m “备注” git push origin master 提交到仓库 git pull origin master 从仓库中拉取 git status 显示代码库状态 git log 打印提交信息(更改信息) git diff 对比当前状态和版本库中状态的变化 git reset --hard e44ff7(提交的状态码中的前六位,可以用git log查看) 恢复到之前的状态 git reset –hard HEAD^ 切换到上一个版本 git checkout -- readme.txt 项目已经提交到暂存区,但未commit到仓库里面 这条命令可以将暂存区的文件撤销
-
切换分支
$ git checkout -b dev Switched to a new branch 'dev' ##git checkout命令加上-b参数表示创建并切换,相当于以下两条命令: $ git branch * dev master ##git branch命令会列出所有分支,当前分支前面会标一个*号。
-
dev分支的工作完成,我们就可以切换回master分支:
$ git checkout master Switched to branch 'master' ##现在,我们把dev分支的工作成果合并到master分支上: $ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
-
合并完成后,就可以放心地删除dev分支了:
$ git branch -d dev Deleted branch dev (was b17d20e). 删除后,查看branch,就只剩下master分支了: $ git branch * master
-
总结
查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name>或者git switch <name> 创建+切换分支:git checkout -b <name>或者git switch -c <name> 合并某分支到当前分支:git merge <name> 删除分支:git branch -d <name>
git reflog 查看操作日志
输出如下:
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
git reset --hard 700ca2a 切换到id为700ca2a的版本
-
用带参数的git log可以看分支的合并情况:
$ git log --graph --pretty=oneline --abbrev-commit * 960055d master:add one more |\ | * e15fd6b dev: add conflict * | 4f4424b master:add conflict |/ * 7764ce1 dev:add dev * 700ca2a add master * c72f984 ss * c85f866 add test01
-
最后,删除feature1分支:
$ git branch -d dev 已删除分支 dev(曾为 e15fd6b)
-
git log --graph查看分支
* commit 960055d97f268cab48f2184fc8971499a4c6a46a |\ Merge: 4f4424b e15fd6b | | Author: master <master> | | Date: Fri Sep 6 14:34:43 2019 +0800 | | | | master:add one more | | | * commit e15fd6bb2a0245979f7c926cceeeb3042bf7a4de | | Author: master <master> | | Date: Fri Sep 6 11:53:36 2019 +0800 | | | | dev: add conflict | | * | commit 4f4424b5da43897e0bcc5f474aedc48633646191 |/ Author: master <master> | Date: Fri Sep 6 11:54:51 2019 +0800 | | master:add conflict | * commit 7764ce15d08e9c54341de93e78f776a632e76cb1 | Author: master <master> | Date: Fri Sep 6 11:34:59 2019 +0800 | | dev:add dev | * commit 700ca2a6944f924835738abe522e2d8dcf99486f | Author: master <master> | Date: Thu Sep 5 19:18:17 2019 +0800 | | add master | * commit c72f984d66864d1fc917f0e8aa922cdbda7fd6e2 | Author: slave2 <slave2@root> | Date: Thu Sep 5 17:24:47 2019 +0800 | | ss | * commit c85f866e8bece1fe9bd602f523f00b272d118551 Author: slave2 <slave2@root> Date: Thu Sep 5 17:12:22 2019 +0800 add test01
-
当你需要切换一道分支去修复bug时
git checkout -b issue-01 #提交了这个修改之后,再切换到另一个分支 git checkout master #然后将两个分支合并,将分支issue -01删除 git merge --no-ff -m "merged bug fix 101" issue-01 #输出: Merge made by the 'recursive' strategy. test01 | 1 + 1 file changed, 1 insertion(+)
-
Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:
$ git stash Saved working directory and index state WIP on master: 960055d master:add one more HEAD 现在位于 960055d master:add one more
#当你从别的分支再切换到这个分支,并切想将隐藏的工作现场恢复
$ git status
On branch dev
nothing to commit, working tree clean
#用git stash list命令查看工作区:
$ git stash list
stash@{0}: WIP on master: 960055d master:add one more -
恢复工作区有两种方式:
一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;
另一种方式是用git stash pop,恢复的同时把stash内容也删了
13.要查看远程库的信息,用git remote:
$ git remote
origin
或者,用git remote -v显示更详细的信息:
$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)