Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove my last commit in my local git repository

Tags:

git

When I do a 'git pull', I have conflict with the head commit. So I did a 'git rebase --abort'

Can I 'save' my commit to a "patch" and then do a git pull?

What I want to emulate is:

  • I did not commit, but I did a 'git stash' instead
  • Do a git pull to avoid any merge error

So I need somehow to 'turn back the clock'. Is that possible with git?

like image 469
michael Avatar asked Apr 11 '10 22:04

michael


People also ask

How do I remove a recent commit in local?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.


1 Answers

Your 7826b2 patch will still cause a conflict when it's applied after pulling, but you can do the following:

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

Another workflow is also possible:

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

The second workflow relies on the fact that Git keeps the 7826b2 commit in the reflog (you can think of it as the recycle bin) even though you reset the changes it introduced with the first line.

like image 57
Alexander Groß Avatar answered Oct 25 '22 05:10

Alexander Groß