我如何强制在 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
其他回答
对我唯一有用的是:
git reset --hard HEAD~5
这会使你们再返于五次,然后是五次,
git pull
我通过查查如何解除Git合并发现这一点。
您可以尝试 Git pull - 力 或者可能通过使用 Git 藏起来来隐藏你的行为 然后运行 git pull 。
我所知道的是一个容易和不痛苦的方法:
$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp
就是这样!
对于那些不喜欢重置的人,我喜欢这样的做法:
git checkout branchname # go to your branch
git fetch origin branchname # fetch the remote
git checkout -b backup # optionally, mark your remote as a backup
git branch -f branchname origin/branchname # force your local branch to be equal to the fetched origin/branchname
您可能会发现此命令有助于丢弃本地更改 :
git checkout <your-branch> -f
然后进行清理( 从工作树上移走未跟踪的文件) :
git clean -f
如果您想要除去未跟踪的文件之外, 还要删除未跟踪的目录 :
git clean -fd