Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore changes to a tracked file

Tags:

git

file

ignore

I want to change a tracked file for development only, but keep the tracked version unchanged.

Most "solutions" for this suggest

git update-index --assume-unchanged

but it's totally useless, because the file will still be changed by checkout or reset. Is there a better solution that survives checkout and reset commands?

like image 815
user923487 Avatar asked Aug 27 '15 13:08

user923487


People also ask

How do I Gitignore an already tracked file?

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.

Should Gitignore 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. Also see this: Should you commit .

How do I Untrack a Git file without deleting it?

Using the git rm –cached Command However, the git rm command provides the –cached option to allow us only to remove files from the repository's index and keep the local file untouched. As we can see, the user-list. txt is “deleted“. Further, since its local copy is still there, it has been marked as “untracked”.

Which command is used to ignore the files which are not tracked?

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.


1 Answers

Quick Fix

This is what I think you're trying to do, change a file, but ignore it when committing.

git update-index --skip-worktree my-file

Here is a good answer regarding the difference between assume-unchanged and skip-worktree.

Git will still warn if you try to merge changes into my-file. Then you will have to "unskip" the file, merge it and "re-skip" it.

git update-index --no-skip-worktree my-file
# merge here
git update-index --skip-worktree my-file

There can also be problems if you modify the file, then switch to a branch where that file has been changed. You may have to do some fancy "skip/unskip" operations to get around that.

Long Term Fix

In the long term, you probably want to separate your "local" changes into a second file. For example, if the file you want to change is a config file, create a "default" config file that you check into the repository. Then, allow a second "overrides" config file that is optional and put that file in your .gitignore.

Then, in your application, read the default config file and then check if the overrides file exists. If it does, merge that data with the data from the default file.

This example is for a config file, but you can use that technique for other kinds of overrides if needed.

like image 123
Justin Howard Avatar answered Sep 30 '22 13:09

Justin Howard