2

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?

Unsuccessful attempts to solve this issue

fatal: 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'
Hydraxize
  • 161
  • 10
  • Does this answer your question? [How can I recover from an erronous git push -f origin master?](https://stackoverflow.com/questions/3973994/how-can-i-recover-from-an-erronous-git-push-f-origin-master) – Joe May 21 '21 at 11:12
  • If you only commit one change to the sever, you should see only one file on the server, no? – Alex May 21 '21 at 11:15
  • 2
    `rm -rf .git/` lost your local history; `git push -f origin master` overwrote what was on the server. You'll need to recover a backup of your local `.git` directory, or ask the admin of your remote repo if it's possible to restore from backup there. You *may* be able to re-clone the repo, then `git reset 4a0d8a5`. – Joe May 21 '21 at 11:18
  • If you have any tags or branches on the remote, this will be simpler. In particular, if you had a tag 'foo' on the original master, you could simply `git fetch --tags; git reset --hard foo` If there is no ref to the original master, you will have to find the sha. Fortunately, you already have it: `4a0d8a5` – William Pursell May 21 '21 at 11:42
  • As @Joe said, `rm -rf .git/` was the real source of the problem. I'd put it more strongly than "lost your local history": this *completely destroyed the Git repository* (which is stored in that `.git` directory), after which you made a new one that had no commits at all. You then made one commit in it, with one file in the one commit. That's why you had to force-push: so as to tell the server to throw away all *its* commits in favor of this new and improved single commit with one file in it. – torek May 21 '21 at 18:44

1 Answers1

5

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
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • That's probably the right answer but it didn't work because of another error message `error: unknown switch s'`. At the end we decided to start from scratch, create a new repo on the server and locally. – Hydraxize May 21 '21 at 12:17