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

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


当前回答

只是做做

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

因此,您可以避免所有不想要的副作用, 比如删除您想要保存的文件或目录, 等等 。

其他回答

不要用git reset --hard这将抹去他们完全不可取的变化,相反:

git pull
git reset origin/master
git checkout <file1> <file2> ...

您当然可以使用git fetch代替git pull因为它显然不会合并, 但如果你通常拉它, 继续拉在这里是有道理的。

所以这里发生的事情就是git pull 更新您的源/ 主管参考; git reset 更新本地分支引用与来源/主管相同,不更新任何文件,所以您的检查状态没有变化;git checkout 将文件返回到您的本地分支索引状态需要时。如果在现场和上游主控上添加了完全相同的文件,索引已经与重置之后的文件匹配,因此在普通情况下,不需要做git checkout完全没有

如果上游分支也包含您想要自动应用的承诺,您可以跟踪进程上微妙的变异:

git pull
git merge <commit before problem commit>
git reset <problem commit>
git checkout <file1> <file2> ...
git pull

我认为答案很可怕,但虽然赫奇猪的答案可能更好,但我不认为它能有那么优雅。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 fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname

因此,您可以避免所有不想要的副作用, 比如删除您想要保存的文件或目录, 等等 。

我试图在三角2Webpack-Starter上使用第2号材料分支, 并度过了一段很漫长的时光。 这是我唯一可以下载和使用该分支的方法。

git clone --depth 1 https://github.com/angularclass/angular2-webpack-starter.git

cd angular2-webpack-starter/

git checkout -b material2

打开工程文件夹, 删除所有非隐藏的文件和文件夹。 留下所有隐藏的文件和文件夹 。

git add .

git commit -m "pokemon go"

git reset --hard

git pull origin material2

(当编辑器弹出时,按“:wq”键,然后按输入)

现在你准备好了

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

. jutignore( )

public/images/*

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