Andreas Kruhlmann
Andreas Kruhlmann

Reputation: 161

How do I stop tracking a file and keep its current state in Git?

I have a configuration file for my python program where users have to insert an API key.

I am tracking the file with Git and I committed the file as just a template form.

I need to use the file myself with my private API key so how do I tell Git that the template version is the "final" version and to not track any further changes?

Upvotes: 0

Views: 135

Answers (2)

MrTux
MrTux

Reputation: 33993

You can flag the file as "skip-worktree", from that moment on changes to this file will be ignored on diff and commit:

git update-index --skip-worktree filename

When merges or pulls affect the file, you will get an error and have to reset the flag (this is done in order to prevent errors).

See https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 141946

I need to use the file myself with my private API key so how do I tell Git that the template version is the "final" version and to not track any further changes?

Once the file added to your repository, adding it to .gitignore will not work since its already tracked by git.

You will need to use the assume-unchanged flag
https://git-scm.com/docs/git-update-index

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

In case you need to print out list of files marked with the --assume-unchanged flag:

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

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

enter image description here

Upvotes: 0

Related Questions