如何在执行 git pull
命令时强制覆盖本地文件?我的本地仓库中包含与服务器上相同文件名的文件。
错误:无法完成合并,未跟踪的工作树文件 'example.txt' 将被覆盖。
如何在执行 git pull
命令时强制覆盖本地文件?我的本地仓库中包含与服务器上相同文件名的文件。
错误:无法完成合并,未跟踪的工作树文件 'example.txt' 将被覆盖。
步骤1(可选)
从本地存储库的根目录开始,保存备份并清空当前文件夹:
mkdir -p ../<branch>-bkp && mv --backup=t * ../<branch>-bkp
步骤2。从远程仓库下载分支的所有文件和文件夹:
git checkout <branch> && git add -A . && git reset --hard origin/<branch> && git pull
你应该将<branch>
替换为你想要覆盖的分支的名称。
注释:
git pull
,git reset --hard
将git reset --hard origin/<branch_to_overwrite>
,请注意,任何仍然存在的垃圾文件都可能在稍后的git push
中潜入远程存储库,即使这不是您的意图。git add -A .
可以防止这种情况发生。References:
https://gitforwindows.org/
https://www.atlassian.com/git/tutorials/undoing-changes/git-reset
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog
我尝试了这里大部分的方法,但都不适用于我的情况。
有效的方法是git merge -X theirs
git pull --force
或者通过使用 git stash
将你的提交暂存起来,然后再运行 git pull
。fatal: refusing to merge unrelated histories
- WyckAutomatic merge failed; fix conflicts and then commit the result.
- Mutoh
origin/branch_to_overwrite
相同的状态,包括代码和提交记录。命令为:git reset --hard origin/branch_to_overwrite
。 - Andrew Atkinsongit branch <branch> -D
git reset <commit> --hard
git branch <branch>
git --set-upstream-to=origin/<branch> <branch>
git pull
git config core.autocrlf false; git ls-files -z | xargs -0 rm; git checkout .
。 - Chloe