如何将我当前的更改提交给git中的其他分支 [英] How to commit my current changes to a different branch in git

查看:151
本文介绍了如何将我当前的更改提交给git中的其他分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候会发生,我在工作目录中进行了一些更改,我意识到这些更改应该在与当前版本不同的分支中提交。这通常发生在我想尝试新事物或做一些测试时,并且我忘记事先创建一个新的分支,但我不想将脏代码提交给主分支。



所以,如何将未提交的更改(或存储在索引中的更改)提交到与当前版本不同的分支?

git存储最常见的用例:

  git stash 
git checkout other-branch
git stash pop

第一个隐藏会隐藏您的更改(基本上是进行临时提交),随后的隐藏pop 会重新应用它们。这让git可以使用它的合并功能。



如果您尝试弹出存储时,会遇到合并冲突......接下来的步骤取决于这些冲突是什么。如果所有隐藏的变更确实属于其他分支,那么您只需要对它们进行排序 - 这是对错误分支进行更改的结果。



<另一方面,如果你真的搞砸了,而且你的工作树混合了两个分支的变化,并且冲突只是你想要在原始分支上提交的冲突,你可以保存一些工作。像往常一样,有很多方法可以做到这一点。这里有一个,从你弹出并看到冲突开始:

 #Unstage everything(警告:这会在您的文件中留下冲突的文件树)
git reset
#添加你想要在这里提交的东西
git add -p#或者git add -i $ b $ git commit
#藏匿处仍然存在;如果它应用干净,pop只会抛出它
git checkout原始分支
git存储pop
#添加用于这个分支的更改
git add -p
git提交
#并丢弃其余
git reset --hard

另外,如果您在这种情况发生之前意识到,只需提交属于当前分支的东西即可。您可以随时回来修改该提交:

  git add -p 
git commit
git stash
git checkout other-branch
git stash pop

当然,记住这一切都花了一些工作,并避免下一次,也许通过在你的提示符中加入 $(__ git_ps1)到你的PS1中你的bashrc 。 (例如,参见 Git in Bash docs。)


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?

解决方案

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:

git stash
git checkout other-branch
git stash pop

The first stash hides away your changes (basically making a temporary commit), and the subsequent stash 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:

# Unstage everything (warning: this leaves files with conflicts in your tree)
git reset
# Add the things you *do* want to commit here
git add -p     # or maybe git add -i
git commit
# The stash still exists; pop only throws it away if it applied cleanly
git checkout original-branch
git stash pop
# Add the changes meant for this branch
git add -p 
git commit
# And throw away the rest
git reset --hard

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:

git add -p
git commit
git stash
git checkout other-branch
git stash pop

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 in your bashrc. (See for example the Git in Bash docs.)

这篇关于如何将我当前的更改提交给git中的其他分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆