我刚刚向错误的分支做出了完美的承诺。 我如何撤消在我的主分支中的最后一次提交,然后采取这些相同的更改,并将它们放入我的升级分支?


当前回答

如果您已经执行了更改,则需要在重置HEAD后强制执行下一次推送。

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

警告:硬重置将撤销工作副本中任何未提交的修改,而强制推将完全覆盖远程分支的状态与本地分支的当前状态。

以防万一,在Windows上(使用Windows命令行,而不是Bash),它实际上是四个^^^^而不是一个,所以它是

git reset --hard HEAD^^^^

其他回答

在常见的情况下,你在提交之前忘记从master切换到你的特性分支:

git checkout -B feature
git branch -f master origin/master

将origin/master替换为您希望主分支指向的提交。例如,如果您希望它指向HEAD后面的3个提交,则使用HEAD~3,如果您希望它指向带有该哈希的提交,则使用a1b2c3d。

其思想是在当前提交上重新创建特性分支并切换到它。然后使master分支指向与origin/master相同的提交。

一般情况下

在一般情况下,你想在你的特性分支上重放在master上完成的提交,如下图所示:

A---B---C---D  $old_master                   A---B---C---D  master
    |        \                                   |        \
    |         G---H---I  master <- HEAD  =>      |         G---H---I
    |                                            | 
    `-E---F  feature                             `-E---F---G'--H'--I' feature <- HEAD

然后使用下面的命令在你的特性分支上选择你在master上做的提交。将$old_master替换为提交主机在进行更改之前所指向的哈希值。

git checkout feature
git cherry-pick $old_master..master
git branch -f master $old_master

如果需要,使用git stash -include-untracked隐藏你的本地更改,然后使用git stash pop取消隐藏。

不重写历史

除了选择特性分支上的更改之外,您还需要git恢复这些更改,而不是将主分支重置到过去。

git checkout feature
git cherry-pick $old_master..master
git checkout master
git revert $old_master..
git checkout feature

确保git不会将你的提交合并到特性中。如果你试图将你的特征分支合并回主分支,这些变化将被忽略,因为我们刚刚恢复了它们。

下面是结果的样子,rG, rH和rI分别是G, H和I的还原提交:

A---B---C---D             rG--rH--rI  master
    |        \           /
    |         G---H---I-`
    | 
    `-E---F---G'--H'--I' feature <- HEAD

用于在错误的分支上进行多次提交

如果对你来说,它只是1次提交,那么有很多其他更简单的重置解决方案可用。对于我来说,我无意中在主分支上创建了大约10个提交,而不是,让我们称之为目标,我不想失去提交历史。

你能做的,也是拯救我的,就是把这个答案作为参考,用一个4步的过程,也就是:

从master创建一个新的临时分支temp 将temp合并到最初用于提交的分支中,即目标 在主服务器上撤销提交 删除临时分支temp。

以下是上述步骤的详细说明:

Create a new branch from the master (where I had accidentally committed a lot of changes) git checkout -b temp Note: -b flag is used to create a new branch Just to verify if we got this right, I'd do a quick git branch to make sure we are on the temp branch and a git log to check if we got the commits right. Merge the temporary branch into the branch originally intended for the commits, i.e. target. First, switch to the original branch i.e. target (You might need to git fetch if you haven't) git checkout target Note: Not using -b flag Now, let's merge the temporary branch into the branch we have currently checkout out target git merge temp You might have to take care of some conflicts here, if there are. You can push (I would) or move on to the next steps, after successfully merging. Undo the accidental commits on master using this answer as reference, first switch to the master git checkout master then undo it all the way back to match the remote using the command below (or to particular commit, using appropriate command, if you want) git reset --hard origin/master Again, I'd do a git log before and after just to make sure that the intended changes took effect. Erasing the evidence, that is deleting the temporary branch. For this, first you need to checkout the branch that the temp was merged into, i.e. target (If you stay on master and execute the command below, you might get a error: The branch 'temp' is not fully merged), so let's git checkout target and then delete the proof of this mishap git branch -d temp

保留更改并取消提交 简单的解决方案是运行该命令。

git reset HEAD^

这将取消提交代码,同时保留您的更改

如果你有一个干净的(未修改的)工作副本

回滚一次提交(确保你注意到了下一步的提交散列):

git reset --hard HEAD^

将该提交拉到不同的分支:

git checkout other-branch
git cherry-pick COMMIT-HASH

如果您已修改或未跟踪更改

还需要注意的是,git reset -hard会杀死你可能拥有的任何未跟踪和修改的更改,所以如果你有这些更改,你可能更喜欢:

git reset HEAD^
git checkout .

如果你想要应用你的更改的分支已经存在(例如分支开发),遵循下面fotanus提供的说明,那么:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

显然,如果你愿意,你可以使用tempbranch或任何其他分支名称来代替my_feature。

同样,如果适用的话,将stash弹出(apply)延迟到合并目标分支之后。