Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

git - 使用当前更改创建Git分支(Create Git branch with current changes)

I started working on my master branch thinking that my task would be easy.

(我开始在我的分支上工作,认为我的任务很简单。)

After a while I realized it would take more work and I want to do all this work in a new branch.

(过了一会儿,我意识到这将需要更多的工作,我想在一个新的分支中完成所有这些工作。)

How can I create a new branch and take all these changes with me without dirtying master ?

(如何创建一个新分支并随身携带所有这些更改而不会弄脏主人 ?)

  ask by willcodejavaforfood translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
  1. 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 ”分支。)

  2. Rewind the master branch to get rid of those three commits.

    (倒回master分支以摆脱这三个提交。)

  3. 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> ,就像所有模式一样)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...