1

I have some secure key files which need to be committed only to the staging branch. But I need to ignore them in all the other branches.

In here Using git, how do I ignore a file in one branch but have it committed in another branch? it shows how to commit in one branch and ignore in another. It didn't work for me. I want to commit in one branch and ignore in all the others. How can I do that?

Community
  • 1
  • 1
THpubs
  • 7,804
  • 16
  • 68
  • 143
  • The answer in your linked post is bogus: see http://stackoverflow.com/a/29583813/1256452 (via http://stackoverflow.com/a/39280410/1256452). – torek Sep 02 '16 at 11:51
  • If you have the file in `.gitignore` then you can still force add it in one branch `git add -f ` and changes will be tracked for that branch only. – Jeff Puckett Sep 02 '16 at 11:52
  • Thanks. But what if we merge that branch into another? Let's say we force add the file to branch `a`. Now I checkout to `b` and merge `a`. Will I get those files in `b`? – THpubs Sep 02 '16 at 16:00

1 Answers1

3

Have the file listed in the .gitignore file in all other branches.

Then in the branch you want it tracked, add a ! before the filename in the .gitignore file. Then manually add the file git add <filename> and it should now be tracked in this current branch but not in the others

g19fanatic
  • 10,567
  • 6
  • 33
  • 63