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 Answers
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.

- 81,660
- 23
- 145
- 268
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:
git fsck
to list dangling blobs and trees (means "files and directories not ended up in any commit" in git jargon)git cat-file -p SHA1-of-the-file-or-directory
to extract them.
Related links:

- 2,268
- 2
- 33
- 34
-
1Thanks! I use `git cat-file blob SHA-1` and find out some previous files. – Jun 23 '20 at 06:50
-
Sorry, I missed the option. Glad you figured out :) – snipsnipsnip Jun 23 '20 at 08:08
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.

- 782
- 6
- 15