If you hadn't made any commit yet, only (1: branch) and (3: checkout) would be enough.
(如果你还没有做出任何提交,只有(1:branch)和(3:checkout)就足够了。)
Or, in one command: git checkout -b newBranch
(或者,在一个命令中: git checkout -b newBranch
)
As mentioned in the git reset
man page :
(正如git reset
手册页中所述 :)
$ git branch topic/wip # (1)
$ git reset --hard HEAD~3 # (2) NOTE: use $git reset --soft HEAD~3 (explanation below)
$ git checkout topic/wip # (3)
- You have made some commits, but realize they were premature to be in the "
master
" branch. (你做了一些提交,但意识到他们还不成熟,不能进入“ master
”分支。)
You want to continue polishing them in a topic branch, so create " topic/wip
" branch off of the current HEAD
. (您想在主题分支中继续抛光它们,因此从当前HEAD
创建“ topic/wip
”分支。)
- Rewind the
master
branch to get rid of those three commits. (倒回master
分支以摆脱这三个提交。)
- Switch to "
topic/wip
" branch and keep working. (切换到“ topic/wip
”分支并继续工作。)
Note: due to the "destructive" effect of a git reset --hard
command (it does resets the index and working tree. Any changes to tracked files in the working tree since <commit>
are discarded), I would rather go with:
(注意:由于git reset --hard
命令的“破坏性”效果(它确实会重置索引和工作树。由于<commit>
被丢弃而对工作树中跟踪文件的任何更改),我宁愿选择:)
$ git reset --soft HEAD~3 # (2)
This would make sure I'm not losing any private file (not added to the index).
(这将确保我没有丢失任何私有文件(未添加到索引)。)
The --soft
option won't touch the index file nor the working tree at all (but resets the head to <commit>
, just like all modes do).
(--soft
选项根本不会触及索引文件或工作树(但是将头重置为<commit>
,就像所有模式一样)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…