Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily hide some changed files in git

Tags:

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?

like image 298
eXavier Avatar asked Oct 21 '25 11:10

eXavier


2 Answers

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>
like image 153
1615903 Avatar answered Oct 23 '25 00:10

1615903


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>
like image 33
Alberto Trindade Tavares Avatar answered Oct 23 '25 01:10

Alberto Trindade Tavares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!