我如何强制在 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 checkout --theirs /path/to/file.extension
git pull origin master

这将在 Git 上的文件上覆盖本地文件

我有一个奇怪的状况, 无论是git clean or git reset works. 我必须在每一个未跟踪的文件上使用以下脚本, 从 git 索引中删除冲突文件 :

git rm [file]

那样我就能好好拉了

1 步( 可选) 从您本地仓库根部中选择, 保存备份, 并清空当前文件夹 :

mkdir -p ../<branch>-bkp && mv --backup=t * ../<branch>-bkp

2. 从远程仓库下载分支的所有文件和文件夹:

git checkout <branch> && git add -A . && git reset --hard origin/<branch> && git pull

将 & lt;branch&gt; 替换为您想要覆盖的分支名称 。

评论:

您可能宁愿手动备份并删除当前文件和文件夹, 而不是第 1 步, 事实上, 我建议您在操作系统 gui 中用一个文件手处理器这样做。 如果您跳过第 1 步, 请注意 : 您将失去本地仓库中的所有工作 ! 重要 : 如果您退出 Git pull 在第 2 步中拖动, 您可能无法从远程仓库获取最新版本 。 根据下面的第二个引用, git 重置 -- hard 将会重置中转区域, 工作目录会匹配

参考文献:https://gitforwindows.org/https://www.atlassian.com/git/tutitors/undoing-changes/git-reset https://www.atlassian.com/git/tutitors/rewriting-history/git-reflog

只是做做

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

来避免所有不想要的副作用,例如删除您想要保存的文件或目录等。

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

git 获取 -- all

随后是:

git 重置 - 硬源/ master 。