3

I need to ignore changes, both local and remote, to a directory. For example I have remote git set with:

  • templates/T1
  • templates/T2
  • templates/Tn

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

    /*
    !/T2

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.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Dexa
  • 1,641
  • 10
  • 25

3 Answers3

3

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.

phts
  • 3,889
  • 1
  • 19
  • 31
2

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!

Mudassir Razvi
  • 1,783
  • 12
  • 33
1

Since you said in the comments that you only need one template checked out, I would like to suggest you make use of gits sparse-checkout.

You can take a look at this answer, which explains it in greater detail, but I will provide a specific example for your case.


  1. Activate sparse-checkout with git config core.sparsecheckout true
  2. Create a sparse-checkout file in .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
  1. 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 T2 file.

Community
  • 1
  • 1
Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73