There is a Github repository(I am not the creator of the repository), I added one file and did the git push -f command. As a result, all files/packages in the repository were deleted except the file I added and pushed. I tried reverting all the deleted files by git revert <hash_of_commit> but it did not revert back all deleted files. Is there a way to restore all the deleted files?
-
You check `git checkout
` and manually copy that file, then move back to your branch and paste that file. – Stanislav Bashkyrtsev Apr 07 '21 at 08:09 -
Try this: https://gist.github.com/agarwalparas/d355a950148702cc7ba82abc4d1943bf It's basically using github's API to get your commit hash (by viewing the repo's event history), then use another github API to create new branch which points to selected hash – Kristian Apr 07 '21 at 08:16
-
@StanislavBashkyrtsev there is only one branch in the repository and it has only the file I pushed – dastan12 Apr 07 '21 at 08:21
-
But the original commit with the deleted file is still in your local repo. Therefore you first need to checkout that commit, and then you'll be able to checkout your only branch again and commit that file again. – Stanislav Bashkyrtsev Apr 07 '21 at 12:11
2 Answers
Try the following steps (all 3):
- Using the
git reflog
command to identify the last-known-good state of the repo - Then,
git reset --hard <commit>
to revert back to it - Then, another
git push --force
to reset the remote repository back to that state
You can also find some details on this post

- 827
- 7
- 14
TL;DR
- use
git reflog
to get hash of deleted commit - use
git checkout <hash>
to checkout/select that commit - use
git branch recover1 ; git checkout recover1
to make a branch that points to the deleted/recovered commit - do something about the recovered branch (e.g.: rebasing your changes to this branch, or push the branch to github, or point master to the recovered branch)
I created a repository for testing this: https://github.com/Kristian-Tan/coba-recover-forced-push
Preparation:
My scenario
This repo should contain this kind of history:
(initial_commit) <- (commit_1) <- (commit_2_bad) <- (commit_3) <- [master,head]
<- (commit_2_good)
commit commit 2 good
will be replaced (by force push) with commit commit 2 bad
The goal here is to recover commit 2 good and make the history as follows:
(initial_commit) <- (commit_1) <- (commit_2_good) <- (commit_3) <- [master,head]
Each commit will create an empty file in root directory with it's commit name (except initial_commit
which only creates README.md)
Steps on how it's done, what commands is run, and what are the outputs, will be written in wiki instead of in repository/source code
Steps
- create new repository in github called Kristian-Tan/coba-recover-forced-push
- create new file README.md through github's web interface and commit it as 'initial_commit'
- open terminal
$ cd /tmp
$ git clone git@github.com:Kristian-Tan/coba-recover-forced-push.git
Cloning into 'coba-recover-forced-push'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
$ cd /tmp/coba-recover-forced-push
- make first commit
$ touch commit_1
$ git add .
$ git commit -m 'commit_1'
[master 87aafec] commit_1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 commit_1
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Kristian-Tan/coba-recover-forced-push.git
1592784..87aafec master -> master
- make second commit (good)
$ touch commit_2_good
$ git add .
$ git commit -m 'commit_2_good'
[master 4a3f751] commit_2_good
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 commit_2_good
$ git push 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% (2/2), 287 bytes | 287.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Kristian-Tan/coba-recover-forced-push.git
87aafec..4a3f751 master -> master
- make second commit (bad)
$ rm commit_2_good
$ touch commit_2_bad
$ git add .
$ git commit --amend -m 'commit_2_bad'
[master 52710b6] commit_2_bad
Date: Wed Apr 7 15:31:38 2021 +0700
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 commit_2_bad
$ git push --force 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% (2/2), 287 bytes | 287.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Kristian-Tan/coba-recover-forced-push.git
+ 4a3f751...52710b6 master -> master (forced update)
- make third commit
$ touch commit_3
$ git add .
$ git commit -m 'commit_3'
[master 726b9ca] commit_3
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 commit_3
$ git push 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% (2/2), 254 bytes | 254.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:Kristian-Tan/coba-recover-forced-push.git
52710b6..726b9ca master -> master
Attempt 1: using https://gist.github.com/agarwalparas/d355a950148702cc7ba82abc4d1943bf
- attempt to recover 2nd good commit
$ curl -u Kristian-Tan https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/events
Enter host password for user 'Kristian-Tan':
[
{
"id": "15843469917",
"type": "GollumEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"pages": [
{
"page_name": "Home",
"title": "Home",
"summary": null,
"action": "edited",
"sha": "7b7c0e0d954d9763c71e1dd4deacad5a9f7293c9",
"html_url": "https://github.com/Kristian-Tan/coba-recover-forced-push/wiki/Home"
}
]
},
"public": true,
"created_at": "2021-04-07T08:35:32Z"
},
{
"id": "15843462908",
"type": "PushEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"push_id": 6863056879,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "726b9ca742619c59ed7efa7f3ae0d1fc9a5aaf13",
"before": "52710b6e7c0383382444ec0d2242b26be38adb58",
"commits": [
{
"sha": "726b9ca742619c59ed7efa7f3ae0d1fc9a5aaf13",
"author": {
"email": "myemail@myprovider",
"name": "Kristian"
},
"message": "commit_3",
"distinct": true,
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/commits/726b9ca742619c59ed7efa7f3ae0d1fc9a5aaf13"
}
]
},
"public": true,
"created_at": "2021-04-07T08:35:03Z"
},
{
"id": "15843442050",
"type": "GollumEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"pages": [
{
"page_name": "Home",
"title": "Home",
"summary": null,
"action": "edited",
"sha": "ae82df1e2efd947d5e9257f8f5a49c3cc61443de",
"html_url": "https://github.com/Kristian-Tan/coba-recover-forced-push/wiki/Home"
}
]
},
"public": true,
"created_at": "2021-04-07T08:33:32Z"
},
{
"id": "15843439721",
"type": "PushEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"push_id": 6863045743,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "52710b6e7c0383382444ec0d2242b26be38adb58",
"before": "4a3f751423e750326c463ac400812a4f51f768a8",
"commits": [
{
"sha": "52710b6e7c0383382444ec0d2242b26be38adb58",
"author": {
"email": "myemail@myprovider",
"name": "Kristian"
},
"message": "commit_2_bad",
"distinct": true,
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/commits/52710b6e7c0383382444ec0d2242b26be38adb58"
}
]
},
"public": true,
"created_at": "2021-04-07T08:33:22Z"
},
{
"id": "15843418773",
"type": "PushEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"push_id": 6863035508,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "4a3f751423e750326c463ac400812a4f51f768a8",
"before": "87aafec7ddcb261425cfe00ce74ba3b7d51c66f4",
"commits": [
{
"sha": "4a3f751423e750326c463ac400812a4f51f768a8",
"author": {
"email": "myemail@myprovider",
"name": "Kristian"
},
"message": "commit_2_good",
"distinct": true,
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/commits/4a3f751423e750326c463ac400812a4f51f768a8"
}
]
},
"public": true,
"created_at": "2021-04-07T08:31:52Z"
},
{
"id": "15843394061",
"type": "GollumEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"pages": [
{
"page_name": "Home",
"title": "Home",
"summary": null,
"action": "edited",
"sha": "6eb6ae58e3e71da932b3a89f58e8a4f68c81cb0b",
"html_url": "https://github.com/Kristian-Tan/coba-recover-forced-push/wiki/Home"
}
]
},
"public": true,
"created_at": "2021-04-07T08:30:09Z"
},
{
"id": "15843386894",
"type": "PushEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"push_id": 6863020078,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "87aafec7ddcb261425cfe00ce74ba3b7d51c66f4",
"before": "15927842cac8305bebbf810d83c42f7574c17537",
"commits": [
{
"sha": "87aafec7ddcb261425cfe00ce74ba3b7d51c66f4",
"author": {
"email": "myemail@myprovider",
"name": "Kristian"
},
"message": "commit_1",
"distinct": true,
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/commits/87aafec7ddcb261425cfe00ce74ba3b7d51c66f4"
}
]
},
"public": true,
"created_at": "2021-04-07T08:29:39Z"
},
{
"id": "15843359934",
"type": "GollumEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"pages": [
{
"page_name": "Home",
"title": "Home",
"summary": null,
"action": "created",
"sha": "ad832555ceaec9b467c52f7c4e639f0675012eb8",
"html_url": "https://github.com/Kristian-Tan/coba-recover-forced-push/wiki/Home"
}
]
},
"public": true,
"created_at": "2021-04-07T08:27:46Z"
},
{
"id": "15843330684",
"type": "CreateEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"ref": "master",
"ref_type": "branch",
"master_branch": "master",
"description": null,
"pusher_type": "user"
},
"public": true,
"created_at": "2021-04-07T08:25:45Z"
},
{
"id": "15843215446",
"type": "CreateEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"ref": null,
"ref_type": "repository",
"master_branch": "master",
"description": null,
"pusher_type": "user"
},
"public": true,
"created_at": "2021-04-07T08:17:44Z"
}
]
- event-of-interest found:
{
"id": "15843418773",
"type": "PushEvent",
"actor": {
"id": 18624029,
"login": "Kristian-Tan",
"display_login": "Kristian-Tan",
"gravatar_id": "",
"url": "https://api.github.com/users/Kristian-Tan",
"avatar_url": "https://avatars.githubusercontent.com/u/18624029?"
},
"repo": {
"id": 355466035,
"name": "Kristian-Tan/coba-recover-forced-push",
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push"
},
"payload": {
"push_id": 6863035508,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "4a3f751423e750326c463ac400812a4f51f768a8",
"before": "87aafec7ddcb261425cfe00ce74ba3b7d51c66f4",
"commits": [
{
"sha": "4a3f751423e750326c463ac400812a4f51f768a8",
"author": {
"email": "myemail@myprovider",
"name": "Kristian"
},
"message": "commit_2_good",
"distinct": true,
"url": "https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/commits/4a3f751423e750326c463ac400812a4f51f768a8"
}
]
},
"public": true,
"created_at": "2021-04-07T08:31:52Z"
},
- we got that commit_2_good have hash of
4a3f751423e750326c463ac400812a4f51f768a8
- attempt to create a new branch that points to that hash
$ curl -u Kristian-Tan -X POST -d '{"ref":"refs/heads/recover1", "sha":"4a3f751423e750326c463ac400812a4f51f768a8"}' https://api.github.com/repos/Kristian-Tan/coba-recover-forced-push/git/refs
Enter host password for user 'Kristian-Tan':
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#create-a-reference"
}
- it seems that we cannot create a reference (branch) for that commit (maybe I make a mistake somewhere, or maybe the branch is already permanently deleted by github since it's already force pushed)
Attempt 2: using git reflog
and git checkout
to get the commit back
- another attempt: using
git reflog
$ git reflog
726b9ca (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: commit: commit_3
52710b6 HEAD@{1}: commit (amend): commit_2_bad
4a3f751 HEAD@{2}: commit: commit_2_good
87aafec HEAD@{3}: commit: commit_1
1592784 HEAD@{4}: clone: from github.com:Kristian-Tan/coba-recover-forced-push.git
$ git checkout 4a3f751
Note: switching to '4a3f751'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 4a3f751 commit_2_good
$ git status
HEAD detached at 4a3f751
nothing to commit, working tree clean
$ ls -al
total 4
drwxr-xr-x 3 kristian kristian 120 Apr 7 15:57 .
drwxrwxrwt 21 root root 600 Apr 7 15:55 ..
-rw-r--r-- 1 kristian kristian 0 Apr 7 15:29 commit_1
-rw-r--r-- 1 kristian kristian 0 Apr 7 15:57 commit_2_good
drwxr-xr-x 8 kristian kristian 280 Apr 7 15:57 .git
-rw-r--r-- 1 kristian kristian 1652 Apr 7 15:26 README.md
using
git reflog
seems like a success, BUT you have to run it in local repository that you force pushed from (you cannot run it from a newly cloned repository)attempt to recover from outside of local repository that you force pushed from (failed)
$ git clone --branch 4a3f751 git@github.com:Kristian-Tan/coba-recover-forced-push.git
Cloning into 'coba-recover-forced-push'...
fatal: Remote branch 4a3f751 not found in upstream origin
$ git clone --branch 4a3f751423e750326c463ac400812a4f51f768a8 git@github.com:Kristian-Tan/coba-recover-forced-push.git
Cloning into 'coba-recover-forced-push'...
fatal: Remote branch 4a3f751423e750326c463ac400812a4f51f768a8 not found in upstream origin
$ git clone git@github.com:Kristian-Tan/coba-recover-forced-push.gitCloning into 'coba-recover-forced-push'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 2), reused 6 (delta 1), pack-reused 0
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (2/2), done.
$ cd coba-recover-forced-push/
$ git checkout 4a3f751423e750326c463ac400812a4f51f768a8
fatal: reference is not a tree: 4a3f751423e750326c463ac400812a4f51f768a8
$ git reflog
726b9ca (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: clone: from github.com:Kristian-Tan/coba-recover-forced-push.git
Recovering
- recovering by creating a branch
$ git checkout 4a3f751423e750326c463ac400812a4f51f768a8
Note: switching to '4a3f751'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 4a3f751 commit_2_good
$ git branch recover1
$ git checkout recover1
Switched to branch 'recover1'
- rebasing to get desired history
(initial_commit) <- (commit_1) <- (commit_2_good) <- (commit_3) <- [master,head]
$ git checkout master
Switched to branch 'master'
$ git rebase -i recover1
- interactive terminal shows up GNU Nano (since it's my default text editor), edit the file as such (comment out
commit_2_bad
)
GNU nano 5.6.1 /tmp/coba-recover-forced-push-1/.git/rebase-merge/git-rebase-todo Modified
# pick a123eb8 commit_2_bad
pick 00db663 commit_3
# Rebase 3baa9f0..00db663 onto 3baa9f0 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
- result:
Successfully rebased and updated refs/heads/master.
$ git log
commit 726b9ca742619c59ed7efa7f3ae0d1fc9a5aaf13 (HEAD -> master)
Author: Kristian <myemail@myprovider>
Date: Wed Apr 7 15:34:50 2021 +0700
commit_3
commit 4a3f751423e750326c463ac400812a4f51f768a8 (recover1)
Author: Kristian <myemail@myprovider>
Date: Wed Apr 7 15:31:38 2021 +0700
commit_2_good
commit 87aafec7ddcb261425cfe00ce74ba3b7d51c66f4
Author: Kristian <myemail@myprovider>
Date: Wed Apr 7 15:29:30 2021 +0700
commit_1
commit 15927842cac8305bebbf810d83c42f7574c17537
Author: Kristian Tanuwijaya <myemail@myprovider>
Date: Wed Apr 7 15:25:45 2021 +0700
initial_commit

- 2,456
- 8
- 23
- 23