在不同的分支上重放最后 N 次 git 提交

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

Replay the last N git commits on a different branch

gitversion-controlcommit

提问by Tom Lehman

I accidentally made 10 commits on branch "testing" when I intended to commit them on branch "master". The other commits on the "testing" branch are garbage, so I don't want to merge it with "master". Instead, I just want to replay the last 10 commits on master.

当我打算在分支“master”上提交它们时,我不小心在分支“testing”上做了 10 次提交。“testing”分支上的其他提交是垃圾,所以我不想将它与“master”合并。相反,我只想重播 master 上的最后 10 次提交。

采纳答案by Ron

  1. git checkout master
  2. git whatchanged testing
  3. git cherry-pick _________
  1. git 结帐大师
  2. git whatchanged 测试
  3. git 挑选 _________

?

?

回答by Talljoe

Rebase should do it.

Rebase 应该这样做。

git rebase -p --onto master testing~10 testing

This will copy the last ten commits on testing to master and make that the new testing (the old testing will be an orphan). Then you can merge master to testing as a fast-forward.

这会将测试的最后十次提交复制到 master 并使新测试成为新测试(旧测试将是孤立的)。然后你可以将 master 合并到 test 作为快进。

git checkout master
git merge testing

回答by Tim

As said in comments, the rebase-inspired answer is leaving the 'garbage' commits orphaned.

正如评论中所说,rebase受启发的答案是让“垃圾”提交成为孤儿

Just use simple tools:

只需使用简单的工具:

git checkout master
git merge testing
git checkout testing
git reset --hard HEAD~10   # Go back 10 commits (*1)
git checkout master

(*1) You will only be "losing" commits from the testingbranch, since you'll have those commits in masterthanks to the merge.

(* 1)您只可以“丢失”从提交testing分支,因为你必须在这些提交master感谢merge