在我的分支上,我在.gitignore中有一些文件

在另一个分支上,这些文件不是。

我想将不同的分支合并到我的分支中,我不关心这些文件是否不再被忽略。

不幸的是,我得到了这个:

以下未跟踪的工作树文件将被合并覆盖

我如何修改我的pull命令来覆盖这些文件,而不需要我自己找到、移动或删除这些文件?


当前回答

更新-一个更好的版本

此工具(https://github.com/mklepaczewski/git-clean-before-merge)将:

删除未跟踪的文件,这些文件与它们的git拉等价物相同, 将更改还原到修改后的文件,这些文件的修改版本与它们的git拉等价物相同, 报告修改/未跟踪的文件,与他们的git拉版本不同, 该工具有——pretend选项,不会修改任何文件。

旧版本

这个答案与其他答案有何不同?

这里给出的方法只删除将被merge覆盖的文件。如果目录中有其他未跟踪(可能被忽略)的文件,此方法将不会删除它们。

解决方案

这段代码将提取所有将被git删除并覆盖的未跟踪文件。

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"

然后就这样做:

git pull

这不是git瓷器命令,所以总是仔细检查它会做什么:

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"

解释——因为有一句台词很吓人:

以下是它的功能:

git pull 2>&1 - capture git pull output and redirect it all to stdout so we can easily capture it with grep. grep -E '^\s - the intent is to capture the list of the untracked files that would be overwritten by git pull. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them. cut -f2- - remove whitespace from the beginning of each line captured in 2. xargs -I {} rm -rf "{}" - us xargs to iterate over all files, save their name in "{}" and call rm for each of them. We use -rf to force delete and remove untracked directories.

如果用瓷器命令代替第1-3步就太好了,但我不知道有什么等价的。

其他回答

除了接受的答案,你当然可以删除文件,如果他们不再需要通过指定文件:

git clean -f '/path/to/file/'

如果你想知道git clean会删除哪些文件,记得先用-n标志运行它。注意,这些文件将被删除。对我来说,我并不关心他们,所以这对我来说是一个更好的解决方案。

如果你考虑使用-f标志,你可以先运行它作为一个演练。你只需要提前知道你接下来会遇到什么样有趣的情况;-P

-n 
--dry-run 
    Don’t actually remove anything, just show what would be done.

清洁/重置/硬签出/重基都不适合我。

所以我只是删除了git抱怨的文件*

rm /path/to/files/that/git/complained/about

*我检查了这个文件是否可以通过在一个单独的文件夹中签出一个全新的回购来删除(文件不在那里)

更新-一个更好的版本

此工具(https://github.com/mklepaczewski/git-clean-before-merge)将:

删除未跟踪的文件,这些文件与它们的git拉等价物相同, 将更改还原到修改后的文件,这些文件的修改版本与它们的git拉等价物相同, 报告修改/未跟踪的文件,与他们的git拉版本不同, 该工具有——pretend选项,不会修改任何文件。

旧版本

这个答案与其他答案有何不同?

这里给出的方法只删除将被merge覆盖的文件。如果目录中有其他未跟踪(可能被忽略)的文件,此方法将不会删除它们。

解决方案

这段代码将提取所有将被git删除并覆盖的未跟踪文件。

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"

然后就这样做:

git pull

这不是git瓷器命令,所以总是仔细检查它会做什么:

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"

解释——因为有一句台词很吓人:

以下是它的功能:

git pull 2>&1 - capture git pull output and redirect it all to stdout so we can easily capture it with grep. grep -E '^\s - the intent is to capture the list of the untracked files that would be overwritten by git pull. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them. cut -f2- - remove whitespace from the beginning of each line captured in 2. xargs -I {} rm -rf "{}" - us xargs to iterate over all files, save their name in "{}" and call rm for each of them. We use -rf to force delete and remove untracked directories.

如果用瓷器命令代替第1-3步就太好了,但我不知道有什么等价的。

唯一对我有用的命令是: (请注意,这会删除所有本地文件)

git fetch --all
git reset --hard origin/{{your branch name}}