可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 )
回答1:
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
回答2:
Using basic git commands only:
This answer is a bit longer than that of Charles, but it consists solely of basic git commands that I can understand and thus remember, eliminating the need to keep looking it up.
Mark your current location (commit first if needed):
git checkout -b temp
Reset (moves) the marker to the other branch without changing working dir:
git reset <branch where you want to go>
now temp and other branch point to the same commit, and your working dir is untouched.
git checkout <branch where you want to go>
since your HEAD is already pointing to the same commit, working dir is not touched
git branch -d temp
Note that these commands are also readily available from any graphical client.
回答3:
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.
回答4:
For the benefit of the reader:
While I think that Charles Bailey's solution is a correct one, this solution needs a tweak when switching to something, which is not a local branch. Also there should be some way how to do it with regular commands which is easy to understand. Here is what I came up with:
git checkout --detach
git reset --soft commitish
git checkout commitish
Explained:
git checkout --detach
is the same as git checkout HEAD^{}
which leaves the current branch behind and goes into "detached head state". So the next modification of HEAD
no more affects any branch. Detaching HEAD
does not affect the worktree nor the index.
git reset --soft commitish
then moves HEAD
to the SHA of the given commitish
. If you want to update the index, too, leave --soft
away, but I do not recommend to do so. This, again, does not touch the worktree, and (--soft
) not the index.
git checkout commitish
then attaches HEAD
to the given commitish
(branch) again. (If commitish
is a SHA nothing happens.) This, too, does not affect index nor worktree.
This solution accepts everything which refers to a commit, so this is ideal for some git
alias. The rev-parse
below is just a test to make sure, nothing breaks in the chain, such that typos do not accidentally switch into detached head state (error recovery would be way more complex).
This leads to following git switch treeish
alias:
git config --global alias.switch '!f() { git rev-parse --verify "$*" && git checkout "HEAD^{}" && git reset --soft "$*" && git checkout "$*"; }; f'
FYI, you can find it in my list of git
aliases.
回答5:
You can overwrite your HEAD file with a different branch name:
echo "ref: refs/heads/MyOtherBranch" > .git/HEAD
回答6:
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
回答7:
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.
回答8:
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.
回答9:
you can make use of
1. git checkout -f <new-branch>
2. git cherry-pick -x <previous-branch-commit-id>
previous-branch-commit-id is the commit from where you want to copy old the data.