是否可以忽略合并中的文件更改?
Is it possible to ignore file changes in a merge?
我有两个分支,Master 和 Exhibition。 Exhibition只是一个地方分支。
当我 运行 在两个分支上进行差异时,它显示 sitemap.xml 在分支和本地配置路径之间存在一些差异。但是,当我从 Exhibition 合并到 Master 时,Sitemap.xml 中的更改不会合并,这很好(因为它们包含本地路径)。
在两个分支上执行 git status
对 add/commit 没有任何意义,除非我进行了其他更改,我添加的那些更改,然后提交,它们在合并中通过。
sitemap.xml 文件不在 git 忽略。
在某些时候我一定设置了一些东西,所以站点地图的更改没有提交到 live master 版本,但我不知道是什么,也不知道如何找出什么。 Git 中有什么允许这样做的吗?
我需要重新创建此行为,因为其他人在新分支中做了一些更改,用他的更改覆盖了我的 sitemap.xml。我错过了一些明显的东西吗?我本以为如果我没有添加或提交它,它会显示在 git 状态中。
您可能将 sitemap.xml
标记为 假设不变。来自 git help update-index
:
In order to set "assume unchanged" bit, use --assume-unchanged option. To unset, use --no-assume-unchanged. To see which files have the "assume unchanged" bit set, use git ls-files -v (see git-ls-files(1)).
以下示例展示了行为
$ git init
Initialized empty Git repository in /tmp/foo/.git/
$ echo 1 > sitemap.xml; git add sitemap.xml; git commit -m "sitemap"
[master (root-commit) f1a1f79] sitemap
1 file changed, 1 insertion(+)
create mode 100644 sitemap.xml
$ git checkout -b exhibition
Switched to a new branch 'exhibition'
$ echo 2 > sitemap.xml
$ git update-index --assume-unchanged sitemap.xml
$ git status
On branch exhibition
nothing to commit, working tree clean
$ echo a > A; git add A; git commit -m a
[exhibition af58c3b] a
1 file changed, 1 insertion(+)
create mode 100644 A
$ git status
On branch exhibition
nothing to commit, working tree clean
参考文献
--[no-]assume-unchanged
When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).
Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.
不太可能有一些设置告诉 git 在合并中忽略站点地图;特别是如果它正在合并来自您同事分支的站点地图更改。您的分支之间的 "merge base" 更有可能与您的 exhibition
分支具有相同版本的 sitemap.xml
。在这种情况下,在合并期间 exhibition
分支不会被视为更改文件,而 master
分支将被视为更改您正在谈论的本地路径;所以 git 将应用 master
分支更改,你最终会得到文件,因为它已经在 master
.
上看到了
你可以用像
这样的命令来测试它
git diff --name-only exhibition $(git merge-base exhibition master)
如果此打印的列表不包含站点地图,则以上说明了您所看到的内容。
如果情况并非如此,那么就不清楚您做了什么导致了这种行为(这有点问题,因为根据您所做的,可能还会产生其他影响)。特别是,你提到 .gitignore
;这与合并的处理方式无关。 .gitignore
所做的唯一一件事就是让指定路径中未跟踪的文件默认保持未跟踪状态。
这类问题通常会引出至少一个建议使用 skip-worktree
或 assume-unchanged
等索引标记的回答。这些 "solutions" 有时可能会起作用,但我不推荐它们。 (一方面,由于索引不共享,它们只会在您的本地存储库中有效;所以一种可能性是您设置了类似的东西,现在 运行 成为其限制之一。)至少,这不是他们旨在支持的行为。
在 git 中并没有真正好的方法来做到这一点。您可以通过设置自定义合并驱动程序来接近,但这仅在文件本身需要合并时才有效;因此,如果文件在 master
上没有更改,这将没有任何效果。
对于需要包含本地路径或配置的文件,最佳解决方案是在源代码管理下存储文件模板,并在构建过程中自行生成文件。在这种情况下,生成的文件不应该放在源代码管理中,因此它要么包含在 .gitignore
中,要么在工作树之外的路径中生成(我个人更喜欢后者)。然后,您可以在源代码管理中包含一个或多个文件,其值将在不同情况下合并到模板中,并使用您的构建工具 select 其中之一或具有本地值的非源代码控制文件。
我有两个分支,Master 和 Exhibition。 Exhibition只是一个地方分支。
当我 运行 在两个分支上进行差异时,它显示 sitemap.xml 在分支和本地配置路径之间存在一些差异。但是,当我从 Exhibition 合并到 Master 时,Sitemap.xml 中的更改不会合并,这很好(因为它们包含本地路径)。
在两个分支上执行 git status
对 add/commit 没有任何意义,除非我进行了其他更改,我添加的那些更改,然后提交,它们在合并中通过。
sitemap.xml 文件不在 git 忽略。
在某些时候我一定设置了一些东西,所以站点地图的更改没有提交到 live master 版本,但我不知道是什么,也不知道如何找出什么。 Git 中有什么允许这样做的吗?
我需要重新创建此行为,因为其他人在新分支中做了一些更改,用他的更改覆盖了我的 sitemap.xml。我错过了一些明显的东西吗?我本以为如果我没有添加或提交它,它会显示在 git 状态中。
您可能将 sitemap.xml
标记为 假设不变。来自 git help update-index
:
In order to set "assume unchanged" bit, use --assume-unchanged option. To unset, use --no-assume-unchanged. To see which files have the "assume unchanged" bit set, use git ls-files -v (see git-ls-files(1)).
以下示例展示了行为
$ git init
Initialized empty Git repository in /tmp/foo/.git/
$ echo 1 > sitemap.xml; git add sitemap.xml; git commit -m "sitemap"
[master (root-commit) f1a1f79] sitemap
1 file changed, 1 insertion(+)
create mode 100644 sitemap.xml
$ git checkout -b exhibition
Switched to a new branch 'exhibition'
$ echo 2 > sitemap.xml
$ git update-index --assume-unchanged sitemap.xml
$ git status
On branch exhibition
nothing to commit, working tree clean
$ echo a > A; git add A; git commit -m a
[exhibition af58c3b] a
1 file changed, 1 insertion(+)
create mode 100644 A
$ git status
On branch exhibition
nothing to commit, working tree clean
参考文献
--[no-]assume-unchanged
When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).
Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.
不太可能有一些设置告诉 git 在合并中忽略站点地图;特别是如果它正在合并来自您同事分支的站点地图更改。您的分支之间的 "merge base" 更有可能与您的 exhibition
分支具有相同版本的 sitemap.xml
。在这种情况下,在合并期间 exhibition
分支不会被视为更改文件,而 master
分支将被视为更改您正在谈论的本地路径;所以 git 将应用 master
分支更改,你最终会得到文件,因为它已经在 master
.
你可以用像
这样的命令来测试它git diff --name-only exhibition $(git merge-base exhibition master)
如果此打印的列表不包含站点地图,则以上说明了您所看到的内容。
如果情况并非如此,那么就不清楚您做了什么导致了这种行为(这有点问题,因为根据您所做的,可能还会产生其他影响)。特别是,你提到 .gitignore
;这与合并的处理方式无关。 .gitignore
所做的唯一一件事就是让指定路径中未跟踪的文件默认保持未跟踪状态。
这类问题通常会引出至少一个建议使用 skip-worktree
或 assume-unchanged
等索引标记的回答。这些 "solutions" 有时可能会起作用,但我不推荐它们。 (一方面,由于索引不共享,它们只会在您的本地存储库中有效;所以一种可能性是您设置了类似的东西,现在 运行 成为其限制之一。)至少,这不是他们旨在支持的行为。
在 git 中并没有真正好的方法来做到这一点。您可以通过设置自定义合并驱动程序来接近,但这仅在文件本身需要合并时才有效;因此,如果文件在 master
上没有更改,这将没有任何效果。
对于需要包含本地路径或配置的文件,最佳解决方案是在源代码管理下存储文件模板,并在构建过程中自行生成文件。在这种情况下,生成的文件不应该放在源代码管理中,因此它要么包含在 .gitignore
中,要么在工作树之外的路径中生成(我个人更喜欢后者)。然后,您可以在源代码管理中包含一个或多个文件,其值将在不同情况下合并到模板中,并使用您的构建工具 select 其中之一或具有本地值的非源代码控制文件。