我刚刚对错误的分支做了很好的提交。 如何撤消我的主分支中的最后一次提交,然后进行相同的更改并将它们放入我的升级分支?
#1楼
这个话题迟了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
是本地分支,而不是远程分支。
#2楼
如果您有干净(未修改)的工作副本
要回滚一个提交(确保在下一步中记下提交的哈希):
git reset --hard HEAD^
将该提交拉入另一个分支:
git checkout other-branch
git cherry-pick COMMIT-HASH
如果您已修改或未跟踪更改
另请注意, git reset --hard
会杀死您可能拥有的任何未经跟踪和修改的更改 ,因此如果您有这些更改 ,您可能更喜欢:
git reset HEAD^
git checkout .
#3楼
如果尚未推送更改,还可以执行软重置:
git reset --soft HEAD^
这将恢复提交,但将提交的更改放回到索引中。 假设分支相对于彼此是最新的,git将允许你进入另一个分支,然后你可以简单地提交:
git checkout branch
git commit
缺点是您需要重新输入提交消息。
#4楼
如果您已经推动了更改,则需要在重置HEAD后强行进行下一次推送。
git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force
警告:硬重置将撤消工作副本中任何未提交的修改,而强制重置将完全覆盖具有本地分支当前状态的远程分支的状态。
以防万一,在Windows上(使用Windows命令行,而不是Bash)它实际上是四个^^^^
而不是一个,所以它是
git reset --hard HEAD^^^^
#5楼
因此,如果您的方案是您已经承诺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/
#6楼
如果您想要应用更改的分支已存在(例如,分支开发 ),请按照下面的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 。
此外,如果适用,请延迟存储弹出(应用),直到您在目标分支合并为止。
#7楼
如果遇到此问题但您有Visual Studio,则可以执行以下操作:
右键单击您的分支,然后选择View History
:
右键单击要返回的提交。 并根据需要恢复或重置。
#8楼
我最近做了同样的事情,我不小心改变了主人,当我应该致力于其他分支。 但我没有推动任何东西。
如果您刚刚提交错误的分支,并且之后没有更改任何内容,并且没有推送到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..."
注意:在上面的例子中,我用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/
#9楼
要详细说明这个答案,如果你有多个提交要转移,例如develop
到new_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