Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git, how do I ignore a file in one branch but have it committed in another branch?

Tags:

git

gitignore

People also ask

How can I ignore particular file while doing commit?

Use Git update-index to ignore changes Or, you can temporarily stop tracking a file and have Git ignore changes to the file by using the git update-index command with the assume-unchanged flag.

Is Gitignore specific to a branch?

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


This solution appears to work only for certain, patched versions of git. See a new answer pointing to workarounds and another answer and subsequent comments for a hint which versions may work.

I wrote a blog post on how to effectively use the excludesfile for different branches, like one for public github and one for heroku deployment.

Here's the quick and dirty:

$ git branch public_viewing
$ cd .git/
$ touch info/exclude_from_public_viewing
$ echo "path/to/secret/file" > info/exclude_from_public_viewing 

then in the .git/config file add these lines:

[core]
excludesfile = +info/exclude


[branch "public_viewing"]
excludesfile = +info/exclude_from_public_viewing

Now all the global ignore stuff is in the info/exclude file and the branch specific is in the info/exclude_from_public_viewing

Hope that helps!

http://cogniton-mind.tumblr.com/post/1423976659/howto-gitignore-for-different-branches


Important hint: The accepted answer by Cognition.Mind doesn't work (anymore, for several years now, or perhaps for vanilla versions of git); see the comments. A valid answer and workaround can be found here:

https://stackoverflow.com/a/29583813/2157640

Another alternative workaround (working for my particular problem, but demanding manual stash operations or implementation of a hook) would be git stash -u -a. This is tedious when the differences are large.

And finally, the solution I'm now going with is having forked my VM that we hold our development environment in, and set up info/excludes appropriately for the branch, respectively deleting the offending, uncommitted files/folders.


Let's say we want to ignore build folder from all other branch except production branch . As we want to push build folder in production.

1) Dont include build in .gitignore . If you do that it will always be ignored for all branches .

2) Create a file exclude_from_public_viewing inside ./.git/info (This folder already exists) folder touch ./.git/info/exclude_from_public_viewing

3) Inside exclude_from_public_viewing write one line (As you are trying to ignore build for all the branches). !build

4)There's an existing file .git/info/exclude . We need to add following line in it.

 build

We want to ignore build folder but hasn't added it in .gitignore . So how git will know what to ignore ? Answer is we are adding it to exclude file and conditionally passing that file to git config

5) Now we have to conditionally unignore build folder for production branch. to do that perform following

6) There is a existing file called ./.git/config we need to add following -

a) excludesfile = +info/exclude below [core]

[core]
     excludesfile = +info/exclude

b) Create a new section in the end of ./.git/config as

[branch "production"]
    excludesfile = +info/exclude_from_public_viewing

Solution 2

There is one smart alternate solution . Lets say you want to add build/ folder in production branch and ignore it in all other branches.

1) Add it to your gitignore file.

2) In production branch, while doing git add, force add build folder: git add -f --all build/


I would strongly advise considering putting those MP3 files on S3. Having them be part of your Heroku push (and thus part of your Heroku slug) will greatly slow down your dyno startup time. Since Heroku uses EC2, if the files are on S3 and are only accessed by your app (if users aren't directly linked to S3) you won't even pay any bandwidth charges, only the charge to store 50MB.


Have you tried having .gitignore be different in your branch?

You should be able to ignore what you want based on the branch you are in as long as the files are not tracked on that branch.