如何将我当前的更改提交到 Git 中的不同分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2944469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to commit my current changes to a different branch in Git
提问by Auron
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.
有时我会在我的工作目录中进行一些更改,然后我意识到这些更改应该在与当前分支不同的分支中提交。这通常发生在我想尝试新事物或做一些测试而我忘记事先创建一个新分支,但我不想将脏代码提交到 master 分支时。
So, how can I make that uncommitted changes (or changes stored in the index) be committed to a different branch than the current one?
那么,如何将未提交的更改(或存储在索引中的更改)提交到与当前分支不同的分支?
回答by Cascabel
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 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.
第一个stash
隐藏您的更改(基本上是临时提交),然后stash pop
重新应用它们。这让 Git 可以使用它的合并功能。
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.
如果,当您尝试弹出 stash 时,遇到了合并冲突……接下来的步骤取决于这些冲突是什么。如果所有隐藏的更改确实属于该另一个分支,则您只需要对它们进行排序 - 这是在错误的分支上进行更改的结果。
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 PS1environment variable in your bashrcfile. (See for example the Git in Bashdocumentation.)
当然,请记住这一切都需要一些工作,下次避免它,也许可以通过将当前分支名称添加$(__git_ps1)
到bashrc文件中的PS1环境变量来将当前分支名称放在提示中。(例如参见Bash文档中的Git。)
回答by tanascius
You can just create a new branch and switch onto it. Commit your changes then:
您可以创建一个新分支并切换到它。然后提交您的更改:
git branch dirty
git checkout dirty
// And your commit follows ...
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.
或者,您也可以检出现有分支(仅git checkout <name>
)。但前提是没有冲突(所有已编辑文件的基础与当前分支中的相同)。否则你会收到一条消息。
回答by Hank Gay
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.
并提供您的提交信息。