I was using git for the first time, I had a directory with several programs written in it and did the following steps
git add .
git commit
, then it i got a message Aborting commit due to empty commit message.
git rm -r -f
ls
i have lost all my code. Is there any way that i can get them back, to my foolishness i don't have backup copy even.Things that i have followed till now
I googled out some found some of the command but they are not working
git stash
if i type this command i get
fatal: bad revision 'HEAD' fatal: bad revision 'HEAD' fatal: Needed a single revision You do not have the initial commit yet
git reset HEAD
, if i type this command i get
Fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. Use '--' to separate paths from revisions
I really need to get those files back!
Steps that i followed to create GIT
mkdir BareRepo
BareRepo
Directory i did git init
, git status
, git config --bool core.bare true
git clone BareRepo/ Programs/
Programs
directory i did all the above said thingBased on the observation of @the-malkolm.
From the information in the question, there are no commits, and the files were not tracked at any time. As such git does not really know about any of the files that have been deleted.
That said however, there is hope. Simply because you attempted to commit your files before deleting them, there is a phantom commit floating around with all of your files in it.
Here's an example:
$ git init
Initialised empty Git repository in /tmp/so/.git/
$ echo "find this text" > README.md
$ git add README.md
$ git commit -v
Aborting commit due to empty commit message.
$ git rm -rf .
rm 'README.md'
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
The above simulates the events in the question, and ordinarily this is the time to start rewriting the code again. There is no commit, the file is gone.
However inspecting the .git
repository yields some information:
$ tree .git/objects/
.git/objects/
├── 91
│ └── 9cdf847a6af7c655c8de1d101385f47f33e0f9
├── d6
│ └── 7d51abe2521dcd00cec138b72f5605125c1e41
├── info
└── pack
There are objects in the git repository despite there being no commits. It's necessary to identify which of the two objects is the tree:
$ git ls-tree 919cdf
fatal: not a tree object
$ git ls-tree d67d51
100644 blob 919cdf847a6af7c655c8de1d101385f47f33e0f9 README.md
$
The first reference is the blob representing the README.md file - there will be one blob per file in the repository, the second in this example is the tree reference.
Once the tree hash has been identified, it can be used to rebuild the index with read-tree:
$ git read-tree d67d51abe2521dcd00cec138b72f5605125c1e41
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#
# 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)
#
# deleted: README.md
$
At this time, the working copy is empty but the lost files are staged for commit.
Commit them:
$ git commit -m "phew"
And checkout to match the committed state of the repository:
$ git checkout .
$ ls
README.md
And then, all files exist and have been committed.