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

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


当前回答

只是做做

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

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

其他回答

解决这一问题的另一种办法是,首先将任何未承诺的改动用git stash然后运行,然后运行

git pull --rebase=interactive -s recursive -X theirs

在互动的重新基础中,你可以改变你所有本地不想要的承诺drop,这将摆脱他们 并离开你 在远程分支的头部 而不引入合并承诺。

现在你可以跑了git stash apply如果你有 本地藏的改变 你想带回来。

您可以在您的工程基准文件夹中以文件忽略该文件 :

. jutignore( )

public/images/*

然后拉动修改,然后从您的 gitignore 文件中删除该行 。

我认为,冲突有两种可能的原因,必须分别加以解决,而且据我所知,上述答案中没有一个涉及这两个问题:

  • 未跟踪的本地文件需要删除, 手动( 安全) 或按其他答案中的建议, 由git clean -f -d

  • 离子分支以外的当地承诺也需要删除。git reset --hard origin/master(取代“校长”的分支)git fetch origin(一)(一)

根据我自己的类似经验,以上Strahinja Kustudidic提出的解决方案是迄今为止最好的。 正如其他人已经指出的那样,仅仅努力重设就会消除全部( 全部)未跟踪的文件, 其中可能包含很多您不想删除的文件, 例如配置文件。 更安全的做法是只删除即将添加的文件, 而对于这个事项, 您可能还想要检查任何即将更新的本地修改过的文件 。

因此,我更新了Kustudic的脚本来做这个。我还修了个打字机(在原文中遗漏了一个字)。

#/bin/sh

# Fetch the newest code
git fetch

# Delete all files which are being added,
# so there are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
do
    echo "Deleting untracked file $file..."
    rm -vf "$file"
done

# Checkout all files which have been locally modified
for file in `git diff HEAD..origin/master --name-status | awk '/^M/ {print $2}'`
do
    echo "Checking out modified file $file..."
    git checkout $file
done

# Finally merge all the changes (you could use merge here as well)
git pull

在 Windows 上执行此单命令 :

git fetch --all & git reset --hard origin/master