-1

I checked out my previous git commit per https://stackoverflow.com/a/2007704/1032531. Note that it stated to include the .. Am I hosed and should I have studied up more on this subject before doing this? How can I get back the changes I was in process of making before checking out this other branch?

[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   public/index.php
        ..and about 40 other files

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

no changes added to commit (use "git add" and/or "git commit -a")

[michael@devserver autoapp]$ git log
commit 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e (HEAD -> master)
Author: Michael Reed <michael@gmail.com>
... and lists the other commits but the one above was the latest one

[michael@devserver autoapp]$ git checkout 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e .
[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

nothing added to commit but untracked files present (use "git add" to track)
[michael@devserver autoapp]$ git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

nothing added to commit but untracked files present (use "git add" to track)
[michael@devserver autoapp]$ git diff
[michael@devserver autoapp]$ git checkout -
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

[michael@devserver autoapp]$
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • Considering the files were not staged or committed, it seems unlikely you'll be able to get those changes back. – dimwittedanimal Jun 25 '18 at 12:41
  • @dimwittedanimal Damn! I had also gone too long before a commit. I've been searching what the period meant and haven't found it yet. Do you know what it does? – user1032531 Jun 25 '18 at 12:42
  • Commit more often is a lesson I learnt the hard way. – evolutionxbox Jun 25 '18 at 13:39
  • Possible duplicate of [Get back the changes after accidental checkout?](https://stackoverflow.com/questions/2961240/get-back-the-changes-after-accidental-checkout) – phd Jun 25 '18 at 19:55

1 Answers1

0

In the command

git checkout 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e .

the . is a path specifier. Providing a path specifier fundamentally changes what checkout does.

Without a path specifier, checkout moves the HEAD. This means it either "checks out" a branch, or "checks out" a commit (which may or may not be the tip of a branch) in "detached HEAD" state. Unless you tell git otherwise, it would warn you before clobbering uncommitted changes (i.e. it would resist doing the checkout).

But with a path specifier, checkout updates the working tree version of a file from another version. Because this command is seen as explicitly requesting working files to be overwritten - and is in fact the way that you would revert unstaged changes - it isn't so cautious as the other form of checkout.

It is unfortunate that these have the same basic command name, because they're very different operations and the latter is much more dangerous than the former.

While git is very good at preserving changes that have been committed (or even stashed), if you lose working files it doesn't offer much help. Short of an external backup solution, I'm afraid you may just have to start over.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52