切换 Git 分支而不检出文件

发布于 2024-08-01 22:16:29 字数 454 浏览 13 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

神经大条 2024-08-08 22:16:30

是的,你可以这样做。

git symbolic-ref HEAD refs/heads/otherbranch

如果您需要在此分支上提交,您也需要重置索引,否则您最终将根据上次签出的分支提交某些内容。

git reset

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
远山浅 2024-08-08 22:16:30

仅使用基本的 git 命令:

这个答案比 Charles 的长一点,但它仅包含我可以理解并记住的基本 git 命令,无需继续查找。

标记您当前的位置(如果需要,请先提交):

git checkout -b temp

将标记重置(移动)到另一个分支而不更改工作目录:

git reset <branch where you want to go>

现在 temp 和其他分支指向相同的提交,并且您的工作目录保持不变。

git checkout <branch where you want to go>

由于您的 HEAD 已经指向相同的提交,因此不会触及工作目录

git branch -d temp

请注意,这些命令也可以从任何图形客户端轻松获得。

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.

我不是你的备胎 2024-08-08 22:16:30

在 v2.24 中,git switch 类似于安全的 git checkout
因此,我将下面的别名重命名为 git hop for
“在不改变工作树的情况下跳到树枝上”

为了读者的利益:

虽然我认为 Charles Bailey 的解决方案是正确的,当切换到非本地分支时,此解决方案需要进行调整。 还应该有一些方法来使用易于理解的常规命令来执行此操作。 这是我想到的:

git checkout --detach
git reset --soft commitish
git checkout commitish

解释:

  • git checkout --detachgit 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 config --global alias.hop '!f() { git rev-parse --verify "$*" && git checkout "HEAD^{}" && git reset --soft "$*" && git checkout "$*"; }; f'

仅供参考,您可以在我的 git 别名

In v2.24 git switch is something like a safe git checkout.
Hence I renamed the alias below to git hop for
"hop on the branch without changing worktree"

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 hop treeish alias:

git config --global alias.hop '!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.

甜中书 2024-08-08 22:16:30

您可以使用不同的分支名称覆盖 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

对风讲故事 2024-08-08 22:16:30

或者只是使用补丁文件从您的其他分支修补到您的主分支

git diff otherbranch master > ~/tmp/otherbranch.diff
git checkout master
git apply ~/tmp/otherbranch.diff

Or just use a patch file to patch from your otherbranch to your master

git diff otherbranch master > ~/tmp/otherbranch.diff
git checkout master
git apply ~/tmp/otherbranch.diff
心凉 2024-08-08 22:16:30

对于如此多的文件,您最好只保留两个存储库,每个分支一个。 您可以根据需要来回拉取更改。 与尝试用 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.

無處可尋 2024-08-08 22:16:30

如果您只是尝试更改远程分支指向的位置,则可以使用“git Push”来完成此操作,而无需触摸本地副本。

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

的格式 参数是可选的加号 +,后跟源引用,后跟冒号 :,后跟目标引用。 它用于指定的内容。 对象远程存储库中的 ref 将被更新。

例如,要更新 foo 以提交 c5f7eba,请执行以下操作:

git push origin c5f7eba:foo

不确定这是否是您想要的。

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.

鱼窥荷 2024-08-08 22:16:30

您可以使用

      1. git checkout -f <new-branch>
      2. git cherry-pick -x <previous-branch-commit-id>

previous-branch-commit-id 是您要复制旧数据的提交。

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.

狼性发作 2024-08-08 22:16:30

假设你想在分支 A 中,但是使用分支 B 中的文件,

使用 git log 找到分支 A 的当前提交引用,例如“99ce9a2”,

git checkout A
git reset --hard B
git reset 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",

git checkout A
git reset --hard B
git reset 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).

一人独醉 2024-08-08 22:16:30

我认为您正在寻找管道命令 <代码>git read-tree。 这将更新索引,但不会更新工作目录中的任何文件。 例如,假设branch是要读取的分支的名称:

git read-tree branch

如果您想提交到刚刚读取的分支,您还需要:

git symbolic-ref HEAD refs/heads/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, 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
逆光飞翔i 2024-08-08 22:16:30

使用两个工作目录(两个工作区域)和一个存储库,甚至两个存储库不是更好的解决方案吗?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文