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

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

问题描述

我想将我已提交给 master 的最后几次提交移动到一个新分支,并在这些提交之前将 master 带回.不幸的是,我的 Git-fu 还不够强大,有什么帮助吗?

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?

即我怎样才能离开这个

master A - B - C - D - E

这个?

newbranch     C - D - E
             /
master A - B 

推荐答案

移动到现有分支

如果您想将提交移动到现有分支,它将如下所示:

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 stash 将未提交的编辑存储到您的 stash.完成后,您可以使用 git stash pop

You can store uncommitted edits to your stash before doing this, using git stash. Once complete, you can retrieve the stashed uncommitted edits with git stash pop

警告:此方法有效,因为您正在使用第一个命令创建一个新分支:git branch newbranch.如果要将提交移动到现有分支,则需要在执行 git reset --hard HEAD~3 之前将更改合并到现有分支中(请参阅 <强>移动到现有分支 以上).如果您不先合并更改,它们就会丢失.

WARNING: This method works because you are creating a new branch with the first command: git branch newbranch. If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3 (see Moving to an existing branch above). If you don't merge your changes first, they will be lost.

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

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 (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

但是一定要确定要返回多少次提交.或者,您可以代替 HEAD~3,只需提供您想要恢复到"的提交的哈希值(或像 origin/master 这样的引用).在 master (/current) 分支上,例如:

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 您将只会输"从 master 分支提交,但别担心,你会在 newbranch 中提交这些提交!

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

警告:对于 Git 2.0 及更高版本,如果您稍后 git rebase 在原始 (master) 分支上创建新分支,则您在变基期间可能需要一个明确的 --no-fork-point 选项,以避免丢失结转提交.branch.autosetuprebase always 设置使这更有可能.详情请参阅 John Mellor 的回答.

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.

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

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