git checkout的使用

来源:互联网 发布:网络办公管理v3.1系统 编辑:程序博客网 时间:2024/05/31 11:04

HEAD可以理解为“头指针”,表示当前工作区的“基础版本”,当执行提交的时候,Head的指向将作为新提交的父提交。

 $ git branch -v # 查看当前在那个分支

下面的操作就叫做git的检出

$ git checkout 76fbM       README.mdNote: checking out '76fb'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example:  git checkout -b new_branch_nameHEAD is now at 76fb938... add Bye.xtx

分离头指针状态(detached HEAD state)就是说HEAD指向的是一个具体的提交,而不是一个引用(分支)。
此时查看reflog即可发现HEAD由指向master分支变为指向一个一个提交ID

$ git reflog --oneline -176fb938 HEAD@{0}: checkout: moving from master to 76fb

checkout与reset不同,master并没有改变。
下面测试在“断头模式”下的提交

$ git checkout 7945M       README.mdNote: checking out '7945'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example:  git checkout -b new_branch_nameHEAD is now at 79452f4... add  Hi

首先checkout到一个提交,然后新建一个文件,add然后commit。这时我们可以在日志里面看到提交信息。

GS@GS-PC /d/Workspaces/GitHubRepository/GitStudy ((79452f4...))$ git commit -m "commit in detached HEAD"[detached HEAD d4406c9] commit in detached HEAD 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 detachedHEAD.txt

在提交中也会提示你现在处于“断头模式”。这时日志中出现我们刚刚的提交。

GS@GS-PC /d/Workspaces/GitHubRepository/GitStudy ((d4406c9...))$ git log --onelined4406c9 commit in detached HEAD #最新的提交79452f4 add  Hi74d5a3d First Comm2307227 Merge https://github.com/gsh199449/GitStudy43b9c5e First Commite8493e4 First6754302 Initial commit

然后我们checkout到master分支

GS@GS-PC /d/Workspaces/GitHubRepository/GitStudy ((d4406c9...))$ git checkout masterM       README.mdWarning: you are leaving 1 commit behind, not connected toany of your branches: #在这里我们看到这个提交没有在连接到任何分支上面  d4406c9 commit in detached HEADIf you want to keep them by creating a new branch, this may be a good timeto do so with: git branch new_branch_name d4406c9Switched to branch 'master'Your branch is behind 'GitStudy/master' by 6 commits, and can be fast-forwarded.  (use "git pull" to update your local branch)

再次查看日志我们发现刚才的提交已经没了,而且在工作区中文件也消失了。

GS@GS-PC /d/Workspaces/GitHubRepository/GitStudy (master)$ git log --oneline76fb938 add Bye.xtx79452f4 add  Hi74d5a3d First Comm2307227 Merge https://github.com/gsh199449/GitStudy43b9c5e First Commite8493e4 First6754302 Initial commit

虽然使用show命令还可以查看,但是git不知道什么时间就会把他彻底的从历史中抹去。

$ git show d4406commit d4406c941f7d154e0fe87afc41e3e578d2303733Author: gsh199449 <gsh199449@hotmail.com>Date:   Fri Nov 29 12:37:48 2013 +0800    commit in detached HEADdiff --git a/detachedHEAD.txt b/detachedHEAD.txtnew file mode 100644index 0000000..e69de29

如果要挽救这个提交可以使用merge命令

$ git merge d4406Merge made by the 'recursive' strategy. detachedHEAD.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 detachedHEAD.txt

merge命令就是把某个分支合并到当前分支,这是我们可以看到日志和工作区都出现了那次提交。

$ git log --oneline3f6488e Merge commit 'd4406'd4406c9 commit in detached HEAD76fb938 add Bye.xtx79452f4 add  Hi74d5a3d First Comm2307227 Merge https://github.com/gsh199449/GitStudy43b9c5e First Commite8493e4 First6754302 Initial commit

同时,在76fb上出现了开发分支

$ git log --graph --oneline*   3f6488e Merge commit 'd4406'|\| * d4406c9 commit in detached HEAD* | 76fb938 add Bye.xtx|/* 79452f4 add  Hi

我们看到这一次的提交出现了两个父提交

$ git cat-file -p HEADtree d42b037f806ee5afd2265c94e0fe15f6cb4578c6parent 76fb93819096c6f7c182218eeaeb18b2ad275b48parent d4406c941f7d154e0fe87afc41e3e578d2303733author gsh199449 <gsh199449@hotmail.com> 1385700536 +0800committer gsh199449 <gsh199449@hotmail.com> 1385700536 +0800Merge commit 'd4406'

git checkout -- filename命令可以用暂存区里面的filename文件来覆盖工作区中的的filename文件。

0 0