Reputation: 171
I wanted to see if I could commit a change in a file but after doing so all the files of the repo have disappeared. How can I restore the previous version?
Here is what I've done (note this is the first I'm connecting to this Git server). First I init my repository:
$ rm -rf .git/
$ git init
$ git remote add origin https://remote.url/myProject.git
$ git commit -m "My first commit"
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.foundry/
.gitignore
.teamcity/
GitVersion.yml
README.md
config.ini
block.py
view.py
entities.py
nothing added to commit but untracked files present (use "git add" to track)
Then as I only modified entities.py I added it to the list for commit:
$ git add entities.py
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: entities.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
.foundry/
.gitignore
.teamcity/
GitVersion.yml
README.md
config.ini
block.py
view.py
Git detected the change in entities.py so I thought I was good to go:
$ git commit -m "entities.py first commit"
[master (root-commit) 1dc09d9] entities.py first commit
1 file changed, 535 insertions(+)
create mode 100644 entities.py
$ git push -f origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 6.64 KiB | 1.66 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://remote.url/myProject.git
+ 4a0d8a5...1dc09d9 master -> master (forced update)
When I wanted to check on the server if the commit worked, all the files of the project have vanished! There is just one file: entities.py. When I run git ls-tree
only one file is return:
$ git ls-tree -r master --name-only
entities.py
As you can see we've lost everything. I used to use TFS in the past and any mistake could be easily rollbacked in 1 minute, here I've spent hours trying to look for the right command in the Git book but can't find how to restore the previous version? Is it at least possible to rollback in Git?
git reset HEAD~
but I had the following errorfatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ git push origin HEAD~1:master
error: src refspec HEAD~1 does not match any
error: failed to push some refs to 'https://remote.url/myProject.git'
Upvotes: 3
Views: 2364
Reputation: 212198
Fortunately, you know the brief sha of the original master on the remote. The output of your initial git push -f
shows you that it was at 4a0d8a5
. If you have shell access to the remote, go there and create a branch (or a tag. You just need to make the commit reachable) at that commit with:
git branch original-master 4a0d8a5 # Run on origin
Now, in the local repo:
git fetch origin
git reset --hard 4a0d8a5
Upvotes: 5