切换 Git 分支而不检出文件
Git 是否可以在不检查所有文件的情况下切换到另一个分支?
切换分支后,我需要删除所有文件,重新生成它们,提交并切换回来。 所以检查文件只是浪费时间(大约有 14,000 个文件 - 这是一个很长的操作)。
为了让一切变得清晰:
我需要所有这些才能将文档上传到GitHub。
我有一个带有 gh-pages 分支的存储库。 当我在本地重建文档时,我将其复制到存储库目录,提交并推送到 GitHub。 但我并不高兴,因为我在本地有两份文档。 我决定创建一个空分支,并在提交后切换到空分支并删除文件。 但切换回来是一个漫长的操作 - 所以我问了这个问题。
我知道我可以离开 gh-pages 分支并删除文件,但我不喜欢肮脏的工作树。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的,你可以这样做。
如果您需要在此分支上提交,您也需要重置索引,否则您最终将根据上次签出的分支提交某些内容。
Yes, you can do this.
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 命令:
这个答案比 Charles 的长一点,但它仅包含我可以理解并记住的基本 git 命令,无需继续查找。
标记您当前的位置(如果需要,请先提交):
将标记重置(移动)到另一个分支而不更改工作目录:
现在 temp 和其他分支指向相同的提交,并且您的工作目录保持不变。
由于您的 HEAD 已经指向相同的提交,因此不会触及工作目录
请注意,这些命令也可以从任何图形客户端轻松获得。
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):
Reset (moves) the marker to the other branch without changing working dir:
now temp and other branch point to the same commit, and your working dir is untouched.
since your HEAD is already pointing to the same commit, working dir is not touched
Note that these commands are also readily available from any graphical client.
为了读者的利益:
虽然我认为 Charles Bailey 的解决方案是正确的,当切换到非本地分支时,此解决方案需要进行调整。 还应该有一些方法来使用易于理解的常规命令来执行此操作。 这是我想到的:
解释:
git checkout --detach
与git checkout HEAD^{}
相同,它将当前分支留在后面并进入“分离”元首国家”。 因此HEAD
的下一次修改不再影响任何分支。 分离HEAD
不会影响工作树或索引。git reset --soft commitish
然后将HEAD
移动到给定commitish
的 SHA。 如果您也想更新索引,请保留--soft
,但我不建议这样做。 同样,这不会触及工作树,并且 (--soft
) 也不会触及索引。git checkout commitish
然后再次将HEAD
附加到给定的commitish
(分支)。 (如果commitish
是 SHA,则不会发生任何情况。)这也不会影响索引或工作树。此解决方案接受引用提交的所有内容,因此这对于某些 git 别名来说是理想的选择。 下面的
rev-parse
只是一个测试,以确保链中没有任何中断,这样打字错误就不会意外切换到分离的头状态(错误恢复会更加复杂)。这会导致以下 git hop treeish 别名:
仅供参考,您可以在我的
git
别名。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:
Explained:
git checkout --detach
is the same asgit checkout HEAD^{}
which leaves the current branch behind and goes into "detached head state". So the next modification ofHEAD
no more affects any branch. DetachingHEAD
does not affect the worktree nor the index.git reset --soft commitish
then movesHEAD
to the SHA of the givencommitish
. 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 attachesHEAD
to the givencommitish
(branch) again. (Ifcommitish
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. Therev-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 hop treeish
alias:FYI, you can find it in my list of
git
aliases.您可以使用不同的分支名称覆盖 HEAD 文件:
echo "ref: refs/heads/MyOtherBranch" > .git/HEAD
You can overwrite your HEAD file with a different branch name:
echo "ref: refs/heads/MyOtherBranch" > .git/HEAD
或者只是使用补丁文件从您的其他分支修补到您的主分支
Or just use a patch file to patch from your otherbranch to your master
对于如此多的文件,您最好只保留两个存储库,每个分支一个。 您可以根据需要来回拉取更改。 与尝试用 git 玩坏血把戏相比,这并不令人惊讶。
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.
如果您只是尝试更改远程分支指向的位置,则可以使用“git Push”来完成此操作,而无需触摸本地副本。
http://kernel.org/pub/software/scm/ git/docs/git-push.html
例如,要更新 foo 以提交 c5f7eba,请执行以下操作:
不确定这是否是您想要的。
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
eg, to update foo to commit c5f7eba do the following:
Not sure if that's what you were after or not.
您可以使用
previous-branch-commit-id 是您要复制旧数据的提交。
you can make use of
previous-branch-commit-id is the commit from where you want to copy old the data.
假设你想在分支 A 中,但是使用分支 B 中的文件,
使用 git log 找到分支 A 的当前提交引用,例如“99ce9a2”,
你现在应该在分支 A 上,具有与 B 相对应的文件夹结构,其中显示为未暂存的更改(历史记录未更改)。
say you want to be in branch A, but with the files from branch B
find the current commit ref of branch A with git log, e.g. "99ce9a2",
you should now be on branch A, with a folder structure corresponding to B, which show up as unstaged changes (A history has not changed).
我认为您正在寻找管道命令 <代码>git read-tree。 这将更新索引,但不会更新工作目录中的任何文件。 例如,假设
branch
是要读取的分支的名称:如果您想提交到刚刚读取的分支,您还需要:
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, assumingbranch
is the name of the branch to read:If you want to then commit to the branch you just read, you will also need to:
使用两个工作目录(两个工作区域)和一个存储库,甚至两个存储库不是更好的解决方案吗?
有 git-
contrib/
部分中的 new-workdir 工具可以帮助您完成此操作。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.