我如何强制将本地文件覆盖在git pull我的本地仓库包含一个文件名文件名与服务器上的文件名相同。

错误: 未跟踪的工作树文件“ example. txt ” 会被合并覆盖


当前回答

所需经费:

  1. 本地音轨更改, 这样这里没有人会失去它们 。
  2. 使本地存储库与远程源存储库匹配 。

解决方案 :

  1. 堆叠本地变化 。
  2. 获取获取与 a 具有清洁联 联 年 月 日 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 月 日 月 的 月 月 月 日 月 月 日 月 的 月 月 月 月 日 月 月 月文件 文件目录目录目录目录忽略忽略. jutignore( )硬重置重置来源来源.

    git stash --include-untracked
    git fetch --all
    git clean -fdx
    git reset --hard origin/master
    

其他回答

对我唯一有用的是:

git reset --hard HEAD~5

这会让你背了五次承诺,然后

git pull

我通过向上看才发现如何解除 Git 合并.

根据我自己的类似经验,以上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

就是这样!

我认为答案很可怕,但虽然赫奇猪的答案可能更好,但我不认为它能有那么优雅。fetchmerge使用定义的战略, 从而保存您本地的更改, 只要它们不是您试图强制覆盖的文件之一 。

首先对您的更改进行承诺

 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 .

。指目录中的所有文件。