Search code examples
gitgit-checkoutgit-switch

Can I retrieve uncommited changes from git after a few known git commands?


I have a local git repo where I did some substantial editing to a file file1.txt.

Then I issued a few git commands to look at older versions of this file (and other files). I totally had forgotten that thsi - of course! - would write old veersions to teh same file I just had edited!

The commands I issued are

  • Some git log ... commands
  • Some git checkout <COMMIT> file1.txt commands
  • Some git checkout <COMMIT> commands
  • One git status command
  • One git switch - and one git switch command
  • A final git checkout master command.

I don't think any of these commands gave me an warning that they would override my uncommitted changes. (I did however get an info about "detached HEAD mode".)

So I hope there is a chance to get the uncommitted changes back. Does git save uncommited changes somewhere before checking out old versions? Can I retrieve my uncommitted changes?


Solution

  • When you did git checkout <COMMIT> file1.txt you removed your uncommitted changes. This command tells git to go to the commit and update the file to the state at that point.

    All the other commands would have just brought your uncommitted changes along.

    git status would also have shown you whether your changes were still there.

    In the future use git stash or do a temp commit to hold on to your changes.