使用Git将最近的提交移至新分支 [英] Move the most recent commit(s) to a new branch with Git

查看:175
本文介绍了使用Git将最近的提交移至新分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把我承诺的最后几个提交提交给一个新的分支,并在提交这些提交之前将主提交回去。不幸的是,我的Git-fu还不够强大,还有什么帮助?

我该怎么办?

  master A  -  B  -  C  -  D  -  E 

到此?

  newbranch C  -  D -  E 
/
主A - B


解决方案

转移到新分支



除非涉及其他情况,否则可以通过分支和回滚来轻松完成。

#注意:任何未提交的更改都将丢失。
git branch newbranch#创建一个新的分支,保存所需的提交
git reset --hard HEAD〜3#将主机移回3次提交(GONE from master)
git checkout newbranch#Go到仍然有所需提交的新分支

但确保有多少提交返回。或者,你可以不用 HEAD〜3 ,只需提供你想要的提交的散列(或者像 origin / master 那样的引用)恢复到 master (/ current)分支上,例如:

  git reset --hard a1b2c3d4 

* 1您只会 不要担心,你会在newbranch提交这些提交!



警告: With Git版本2.0及更高版本,如果您稍后 git rebase 原始分支( master )分支上的新分支,您可以在rebase期间需要一个明确的 - no-fork-point 选项来避免遗失提交。有 branch.autosetuprebase总是集合更有可能。详情请参阅 John Mellor的回答

分支

警告:上述方法的工作原理是您使用第一个命令创建新分支 git branch newbranch 。如果您想要使用现有分支,则需要在执行 git reset --hard HEAD〜3 前将您的更改合并到现有分支 code>。如果您不先合并您的更改,他们将会丢失。因此,如果您正在使用现有分支,它将如下所示:

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD〜3#返回3次提交。你会*失去未落实的工作。
git checkout existingbranch


I'd like to move the last several commits I've committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not strong enough yet, any help?

I.e. How can I go from this

master A - B - C - D - E

to this?

newbranch     C - D - E
             /
master A - B 

解决方案

Moving to a new branch

Unless there are other circumstances involved, this can be easily done by branching and rolling back.

# Note: Any changes not committed will be lost.
git branch newbranch      # Create a new branch, saving the desired commits
git reset --hard HEAD~3   # Move master back by 3 commits (GONE from master)
git checkout newbranch    # Go to the new branch that still has the desired commits

But do make sure how many commits to go back. Alternatively, you can instead of HEAD~3, simply provide the hash of the commit (or the reference like origin/master) you want to "revert back to" on the master (/current) branch, e.g:

git reset --hard a1b2c3d4

*1 You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!

WARNING: With Git version 2.0 and later, if you later git rebase the new branch upon the original (master) branch, you may need an explicit --no-fork-point option during the rebase to avoid losing the carried-over commits. Having branch.autosetuprebase always set makes this more likely. See John Mellor's answer for details.

Moving to an existing branch

WARNING: The method above works because you are creating a new branch with the first command: git branch newbranch. If you want to use an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3. If you don't merge your changes first, they will be lost. So, if you are working with an existing branch it will look like this:

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

这篇关于使用Git将最近的提交移至新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆