本地改了一个已被跟踪的文件,又不想提交,.gitignore 经常帮不上忙。它只对尚未跟踪的文件生效。文件一旦进过索引,改了还是会出现在 git status 里。
下面分两种目标:只自己忽略,和整个团队都忽略。
只自己忽略:不改远程、不影响别人
--assume-unchanged | --skip-worktree | |
|---|---|---|
| 本地修改不出现在 status | 是 | 是 |
| 适合 | 临时改、单机用 | 本地配置、长期改、多人仓库 |
| 别人改了同一文件再 pull | 容易冲突 | 更倾向保住本地修改 |
| 恢复 | --no-assume-unchanged | --no-skip-worktree |
临时藏一下,用 assume-unchanged:
bash
git update-index --assume-unchanged path/to/local.conf
git update-index --no-assume-unchanged path/to/local.conf本地配置要长期改、仓库里还有一份模板,用 skip-worktree:
bash
git update-index --skip-worktree path/to/local.conf
git update-index --no-skip-worktree path/to/local.conf这两条都只改本机索引,不推远程。
整个团队都忽略:用 .gitignore
仓库根目录放 .gitignore,例如:
text
*.log
/config/local.conf然后提交并推送这个文件。
如果目标文件已经被跟踪,先从索引拿掉,本地文件保留:
bash
git rm --cached path/to/local.conf确认 .gitignore 里已经写了对应规则,再提交一次:
bash
git commit -m "停止跟踪 local.conf,交给 gitignore 管理"之后再改这个文件,git status 就不会再提它。注意:从索引拿掉等于告诉所有人「仓库里不再管这个文件」。协作仓库里动已经被跟踪的路径,先和同事说一声。