使用当前更改创建Git分支

使用当前更改创建Git分支

本文介绍了使用当前更改创建Git分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在我的主分支上工作,认为我的任务很简单。过了一段时间,我意识到这需要更多的工作,我想在新的分支中完成所有这些工作。如何创建一个新的分支并在不弄脏主人的情况下与我一起进行所有这些更改? 解决方案

在一个命令中: git checkout -b newBranch



正如:

  $ (1)
$ git reset --hard HEAD〜3#(2)注意:使用$ git reset --soft HEAD〜3(解释如下)
$ git checkout topic / wip#(3)




  1. 您已经做了一些提交,他们在 master 分支中为时尚早。您希望继续在主题分支中对其进行打磨,从当前的 HEAD 中创建 topic / wip 分支。 。
  2. 重绕 master 分支以摆脱这三个提交。

  3. 切换到 topic / wip 分支并继续工作。






注意:由于 git reset --hard 命令的破坏性效果(它会重置索引和工作树。由于< commit> 被丢弃,所以在工作树中跟踪文件),我宁可去:

<$ p $ $ git reset --soft HEAD〜3#(2)

这将确保我不会丢失任何私人文件(未添加到索引)。

- soft 选项不会触及索引文件或者工作树(但像所有模式一样,将头重置为< commit> )。


I started working on my master branch thinking that my task would be easy. After a while I realised 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?

解决方案

If you hadn't made any commit yet, only (1: branch) and (3: checkout) would be enough.
Or, in one command: git checkout -b newBranch

As mentioned in the git reset man page:

$ 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. You want to continue polishing them in a topic branch, so create "topic/wip" branch off of the current HEAD.
  2. Rewind the master branch to get rid of those three commits.
  3. Switch to "topic/wip" branch and keep working.


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 --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).

这篇关于使用当前更改创建Git分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:12