Git ignore directory without making changes to remote
I need to ignore changes, both local and remote, to a directory. For example I have remote git set with:
And I want to set local git so that it track only T2 for example, and ignore others.
What I tried was this:
git rm -r --cached templates/T1 git rm -r --cached templates/Tn
And then added this to the .gitignore inside of templates dir
But after doing this I got bunch of files marked as deleted that want to be pushed to remote, I do not want this. I need remote to keep having all templates, but on this single local machine I need only one template tracked.
3 Solutions collect form web for “Git ignore directory without making changes to remote”
To ignore some files in git they should be removed from remote server and added to .gitignore. So this approach doesn’t work for you.
To keep files on a remote server but not to track them you can with
git update-index --assume-unchanged files. This will keep some initial version of T1, Tn files on a remote and will not track them on your local repo. So
git status will not list changed T1, Tn.
You cannot ignore an already tracked file even if you add it to .gitignore or exclude.
What you can do is write a pre-commit hook that reverts any changes in the directories you don’t want to track.
However you will have them in your repository! You cannot delete them without bothering git.
Hope it helps!
Since you said in the comments that you only need one template checked out, I would like to suggest you make use of gits
You can take a look at this answer, which explains it in greater detail, but I will provide a specific example for your case.
git config core.sparsecheckout true
- Create a
.git/info; you need to specifiy patterns for the files you want to checkout
In your case this file could look like this:
* # Add everything for checkout !templates/ # Don't checkout the content of templates templates/T2 # Checkout T2 in templates
- Update your working tree
git read-tree -m -u HEAD
The last command should be executed in the root of your repository, with a clean working tree (no changed files etc.).
This should leave your working trees
templates directory empty except for the