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

时间:2010-05-31 05:30:22

标签: git git-commit

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

11 个答案:

答案 0 :(得分:838)

如果您尚未推送更改,也可以进行软重置:

git reset --soft HEAD^

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

git checkout branch
git commit

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

答案 1 :(得分:106)

如果您有干净(未修改)的工作副本

回滚一次提交(确保在下一步中记下提交的哈希值):

git reset --hard HEAD^

将该提交拉入另一个分支:

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

如果您有修改或未跟踪的更改

另请注意,git reset --hard杀死您可能拥有的任何未经跟踪和修改的更改,因此如果您拥有自己喜欢的更改

git reset HEAD^
git checkout .

答案 2 :(得分:104)

这个话题迟了4年,但这可能对某人有所帮助。

如果您在提交之前忘记创建新分支并在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

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

答案 3 :(得分:20)

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

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

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

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

git reset --hard HEAD^^^^

答案 4 :(得分:13)

我最近也做了同样的事情,我不小心提交了一个更改为master,当我应该已经提交到其他分支。但我没有推动任何东西。

如果您刚刚承诺错误的分支,并且之后没有更改任何内容,并且没有推送到回购,那么您可以执行以下操作:

// 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..."

注意:在上面的例子中,我用git reset HEAD~1重写1次提交。但是如果你想倒回n次提交,那么你可以执行git reset HEAD~n。

此外,如果您最终提交到错误的分支,并且在意识到您提交到错误的分支之前最终还要编写更多代码,那么您可以使用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

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

答案 5 :(得分:8)

对于错误分支上的多次提交

如果对您来说,这大约是一次提交,那么还有许多其他更容易的重置解决方案可用。对我来说,我有大约10个我在master分支上意外创建的提交,而不是我们称之为branch_xyz,并且我不想丢失提交历史记录。

您可以做什么,而救了我的是使用this answer作为参考,使用了四个步骤,即-

  1. master创建新的临时分支
  2. 合并到最初用于提交的分支中,即branch_xyz
  3. 撤消在master上的提交
  4. 删除临时分支。

以下是上述详细步骤-

  1. master(我不小心进行了许多更改的地方)创建新分支

    git checkout -b temp_branch_xyz
    

    注意:-b标志用于创建新分支
    只是为了验证我们是否正确,我将快速执行git branch以确保我们在temp_branch_xyz分支上,并使用git log来检查提交是否正确。< / p>

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

    git checkout branch_xyz
    

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

    分支中
    git merge temp_branch_xyz
    

    如果有的话,您可能需要在这里解决一些冲突。成功合并后,您可以按(我会)或继续进行下一步。

  3. this answer为参考撤消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
    

去那里。

答案 6 :(得分:7)

因此,如果你的情况是你已经承诺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

现在,master的所有提交都将在another-branch上。

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

答案 7 :(得分:3)

要详细说明this的答案,以防万一您有多个提交要移出,例如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

答案 8 :(得分:1)

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

此外,如果适用,请延迟隐藏弹出窗口(应用),直到您在目标分支机构合并为止。

答案 9 :(得分:1)

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

右键点击您的分支,然后选择View History

enter image description here

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

enter image description here

答案 10 :(得分:1)

对我来说,这是通过还原我推送的提交,然后将该提交樱桃挑选到另一个分支来解决的。

git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH

您可以使用git log查找正确的哈希,然后可以随时更改这些更改!