我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
我如何强制将本地文件覆盖在git pull
我的本地仓库包含一个文件名文件名与服务器上的文件名相同。
错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖
当前回答
看起来最好的办法是首先做到:
git clean
删除所有未跟踪的文件,然后继续按常规git pull
...
其他回答
我认为答案很可怕,但虽然赫奇猪的答案可能更好,但我不认为它能有那么优雅。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
如果有冲突,则更改。
警告:
对已跟踪文件的本地更改将丢失 。 @ info: whatsthis
任何本地文件否吉特追踪到的不会受到影响。
第一,更新所有origin/<branch>
最晚的 refs :
git fetch --all # if this doesn't work try `git pull -f` (see comments)
备份当前分支( 例如 )master
):
git branch backup-master
跳转到最新承诺origin/master
并检出这些文件 :
git reset --hard origin/master
git fetch
从远程下载最新数据,不试图合并或重标任何内容。
git reset
将主分支重置为您刚获取的 。--hard
选项选项 更改工作树中的全部文件以匹配文件origin/master
.
[*]值得指出的是,通过从master
在重设前:
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all # if this doesn't work try `git pull -f` (see comments)
git reset --hard origin/master
在那之后,一切旧事,将永居其中;new-branch-to-save-current-commits
.
将丢失未承诺的更改( 即使是第 阶段的更改) 。 请确保隐藏并承诺您需要的一切 。 为此您可以运行以下操作 :
git stash
然后重新应用这些未承诺的修改:
git stash pop
根据我自己的类似经验,以上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 merge -X theirs
比较容易的方式是:
git checkout --theirs /path/to/file.extension
git pull origin master
这将用 Git 上的文件覆盖您的本地文件