4

I'm still quite new to git revisioning and maybe just lost plenty of work (but I hope there is a way to go back, that's why I'm asking here).

I use SourceTree on OsX for git revisioning. I didn't commit for quite a long while and now just wanted to commit plenty of changes. I selected to push immediately after the commit. When the push took very long, I checked my folder and noticed that I accidentally had selected a big build folder that should not be checked in. So I cancelled the push (I got a message something like commit was successful but push was not).

So, even though I interrupted the push, I still saw this commit in my master branch. I wanted to undo this, so I could commit again (this time without the build folder). I rightclicked on the commit and there was a 'Reverse commit...' option, which I clicked. But instead of removing this revision, it added another revision labeled 'Revert "Name of the commit i wanted to undo"'.

Bad thing is - now all my local changes seem to have gone and my files are on the state from one month ago.

since the commit (of which I cancelled the push) is still in the list, I wonder if I can somehow go back there? Is there any chance to get the local changes I made during last month back? I do have a backup of my files, but that's also almost two weeks old.

This is how sourcetree currently looks like:

SourceTree Screenshot

as you can see, the previous successful commit/push i made was on may 20. Then today I tried to commit/push new features, but interrupted during pushing. Then i 'reversed' this (interrupted) commit, which aparently reset all my local files to the state from may 20. Sourcetree shows that there are 2 Pushs to make (which I assume are the interrupted push and the push for the 'reverse' commit).

Is there any way I can go back to my local state I had right before committing today?

Thank you so much

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76

1 Answers1

8

First undo the revert commit, throwing away the changes it created:

git reset --hard HEAD^

Then undo the updated render graphics commit, but keep those changes in your working tree (your local files):

git reset HEAD^
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • i want to go back to my local, uncommited changes from before the commit today. Those that i commited (but not pushed) in the 'updated render graphics loupe...' commit. Will this do it? – matthias_buehlmann Jun 12 '13 at 17:46
  • I don't want to leave my local files as they are now (as they are currently in the state as of may 20!). I want them to be in the state as they were when I commited (but not pushed!) today – matthias_buehlmann Jun 12 '13 at 17:50
  • I need the local changes from before the first commit today. not the local changes as they are right now. or let's say, i need the commit labeled 'updated render graphics' - which I did not push to remote however – matthias_buehlmann Jun 12 '13 at 17:51
  • @user1282931 ok, I updated the answer to reflect that you want remove both commits, but keep the state of the files after the first commit. – Klas Mellbourn Jun 12 '13 at 17:54
  • great - and this works even though the commits were not pushed to remote? I will try this, thank you. Is it enough to make a full copy of the folder containing the .git folder to have a backup of the current state, in case I mess up even more? :) – matthias_buehlmann Jun 12 '13 at 17:55
  • @user1282931 Yes, it is enough. Git repository information is completely inside the `.git` folder (unless you have a really weird setup) – Klas Mellbourn Jun 12 '13 at 17:57
  • It worked! I could kiss you! :) just for my understanding. When I committed the changes the first time, they were saved somewhere locally (where?) then I reversed this commit, which reset all my local files to the state of the previous commit and removed local changes - but the files I commited previously were still locally available somewhere? – matthias_buehlmann Jun 12 '13 at 18:26
  • @user1282931 Glad that it worked. Don't forget to mark my answer as accepted ;) Every commit you do effectively stores a complete image of your local files the git repository (under the .git folder). So you can always go back to the state of your local files as they were at a particular commit – Klas Mellbourn Jun 12 '13 at 18:49
  • and this image is deleted as soon as the commit is pushed successfully to remote, or will it take space on my disk forever? – matthias_buehlmann Jun 12 '13 at 19:30
  • @user1282931 It will take space on your disk forever. Eventually Git packs your commits so they don't take up that much space. – Klas Mellbourn Jun 13 '13 at 07:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31704/discussion-between-klas-mellbourn-and-user1282931) – Klas Mellbourn Jun 13 '13 at 08:01