如何从git push -f中恢复?

时间:2021-09-14 21:42:10

I have done a git push -f on a branch and now lost the other persons work.

我在一个分支上做了一个git push -f,现在又失去了其他人的工作。

I could able to see in my github that the other person work is shown (shown in activity), but not in the remote branch (where I pushed my changes). Unfortunately I don't have my local repo sync up with remote. So I don't have other persons code in my local. Is there a way I can get that to my local and recover?

我可以在github中看到其他人的工作显示(在活动中显示),而不是在远程分支中(在那里我推动了更改)。不幸的是,我没有本地的repo与remote同步。所以我在本地没有其他人的代码。有没有办法让我的家乡恢复健康?

3 个解决方案

#1


4  

Before doing anything else, please do these 3 steps - backup your local repo, tell the other guy to backup his, and backup the remote repo as they are now. Nothing fancy, just Ctrl+C, Ctrl+V would do. Do it on your entire project so you backup the working tree as well. If you don't have direct access to the remote server, a git clone would have to suffice, though I would prefer a real directory copy.

在做其他事情之前,请做这3个步骤-备份您的本地repo,告诉另一个人备份他的,并备份远程的repo,就像他们现在一样。没什么花哨的,按Ctrl+C, Ctrl+V就可以了。在整个项目中都这样做,这样就可以备份工作树了。如果您没有直接访问远程服务器,那么一个git克隆就足够了,尽管我更喜欢真正的目录副本。

Once you've done that, the other guy can use git push -f to reupload his branch. If he has new commits, you will have to figure out which commit was last pushed by him, but I'm fairly sure no actual commits will be lost. If the other guy has done git pull -f, even then he can use git reflog to recover his local branch.

一旦你这样做了,另一个人就可以使用git push -f来重新上传他的分支。如果他有新的提交,您将必须找出他最后推动了哪个提交,但是我相当确定没有实际提交将会丢失。如果另一个人做了git pull -f,即使这样,他也可以使用git reflog来恢复他的本地分支。

Since you're using Github, you might also see https://*.com/a/35273807/492336. Apparently there is a Github’s Events API which might be useful.

由于使用Github,您还可以看到https://*.com/a/35273807/492336。显然,有一个Github的事件API可能是有用的。

#2


3  

You can access to GitHub's reflog via REST-API

您可以通过REST-API访问GitHub的reflog。

This will show you events:

这将显示事件:

$ curl https://api.github.com/repos/<user>/<repo>/events

After you've found broken commit just add a reference to it:

在你发现了错误的提交之后,只需添加一个引用:

POST /repos/<user>/<repo>/git/refs
{
  "ref": "refs/heads/featureA",
  "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd"
}

or with curl:

或与旋度:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" \
    -X POST -d '{"ref":"refs/heads/featureA", \
                 "sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}' \
    https://api.github.com/repos/<user>/<repo>/git/refs

#3


-6  

I am not a git user, but few days ago I came a cross this post, I guess this one has solution to your problem as well.

我不是一个git用户,但是几天前我收到了这个帖子,我猜这个也解决了你的问题。

http://ohshitgit.com/

http://ohshitgit.com/

# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"
# now your changes are on the correct branch

#1


4  

Before doing anything else, please do these 3 steps - backup your local repo, tell the other guy to backup his, and backup the remote repo as they are now. Nothing fancy, just Ctrl+C, Ctrl+V would do. Do it on your entire project so you backup the working tree as well. If you don't have direct access to the remote server, a git clone would have to suffice, though I would prefer a real directory copy.

在做其他事情之前,请做这3个步骤-备份您的本地repo,告诉另一个人备份他的,并备份远程的repo,就像他们现在一样。没什么花哨的,按Ctrl+C, Ctrl+V就可以了。在整个项目中都这样做,这样就可以备份工作树了。如果您没有直接访问远程服务器,那么一个git克隆就足够了,尽管我更喜欢真正的目录副本。

Once you've done that, the other guy can use git push -f to reupload his branch. If he has new commits, you will have to figure out which commit was last pushed by him, but I'm fairly sure no actual commits will be lost. If the other guy has done git pull -f, even then he can use git reflog to recover his local branch.

一旦你这样做了,另一个人就可以使用git push -f来重新上传他的分支。如果他有新的提交,您将必须找出他最后推动了哪个提交,但是我相当确定没有实际提交将会丢失。如果另一个人做了git pull -f,即使这样,他也可以使用git reflog来恢复他的本地分支。

Since you're using Github, you might also see https://*.com/a/35273807/492336. Apparently there is a Github’s Events API which might be useful.

由于使用Github,您还可以看到https://*.com/a/35273807/492336。显然,有一个Github的事件API可能是有用的。

#2


3  

You can access to GitHub's reflog via REST-API

您可以通过REST-API访问GitHub的reflog。

This will show you events:

这将显示事件:

$ curl https://api.github.com/repos/<user>/<repo>/events

After you've found broken commit just add a reference to it:

在你发现了错误的提交之后,只需添加一个引用:

POST /repos/<user>/<repo>/git/refs
{
  "ref": "refs/heads/featureA",
  "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd"
}

or with curl:

或与旋度:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" \
    -X POST -d '{"ref":"refs/heads/featureA", \
                 "sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}' \
    https://api.github.com/repos/<user>/<repo>/git/refs

#3


-6  

I am not a git user, but few days ago I came a cross this post, I guess this one has solution to your problem as well.

我不是一个git用户,但是几天前我收到了这个帖子,我猜这个也解决了你的问题。

http://ohshitgit.com/

http://ohshitgit.com/

# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"
# now your changes are on the correct branch