由于外部奇怪的限制,我无法修改我的存储库中的.gitignore
文件。除了修改.gitignore
文件,是否有一种方法可以忽略文件和目录?即使是全局解决方案,例如将应用于我所有的存储库的全局配置。
由于外部奇怪的限制,我无法修改我的存储库中的.gitignore
文件。除了修改.gitignore
文件,是否有一种方法可以忽略文件和目录?即使是全局解决方案,例如将应用于我所有的存储库的全局配置。
如果您可以修改.git/info/exclude
,您可以将相同的规则放在那里。但是该文件仅存在于您的本地存储库中。
$GIT_DIR/info/exclude
中读取的模式。core.excludesfile
指定的文件中读取的模式。后两者可能是你问题的解决办法,但是:
(另请参阅SO question)
另外两个解决方案涉及更新索引(git update-index
):
git update-index --assume-unchanged
: 参见 "Git:仅在本地存储库中取消跟踪文件并将其保留在远程存储库中"。Elijah Lynn 在 评论 中提到了这一点。
git update-index --assume-unchanged
on directory"。--no-assume-unchange
来撤销该效果:参见 "是否可能在assume-unchanged
受保护的文件上进行git add
操作?"。但是,当您切换到另一个分支或运行git pull
时,该“忽略”状态可能会被重置。因此有另一个选项:
git update-index --skip-worktree
;参见:
两者之间的区别在"Git - 'assume-unchanged
'和'skip-worktree
'之间的差异"中有解释。
update-index --assume-unchanged
命令,参见https://dev59.com/uHRB5IYBdhLWcg3wWF8H#25253144。 - Elijah Lynnupdate-index --skip-work-tree
命令。我已经编辑了答案,添加了一些链接到我以前的回答,说明这两个 update-index
命令的用法。 - VonC有三种方法可以告诉GIT忽略哪些文件:
.gitignore
文件$GIT_DIR/.git/info/exclude
core.excludesfile
指定的文件后两种方法可以解决您的问题。
更多信息请参见gitignore(5)。
git update-index --assume-unchanged
的问题是您无法对未跟踪的文件执行此操作。您说:
我理解您的意思可能是您无法将任何更改推送到远程的我不能修改我的仓库中的.gitignore文件。
.gitignore
。如果是这种情况,您可以将未跟踪的文件添加到本地的.gitignore
中,然后执行git update-index --assume-unchanged .gitignore
,以便您对.gitignore
的更改从不被推送。现在,您正在忽略(可能是)未跟踪的文件,并且不会影响远程的.gitignore
文件。另一种方法:
.gitignore
git update-index --assume-unchanged .gitignore
忽略已跟踪文件的本地更改:
git update-index --assume-unchanged my-file.php
取消忽略已跟踪文件的本地更改:
git update-index --no-assume-unchanged my-file.php
来源:git help update-index
--[no-]assume-unchanged
...
This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked
files (akin to what .gitignore does for untracked files). 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.
tracked_dir1/tracked_dir2/untracked_dir/
中,然后添加一个tracked_dir1/tracked_dir2/untracked_dir/.gitignore
文件,内容如下:
*
即可。$ cat > tracked_dir1/tracked_dir2/untracked_dir/.gitignore
*
<Ctrl+D>
使用vim或emacs修改.git/info/exclude
对我来说很有效。
注意@Desko27的评论,然而,如果您想忽略已经被Git跟踪的文件,则此方法不起作用。
因此,在跟踪之前尽早忽略文件。
.git
目录... - zakdances.git
文件夹实际上位于父仓库/.git/modules/子模块名称
。所以你需要编辑父仓库/.git/modules/子模块名称/info/exclude
文件。 - Kara Brightwell