我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
当前回答
我知道一种容易和痛苦的方法:
$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp
就是这样!
其他回答
我知道一种容易和痛苦的方法:
$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp
就是这样!
看起来最好的办法是首先做到:
git clean
删除所有未跟踪的文件,然后继续按常规git pull
...
一旦您进行了 Git 拖动, 您就会得到不匹配的文件列表。 如果文件数量不大, 那么您就可以检查这些文件, 此动作将会覆盖这些文件 。
git 检出 - <filename>
我通常会这样做, 如果为了快速检查, 我修改服务器上的本地文件( 没有推荐的, 以及您在获取此议题: D 背后的理由) , 我找到解决方案后会检查被修改的文件 。
所需经费:
解决方案 :
获取获取与 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