eXavier
eXavier

Reputation: 4981

Temporarily hide some changed files in git

Whenever I do a work task, I create a local branch in git, then I need to modify a bunch of configuration files (mostly containing connection strings to databases) to set to my local environment before the actual work. These changes I don't want to push to the repository later after I finish my task.

Actually I would prefer to filter them out when reviewing changed files (git status).

I could do a first commit to my local branch and revert it before pushing. But there's a chance I forget it. Is there a better way for this?

Upvotes: 4

Views: 699

Answers (2)

1615903
1615903

Reputation: 34859

The most recommended solution is to use:

git update-index --skip-worktree <file>

Documentation:

--[no-]skip-worktree

When one of these flags is specified, the object name recorded for the paths are not updated. Instead, these options set and unset the "skip-worktree" bit for the paths. See section "Skip-worktree bit" below for more information.

And the skip-worktree bit:

Skip-worktree bit

Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

To elaborate, "reading" means checking for file existence, reading file attributes or file content. The working directory version may be present or absent. If present, its content may match against the index version or not. Writing is not affected by this bit, content safety is still first priority. Note that Git can update working directory file, that is marked skip-worktree, if it is safe to do so (i.e. working directory version matches index version)

Although this bit looks similar to assume-unchanged bit, its goal is different from assume-unchanged bit’s. Skip-worktree also takes precedence over assume-unchanged bit when both are set.

The --assume-unchanged flag is intended for performance improvement, whereas --skip-worktree is intended for cases where you do change the file and want git to skip it anyway.

To undo, use:

git update-index --no-skip-worktree <file>

Upvotes: 3

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10396

To temporarily ignore changes in a specific file, you can do this:

git update-index --assume-unchanged <file>

When you want to track changes again of it, you just need to run:

git update-index --no-assume-unchanged <file>

Upvotes: 3

Related Questions