简体   繁体   中英

How to ignore files locally without adding it in .gitignore

I have a github repository, which I checked out locally.

  • I changed my environment variables in environment.local-dev.ts file. (It usually points to localhost, but as I don't have local api repository installed, I updated file to point to my colleagues local api)
  • Now, the changed configuration is specific to my need. So, I don't want to commit these changes to the Github repository

I searched for two options :

  • To add file in .gitignore . But the problem is, I will still see .gitignore , which I might commit accidently.
  • To add it in .git/info/exclude . I did this, but I am still seeing the changed files when I do git status

The Github repository, I am working on is already created by someone else. So cannot change the initial configuration. Also other people are working on the same repository so cannot commit changes which might affter other people negatively.

So, the problem is to find a way to not commit files which are being tracked by git . Is there a way to do this ?

EDIT : as pointed by @Philippe skip-worktree would be more appropriate than assume-unchanged . In this answer, everything is the same but you just switch the option.

.gitignore and .git/info/exclude are only to exclude untracked files.

To ignore local modifications of tracked files, you can use git update-index --assume-unchanged FILES... .
But there is a risk to forget modifications that need to be committed in the file.
To get the modification visible again: 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")

You can define some aliases:

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

A better way would be to have an local ignored configuration overwritting file.

Try this:

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

For further reading, see git update-index documentation and other related questions, like Git - Difference Between 'assume-unchanged' and 'skip-worktree' .

Contrary to what said other answers, that not the assume-unchanged feature (that is reserved for big files that is costly to check) that you should use but skip-worktree .

See https://stackoverflow.com/a/13631525/717372

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM