为啥 Git checkout commit_id 会得到一个新分支?

2014-10-05 12:24:25 +08:00
 click
Git checkout 6d5d216出现了一个新分支:
(detached from 6d5d216)

不是恢复到之前的状态吗?
18595 次点击
所在节点    git
8 条回复
messense
2014-10-05 12:42:46 +08:00
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

不是新分支...你想做的是 git reset commit_id 吧?
billlee
2014-10-05 12:47:54 +08:00
这个状态还不算是一个新分支吧,叫做 detached 状态。如果你这时做了个 commit, 那么这个 commit 就是属于一个新分支的,但如果不给这个新分支命名,然后又 checkout 了其他分支,那么这个新分支的引用计数就变为0了,会被回收。

在新版本的 git 里应该是这样提示的:

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name
click
2014-10-05 14:21:42 +08:00
@messense 其实我想主分支恢复到之前的状态(合并之前)
ceyes
2014-10-05 14:32:18 +08:00
这相当于开了个“临时分支”, `git branch` 看一下就明白了
要恢复的话用`git reset <id>` (注:默认是 --soft, 当前的改动还在,想完全回到之前加 --hard)
jsfaint
2014-10-05 16:48:32 +08:00
git checkout hashid是切换workspace到一个已提交的commit
git checkout -b hashid才是创建一个新分支并checkout
楼主reset的话,可以
git reset hashid --hard就会丢掉hashid这个commit之后所有的修改(慎用!)
zix
2014-10-06 16:04:00 +08:00
按你的意思,你需要reset,不过慎用--hard就是了(--soft或者--mixed都可以),附一个链接:
http://marklodato.github.io/visual-git-guide/index-zh-cn.html
billlee
2014-10-06 20:13:33 +08:00
如果想要谨慎一点,可以先 git branch hold, 再做 git reset. 这样会在 master 分支上把 commits 丢弃,但新开的 hold 分支还是原来的样子。确认无误后再 git branch -D hold.
hww
2014-10-07 11:10:05 +08:00
git checkout -b "新分支名称"

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/137117

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX