snehanshu.js
snehanshu.js

Reputation: 167

How to prevent ignored file in one branch to be detected in another branch in git?

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?

Upvotes: 0

Views: 125

Answers (2)

tul
tul

Reputation: 1779

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.

Upvotes: 0

torek
torek

Reputation: 488453

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.)

Upvotes: 1

Related Questions