无法将 git 远程分支恢复到较早的提交



我正在清理一些跟踪问题,并希望从跟踪中删除一些文件。我搞砸了,删除了一堆我想恢复的文件。在这种状态下,我不知不觉地按下了遥控器。

好吧,是时候回去解决问题了。幸运的是,我想要的承诺已经载入史册:

black-rainbows: scottnla$ git log -2
commit 76fa0b491c2424d93b16c1fe002401eaa17d481e
Author: scottnla
Date:   Thu Oct 16 21:57:57 2014 -0400
    Fixed tracking issue
commit 078cf16ee1bebfe7130b79d5958ecb0baf855002
Author: scottnla
Date:   Thu Oct 16 21:55:50 2014 -0400
    Finished preliminary analysis script

好吧,一切都没有失去。所以我可以使用git reset --hard到达我想要的地方,对吧?

black-rainbows: scottnla$ git reset --hard 
078cf16ee1bebfe7130b79d5958ecb0baf855002
HEAD is now at 078cf16 Finished preliminary analysis script

Phew。我检查了文件,这正是我想要的。

所以我试着把这个承诺推回到远程。。。

black-rainbows:scottnla$ git push origin master
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to <git>
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

现在这真的变得一团糟。我尝试了不同的方法来拉取远程并将其与当前分支合并,但它总是删除文件(这是有道理的,因为我只是恢复了历史记录,它一直在重播提交)。

既然我的本地版本的回购正是我想要的,我该如何将其推送到远程?

如果你绝对确定

既然我的本地版本的回购正是我想要的,我该如何将其推送到远程?

然后你可以做

git push -f remote-name branch-name

在您的情况下是

git push -f origin master

-f表示要强制推送更改。如果你和其他人一起工作,这不是一个好主意。

最新更新