theanine
theanine

Reputation: 1008

Is it possible to index a file that is gitignored?

Here's my specific situation:

Branches:
branch-a
branch-b
branch-c

Working directory:
file-a
file-b
...

I don't want file-a checked in to the repository, but I do want to keep a different file-a stored somewhere for every branch. Let's say this file-a contains something unique to each branch like the branch name.

Is there a way to do this?

I would think I would want to put file-a into .gitignore so that it isn't added to the repo. However, the problem with this is I can't index the file after doing that.

What I think I need is something like .gitignore but for the transition from the staging area to the commit.

Upvotes: 0

Views: 258

Answers (4)

ElpieKay
ElpieKay

Reputation: 30868

If you don't want to commit it, why do you want to stage it?

You could make a pre-commit hook.

#!/bin/bash

git reset <path_of_file-a>

You can git add it to the index but it will not be committed by this hook. If you do want to commit it, add the option -n to bypass this hook when git commit. If you want to keep the changes made to file-a before git reset, you could add git stash before it.

Upvotes: 0

David E
David E

Reputation: 1444

One possibility is to use git hooks to help you out. It's not exactly simple, but then again your request doesn't really fit in with the standard git paradigm.

Say you want file-a.txt to behave as mentioned. Then you create files (say) with the following names:

file-a.txt.br_master, file-a.txt.br_dev, ...

etc

In each, you put whatever you'd like the file to be on that branch. Then open up / create the file .git/info/exclude (which is like a local .gitignore that doesn't get deployed), and add the line:

file-a.txt*

This will prevent all the above files from appearing as committable files in your workspace.

The wizardry that would tie it together would be a git hook (see eg https://www.atlassian.com/git/tutorials/git-hooks/local-hooks for a tutorial) that runs post-checkout. The script would check that it's a branch checkout, and then copy the file file-a.txt.br_{branch_name} to file-a.txt. The exact form of the script is up to you, but could be bash, or something like python if you'd prefer.

Upvotes: 4

torek
torek

Reputation: 487993

No, you can't do that.

You can use the (new since Git version 2.5) git worktree multiple-work-trees feature to keep each branch in its own private work-tree, and then just have these files untracked or ignored, and never change branches in any work-tree.

Note that there are still some bugs in git worktree even as of Git version 2.9. The more glaring ones were fixed in 2.6, but work is still ongoing here. You should be fine if you are at 2.6 or higher and avoid anything particularly tricky or unusual, though.

Upvotes: 2

Bryce Drew
Bryce Drew

Reputation: 6729

Add these files to your repo and commit. git add file-a and commit. When you go to merge or push your branches to the remote, either:

1: If you don't care about your repo having an empty commit, remove the file using filter-branch:

git filter-branch --tree-filter 'rm -f file-a' HEAD

2: If you care about having an empty commit. Your commit is just the file you want to get rid of. You can find all commits that change that file by:

git log --follow -p -- file-a

and then Choose to drop the commits that changed the file. (interactive rebase)

Upvotes: 1

Related Questions