How to switch branches with git and get ignored files removed?
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 bran开发者_运维百科ch, 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)?
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.
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).
精彩评论