本地副本在主副本之前



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

现在的问题是,当我尝试推送到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.

自从克隆它以来,我还没有把任何东西推到回购中,也没有其他人在做这个项目,所以我不确定为什么它会领先于我的本地副本。简单地将其克隆到另一个目录是否会导致主目录领先?

所以我的问题是,我如何在不首先从master中拉入的情况下继续推进,因为我确信git pull只会删除我对原始目录(第一个克隆(中的本地文件所做的更改。如果必须的话,我会手动复制/粘贴以更新文件,因为文件不多,而且之后我不会返回到这个项目。但我只是想澄清一下,以及更新回购最安全的方法,即使是复制粘贴,尽管我认为还有更好的方法。

我仍然掌握着Git的窍门,所以任何指导都很感激。谢谢你抽出时间。

git fetch origin master

此命令将在不接触本地repo或工作目录的情况下更新远程repo的本地副本。通过这种方式,您可以通过运行以下命令来查看远程分支和本地分支之间的区别:

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

或力不足-f

git push origin master -f

最新更新