Chriz
Chriz

Reputation: 129

Restore files in a git-folder

I am in big trouble, as I was working on a project and all files are gone in the very moment, where I wanted to store them into a remote repository.

Here is what I did: I have a project, let's say in folder C:/project/
Then I created a remote repository and wanted to push my files to it.
I use the tool Git Extensions for Windows.
I created a new repository inside my project-folder. Immediately it sayd I should stage 1000+ files. So I staged them. Then I wanted to push them, so I had to set up the remote-repository, which I did.
Afterwards it showed that the staged files were on a different branch, so I switched to remote-master. Then I wanted to merge with the staged files, but they were not there anymore. So I wanted to switch back, but the other branch was not there anymore. All my previous files are gone. I tried to checkout local-master with no success.
Is there any way to get all my files back, or did I really delete them all? Maybe just resetting the whole repository?

I'd really appreciate your help here.

Greetings Chriz

Upvotes: 0

Views: 178

Answers (1)

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31225

As you probably know, git stores every versions of your files under the yourProject/.git/ folder.

It is possible to rollback to previous commits even if git history was altered by commands such as reset --hard, commit --amend, rebase etc thanks to the reflog.

Disclaimer : when git history is altered, some commits might be in a detached state which makes them elligible for garbage collection. Before going further, it is strongly recommended to backup the whole project directory or those commits might be permanently lost.

From git documentation :

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

The command git reflog shows the modifications that were made in the history. This enables to git reset to the desired state and rollback an unwanted git history alteration.

Example :

  • I create an empty repository
  • I create a file test.txt with the content hello
  • I commit it
  • I append the text world to test.txt
  • I amend the commit (this does not modify the previous commit but actually create a new commit. The previous commit still exists but is in detached state).
  • I use git reflog to find the previous commit.
  • I use git reset --hard to restore the previous commit.

Output :

/tmp/test [ mkdir /tmp/test
/tmp/test [ cd /tmp/test
/tmp/test [ git init
/tmp/test [ echo 'hello' >> test.txt
/tmp/test [ git add test.txt
/tmp/test [ git commit -m 'First commit'
[master (root commit) 60292e4] First commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
/tmp/test [ echo 'world' >> test.txt
/tmp/test [ git commit --amend -m 'oops'
[master 76ce76f] oops
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
/tmp/test [ git reflog
76ce76f HEAD@{0}: commit (amend): oops
60292e4 HEAD@{1}: commit (initial): First commit
/tmp/test [ git reset --hard 60292e4
/tmp/test cat test.txt
hello

Upvotes: 1

Related Questions