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:

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

And I want to set local git so that it track only T2 for example, and ignore others.

  • How do we connect to TFS from all Git repositories in a Team Project?
  • Replacing master branch with forked branch in local environment
  • Convert Mercurial project to Git
  • check detailed file list to push in git
  • Git: Rebase or Merge to update a local branch
  • setting --ff-only as a default for git pull but not for git merge
  • 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.

  • Renaming with Git: Using git mv vs deleting a file and adding another
  • Git pull change log
  • Gitlab/Gitolite issue while cloning repo
  • How do I force a subtree push to overwrite remote changes?
  • Run tests only for staged files: `git stash -k -u` and `git stash pop` will raise conflicts on partial staged files
  • Git strategy to backport bugfixes into older branches (cherry-pick vs. merge)
  • 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 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.

    Git Baby is a git and github fan, let's start git clone.