在 master 之前的本地副本

问题描述

我最近把我用 Git 跟踪的一个项目弄得一团糟。我做了一些更改,而不是恢复我本地存储库的更改,我决定再次将存储库克隆到我机器上的另一个目录中,以创建各种备份(我知道有点多余)。然后我继续确实恢复了我原始本地存储库中的更改,并且没有使用/不需要新的克隆,因为我能够修复原始本地副本中的所有内容

现在的问题是当我尝试推送到 GitHub 时,Git 抛出以下错误

To github.com:<my-username>/<repo-name>.git
 ! [rejected]        master -> master (fetch first)
error: Failed to push some refs to 'github.com:<my-username>/<repon-ame>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g.,'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

自从克隆它以来,我没有向 repo 推送任何内容,并且没有其他人在该项目上工作,所以我不确定为什么它会领先于我的本地副本。简单地将其克隆到另一个目录是否会导致 master 领先?

所以我的问题是,我如何在不先从 master 中拉入的情况下继续推进它,因为我确定 git pull 只会擦除我对本地文件所做的更改,在原始目录中(第一个克隆)。如果需要,我将手动复制/粘贴以更新文件,因为数量不多,之后我不会再回到这个项目。但我只是在寻找一些说明,以及更新 repo 的最安全方式,即使是复制和粘贴,尽管我想有更好的方法

我仍然掌握 Git 的窍门,因此感谢您提供任何指导。感谢您抽出宝贵时间。

解决方法

git fetch origin master

此命令将更新远程存储库的本地副本,而不会触及您的本地存储库或工作目录。这样你就可以通过运行以下命令来查看远程分支和本地分支之间的区别:

git diff origin/master

然后您可以根据需要运行以下操作之一:

# Run this to keep any changes from the remote branch
git rebase origin/master
git push origin master

# Run this to overwrite changes on the remote branch
git push --force origin master
,

尝试使用 -f 标志 git push origin master -f

,

试试这个 git 命令

git push origin master --force

或者缺少 force -f

git push origin master -f