Recover a git repo which was overwritten with an older version

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I've just run tar x by mistake instead of tar c, and overwrote my git repo with a few days older version. This will have left alone any files I had newly created though, including objects in .git. How can I recover as much as possible?







share|improve this question


























    up vote
    2
    down vote

    favorite












    I've just run tar x by mistake instead of tar c, and overwrote my git repo with a few days older version. This will have left alone any files I had newly created though, including objects in .git. How can I recover as much as possible?







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I've just run tar x by mistake instead of tar c, and overwrote my git repo with a few days older version. This will have left alone any files I had newly created though, including objects in .git. How can I recover as much as possible?







      share|improve this question














      I've just run tar x by mistake instead of tar c, and overwrote my git repo with a few days older version. This will have left alone any files I had newly created though, including objects in .git. How can I recover as much as possible?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 '17 at 15:00

























      asked Nov 10 '17 at 14:19









      sourcejedi

      19.5k32681




      19.5k32681




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Make a copy of the whole mess first, just in case.



          git fsck will show dangling objects and commits. Find the newest dangling commit. I fed all the commit hashes to gitk, and it just shows the most "recent" one at the top. As well as only having a few commits to deal with, I only had a single branch to recover - this would be more complex if there was more than one head.



          Then git stash to stash currently uncommitted changes for inspection.



          I was then able to git reset --hard to the commit hash determined above.



          git stash pop also merged back pretty cleanly. I think I got pretty much everything I wanted back again.






          share|improve this answer




















          • If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
            – Satō Katsura
            Nov 10 '17 at 14:29










          • That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
            – sourcejedi
            Nov 10 '17 at 14:58











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403762%2frecover-a-git-repo-which-was-overwritten-with-an-older-version%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Make a copy of the whole mess first, just in case.



          git fsck will show dangling objects and commits. Find the newest dangling commit. I fed all the commit hashes to gitk, and it just shows the most "recent" one at the top. As well as only having a few commits to deal with, I only had a single branch to recover - this would be more complex if there was more than one head.



          Then git stash to stash currently uncommitted changes for inspection.



          I was then able to git reset --hard to the commit hash determined above.



          git stash pop also merged back pretty cleanly. I think I got pretty much everything I wanted back again.






          share|improve this answer




















          • If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
            – Satō Katsura
            Nov 10 '17 at 14:29










          • That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
            – sourcejedi
            Nov 10 '17 at 14:58















          up vote
          2
          down vote



          accepted










          Make a copy of the whole mess first, just in case.



          git fsck will show dangling objects and commits. Find the newest dangling commit. I fed all the commit hashes to gitk, and it just shows the most "recent" one at the top. As well as only having a few commits to deal with, I only had a single branch to recover - this would be more complex if there was more than one head.



          Then git stash to stash currently uncommitted changes for inspection.



          I was then able to git reset --hard to the commit hash determined above.



          git stash pop also merged back pretty cleanly. I think I got pretty much everything I wanted back again.






          share|improve this answer




















          • If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
            – Satō Katsura
            Nov 10 '17 at 14:29










          • That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
            – sourcejedi
            Nov 10 '17 at 14:58













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Make a copy of the whole mess first, just in case.



          git fsck will show dangling objects and commits. Find the newest dangling commit. I fed all the commit hashes to gitk, and it just shows the most "recent" one at the top. As well as only having a few commits to deal with, I only had a single branch to recover - this would be more complex if there was more than one head.



          Then git stash to stash currently uncommitted changes for inspection.



          I was then able to git reset --hard to the commit hash determined above.



          git stash pop also merged back pretty cleanly. I think I got pretty much everything I wanted back again.






          share|improve this answer












          Make a copy of the whole mess first, just in case.



          git fsck will show dangling objects and commits. Find the newest dangling commit. I fed all the commit hashes to gitk, and it just shows the most "recent" one at the top. As well as only having a few commits to deal with, I only had a single branch to recover - this would be more complex if there was more than one head.



          Then git stash to stash currently uncommitted changes for inspection.



          I was then able to git reset --hard to the commit hash determined above.



          git stash pop also merged back pretty cleanly. I think I got pretty much everything I wanted back again.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 '17 at 14:19









          sourcejedi

          19.5k32681




          19.5k32681











          • If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
            – Satō Katsura
            Nov 10 '17 at 14:29










          • That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
            – sourcejedi
            Nov 10 '17 at 14:58

















          • If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
            – Satō Katsura
            Nov 10 '17 at 14:29










          • That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
            – sourcejedi
            Nov 10 '17 at 14:58
















          If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
          – Satō Katsura
          Nov 10 '17 at 14:29




          If you didn't do anything "clever" between the backup point and the restore point all commits should be already there, you might be able to get away with just moving HEAD. You'll lose some of the changes not already committed though.
          – Satō Katsura
          Nov 10 '17 at 14:29












          That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
          – sourcejedi
          Nov 10 '17 at 14:58





          That's what I did with the git reset --hard HASH command. The real trick is the bit before that, finding the hash of the commit you want to move to.
          – sourcejedi
          Nov 10 '17 at 14:58


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403762%2frecover-a-git-repo-which-was-overwritten-with-an-older-version%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay