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

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


当前回答

基于我自己的类似经验, 以上strahinja kustudic 提供的解决方案远为最佳。 正如其他人指出的, 简单的硬重置将删除所有未跟踪的文件, 其中可能包含许多您不希望删除的文件, 比如配置文件。 更安全的是, 只删除即将添加的文件, 而对于这个事项, 您可能还想要检查任何即将更新的本地修改过的文件 。

因此,我更新了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

其他回答

这四个命令对我管用

git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master

执行这些命令后要检查/拉动

git pull origin master

我尝试了很多,但终于成功地 与这些命令。

而不是与 Git pull 合并, 请尝试一下 :

git 获取 -- all

随后是:

git 重置 - 硬源/ master 。

i 认为冲突有两种可能的原因,必须分别解决,而且只要我能够看出上述答案没有涉及两个问题:

未跟踪的本地文件需要删除, 手动( 安全) 或按照其他回答中的建议, 由 Git 清洁 - f - d 本地承诺删除, 不在远程分支的本地承诺也需要删除 。 最容易实现的方法是: git 重设 - hard 来源/ master (由您正在工作的分支替换“ 主机 ” , 并先运行 git 抓取来源 )

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

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

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


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