Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch branches with git and get ignored files removed?

Tags:

git

Given I have a master branch and a other branch. In the other branch are files I don't want to commit (e.g. log files) so I ignored them in .gitignore.

When I switch to the master branch, these ignored files stay but they belong only to the other branch.

Is there a way to attach some ignored files to a specific branch (without committing them)?

like image 841
Juri Glass Avatar asked Nov 07 '09 08:11

Juri Glass


2 Answers

It's not possible. If you tell git to ignore these files, git will ignore them, ie. it will not touch them no matter what git operation you are performing.

You could either use a branch-specific naming scheme for your log files or save them in a different directory for each branch.

Alternatively, you could write a hook script (post-checkout) that manages files in a separate git repository.

like image 169
Ferdinand Beyer Avatar answered Sep 28 '22 06:09

Ferdinand Beyer


Git intentionally makes it hard to lose data by accident. For example, what if there were source files that you forgot to git add? There is a git clean command to help you clean up your working copy. Passing the -x flag causes it to also remove files normally ignored by .gitignore. E.g.,

git clean -f -x

If you want more control, you can pass the --dry-run flag to get the list of files and then you could write some shell script to look for certain kinds of files (e.g., *.log).

Then, you could install this script as a post-checkout script (see git-hooks for examples).

like image 29
Pat Notz Avatar answered Sep 28 '22 07:09

Pat Notz