views:

636

answers:

6

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 times (and there are about 14000 files - it is a long operation).

To make everything clear:

I need all this to upload documentation to github.

I have repo with gh-pages branch. When I rebuild documentation locally, I copy it to repo directory, commit and push to github. But I was not happy because I had two copies of documentation locally. And I decided to creaty empty branch and after commiting 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 gh-pages branch and delete files, but I don't like dirty working trees )

+3  A: 

You can overwrite your HEAD file with a different branch name:

echo "ref: refs/heads/MyOtherBranch" > .git/HEAD

It's probably better to use the symbolic-ref command to do this for you: `git symbolic-ref HEAD refs/heads/MyOtherBranch` http://www.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html
Greg Hewgill
+7  A: 

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
Charles Bailey
+1  A: 

I think you're looking for the plumbing command git read-tree. This will update the index but will not update any files in your working directory. For example, assuming branch is the name of the branch to read:

git read-tree branch

If you want to then commit to the branch you just read, you will also need to:

git symbolic-ref HEAD refs/heads/branch
Greg Hewgill
no I only need to switch branch, no any other changes - so symbolic-ref is good enough
tig
A: 

With so many files, you may be best off just keeping two repos, one for each branch. You can pull changes back and forth as needed. This is going to be less surprising than trying to play scurvy tricks with git.

Norman Ramsey
You can use `git-new-worktree` for that instead (in `contrib/`)
Jakub Narębski
Ah yes, for every problem, git has a new feature...
Norman Ramsey
+1  A: 

Wouldn't be a better solution to have two working directories (two working areas) with one repository, or even two repositories?

There is git-new-workdir tool in contrib/ section to help you with this.

Jakub Narębski
+1 I didn't know about that, thanks
MBO
A: 

If you are simply trying to change where a remote branch points, you can do it with "git push" without touching your local copy.

http://kernel.org/pub/software/scm/git/docs/git-push.html

The format of a <refspec> parameter is an optional plus +, followed by the source ref <src>, followed by a colon :, followed by the destination ref <dst>. It is used to specify with what <src> object the <dst> ref in the remote repository is to be updated.

eg, to update foo to commit c5f7eba do the following:

git push origin c5f7eba:foo

Not sure if that's what you were after or not.

Tim Abell
The question already got an answer: http://stackoverflow.com/questions/1282639/switch-git-branch-without-files-checkout/1282706#1282706
tig