有一个 Github 存储库(我不是存储库的创建者),我添加了一个文件并执行了 git push -f 命令。结果,除了我添加和推送的文件之外,存储库中的所有文件/包都被删除了。我尝试通过 git revert
答案 0 :(得分:0)
git reflog
获取已删除提交的哈希git checkout <hash>
签出/选择该提交git branch recover1 ; git checkout recover1
创建一个指向已删除/恢复的提交的分支我创建了一个用于测试的存储库:https://github.com/Kristian-Tan/coba-recover-forced-push
这个 repo 应该包含这样的历史:
(initial_commit) <- (commit_1) <- (commit_2_bad) <- (commit_3) <- [master,head]
<- (commit_2_good)
commit commit 2 good
将被替换(通过强制推送)提交 commit 2 bad
这里的目标是恢复提交 2 并创建如下历史记录:
(initial_commit) <- (commit_1) <- (commit_2_good) <- (commit_3) <- [master,head]
每次提交都会在根目录中创建一个空文件,并带有提交名称(initial_commit
除外,它只创建 README.md)
关于如何完成的步骤、运行什么命令以及输出什么,将写在 wiki 而不是存储库/源代码中
$ 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
$ 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
$ 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
$ 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)
$ 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
$ 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"
}
]
{
"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"
},
4a3f751423e750326c463ac400812a4f51f768a8
$ 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"
}
git reflog
和 git checkout
取回提交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
使用 git reflog
似乎很成功,但是您必须在强制推送的本地存储库中运行它(您不能从新克隆的存储库中运行它)
尝试从您强制推送的本地存储库外部恢复(失败)
$ 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
$ 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'
(initial_commit) <- (commit_1) <- (commit_2_good) <- (commit_3) <- [master,head]
$ git checkout master
Switched to branch 'master'
$ git rebase -i recover1
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.
#
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
答案 1 :(得分:0)
尝试以下步骤(全部 3 个):
git reflog
命令确定存储库的最后一个已知良好状态git reset --hard <commit>
返回到它git push --force
将远程存储库重置回该状态您还可以在this post
上找到一些详细信息