我如何强制在 git pull 上覆盖本地文件 ? 我的本地仓库包含一个与服务器相同的文件名文件 。

错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖


当前回答

对于那些不喜欢重置的人,我喜欢这样的做法:


git checkout branchname      # go to your branch
git fetch origin branchname  # fetch the remote
git checkout -b backup       # optionally, mark your remote as a backup
git branch -f branchname origin/branchname # force your local branch to be equal to the fetched origin/branchname


其他回答

我所知道的是一个容易和不痛苦的方法:

$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp

就是这样!

最顶尖的答案会给有类似问题的人带来问题, 但不想失去本地文件。 例如,

以下版本将您本地的更改引入临时分支( tmp) , 检查原始分支( 我假设是母分支) 并合并更新 。 您可以用隐藏来做到这一点, 但我发现通常更容易使用分支/ 合并方法 。

git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master

git fetch origin master
git merge -s recursive -X theirs origin master

假设另一个仓库是源主

不要使用 git 重置 -- hard. 来擦除他们可能完全不可取的更改。 相反 :

git pull
git reset origin/master
git checkout <file1> <file2> ...

当然,你可以使用Git抓取而不是Git拉动, 因为显然它不会合并, 但如果你通常拉动它, 继续拉动这里是有道理的。

所以这里发生的情况是 git put 更新您的源/ 主管参考文件; git 重置更新您本地的分支引用与源/ 主管的引用相同, 无需更新任何文件, 所以您的检查状态没有变化; 然后按需要, git 检查退出将文件返回到您本地的分支索引状态 。 如果在现场和上游主控中添加了完全相同的文件, 索引已经符合重置之后的文件, 因此在常见情况下, 您不需要在

如果上游分支也包含您想要自动应用的承诺,您可以在进程上跟随细微的变异:

git pull
git merge <commit before problem commit>
git reset <problem commit>
git checkout <file1> <file2> ...
git pull

我有一个类似的问题。 我必须这样做:

git reset --hard HEAD
git clean -f
git pull

将索引和头重置为源/ 主机,但不重置工作树 :

git reset origin/master