如何删除我在本地git存储库中的最后一次提交

时间:2010-04-11 22:44:23

标签: git

当我做'git pull'时,我与头部提交有冲突。所以我做了'git rebase --abort'

我可以'保存'我的提交到“补丁”然后做一个git pull吗?

我想模仿的是:

  • 我没有提交,但我做了'git stash'而不是
  • 执行git pull以避免任何合并错误

所以我需要以某种方式'扭转时钟'。用git可以吗?

4 个答案:

答案 0 :(得分:66)

您的7826b2补丁在拉动后仍然会引起冲突,但您可以执行以下操作:

git reset --soft HEAD^
git stash
git pull
git stash pop # Will cause a conflict
git commit    # Re-commit 7826b2

另一种工作流程也是可能的:

git reset --hard HEAD^
git pull
git cherry-pick 7826b2 # Will cause a conflict

第二个工作流程依赖于Git将7826b2提交保留在reflog中(您可以将其视为回收站),即使您重置了第一行引入的更改。

答案 1 :(得分:5)

如果您确实在本地存储库中提交了更改,则可以直接进行更改:

  

git fetch origin

     

git rebase origin / master

这将从本地主服务器中解除您的本地提交,从origin / master中应用新的提交,然后重放您的本地提交。

在您的情况下,这将导致冲突,git将告诉您需要编辑哪些文件来解决冲突。执行此操作后,git add这些文件,然后git commit没有参数重新提交更改。

答案 2 :(得分:3)

首先我建议对你的分支进行备份:

git branch your_branch_backup

然后最简单的方法是:

git reset --soft <commit> #Go back in time but without loosing your changes
git stash                 #Stash your changes
git pull                  #Refresh your branch with origin
git stash pop             #Re-apply your changes on the refreshed branch 

然后你可以照常提交

答案 3 :(得分:0)

有一种方法,实际上在git中,我确信有很多方法可以做到这一点......

你可以这样做:

git stash     # Stash your changes
git pull      # Pull the changes
git stash pop # Pop your stash

你可以这样做:

git pull --rebase # Bring in the changes and apply yours on top of it.

你可以这样做:

git stash     # Stash your changes
git checkout <commit> # Pull a specific commit.
git stash pop # Pop your stash

希望它有所帮助。