2

I have work on my project and backup only with "git add .". Unfortunately, I forget to commit my code and I loss the file. Right now, I git clone the previous code I have last time. How can I track the history on my previous "git add" and able to restore the code I miss? Thank you.

3 Answers3

2

How can I track the history on my previous "git add" and able to restore the code I miss?

You can't. Git doesn't track any changes until you commit them. If you still have the directory where you made these changes, you can view the final result there, but you can't get the history of the changes because there is none.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

git add ed files are stored under .git/objects for a while, even if they have not been committed. You may be able to recover them if it is not GC'd.

To find out files you lost:

  1. git fsck to list dangling blobs and trees (means "files and directories not ended up in any commit" in git jargon)
  2. git cat-file -p SHA1-of-the-file-or-directory to extract them.

Related links:

snipsnipsnip
  • 2,268
  • 2
  • 33
  • 34
1

Unfortunately, git only keeps the history of committed files. Even so, unless the commits have been pushed to the remote server, cloning all over again would erase any local history.

As a piece of advice, committing adds a reflog entry which makes it much easier to go back to a previous state.

You should also check the stash functionality.

Cristian Sarghe
  • 782
  • 6
  • 15