git 如何将更改提交到新分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14655816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:24:49  来源:igfitidea点击:

How to commit changes to a new branch

git

提问by user1988385

I just made changes to a branch. How can I commit the changes to the other branch?

我刚刚对一个分支进行了更改。如何将更改提交到另一个分支?

I am trying to use:

我正在尝试使用:

git checkout "the commmit to the changed branch" -b "the other branch"

However, I don't think this is the right thing to do, because in this case I'm creating a new branch instead of committing the changes to "the other branch".

但是,我不认为这是正确的做法,因为在这种情况下,我正在创建一个新分支,而不是将更改提交到“另一个分支”。

Should I use the following command instead?

我应该使用以下命令吗?

git merge "the other branch"

回答by John Brodie

git checkout -b your-new-branch

git checkout -b your-new-branch

git add <files>

git add <files>

git commit -m <message>

git commit -m <message>

First, checkout your new branch. Then add all the files you want to commit to staging. Lastly, commit all the files you just added. You might want to do a git push origin your-new-branchafterward so your changes show up on the remote.

首先,检查您的新分支。然后添加要提交到暂存的所有文件。最后,提交您刚刚添加的所有文件。您可能想git push origin your-new-branch稍后再做,以便您的更改显示在遥控器上。

回答by miguelmorin

If you haven't committed changes

如果您尚未提交更改

If your changes are compatible with the other branch

如果您的更改与其他分支兼容

This is the case from the question because the OP wants to commit to a new branch and also applies if your changes are compatible with the target branch without triggering an overwrite.

这是问题的情况,因为 OP 希望提交到新分支,并且如果您的更改与目标分支兼容而不会触发覆盖,则也适用。

As in the accepted answer by John Brodie, you can simply checkout the new branch and commit the work:

正如 John Brodie 接受的答案一样,您可以简单地检出新分支并提交工作:

git checkout -b branch_name
git add <files>
git commit -m "message"

If your changes are incompatible with the other branch

如果您的更改与其他分支不兼容

If you get the error:

如果您收到错误:

error: Your local changes to the following files would be overwritten by checkout:
...
Please commit your changes or stash them before you switch branches

Then you can stash your work, create a new branch, then pop your stash changes, and resolve the conflicts:

然后你可以存储你的工作,创建一个新分支,然后弹出你的存储更改,并解决冲突:

git stash
git checkout -b branch_name
git stash pop

It will be as if you had made those changes after creating the new branch. Then you can commit as usual:

就好像您在创建新分支后进行了这些更改一样。然后你可以像往常一样提交:

git add <files>
git commit -m "message"

If you have committed changes

如果您已提交更改

If you want to keep the commits in the original branch

如果您想将提交保留在原始分支中

See the answer by Carl Norum with cherry-picking, which is the right tool in this case:

请参阅 Carl Norum 的答案,挑选樱桃,这是在这种情况下的正确工具:

git checkout <target name>
git cherry-pick <original branch>

If you don't want to keep the commits in the original branch

如果您不想将提交保留在原始分支中

See the answer by joeytwiddle on this potential duplicate. Follow any of the above steps as appropriate, then roll back the original branch:

请参阅 joeytwiddle 关于此潜在重复项的答案。根据需要执行上述任何步骤,然后回滚原始分支:

git branch -f <original branch> <earlier commit id>

If you have pushed your changes to a shared remote like GitHub, you should not attempt this roll-back unless you know what you are doing.

如果您已将更改推送到共享远程(如 GitHub),则除非您知道自己在做什么,否则不应尝试此回滚。

回答by Carl Norum

If I understand right, you've made a commit to changed_branchand you want to copy that commit to other_branch? Easy:

如果我理解正确,您已经提交changed_branch并希望将该提交复制到other_branch? 简单:

git checkout other_branch
git cherry-pick changed_branch