如何将 git 提交从 master 移动到不同的现有分支

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

How to move git commits from master to a different existing branch

gitbranch

提问by gitq

I checked in a few commits to masterthat should have been checked into develop. What git commands do I used to remove those commits from the master branch and include in the develop branch?

我签入了一些本master应签入的提交develop。我使用哪些 git 命令从 master 分支中删除这些提交并包含在 develop 分支中?

回答by W?odzimierz Gajda

If I'm not mistaken, you had two synchronized branches, masterand dev, and simply forgot to switch the branch, before your commits.

如果我没记错的话,您有两个同步分支 masterdev,并且在您提交之前忘记切换分支。

If that is the case, we have:

如果是这样,我们有:

----------------
git log in dev

xxx
yyy
...
----------------

and:

和:

----------------
git log in master

ccc
bbb
aaa
        <---- here you forgot to switch branch
xxx
yyy
...
----------------

The solution is:

解决办法是:

First, make sure, that:

首先,请确保:

git status -s

returns empty results.

返回空结果。

Next, get all your new commits from masterto devwith:

接下来,让所有的新提交masterdev用:

git checkout dev
git merge master

Now return to you master:

现在回到你身边master

git checkout master

Remove unnecessary commits:

删除不必要的提交:

git reset --hard HEAD~3

The number ~3is the number of commits you want to remove.

数字~3是您要删除的提交数。

Remember: git status -shave to return empty results. Otherwise, git reset --hardcan cause data loss.

请记住:git status -s必须返回空结果。否则,git reset --hard可能会导致数据丢失。

回答by Nils Werner

You can cherry-pickyour commits over to the developand afterwards interactively rebaseyour masterbranch:

您可以cherry-pick切换到您提交develop事后交互rebase您的master分支:

  1. git checkout develop
  2. git cherry-pick aabbcc
  3. git cherry-pick ddeeff
  4. ....
  5. git checkout master
  6. git rebase 123456 -i
  1. git checkout develop
  2. git cherry-pick aabbcc
  3. git cherry-pick ddeeff
  4. ....
  5. git checkout master
  6. git rebase 123456 -i

where 123456is a commit before you made a mistake. This will open an editor that shows every commit that will be affected by the rebase. Delete the lines that correspond to the commits you want to discard and quit the editor.

123456在你犯错之前提交在哪里。这将打开一个编辑器,显示将受 rebase 影响的每个提交。删除与要放弃的提交对应的行并退出编辑器。

回答by Jens Peters

For coping into another branch you can use cherry picking:

为了应对另一个分支,您可以使用樱桃采摘:

git cherry-pick <commit>

Deleting is not that easy. You can use rebase and squash or edit the commit:

删除不是那么容易。您可以使用变基和压缩或编辑提交:

git rebase -i <commit>~1

But I am not sure when chosing edit during rebase if you can edit the files also rather than the commit message only.

但是我不确定在 rebase 期间选择编辑时是否可以编辑文件,而不仅仅是提交消息。

回答by bvidal

There are usually several ways to do the same thing in git one possible way:

通常有几种方法可以在 git 中以一种可能的方式做同样的事情:

git checkout develop
git cherry-pick XXX // XXX being the sha1 of the commit you want to grab
git checkout master
git rebase --interactive HEAD~IDX // IDX being the position of the last "good" commit compared to HEAD

The last command will display all review from HEAD to the last good commit and all you have to do is deleted the line of the commit to moved to branch develop

最后一个命令将显示从 HEAD 到最后一次良好提交的所有,您所要做的就是删除提交行以移动到分支开发