Remove local file on one branch in a git directory, but keep it locally on another branch

I have a git repository with two branches. One is the master branch where my code is, while the other is called gh-pages (for documentation on github pages).
Now, at the master branch, some files are created after I run my program, and I want them to be visible locally (but untracked, e.g. log files). However, I do not want these files to be visible on my other branch. So I do:

$ python program.py  # On master: create test1 and test2
$ ls test?  # shows test1 and test2 are here
$ git checkout gh-pages
$ ls test?  # shows test1 and test2 are here, but I don't want them on this branch
$ rm test?
$ git checkout master
$ ls test?  # test1 and test2 are gone, but they should still be here

Are there any way to have local files on one git branch, but not on another?

Upvotes: 2

Views: 209

Answers (1)

Richard Dally
Richard Dally

Reputation: 1450

git stash can help you saving untracked files and switching between branches.
git stash man page

Example: you are on master and want to switch to gh-pages.
$ git stash save --all <stash name> (1)
$ git checkout gh-pages (2)
Working on gh-pages...
$ git checkout master (3)
$ git stash pop <stash name> (4)

(1) Saving untracked files and ignored files to make working directory clean.
(2) Switching to gh-pages
(3) Going back to master
(4) Retrieving untracked files you saved previously.

Edited command to match you requirements.
To only stash untracked files: --include-untracked
To stash ignored and untracked files: --all (no need to repeat --include-untracked)

Upvotes: 4

Related Questions