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

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


当前回答

我认为答案很可怕,但虽然赫奇猪的答案可能更好,但我不认为它能有那么优雅。fetchmerge使用定义的战略, 从而保存您本地的更改, 只要它们不是您试图强制覆盖的文件之一 。

首先对您的更改进行承诺

 git add *
 git commit -a -m "local file server commit message"

如果有冲突, 取回更改并覆盖

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

-X是一个选项名称,并且theirs是此选项的值。 您选择使用their更改(另一个选项是ours如果有冲突,则更改。

其他回答

只是做做

git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname

因此,您可以避免所有不想要的副作用, 比如删除您想要保存的文件或目录, 等等 。

重置索引和头部origin/master,但不要重置工作树:

git reset origin/master

一旦您进行了 Git 拖动, 您就会得到不匹配的文件列表。 如果文件数量不大, 那么您就可以检查这些文件, 此动作将会覆盖这些文件 。

git 检出 - <filename>

我通常会这样做, 如果为了快速检查, 我修改服务器上的本地文件( 没有推荐的, 以及您在获取此议题: D 背后的理由) , 我找到解决方案后会检查被修改的文件 。

看起来最好的办法是首先做到:

git clean

删除所有未跟踪的文件,然后继续按常规git pull...

对于那些不喜欢的人reset我喜欢这个方法:


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