Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I undo commits that haven't been pushed yet?

I made two commits without pushing them. There have been multiple other commits pushed to my branch. I haven't yet pulled those commits. I want to destroy my two commits that haven't been pushed, and then just get the multiple other commits that have been pushed by others. Can someone help me with this?

I thought to reverse the two commits, one by one. But I want to keep my other changes. Will this work?

like image 940
SirRupertIII Avatar asked Jan 11 '23 05:01

SirRupertIII


1 Answers

If you have two commits that you haven't pushed, and you don't want them at all, then rather than reverse then, you should reset to the commit that want to start from and then pull.

So, I'm assuming that you have the following situation.

$ git log --oneline --graph
* fde0ba4 Commit 2 (don't want)
* d7be6f3 Commit 1 (don't want)
* 7a70e45 Before my new commits (start here!)
* db58591 Even earlier
...

And so on. You don't want the first two commits, but you want the the third onwards. The first step is to reset back to where you want to start from.

git reset --hard 7a70e45

Note that this is a destructive operation -- you'll lose those commits, and the content of those commits, so do this with care! See the git reset documentation if you're not sure if --hard is appropriate.

After resetting, you should be able to pull without a problem.

git pull
like image 102
tbekolay Avatar answered Jan 18 '23 23:01

tbekolay