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

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


当前回答

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

工作是 Git 合并 -x 他们的工作

其他回答

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

@lauri在 @lauri 遭遇时, 听从David avsajanishvili的建议,

而不是 (git> 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}

只是做做

git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname

来避免所有不想要的副作用,例如删除您想要保存的文件或目录等。

1 步( 可选) 从您本地仓库根部中选择, 保存备份, 并清空当前文件夹 :

mkdir -p ../<branch>-bkp && mv --backup=t * ../<branch>-bkp

2. 从远程仓库下载分支的所有文件和文件夹:

git checkout <branch> && git add -A . && git reset --hard origin/<branch> && git pull

将 & lt;branch&gt; 替换为您想要覆盖的分支名称 。

评论:

您可能宁愿手动备份并删除当前文件和文件夹, 而不是第 1 步, 事实上, 我建议您在操作系统 gui 中用一个文件手处理器这样做。 如果您跳过第 1 步, 请注意 : 您将失去本地仓库中的所有工作 ! 重要 : 如果您退出 Git pull 在第 2 步中拖动, 您可能无法从远程仓库获取最新版本 。 根据下面的第二个引用, git 重置 -- hard 将会重置中转区域, 工作目录会匹配

参考文献:https://gitforwindows.org/https://www.atlassian.com/git/tutitors/undoing-changes/git-reset https://www.atlassian.com/git/tutitors/rewriting-history/git-reflog

最顶尖的答案会给有类似问题的人带来问题, 但不想失去本地文件。 例如,

以下版本将您本地的更改引入临时分支( tmp) , 检查原始分支( 我假设是母分支) 并合并更新 。 您可以用隐藏来做到这一点, 但我发现通常更容易使用分支/ 合并方法 。

git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master

git fetch origin master
git merge -s recursive -X theirs origin master

假设另一个仓库是源主

对于那些不喜欢重置的人,我喜欢这样的做法:


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