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

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


当前回答

所有这些解决方案的问题在于,它们要么过于复杂,要么是一个更大的问题,就是它们从网络服务器上移除了所有未跟踪的文件,我们并不想这样做,因为服务器上总是有需要的配置文件,而不是Git仓库。

以下是我们使用的最干净的解决方案:

# 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
    rm -f -- "$file"
done

# Checkout all files which were locally modified
for file in `git diff --name-status | awk '/^[CDMRTUX]/ {print $2}'`
do
    git checkout -- "$file"
done

# Finally pull all the changes
# (you could merge as well e.g. 'merge origin/master')
git pull
  • 第一个命令获取最新数据 。

  • 第二次命令检查是否正在将任何文件添加到存储库中,并从本地存储库中删除那些会造成冲突的未跟踪文件。

  • 第三个命令检查所有本地修改的文件 。

  • 最后,我们努力更新最新版本, 但这次没有任何冲突, 因为回购中的未追踪文件已经不存在了, 所有本地修改过的文件都已经和存储库一样了。

其他回答

对于那些不喜欢的人reset我喜欢这个方法:


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 pull --force或者通过使用git stash然后运行git pull.

1: 重置为上一个承诺

git reset --hard HEAD

2: 删除未跟踪文件

git clean -f

3: 作出承诺

git pull

资料来源:

我尝试了大部分我在这里能找到的东西,

有效的是git merge -X theirs

比较容易的方式是:

git checkout --theirs /path/to/file.extension
git pull origin master

这将用 Git 上的文件覆盖您的本地文件