Switch Git branch without files checkout

tig picture tig · Aug 15, 2009 · Viewed 44.1k times · Source

Is it possible in Git to switch to another branch without checking out all files?

After switching branch I need to delete all files, regenerate them, commit and switch back. So checking out files is just a waste of time (and there are about 14,000 files - it is a long operation).

To make everything clear:

I need all this to upload documentation to GitHub.

I have a repository with the gh-pages branch. When I rebuild documentation locally, I copy it to the repository directory, commit and push to GitHub. But I was not happy, because I had two copies of documentation locally. And I decided to create an empty branch and after committing, switch to empty and delete files. But switching back is a long operation - so I asked this question.

I know that I can just leave on the gh-pages branch and delete files, but I don't like dirty working trees.

Answer

CB Bailey picture CB Bailey · Aug 15, 2009

Yes, you can do this.

git symbolic-ref HEAD refs/heads/otherbranch

If you need to commit on this branch, you'll want to reset the index too otherwise you'll end up committing something based on the last checked out branch.

git reset