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

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


当前回答

1: 重置为上一个承诺

git reset --hard HEAD

2: 删除未跟踪文件

git clean -f

3: 作出承诺

git pull

资料来源:

其他回答

您可以在您的工程基准文件夹中以文件忽略该文件 :

. jutignore( )

public/images/*

然后拉动修改,然后从您的 gitignore 文件中删除该行 。

而不是合并git pull,试试这个:

git fetch --all

随后是:

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 reset --hard
git pull

警告,如果您在 gitignore 文件中有任何目录/ * 条目, 这样做将永久删除您的文件 。

有些答案似乎很可怕。 从发生在Lauri身上的可怕意义来看,

相反(给予 > v1.7.6):

git stash --include-untracked
git pull

稍后,您可以清理藏匿历史。

手动,一对一:

$ git stash list
stash@{0}: WIP on <branch>: ...
stash@{1}: WIP on <branch>: ...

$ git stash drop stash@{0}
$ git stash drop stash@{1}

残酷地说,所有 - 一次:

$ git stash clear

当然,如果你想回到你藏的东西:

$ git stash list
...
$ git stash apply stash@{5}