0

My repository is a mess right now...

I have a messed up merges and commits... I want to go back to a specific commit because it has the clean files. Right now I have a few filename.orig in my local, and its in my recent merge because of conflicts I was going through. I'd like to revert my local files to the clean commit (deleting all the crap), and remove all the bad commit/merge and make the clean commit master. Is this possible?

This is what my commit history looks like:

Merge (sha-1) ->
Clean Commit (sha-2) -> 
bad commit (sha-3) ->
bad merge (sha-4) ->
bad merge with .orig (sha-5)
hellomello
  • 8,219
  • 39
  • 151
  • 297
  • Have you pushed this remotely yet, or is it all just local still? Also, which "specific commit" were you "looking to go back to" exactly? –  Jun 27 '14 at 03:37
  • @Cupcake it is pushed to remote already, and I want to go back to `sha-2` – hellomello Jun 27 '14 at 04:13
  • Are you sharing the branch with other people? If yes, use the `git revert` solution in [this answer](http://stackoverflow.com/a/4114122/456814). If not, then you also have the option of using the `git reset --hard` option in [this answer](http://stackoverflow.com/a/4114122/456814), but you'll need to force push the new commits to your remote. –  Jun 27 '14 at 04:13

2 Answers2

1

As I already pointed out in the comments, your question is basically a duplicate of How to revert Git repository to a previous commit?, where the top voted answer offers two solutions that are relevant to you.

Solution 1: Hard Reset

You mentioned that you already pushed to your remote. That's OK, if you're not sharing your branch with other people, then you can simply do a hard reset back to the commit that you want to restore, and then force push back to your remote:

git reset --hard sha-2
git push origin HEAD -f

Warning: don't do this if you're sharing your branch with other people, or at least if it's not OK with them if you throw away commits on the remote branch like this. Those other people may have their own work based off of the old commits that you're throwing away, so it creates more work for those users to resync their own commits with the newly reset branch.

If you can't use this solution for the reason above, then you can use the following alternative solution, as mentioned in How to revert Git repository to a previous commit?.

Solution 2: Revert

This solution simply reverts the previous commits by creating new commits that are a reverse-patch. This is safe to use even if other users are sharing your branch, because it doesn't throw away new commits, it just creates new ones, so it isn't as confusing to synchronize with as throwing away commits would be:

git revert --no-edit sha2..sha5

That will revert the commits in the range (sha2, sha5], where sha2 is an exclusive starting point, meaning it isn't included in the reverts. Then you can simply push the results to your remote,

git push origin HEAD

Documentation

Additional Notes

I tried to have this question closed as a duplicate of How to revert Git repository to a previous commit?, but it looks like the question isn't getting enough close votes, and might not be closed, so that's why I decided I better answer it just in case it isn't.

Community
  • 1
  • 1
0

Try to use:

git reset --hard <previous-commit-hash>

This resets everything to your previous commit. and you will get the fresh copy of your previous-commit-hash.

Satish Azad
  • 2,302
  • 1
  • 16
  • 35
  • **Obligatory Warning: don't do this** if other people are sharing your branch with you, and they're not OK with you throwing away commits like this, because you'll force everyone else to resync with the new changes, which could possibly lead to a lot of confusion. **You'll find more complete answers about what to do in this situation [over here](http://stackoverflow.com/a/4114122/456814)**. –  Jun 27 '14 at 22:19