git
› Pro Git
› Atlassian Git Tutorial
› Pro Git 简体中文翻译
› GitX
click
V2EX  ›  git

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

  •  
  •   click · Oct 5, 2014 · 19637 views
    This topic created in 4433 days ago, the information mentioned may be changed or developed.
    Git checkout 6d5d216出现了一个新分支:
    (detached from 6d5d216)

    不是恢复到之前的状态吗?
    8 replies  •  2014-10-07 11:10:05 +08:00
    messense
    messense
    Oct 5, 2014❤️1
    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
    billlee
    Oct 5, 2014❤️2
    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
    click
    OP
    Oct 5, 2014
    3
    @messense 其实我想主分支恢复到之前的状态(合并之前)
    ceyes
    ceyes
    Oct 5, 2014❤️1
    4
    这相当于开了个“临时分支”, `git branch` 看一下就明白了
    要恢复的话用`git reset <id>` (注:默认是 --soft, 当前的改动还在,想完全回到之前加 --hard)
    jsfaint
    jsfaint
    Oct 5, 2014❤️1
    5
    git checkout hashid是切换workspace到一个已提交的commit
    git checkout -b hashid才是创建一个新分支并checkout
    楼主reset的话,可以
    git reset hashid --hard就会丢掉hashid这个commit之后所有的修改(慎用!)
    zix
    zix
    Oct 6, 2014
    6
    按你的意思,你需要reset,不过慎用--hard就是了(--soft或者--mixed都可以),附一个链接:
    http://marklodato.github.io/visual-git-guide/index-zh-cn.html
    billlee
    billlee
    Oct 6, 2014
    7
    如果想要谨慎一点,可以先 git branch hold, 再做 git reset. 这样会在 master 分支上把 commits 丢弃,但新开的 hold 分支还是原来的样子。确认无误后再 git branch -D hold.
    hww
    hww
    Oct 7, 2014
    8
    git checkout -b "新分支名称"
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 22:27 · PVG 06:27 · LAX 14:27 · JFK 17:27
    ♥ Do have faith in what you're doing.