如何在不在.gitignore中添加文件的情​​况下在本地忽略文件

时间:2017-08-18 09:37:31

标签: git github

我有一个github存储库,我在本地检查过。

  • 我更改了environment.local-dev.ts文件中的环境变量。 (它通常指向localhost,但由于我没有安装本地api存储库,我更新了文件以指向我的同事本地api)
  • 现在,更改的配置特定于我的需要。所以,我不想将这些更改提交给Github存储库

我搜索了两个选项:

  • .gitignore中添加文件。但问题是,我仍然会看到.gitignore,我可能会意外地犯下这个问题。
  • 将其添加到.git/info/exclude。我这样做了,但是当我git status
  • 时,我仍然看到更改的文件

我正在处理的Github存储库已经由其他人创建。所以无法改变初始配置。 其他人也在同一个存储库上工作,因此无法提交可能会对其他人产生负面影响的更改。

因此,问题是要找到not commit tracked by git文件的方法。有没有办法做到这一点?

3 个答案:

答案 0 :(得分:5)

编辑:正如@Philippe skip-worktree指出的那样比assume-unchanged更合适。在这个答案中,一切都是一样的,但你只需要切换选项。

.gitignore.git/info/exclude仅用于排除未跟踪的文件。

要忽略对跟踪文件的本地修改,您可以使用git update-index --assume-unchanged FILES... 但是存在忘记需要在文件中提交的修改的风险 要再次显示修改:git update-index --no-assume-unchanged FILES...

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

$ git update-index --assume-unchanged file

$ git status
On branch master
nothing to commit, working tree clean

$ git ls-files -v
h file

$ git update-index --no-assume-unchanged file

$ git ls-files -v
H file

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

您可以定义一些别名:

[alias]
    forget = update-index --assume-unchanged
    unforget = update-index --no-assume-unchanged
    forgotten = ! git ls-files -v | grep ^[a-z]

更好的方法是使本地忽略配置覆盖文件。

答案 1 :(得分:2)

试试这个:

git update-index --skip-worktree environment.local-dev.ts

如需进一步阅读,请参阅git update-index documentation和其他相关问题,例如Git - Difference Between 'assume-unchanged' and 'skip-worktree'

答案 2 :(得分:1)

与其他答案相反,不是assume-unchanged功能(保留用于检查成本高昂的大文件)应该使用skip-worktree

请参阅https://stackoverflow.com/a/13631525/717372

相关问题