Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to ignore changes to a tracked file?

I have a file with database settings in my project which I have set to some defaults. The file is tracked by Git and checked in. Since this file will be edited with different values various developer machines, is there a way I can tell Git to ignore new changes to this file?

I tried adding the file to the .gitignore file, but since the file is tracked it isn't ignored. This is alright and good in other situations, but I am wondering if there is something I can do here?

like image 560
Eugen Konkov Avatar asked May 09 '18 11:05

Eugen Konkov


People also ask

How do I ignore an already tracked file in Git?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

How do I ignore changes in git?

Use Git update-index to ignore changes To resume tracking, run the git update-index command with the --no-skip-worktree flag. Or, you can temporarily stop tracking a file and have Git ignore changes to the file by using the git update-index command with the assume-unchanged flag.

Should Git ignore be tracked?

Yes, you can track the . gitignore file, but you do not have to. The main reason of having this file into repository is to have everyone working on the project, ignoring same files and folders.

How do I ignore a Git file without Gitignore?

You just need to add the file to . git/info/exclude in the same way you would add it to . gitignore .


2 Answers

One may use git update-index --skip-worktree FILE for this purpose.

It is similar to --assume-unchanged but, unlike --assume-unchanged, is not just an unpredictable performance trick.

To cancel --skip-worktree effects and unset the flag use --no-skip-worktree.

Downsides

  • This flag is for local repository only and cannot be pushed to the remote. Thus every clone that needs to ignore the file should do it manually.
  • You may face conflicts when switching to another branch or using git pull. (more details)
like image 87
AXO Avatar answered Sep 23 '22 19:09

AXO


I recommend naming the source-controlled file differently than its actual expected name. For example, if the file is normally named config.json, then name your example file config.json.dist and commit this file. Then add config.json to your .gitignore file. Your devs would simply cp config.json.dist config.json after cloning, and then edit it as required, making subsequent commits without having to worry about accidentally changing the default file or forgetting to toggle some setting on and off all the time.

You might even edit your code to search for config.json first, and if that doesn't exist, fall back to config.json.dist. This would allow the devs to work without even performing the copy step. (This is how PHPUnit works.)

like image 31
Alex Howansky Avatar answered Sep 23 '22 19:09

Alex Howansky