如何修复提交到错误的 Git 分支?
我刚刚对错误的分支做出了完美的承诺。 如何撤消主分支中的最后一次提交,然后进行相同的更改并将它们放入我的升级分支中?
I just made a perfectly good commit to the wrong branch.
How do I undo the last commit in my master branch and then take those same changes and get them into my upgrade branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果您尚未推送更改,您还可以进行软重置:
这将恢复提交,但将提交的更改放回到索引中。假设分支彼此相对最新,git 将允许您签出另一个分支,然后您可以简单地提交:
-c ORIG_HEAD
部分很有用再次输入提交消息。If you haven't yet pushed your changes, you can also do a soft reset:
This will revert the commit, but put the committed changes back into your index. Assuming the branches are relatively up-to-date with regard to each other, git will let you do a checkout into the other branch, whereupon you can simply commit:
The
-c ORIG_HEAD
part is useful to not type commit message again.这个话题晚了四年,但这可能对某人有帮助。
如果您忘记在提交之前创建一个新分支并在 master 上提交所有内容,无论您做了多少次提交,以下方法都会更容易:
现在您的 master 分支等于
origin/master
和 all新提交位于my_feature
上。请注意,my_feature
是本地分支,而不是远程分支。4 years late on the topic, but this might be helpful to someone.
If you forgot to create a new branch before committing and committed all on master, no matter how many commits you did, the following approach is easier:
Now you have your master branch equals to
origin/master
and all new commits are onmy_feature
. Note thatmy_feature
is a local branch, not a remote one.如果您有一个干净的(未修改的)工作副本
要回滚一个提交(请确保记下下一步的提交哈希值):
将该提交拉入另一个分支:
如果您已修改或未跟踪的更改
另请注意 < code>git reset --hard 将杀死您可能拥有的任何未跟踪和修改的更改,因此,如果您有那些您可能更喜欢的更改:
If you have a clean (un-modified) working copy
To rollback one commit (make sure you note the commit's hash for the next step):
To pull that commit into a different branch:
If you have modified or untracked changes
Also note that
git reset --hard
will kill any untracked and modified changes you might have, so if you have those you might prefer:如果您已经推送了更改,则需要在重置 HEAD 后强制执行下一次推送。
警告:硬重置将撤消工作副本中任何未提交的修改,而强制推送将用本地分支的当前状态完全覆盖远程分支的状态。
以防万一,在 Windows 上(使用 Windows 命令行,而不是 Bash)它实际上是四个
^^^^
而不是一个,所以它是If you already pushed your changes, you will need to force your next push after resetting the HEAD.
Warning: a hard reset will undo any uncommitted modifications in your working copy, while a force push will completely overwrite the state of the remote branch with the current state of the local branch.
Just in case, on Windows (using the Windows command line, not Bash) it's actually four
^^^^
instead of one, so it's我最近做了同样的事情,我不小心将更改提交给了 master,而我本应该提交给其他分支。但我没有推动任何东西。
如果您只是提交了错误的分支,并且此后没有更改任何内容,也没有推送到存储库,那么您可以执行以下操作:
注意:在上面的示例中,我使用 git reset HEAD~1 倒回 1 次提交。但如果你想倒回 n 次提交,那么你可以执行 git reset HEAD~n。
另外,如果您最终提交了错误的分支,并且在意识到提交了错误的分支之前还编写了更多代码,那么您可以使用 git stash 来保存正在进行的工作:
注意:我使用了这个网站作为参考
https: //www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/
I recently did the same thing, where I accidentally committed a change to master, when I should have committed to other-branch. But I didn't push anything.
If you just committed to the wrong branch, and have not changed anything since, and have not pushed to the repo, then you can do the following:
NOTE: in the above example, I was rewinding 1 commit with git reset HEAD~1. But if you wanted to rewind n commits, then you can do git reset HEAD~n.
Also, if you ended up committing to the wrong branch, and also ended up write some more code before realizing that you committed to the wrong branch, then you could use git stash to save your in-progress work:
NOTE: I used this website as a reference
https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/
对于错误分支上的多次提交
如果对您来说只有大约 1 次提交,那么还有很多其他更容易的重置解决方案可用。对我来说,我有大约 10 个提交是我不小心在
master
分支上创建的,而不是(我们称之为target
),而且我不想丢失提交历史记录。在这种情况下,使用此答案作为参考,您可以应用以下 4 步流程 -
temp
frommaster
temp
合并到最初用于提交的分支,即target
master 上的提交
temp
。以下是上述步骤的详细信息:
从
master
创建一个新分支(我不小心在其中提交了很多更改)注意:
-b
标志用于创建新分支为了验证我们是否正确,我会快速执行
git 分支
以确保我们位于temp
分支和git 日志
> 检查我们的提交是否正确。将临时分支合并到最初用于提交的分支,即
target
。首先,切换到原始分支,即
target
(如果没有,您可能需要git fetch
)注意:不使用
-b
标志现在,让我们将临时分支合并到我们当前签出的分支中
target
如果有的话,您可能需要处理一些冲突。成功合并后,您可以推送(我会)或继续下一步。
使用此答案作为参考撤消
master
上的意外提交,首先切换到大师
然后使用下面的命令将其全部撤消以匹配远程(或者如果需要,可以使用适当的命令来匹配特定的提交)
同样,我会在前后执行
git log
只是为了确保预期的更改生效。擦除证据,即删除临时分支。为此,首先,您需要检查
temp
合并到的分支,即target
(如果您留在master
上并执行下面的命令,您可能会收到错误:分支“temp”未完全合并
),所以让我们然后删除这次事故的证据
For multiple commits on the wrong branch
If, for you, it is just about 1 commit, then there are plenty of other easier resetting solutions available. For me, I had about 10 commits that I'd accidentally created on
master
branch instead of, let's call ittarget
, and I did not want to lose the commit history.In this case, using this answer as a reference, you can apply the following 4-step process -
temp
frommaster
temp
into the branch originally intended for commits, i.e.target
master
temp
.Here are the above steps in detail:
Create a new branch from the
master
(where I had accidentally committed a lot of changes)Note:
-b
flag is used to create a new branchJust to verify if we got this right, I'd do a quick
git branch
to make sure we are on thetemp
branch and agit 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 togit fetch
if you haven't)Note: Not using
-b
flagNow, let's merge the temporary branch into the branch we have currently checkout out
target
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 a reference, first switch to themaster
then undo it all the way back to match the remote using the command below (or to a particular commit, using the appropriate command, if you want)
Again, I'd do a
git log
before and after just to make sure that the intended changes took effect.Erase the evidence, i.e. delete the temporary branch. For this, first, you need to checkout the branch that the
temp
was merged into, i.e.target
(If you stay onmaster
and execute the command below, you might get anerror: The branch 'temp' is not fully merged
), so let'sand then delete the proof of this mishap
因此,如果您的情况是您已提交给
master
但打算提交给another-branch
(该分支可能已经存在,也可能不存在),但您还没有推送然而,这很容易解决。现在,您对
master
的所有提交都将位于another-branch
上。源自:http://haacked.com/archive/2015/06 /29/git-migrate/
So if your scenario is that you've committed to
master
but meant to commit toanother-branch
(which may or not may not already exist) but you haven't pushed yet, this is pretty easy to fix.Now all your commits to
master
will be onanother-branch
.Sourced with love from: http://haacked.com/archive/2015/06/29/git-migrate/
详细说明这个答案,以防您有多个提交需要移动,例如
develop
到新分支
:To elaborate on this answer, in case you have multiple commits to move from, e.g.
develop
tonew_branch
:如果您遇到此问题并且您有 Visual Studio,则可以执行以下操作:
右键单击您的分支并选择
查看历史记录
:右键单击“提交”想要回去。并根据需要恢复或重置。
If you run into this issue and you have Visual Studio, you can do the following:
Right-click on your branch and select
View History
:Right-click on commit you want to go back to. And Revert or Reset as needed.
在常见情况下,您忘记在提交之前从
master
切换到feature
分支:将
origin/master
替换为您想要的提交 < code>master 指向的分支。例如,如果您希望它指向HEAD
之后的 3 个提交,请使用HEAD~3
,如果您希望它指向提交,请使用a1b2c3d
用那个哈希。这个想法是在当前提交上重新创建
feature
分支并切换到它。然后使master
分支指向与origin/master
相同的提交。一般情况
在一般情况下,您希望在
feature
分支上重播在master
上完成的提交,如下图所示:然后挑选您在
feature
上所做的提交使用以下命令在您的feature
分支上使用 code>master 。将$old_master
替换为进行更改之前master
所指向的提交的哈希值。如果需要,请使用 git stash --include-untracked 存储本地更改,然后使用 git stash pop 取消存储它们。
无需重写历史记录
除了挑选
feature< 上的更改之外,您还需要
git 恢复
更改,而不是将master
分支重置为过去。 /代码> 分支。确保不要将您的提交
git merge
到feature
中。如果您尝试将feature
分支合并回master
,这些更改将被忽略,因为我们刚刚恢复了它们。结果如下所示,其中
rG
、rH
和rI
是G
、的恢复提交>H
和I
分别:In the common case where you forgot to switch from
master
to yourfeature
branch before commiting:Replace
origin/master
with the commit you want yourmaster
branch to point to. For example, useHEAD~3
if you want it to point 3 commits behindHEAD
, ora1b2c3d
if you want it to point to the commit with that hash.The idea is to recreate the
feature
branch on the current commit and switch to it. Then make themaster
branch point to the same commit asorigin/master
.General case
In the general case where you want to replay the commits done on
master
on yourfeature
branch, like the following diagram:Then cherry-pick the commits you made on
master
on yourfeature
branch using the following commands. Replace$old_master
by the hash of the commitmaster
was pointing to before you made your changes.If needed, stash your local changes using
git stash --include-untracked
, and then unstash them later usinggit stash pop
.Without rewriting history
Instead of resetting the
master
branch into the past, you need togit revert
the changes, in addition to cherry-picking the changes on thefeature
branch.Make sure not to
git merge
your commits intofeature
. Those changes will be ignored if you try to merge yourfeature
branch back intomaster
, because we just reverted them.Here's what the result would look like, with
rG
,rH
andrI
the revert commits ofG
,H
andI
respectively:保留更改并取消提交
简单的解决方案是运行此命令。
这将取消提交代码,同时保留您的更改
Keep changes and Uncommit
The simple solution is to run this command.
This will uncommit the code, while keeping your changes
对我来说,这是通过恢复我推送的提交,然后挑选该提交到另一个分支来解决的。
您可以使用 git log 来查找正确的哈希值,并且您可以随时推送这些更改!
For me, this was solved by reverting the commit I had pushed, then cherry-picking that commit to the other branch.
You can use
git log
to find the correct hash, and you can push these changes whenever you like!如果您想要应用更改的分支已经存在(例如分支 develop),请按照 fotanus 下面,然后:
显然,如果您愿意,您可以使用 tempbranch 或任何其他分支名称来代替 my_feature 。
另外,如果适用,请延迟存储弹出(应用),直到合并到目标分支后。
If the branch you wanted to apply your changes to already exists (branch develop, for example), follow the instructions that were provided by fotanus below, then:
And obviously you could use tempbranch or any other branch name instead of my_feature if you wanted.
Also, if applicable, delay the stash pop (apply) until after you've merged at your target branch.
为此,我对错误的分支进行了大约 6 次提交,并在注意到之前将它们推送到了远程。我按照以下步骤操作,他们提供了帮助:
我做了一个 git log 来查看我所做的提交:
<前><代码> git log --oneline
从以前的提交和推送列表中,我选择了我需要做头部的提交,即列表中带有“head”标题的第一个提交因为它代表了您所做的新提交的完整列表。保存 ID。
![我的代码示例](这里我演示了提交列表和我选择的提交列表。最上面的一个因为它是我最后一次提交的头部)
然后我将此分支重置回使用前的 6 次提交;
我签出到新分支,我打算放置我错误提交的提交;
使用
将整个提交列表带到您的新分支。
然后使用
<前><代码> git push --force
对您的远程存储库进行所有这些更改
For this, I had made about 6 commits to the wrong branch and pushed them to the remote before noticing. I followed these steps and they helped:
I did a git log to view the commits I had made with:
From the list of previous commits and push, I chose the commit that I needed to make the head ie the first commit with the "head" title in the list as it represents the whole list of new commits you made. Save the id.
![example of my code](here I demonstrate the list of commits and the one I chose. The top one as it was the head of my last commits)
Then I reset this branch back to the 6 commits before using;
I checkout to the new branch I intend to put the commits I had mistakenly commited;
Use
to bring the entire list of commits to your new branch.
Then use
to make all these changes to your remote repo as well