0

Let say I have two branches in my Git repository.

  1. master

  2. slave

Now while in master, I created a file (holding some sensitive information) called apikey.env. I added it to .gitignore file since I don't want to commit it. After that I committed the .gitignore with the new record.

Now, when I checkout to slave branch, I still find the apikey.env file there, about which, git says that it is untracked. If I delete the file, I gets removed from master branch as well.

So, what I want is that the file apikey.env should appear only in master branch but not in the slave branch, and I don't want to commit the file even in master branch. What is the best way to achieve this?

snehanshu.js
  • 137
  • 1
  • 12
  • Check solution at : https://stackoverflow.com/questions/1836742/using-git-how-do-i-ignore-a-file-in-one-branch-but-have-it-committed-in-another – code707 Jun 26 '18 at 06:17

2 Answers2

1

There is no one best way. A simple way that works, though, is to add that path name to .gitignore in your other branch, and commit that .gitignore file there as well.

There's an incorrect assumption in your problem statement though:

If I delete the [apikey.env] file, it gets removed from master branch as well.

The file should not be in (any commits on) master at all, and given how you phrased the text before this, it sounds like it is not in them. So this file, while it is in your work-tree, is not in any branch! (This phrasing is not very exact because we have not properly defined the word branch. See What exactly do we mean by "branch"? for more about that.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • The simple way you mentioned works, but what if I have a lot of branches? Should I commit to `.gitignore` in those branches also? And by `branch`, I mean whatever Github portrays branches as. – snehanshu.js Jun 25 '18 at 19:08
  • @overflow_yoho_hoolala - Consider that `.gitignore` is content just like any other file you commit. So if your project must have a particular (function | class | page | whatever the units of code are for your type of project), and it must be available on all branches, you don't commit it on all branches; you commit it on the first branch, and as you branch from that first branch, the files are all there. Ignore rules defined in `.gitignore` are the same; you only seem to have to commit the ignore rule twice because you'd already created a second branch before you introduced the rule. – Mark Adelsberger Jun 25 '18 at 19:16
  • @MarkAdelsberger Yeah, the branches already branched out before I could commit the ignore rule in the first branch. ;) – snehanshu.js Jun 25 '18 at 20:09
0

A simple way to achieve it is to add it to an ignore file which is outside of your version control, and so won't change when you switch branches. The downside of this is that you have to replicate this non-versioned ignore file for each working directory / team member who gets this repo.

If it's just for this working directory, you can add to .git/info/exclude (same format as .gitignore).

If you want it to affect all workspaces on your local machine, you can add it to a .gitignore in your home dir and tell git to use that.

git config --global core.excludesfile ~/.gitignore

This will cause the file to be ignored by all git repos on your local machine.

tul
  • 1,709
  • 16
  • 15