简体   繁体   English

如何将提交移动到另一个分支?

[英]How to move commits to another branch?

I'd like to move my last few commits from master into a branch of their own. 我想将我最后几次提交从master转移到他们自己的分支中。

The tree on my PC looks like that: 我电脑上的树看起来像这样:

   W (some branch)
  /       
X1--X2--X3--X4--Y--Z1--Z2 (master)

I'd like it to look like: 我希望它看起来像:

   W (some branch)
  /       
X1--X2--X3--X4 (master)
             \
              Y--Z1--Z2 (my new branch)

However, the tree at GitHub looks like: 但是,GitHub上的树看起来像:

   W (some branch)
  /       
X1--X2--X3--X4--Y (master)

That's what I saw as a solution for moving the last commits to another branch: 这就是我所看到的将最后提交移动到另一个分支的解决方案:

git checkout master
git branch my_new_branch
git reset <commit_id>

My question is: would I be able to successfully push to GitHub after moving the commits into a new branch and if so would it require to do something else than these three commands? 我的问题是:在将提交移动到新分支后,我是否能成功推送到GitHub?如果是这样,它还需要做除了这三个命令之外的其他操作吗?

(I assume <commit_id> is the object name of X4 ...) (我假设<commit_id>X4的对象名...)

Those commands will indeed end you up with what you want locally. 这些命令确实会让你最终得到你想要的东西。 (You might want to use git reset --hard to keep the working tree and index the same as the commit you're resetting to, but as usual, be very careful that git status is clean before you use that command.) (你可能想使用git reset --hard来保持工作树和索引与你重置的提交一样,但是像往常一样,在使用该命令之前要非常小心git status是干净的。)

If you afterwards try to push master to GitHub, it will tell you that everything's already up to date, because the master on GitHub is one commit ahead. 如果你之后尝试将master推送到GitHub,它会告诉你所有内容都已经是最新的,因为GitHub上的master是一个提前提交。 You can force the push, so that master is reset on GitHub, but that's rewriting public history, so you should only do that if (a) you're the only who'll have fetched master from GitHub or (b) you can let your collaborators know what do so that they don't accidentally merge Y back in. If that's OK, you can do: 你可以强制推送,以便在GitHub上重置master,但是这会重写公共历史记录,所以你应该这样做,如果(a)你是唯一一个从GitHub获取master或(b)你可以让你的合作者知道是什么让他们不小心将Y合并回来。如果没关系,你可以这样做:

 git push --force origin master

... and then master on GitHub will be the same as your local version. ...然后GitHub上的master将与您的本地版本相同。

It should work but you will need: 它应该工作,但你需要:

  • to push -f origin master (force the push, which will spell trouble if anyone else has already clone your repo) push -f origin master (强制推送,如果其他人已经克隆你的回购,这将会造成麻烦)
  • to push then your new branch (which will then find the X4 sha1 on top of which said new branch will be set). 然后推送你的新分支(然后将在其上找到X4 sha1,表示将设置新分支)。

Nobody has mentioned that you can simply cherry pick the commit into another branch. 没有人提到你可以简单地将提交选择到另一个分支。 This way your commit message will still be there for you. 这样您的提交消息仍然适合您。

Just copy the revision id of your commit you want to move. 只需复制您要移动的提交的修订ID。 git log --oneline

Than create a new branch on top of the former commit and change to it: git checkout -b "$newBranchName" HEAD~$n (HEAD~$n stands for the n-th commit) or git checkout -b "$newBranchName" $formerRevsion 比在前一次提交之上创建一个新分支并更改为它: git checkout -b "$newBranchName" HEAD~$n (HEAD~ $ n代表第n次提交)或git checkout -b "$newBranchName" $formerRevsion

Now you just have to cherry pick the wanted commit: git cherry-pick $revision 现在你只需要选择想要的提交: git cherry-pick $revision

The old commit will be still in the other branch but you can fix that with rebase. 旧提交仍将在另一个分支中,但您可以使用rebase修复它。 Move back to your old branch and use rebase: git rebase -i HEAD~$n 移回旧分支并使用rebase: git rebase -i HEAD~$n

Just delete the commit line of the cherry picked commit and your branch will be rebased with out it. 只需删除樱桃挑选提交的提交行,您的分支就会被重新定位。 You have to use git push -f because the revisions of all rebased commits have changed. 你必须使用git push -f因为所有重新提交的提交的修订都已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM