git (sourcetree) go back to commit that has not been pushed and was 'reversed' accidentally

12,436

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^
Share:
12,436
matthias_buehlmann
Author by

matthias_buehlmann

Independent Software Engineer and Entrepreneur. Developed software for clients including Google, ABB, Swiss Federal Railways, Swiss Post ...

Updated on June 15, 2022

Comments

  • matthias_buehlmann
    matthias_buehlmann almost 2 years

    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

  • matthias_buehlmann
    matthias_buehlmann almost 11 years
    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
    matthias_buehlmann almost 11 years
    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
    matthias_buehlmann almost 11 years
    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
  • Klas Mellbourn
    Klas Mellbourn almost 11 years
    @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.
  • matthias_buehlmann
    matthias_buehlmann almost 11 years
    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? :)
  • Klas Mellbourn
    Klas Mellbourn almost 11 years
    @user1282931 Yes, it is enough. Git repository information is completely inside the .git folder (unless you have a really weird setup)
  • matthias_buehlmann
    matthias_buehlmann almost 11 years
    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?
  • Klas Mellbourn
    Klas Mellbourn almost 11 years
    @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
  • matthias_buehlmann
    matthias_buehlmann almost 11 years
    and this image is deleted as soon as the commit is pushed successfully to remote, or will it take space on my disk forever?
  • Klas Mellbourn
    Klas Mellbourn almost 11 years
    @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
    Klas Mellbourn almost 11 years