如何修复提交到错误的 Git 分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2941517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:28:11  来源:igfitidea点击:

How to fix committing to the wrong Git branch?

gitgit-commit

提问by mikewilliamson

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?

我刚刚对错误的分支做了一个非常好的提交。如何撤消 master 分支中的最后一次提交,然后进行相同的更改并将它们放入我的升级分支?

回答by Blair Holloway

If you haven't yet pushed your changes, you can also do a soft reset:

如果您还没有推送您的更改,您还可以进行软重置:

git reset --soft HEAD^

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:

这将恢复提交,但将提交的更改放回索引中。假设分支相对于彼此是最新的,git 会让你结账到另一个分支,然后你可以简单地提交:

git checkout branch
git commit

The disadvantage is that you need to re-enter your commit message.

缺点是需要重新输入提交信息。

回答by fotanus

4 years late on the topic, but this might be helpful to someone.

在这个话题上晚了 4 年,但这可能对某人有帮助。

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:

如果你在提交之前忘记创建一个新分支并在 master 上提交所有,无论你做了多少次提交,下面的方法更容易:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

Now you have your master branch equals to origin/masterand all new commits are on my_feature. Note that my_featureis a local branch, not a remote one.

现在您的主分支等于origin/master并且所有新提交都在my_feature. 请注意,这my_feature是一个本地分支,而不是远程分支。

回答by Michael Mrozek

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):

回滚一个提交(确保记下下一步的提交哈希):

git reset --hard HEAD^

To pull that commit into a different branch:

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

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

If you have modified or untracked changes

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

Also note that git reset --hardwill kill any untracked and modified changesyou might have, so if you have those you might prefer:

另请注意,这git reset --hard终止您可能拥有的任何未跟踪和修改的更改,因此如果您有可能更喜欢的更改

git reset HEAD^
git checkout .

回答by Igor Zevaka

If you already pushed your changes, you will need to force your next push after resetting the HEAD.

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

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

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

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

git reset --hard HEAD^^^^

回答by Ali Mizan

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.

我最近做了同样的事情,当我应该提交给其他分支时,我不小心将更改提交给了 master。但我什么都没推。

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:

如果您刚刚提交到错误的分支,并且此后没有更改任何内容,并且没有推送到 repo,那么您可以执行以下操作:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

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.

注意:在上面的例子中,我用 git reset HEAD~1 回退了 1 个提交。但是如果你想回退 n 次提交,那么你可以执行 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:

此外,如果您最终提交到错误的分支,并且在意识到您提交到错误的分支之前还编写了更多代码,那么您可以使用 git stash 来保存正在进行的工作:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

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/

注意:我使用这个网站作为参考 https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/

回答by Lorcan O'Neill

So if your scenario is that you've committed to masterbut meant to commit to another-branch(which may or not may not already exist) but you haven't pushed yet, this is pretty easy to fix.

因此,如果您的情况是您已承诺master但打算承诺another-branch(可能已经存在,也可能不存在),但您还没有推送,这很容易解决。

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

Now all your commits to masterwill be on another-branch.

现在,您对的所有承诺master都将启用another-branch

Sourced with love from: http://haacked.com/archive/2015/06/29/git-migrate/

源自于爱:http: //haacked.com/archive/2015/06/29/git-migrate/

回答by arsenius

To elaborate on thisanswer, in case you have multiple commits to move from, e.g. developto new_branch:

为了阐述这个答案,如果你有多次提交到例如从移动developnew_branch

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started

回答by Mitali Cyrus

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 accidentally made to masterinstead of, let's call it branch_xyz, and I did not want to lose the commit history.

如果对您来说,它只是大约 1 次提交,那么还有许多其他更简单的重置解决方案可用。对我来说,我意外地做了大约 10 次提交,master而不是,我们称之为branch_xyz,我不想丢失提交历史记录。

What you could do, and what saved me was using this answeras a reference, using a 4 step process, which is -

你能做什么,以及拯救我的是使用这个答案作为参考,使用 4 步过程,即 -

  1. Create a new temporary branch from master
  2. Merge into the branch originally intended for commits, i.e. branch_xyz
  3. Undo commits on master
  4. Delete the temporary branch.
  1. 从创建一个新的临时分支 master
  2. 合并到最初用于提交的分支,即 branch_xyz
  3. 撤消提交 master
  4. 删除临时分支。

Here are the above steps in details -

以下是上述步骤的详细信息 -

  1. Create a new branch from the master(where I had accidentally committed a lot of changes)

    git checkout -b temp_branch_xyz
    

    Note: -bflag is used to create a new branch
    Just to verify if we got this right, I'd do a quick git branchto make sure we are on the temp_branch_xyzbranch and a git logto check if we got the commits right.

  2. Merge the temporary branch into the branch originally intended for the commits, i.e. branch_xyz.
    First, switch to the original branch i.e. branch_xyz(You might need to git fetchif you haven't)

    git checkout branch_xyz
    

    Note: Not using -bflag
    Now, let's merge the temporary branch into the branch we have currently checkout out branch_xyz

    git merge temp_branch_xyz
    

    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.

  3. Undo the accidental commits on masterusing this answeras reference, first switch to the master

    git checkout master
    

    then undo it all the way back to match the remote (or to particular commit, if you want)

    git reset --hard origin/master
    

    Again, I'd do a git logbefore and after just to make sure that the intended changes took effect.

  4. 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. branch_xyz(If you stay on masterand execute the command below, you might get a error: The branch 'temp_branch_xyz' is not fully merged), so let's

    git checkout branch_xyz
    

    and then delete the proof of this mishap

    git branch -d temp_branch_xyz
    
  1. master(我不小心提交了很多更改的地方) 创建了一个新分支

    git checkout -b temp_branch_xyz
    

    注意:-b标志用于创建一个新分支
    只是为了验证我们是否正确,我会快速git branch确保我们在temp_branch_xyz分支上并git log检查我们是否正确提交。

  2. 将临时分支合并到最初用于提交的分支中,即branch_xyz.
    首先,切换到原始分支即branch_xyzgit fetch如果你没有,你可能需要)

    git checkout branch_xyz
    

    注意:不使用-b标志
    现在,让我们将临时分支合并到我们当前检出的分支中branch_xyz

    git merge temp_branch_xyz
    

    如果有冲突,您可能需要处理这里的一些冲突。成功合并后,您可以推动(我愿意)或继续下一步。

  3. 撤消master使用此答案作为参考的意外提交,首先切换到master

    git checkout master
    

    然后将其撤回以匹配远程(或特定提交,如果需要)

    git reset --hard origin/master
    

    同样,我会在git log之前和之后做一次,以确保预期的更改生效。

  4. 擦除证据,即删除临时分支。为此,首先您需要检出临时合并到的分支,即branch_xyz(如果您继续master执行下面的命令,您可能会得到一个error: The branch 'temp_branch_xyz' is not fully merged),所以让我们

    git checkout branch_xyz
    

    然后删除这起事故的证据

    git branch -d temp_branch_xyz
    

There you go.

你去吧。

回答by Trevor

If you run into this issue and you have Visual Studio, you can do the following:

如果您遇到此问题并且您拥有 Visual Studio,则可以执行以下操作:

Right-click on your branch and select View History:

右键单击您的分支并选择View History

enter image description here

在此处输入图片说明

Right-click on commit you want to go back to. And Revert or Reset as needed.

右键单击要返回的提交。并根据需要恢复或重置。

enter image description here

在此处输入图片说明

回答by fbicknel

If the branch you wanted to apply your changes to already exists (branch develop, for example), follow the instructions that were provided by fotanusbelow, then:

如果您想要应用更改的分支已经存在(例如,分支develop),请按照下面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

And obviously you could use tempbranchor any other branch name instead of my_featureif you wanted.

显然,如果您愿意,您可以使用tempbranch或任何其他分支名称而不是my_feature

Also, if applicable, delay the stash pop (apply) until after you've merged at your target branch.

此外,如果适用,将存储弹出(应用)延迟到您在目标分支合并之后。