V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
click
V2EX  ›  git

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

  •  
  •   click · 2014-10-05 12:24:25 +08:00 · 18585 次点击
    这是一个创建于 3492 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Git checkout 6d5d216出现了一个新分支:
    (detached from 6d5d216)

    不是恢复到之前的状态吗?
    8 条回复    2014-10-07 11:10:05 +08:00
    messense
        1
    messense  
       2014-10-05 12:42:46 +08:00   ❤️ 1
    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
        2
    billlee  
       2014-10-05 12:47:54 +08:00   ❤️ 2
    这个状态还不算是一个新分支吧,叫做 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
        3
    click  
    OP
       2014-10-05 14:21:42 +08:00
    @messense 其实我想主分支恢复到之前的状态(合并之前)
    ceyes
        4
    ceyes  
       2014-10-05 14:32:18 +08:00   ❤️ 1
    这相当于开了个“临时分支”, `git branch` 看一下就明白了
    要恢复的话用`git reset <id>` (注:默认是 --soft, 当前的改动还在,想完全回到之前加 --hard)
    jsfaint
        5
    jsfaint  
       2014-10-05 16:48:32 +08:00   ❤️ 1
    git checkout hashid是切换workspace到一个已提交的commit
    git checkout -b hashid才是创建一个新分支并checkout
    楼主reset的话,可以
    git reset hashid --hard就会丢掉hashid这个commit之后所有的修改(慎用!)
    zix
        6
    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
        7
    billlee  
       2014-10-06 20:13:33 +08:00
    如果想要谨慎一点,可以先 git branch hold, 再做 git reset. 这样会在 master 分支上把 commits 丢弃,但新开的 hold 分支还是原来的样子。确认无误后再 git branch -D hold.
    hww
        8
    hww  
       2014-10-07 11:10:05 +08:00
    git checkout -b "新分支名称"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2664 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 04:54 · PVG 12:54 · LAX 21:54 · JFK 00:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.