我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
当前回答
解决这一问题的另一种办法是,首先将任何未承诺的改动用git stash
然后运行,然后运行
git pull --rebase=interactive -s recursive -X theirs
在互动的重新基础中,你可以改变你所有本地不想要的承诺drop
,这将摆脱他们 并离开你 在远程分支的头部 而不引入合并承诺。
现在你可以跑了git stash apply
如果你有 本地藏的改变 你想带回来。
其他回答
您可能会发现此命令有助于丢弃本地更改 :
git checkout <your-branch> -f
然后进行清理(从工作树上移走未追踪的文件) :
git clean -f
如果您想要除去未跟踪的文件之外, 还要删除未跟踪的目录 :
git clean -fd
你可以试试git pull --force
或者通过使用git stash
然后运行git pull
.
根据我自己的类似经验,以上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
只是做做
git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname
因此,您可以避免所有不想要的副作用, 比如删除您想要保存的文件或目录, 等等 。
我不知道为什么有人不谈FETCH_HEAD
还没有。
git fetch origin master && git reset --hard FETCH_HEAD
如果你想把它放在别名里,命令是:
git config --global alias.fpull '!git fetch origin master && git reset --hard FETCH_HEAD'