我刚开始使用 Git。我对本地文件夹中的网站进行了更改。当我想推动它们通过时,发生了错误:
[rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/id/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
我提出了建议的拉取请求,没有意识到这会删除我所有的本地更改。
我的终端仍然显示以前的命令和提交。有没有办法将我的本地文件夹恢复到拉取请求之前的先前版本?
简历:
git pull <branch>
,没有意识到它会删除我的本地更改。我会尽力快速整理你的想法
Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.
换句话说:在您的服务器(远程)上进行了更改,而您当前分支中没有本地更改。 Git 不知道您想如何继续进行这些更改。git pull
不同。据我所知,您不需要拉取请求,您希望通过 git pull
更新当前的本地分支。不要将拉取请求与 git pull
https://git-scm.com/docs/git-pullgit reset
是少数可能破坏代码更改的危险命令之一。请小心使用该命令并首先了解它。它并不是为了“删除你最后的 git 操作”。gitk
(始终与 git 一起安装的工具)来查看历史记录、创建分支、合并并可视化您正在做的事情。git checkout <yourCommitHash> -b NewBranch
在您的提交上创建一个新分支并检查它
git merge <currentLocalBranch
将当前本地分支合并到 NewBranch
git push
我认为@tomwaitforitmy 提供的答案包含了很多很好的建议。但对于你的情况,我认为解决方案非常简单:
您可以使用以下方式签出“master”的旧版本或任何分支:
git checkout master@{1}
。
在该示例中,我们将跳回到您刚刚更新它之前的 master 状态(使用
git pull
或 git commit something..
或 git reset ...
如果您想“进一步返回”,请使用 {2}
或 {3}
等等。你可以回到很远的地方。
您还可以查看这些旧参考,而无需使用以下命令进行结帐:
git reflog
(这将向您显示更改本地开发存储库上“主”参考的所有活动)。
完成上述检查后,您将处于“分离头”状态。 如果/当你找到你喜欢的 master 版本时,你可以用这样的东西再次“正确”一切:
git checkout master
,然后git reset master@{XXX}
使用你找到的任何 xxx 参考有你的旧作品。
完成that后,您将再次进入
master
,但它将是您本地的master。现在您已经掌握了这些,您可以弄清楚如何正确合并现在明显存在于您的中央存储库中的“非快进”更改。