loading...

How to ignore files already managed with Git locally

nishina555 profile image Toshiharu Nishina ・2 min read

There are ways to write in .gitignoreor.git / info / exclude not to manage files with Git.
These methods are for files that have not been managed with Git yet.

However, There are times when you want to ignore files that are already managed with Git locally
For example, you want to exclude from Git's management, such as when you modify the application's configuration file for local use.

In this post, I will explain the Git command which can ignore files even if they are already managed by Git.

There are two ways

  • git update-index --skip-worktree
  • git update-index --assume-unchanged

git update-index --skip-worktree

When to use?

--skip-worktree is the flag which means the files should change locally.

That is, Use the command when you want to modify files managed by Git locally (or updated automatically) but you do not want Git to manage that change.

Because the command is to prevent local changes from being managed by Git, we will use the command in most cases.

Exclude from the management of Git

$ git update-index --skip-worktree path/to/file

How to confirm

$ git ls-files -v | grep ^S
  • git ls-files shows all files managed by git.
  • -v check the file being ignored.
  • --skip-worktree is displayed withS.

Restore to the management of Git

$ git update-index --no-skip-worktree path/to/file

git update-index --assume-unchanged

When to use?

--assume-unchanged is the flag which means the files should not change locally.

In other words, it is used when ignore files that you do not need to change locally (or should not change).

--assume-unchanged is used when you want to speed up Git's behavior by ignoring unnecessary files.

Also, since it is an idea to ignore local changes, git reset - hard command will delete local changes.

Exclude from the management of Git

$ git update-index --assume-unchanged path/to/file

How to confirm

git ls-files -v | grep ^h
  • assume-unchanged is displayed withh.

Restore to the management of Git

git update-index --no-assume-unchanged path/to/file

by @nishina555

Posted on Jun 12 '18 by:

nishina555 profile

Toshiharu Nishina

@nishina555

A web developer for the fifth year.Rails/React/GraphQL.

Discussion

markdown guide
 

Looks like in my version of git (2.21.0 on Mac) it lists them with a lowercase 's' (not an uppercase 'S'), so using the '-i' (ignore case) flag with 'grep' makes it compatible:

$ git ls-files -v | grep -i ^S
s Audit/base.py
s etl.ini

$ git --version
git version 2.21.0 (Apple Git-122.2)
 

Thank you It's really helpful

What I've understood so far if I am not wrong, we use --skip-worktree to sort of unstage a file, any local modification made to the file it won't appear when we run git status even when we commit, no changes are saved is that it ?

And what's the difference between --skip-worktree and --assume-unchanged ??

Thank you in advance Sir

 

Thanks for this.
Just one note to avoid false negatives: use -E flag in grep (in my case at least).

git ls-files -v | grep -E '^S'
git ls-files -v | grep -E '^h'
 

Where do these changes actually get saved to?

 
 

This helped me a lot, thanks for the throughness!