简体   繁体   English

GIT提交到错误的分支-TortoiseGit解决方案

[英]GIT commit to the wrong branch - TortoiseGit solution

I am trying to get used to the git repository. 我试图适应git存储库。

I had pushed a commit with edited new branch to the wrong one (which was added to the one I need). 我已将带有已编辑的新分支的提交推送到错误的分支(已添加到我需要的分支中)。 What should i do to cancel the commit? 我应该怎么做才能取消提交?

I work on Windows 7, using TortoiseGit. 我使用TortoiseGit在Windows 7上工作。

To delete the commit you could checkout the branch where you mistakenly created it and reset the last commit (as pointed out on nafas' link): 要删除提交,您可以检出错误创建分支的分支并重置最后一个提交(如nafas链接中所指出):

git reset HEAD^

But if you added the commit to the wrong branch you might want to move it before deleting it. 但是,如果将提交添加到错误的分支,则可能需要先删除它,然后再删除它。 To do so you first need to know its id, which you can get running the rev-parse command when you are checked out in the commit you want to move: 为此,您首先需要知道其ID,在要移动的提交中检出时,可以运行它的rev-parse命令:

git rev-parse HEAD

This will return the sha, something like 8a011a056ae70bcdd58dfb576552c2d0d2e80047 . 这将返回sha,类似于8a011a056ae70bcdd58dfb576552c2d0d2e80047 Now with the cherry-pick command you can bring that commit to the right branch. 现在,使用cherry-pick命令,您可以将该提交带入正确的分支。 Checkout the branch where you initially meant to create the commit run the command with the previously obtained id: 检出最初用于创建提交的分支,并使用先前获得的ID运行命令:

git cherry-pick 8a011a056ae70bcdd58dfb576552c2d0d2e80047

That would copy the commit to your current branch. 那会将提交复制到您的当前分支。 Now you can reset the wrong commit and delete the staged files, or update the wrong branch to one commit before, for instance: 现在,您可以重置错误的提交并删除暂存的文件,或者将错误的分支更新为之前的一次提交,例如:

git checkout wrongBranch^
git branch -f wrongBranch

Finally, to update the remote branch with a previous commit you would need to force the push: 最后,要使用先前的提交更新远程分支,您将需要强制执行推送:

git push -f origin wrongBranch

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

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