我如何强制在 git pull 上覆盖本地文件 ? 我的本地仓库包含一个与服务器相同的文件名文件 。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
我如何强制在 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
其他回答
您可以在您的工程基准文件夹中以文件忽略该文件 :
. jutignore( )
public/images/*
然后拉动修改,然后从您的 gitignore 文件中删除该行 。
基于我自己的类似经验, 以上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 fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname
来避免所有不想要的副作用,例如删除您想要保存的文件或目录等。
我读过所有答案,但我在找一个命令来做这个。 这就是我所做的。在.gitconfig 中添加了一个 Git 化名。
[alias]
fp = "!f(){ git fetch ${1} ${2} && git reset --hard ${1}/${2};};f"
您的命令以
git fp origin master
等于
git fetch origin master
git reset --hard origin/master
这四个命令对我管用
git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master
执行这些命令后要检查/拉动
git pull origin master
我尝试了很多,但终于成功地 与这些命令。