0

I needed to fix a change from a previous release. So I commited my current changes using git -a -m "latest"

and then followed this QA here: How to revert Git repository to a previous commit? to jump back about a month. This worked very well. I made the changes needed. I then performed a

git checkout master

To get back to the present. Unfortunatley I then discovered that when I performed my commit - I'd forgotten to perform a

git add .

As such although any changes I've made were there...any new files did not get committed.

Are these permanently lost or is there a way back?

Community
  • 1
  • 1
Chris Nevill
  • 5,922
  • 11
  • 44
  • 79
  • They would be untracked files and so should not have been touched by switching branches, resetting etc. they would only be removed by something like `git clean`. Did you check if they're still there? – Jonathan Wakely Sep 09 '15 at 10:11
  • Ah... I've figured it out what I've done... I performed a git add. when I was in the old branch... hence I've added the new files to the old branch?! – Chris Nevill Sep 09 '15 at 10:19
  • Thus they were then tracked... but disappear under the current branch...! – Chris Nevill Sep 09 '15 at 10:20
  • 1
    That explains it. You might want to consider making your shell prompt display your Git status (see https://git-scm.com/book/en/v2/Git-in-Other-Environments-Git-in-Bash) which can show a `%` to tell you there are untracked files. – Jonathan Wakely Sep 09 '15 at 10:57
  • I'll take a look at that thanks. – Chris Nevill Sep 09 '15 at 12:58

1 Answers1

2

those files are not lost at all:

when switching to another branch, any uncommitted files will be left untouched. that is: you will find them in your working directory, even if you are in the new branch!

if the switch-to branch already contains files with the same name, git will refuse to switch, and ask you to remove or commit the local files first.

if however, you manually remove those files, then you have just deleted files outside the control of git - which means that the only way to recover those files is outside of git as well (e.g. your local backup)

umläute
  • 28,885
  • 9
  • 68
  • 122