5

There's a repository that's not mine on github. I cloned it so now I have a copy of it in my github account and I have a copy of that repo on my computer. I made a branch that has its own directory and a whole lot of files which I then committed to that branch, call it apple. When I switch back to the master branch I notice in status that the directory and all the files in it that I committed to on apple show up in the status. So now I have a hundred or so files that every time I switch over to master are visible.

My question is how can I make it so that when I switch back over to master those files I have in the apple branch are ignored? I'm sure I could amend the .gitignore in master but that would be changing the project's .gitignore.

Thanks


EDIT: Commenters have pointed out that if the files were committed in another branch they should not show up when I switch branches and this is correct. After working with everyone who helped me here what I discovered had happened was one of the subdirectories in the directory I created on the apple branch had its own .gitignore that ignored a lot files. That .gitignore was committed in the apple branch and when I switched branches (for example to the master branch) that directory specific .gitignore disappeared and all the files that were originally ignored on the apple branch showed up in any other branch in git-gui as being untracked. So I had a whole lot of files that I thought were committed on the apple branch but actually weren't. Sorry for the confusion! I will be marking Ryan Stewart's answer as correct.

loop
  • 3,460
  • 5
  • 34
  • 57
  • 1
    google and stack could do the work http://stackoverflow.com/questions/1836742/using-git-how-do-i-ignore-a-file-in-one-branch-but-have-it-committed-in-another – Casabian Mar 21 '13 at 01:22
  • 1
    The referenced link modifies .gitignore, which the question rules out because "that would be changing the project's .gitignore." – GoZoner Mar 21 '13 at 01:26
  • If the files were committed in another branch, they should not show up when you switch to a branch where they are not present. – qqx Mar 21 '13 at 01:33

2 Answers2

6

You can set your own 'global ignore' file as such:

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

You then populate ~/.gitignore_global as you please. Additionally you can edit

.git/info/exclude

just like .gitignore but it will only apply to that .git repository.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • I think what I'm looking for is a way I can ignore that directory in any branch outside of the one where it's relevant. Is there a way to do that? – loop Mar 21 '13 at 01:33
  • The 'apple' stuff shouldn't be visible on the master branch. See Ryan Stewart's answer above. – GoZoner Mar 21 '13 at 01:36
  • 1
    Perfect, adding files to .git/info/exclude worked for me. – Leo May 25 '16 at 15:45
4

If you truly added new files and committed them on branch apple, then switched back to master, which doesn't contain apple in its history, then those new files won't show up. Something about your description of the problem doesn't make sense.

Perhaps you don't realize that in git, you must git add files before you git commit them (unless you specifically use git commit <files>)? Specifically, if you added directory banana on branch apple, then you could do the following:

git add banana
git commit -m "added the banana feature"
git checkout master

And then you'd be in the master branch, and there'd be no banana. After a git checkout apple, banana would be back again because it exists in that branch.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • If I switch back to master, the directory that I committed in apple is listed as an untracked directory – loop Mar 21 '13 at 01:40
  • Then at least something in that directory isn't committed. – Ryan Stewart Mar 21 '13 at 01:44
  • A `git status -u` will show you a full list of all uncommitted files instead of just top-level directories that are untracked. – Ryan Stewart Mar 21 '13 at 01:44
  • I'm using the git-gui on windows and it shows every file in the directory for unstaged changes. It makes it difficult for me to find the changes I actually want when I'm on master. – loop Mar 21 '13 at 02:09
  • You originally said "untracked". That's not the same as "unstaged". I still think that you just haven't committed the files in the branch. Try following my instructions above and looking at the results. – Ryan Stewart Mar 21 '13 at 02:42
  • yeah I did some investigating and you are correct. I've updated my original post and marked your answer as correct. – loop Mar 21 '13 at 03:13