Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git excludesfile for a branch

Tags:

I want to ignore certain files within a branch without having to rely on a tracked .gitignore file that will be overwritten during merges with other branches.

I’ve closely followed a Stack Overflow answer along with the linked blog post, but my repo doesn’t seem to be recognizing the specified excludesfile in my .git/config. The Git documentation (git-config, gitignore) doesn’t seem to show how to specify and address an excludes file for a specific branch.

My .git/config looks like this:

[core]         repositoryformatversion = 0         filemode = true         bare = false         logallrefupdates = true         ignorecase = true         precomposeunicode = true         excludesfile = +/info/exclude  [branch "myspecialbranch"]         excludesfile = +/info/exclude_specialbranch 

My .git/info/exclude file looks like this (by default, I didn’t touch it):

# git ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of # exclude patterns (uncomment them if you want to use them): # *.[oa] # *~ .DS_Store 

I don’t have a .gitignore file in my “specialbranch”.

If I try to ignore a file like info.php, my .git/info/exclude_specialbranch file looks like this:

info.php 

… but it doesn’t ignore the file.

If I run git status --ignored, it only lists the .DS_Store file from the default exclude file.

However, if I add info.php to my .git/info/exclude file:

# git ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of # exclude patterns (uncomment them if you want to use them): # *.[oa] # *~ .DS_Store info.php 

it ignores the file perfectly.

Note that it still works without the line excludesfile = +/info/exclude under [core] in .git/config. Git seems to know where the system-wide exclude is without me having to tell it.

I have a feeling that .git/config isn’t recognizing the address of my custom excludes file:

excludesfile = +/info/exclude_specialbranch 

… most likely because of the use of + to describe the location of the .git directory.

What’s the correct method for addressing custom exclude files in the (more recent) versions of Git?

I’m running OSX 10.9.5 and Git version 2.2.1.

like image 991
goredwards Avatar asked Apr 11 '15 15:04

goredwards


People also ask

Is Gitignore specific to a branch?

Unfortunately, git doesn't support branch-specific . gitignore files or directives. Using different .

How do I ignore a file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

What is git info exclude?

git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.

How do I ignore a .class file in git?

The easiest and most common way to ignore files is to use a gitignore file. Simply create a file named . gitignore in the repository's root directory. Then, add names and patterns for any files and directories that should not be added to the repository.


1 Answers

Git does not support per-branch excludes files

You’re trying to achieve something that Git does not support. The blog post is the original source of this hoax that the Stack Overflow answer only parroted. As noted in comments under that answer, even the original blog post contains discussion that brings out that the solution does not work and it links to a newer blog post that mentions that even the author is unable to reproduce the behavior.

Why it does not work? If you read man gitignore and man git-config, you’ll find just core.excludesfile referenced. No branch.<name>.excludesfile there. The core.excludesfile is meant to enable you to exclude e.g. Vim .swp files or other temporary stuff your software uses.

core.excludesfile

In addition to .gitignore (per-directory) and .git/info/exclude, Git looks into this file for patterns of files which are not meant to be tracked. "~/" is expanded to the value of $HOME and "~user/" to the specified user’s home directory. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead. See gitignore(5).

Work-around

I believe that the best approximation of a per-branch excludes file is achieved using the post-checkout hook and realization of the .gitignore via a symlink.

Each branch would have e.g. a .gitignores directory with files named after the corresponding branches. Then there would be a .gitignores/__default file that would be used by default. The .gitignore would be excluded by all the excludes files and would be created by the post-checkout hook as a symlink to the corresponding file in .gitignores.

If you don’t want to track the excludes files, you may do the same with .git/info/exclude file as a symlink to .git/info/excludes/__default etc.

like image 53
Palec Avatar answered Sep 19 '22 15:09

Palec