如何将我当前的更改提交到 Git 中的不同分支
有时,我会在工作目录中进行一些更改,并且我意识到这些更改应该提交到与当前目录不同的分支中。当我想要尝试新事物或进行一些测试并且忘记事先创建新分支,但我不想将脏代码提交到主分支时,通常会发生这种情况。
那么,如何将未提交的更改(或存储在索引中的更改)提交到与当前分支不同的分支?
Sometimes it happens that I make some changes in my working directory, and I realize that these changes should be committed in a branch different to the current one. This usually happens when I want to try out new things or do some testing and I forget to create a new branch beforehand, but I don't want to commit dirty code to the master branch.
So, how can I make that uncommitted changes (or changes stored in the index) be committed to a different branch than the current one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他答案建议检查另一个分支,然后提交给它,只有在考虑到本地修改可以进行检查的情况下才有效。如果没有,您就处于
git stash
最常见的用例中:第一个
stash
隐藏了您的更改(基本上是进行临时提交),而后续的stash 隐藏了您的更改(基本上是进行临时提交) >stash pop
重新应用它们。这让 Git 可以使用它的合并功能。如果当您尝试弹出存储时遇到合并冲突...接下来的步骤取决于这些冲突是什么。如果所有隐藏的更改确实属于另一个分支,您只需对它们进行排序 - 这是在错误的分支上进行更改的结果。
另一方面,如果您真的搞砸了,并且您的工作树混合了两个分支的更改,并且冲突仅存在于您想要提交回原始分支的更改中,那么您可以节省一些工作。像往常一样,有很多方法可以做到这一点。这是一个,从您弹出并看到冲突之后开始:
或者,如果您提前意识到这种情况将会发生,只需提交属于当前分支的内容即可。您随时可以回来修改该提交:
当然,请记住这一切都需要一些工作,下次可以避免这样做,也许可以通过添加
$(__git_ps1)< 将当前分支名称放入提示中/code> 到 PS1 环境变量 bashrc 文件。 (例如,请参阅 Bash 中的 Git 文档。)
The other answers suggesting checking out the other branch, then committing to it, only work if the checkout is possible given the local modifications. If not, you're in the most common use case for
git stash
:The first
stash
hides away your changes (basically making a temporary commit), and the subsequentstash pop
re-applies them. This lets Git use its merge capabilities.If, when you try to pop the stash, you run into merge conflicts... the next steps depend on what those conflicts are. If all the stashed changes indeed belong on that other branch, you're simply going to have to sort through them - it's a consequence of having made your changes on the wrong branch.
On the other hand, if you've really messed up, and your work tree has a mix of changes for the two branches, and the conflicts are just in the ones you want to commit back on the original branch, you can save some work. As usual, there are a lot of ways to do this. Here's one, starting from after you pop and see the conflicts:
Alternatively, if you realize ahead of the time that this is going to happen, simply commit the things that belong on the current branch. You can always come back and amend that commit:
And of course, remember that this all took a bit of work, and avoid it next time, perhaps by putting your current branch name in your prompt by adding
$(__git_ps1)
to your PS1 environment variable in your bashrc file. (See for example the Git in Bash documentation.)您只需创建一个新分支并切换到它即可。然后提交更改:
或者,您也可以签出现有分支(只需
git checkout
)。但前提是不存在冲突(所有编辑文件的基础与当前分支中的相同)。否则您会收到一条消息。请注意,在切换到现有发散分支的情况下,您可以使用 -m 选项告诉 git 尝试合并更改,即 git checkout -m
You can just create a new branch and switch onto it. Commit your changes then:
Alternatively, you can also checkout an existing branch (just
git checkout <name>
). But only, if there are no collisions (the base of all edited files is the same as in your current branch). Otherwise you will get a message.Note that in the case of switching to existing divergent branch you can use -m option to tell git to try to merge changes, i.e.
git checkout -m <name>
git checkout my_other_branch
git add my_file my_other_file
git commit -m
并提供您的提交消息。
git checkout my_other_branch
git add my_file my_other_file
git commit -m
And provide your commit message.