我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
当前回答
所需经费:
解决方案 :
获取获取与 a 具有清洁联 联 年 月 日 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 月 日 月 的 月 月 月 日 月 月 日 月 的 月 月 月 月 日 月 月 月文件 文件和目录目录目录目录忽略忽略. jutignore( )和硬重置重置至来源来源.
git stash --include-untracked
git fetch --all
git clean -fdx
git reset --hard origin/master
其他回答
根据我自己的类似经验,以上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 branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp
就是这样!
我认为答案很可怕,但虽然赫奇猪的答案可能更好,但我不认为它能有那么优雅。fetch
和merge
使用定义的战略, 从而保存您本地的更改, 只要它们不是您试图强制覆盖的文件之一 。
git add *
git commit -a -m "local file server commit message"
git fetch origin master
git merge -s recursive -X theirs origin/master
-X
是一个选项名称,并且theirs
是此选项的值。 您选择使用their
更改(另一个选项是ours
如果有冲突,则更改。
如果您正在制定您的代码, 发现新的更改是一个巨大的错误或意外, 您可以简单地使用其他选项, 比如 :
git restore .
。指目录中的所有文件。