在不接触工作树的情况下切换分支?

发布于 2024-11-08 19:15:01 字数 87 浏览 2 评论 0原文

我目前位于调试分支上,并且想切换到主分支,而不修改工作树(保留其在调试分支中的方式),因此我可以将一些更改提交到主分支中。

有办法做到这一点吗?

I am currently on a debug branch, and would like to switch to the master branch, without modifying the working tree (leave it the way it is in the debug branch), so I can commit some of the changes into the master branch.

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

春风十里 2024-11-15 19:15:01

您可以执行以下操作:

git checkout --detach
git reset --soft master
git checkout master

说明:

如果您位于 debug 分支并执行 git reset --soft master 您将保持工作树和索引不变并移至master 指向的提交。问题是,debug 也将重置为此提交。因此,您对 debug 的提交“丢失”了(好吧,不是真的,但它们不再可以直接访问),并且您仍然位于 debug 分支上。

为了防止 git reset 移动 debug 但仍将 HEAD 设置为 master 提交,您首先执行 git checkout --detachHEAD 直接指向当前提交(请参阅 man git-checkout,“分离头”部分)。然后您可以在不接触 debug 分支的情况下进行重置。

现在 HEAD 直接指向 master 指向的提交,即它仍然是分离的。您只需 git checkout master 即可附加到 master,现在就可以在 master 分支上提交。

请注意,git checkout(默认情况下且未传递路径时)仅更新“源”和“目标”提交之间已更改的文件,并保留对工作树中文件的本地修改。由于在这种情况下两次提交是相同的,因此工作目录中的任何文件都不会被触及。

You can do the following:

git checkout --detach
git reset --soft master
git checkout master

Explanation:

If you are on the debug branch and would do git reset --soft master you would leave your working tree and index untouched and move to the commit master points to. The problem is, debug will be reset to this commit too. So your commits on debug are "lost" (well, not really, but they are not directly accessible anymore) and you are still on the debug branch.

To prevent git reset from moving debug but still setting your HEAD to the master commit, you first do git checkout --detach to point HEAD directly to your current commit (see man git-checkout, section "DETACHED HEAD"). Then you can do the reset without touching the debugbranch.

Now HEAD is pointing directly to the commit master points to, i.e. it is still detached. You can simply git checkout master to attach to master and are now ready to commit on the master branch.

Note that git checkout (by default and when no path is passed) only updates files that have been changed between the "source" and "target" commit and local modifications to the files in the working tree are kept. As both commits are the same in this case, no files in the working directory are touched.

执笏见 2024-11-15 19:15:01

此答案使用低级“管道”命令。当心。如果您更喜欢“porcelain”命令,请转到这个答案会产生相同的结果。

您可以将头重置为指向 master,而不更改索引或工作树:

git symbolic-ref HEAD refs/heads/master

您可能应该重置索引,以便可以有选择地应用工作树更改,否则您最终可能会提交 master 和调试分支之间的所有差异,这可能是一件坏事。

git reset

完成所需的提交后,您可以使用以下命令返回到调试分支:

git symbolic-ref HEAD refs/heads/debug-branch
git reset

This answer uses low-level "plumbing" commands. Be careful. If you prefer "porcelain" commands, go with this answer which produces the same results.

You can reset your head to point at master without changing the index or working tree with:

git symbolic-ref HEAD refs/heads/master

You should probably reset the index so that you can selectively apply your working tree changes, otherwise you may end up committing all the differences between master and the debug branch, which is probably a bad thing.

git reset

Once you've made the commit that you want to make you can return to your debug branch with:

git symbolic-ref HEAD refs/heads/debug-branch
git reset
攀登最高峰 2024-11-15 19:15:01

您可以存储(git stash)您的更改、切换分支、取消存储(git stash pop)您的更改、添加并提交更改。

如果您想要调试的确切状态,只需将调试合并到主控中(或重置主控以进行调试)。

You can stash (git stash) your changes, switch branches, unstash (git stash pop) your changes, add and commit the changes.

If you want the exact state of debug, then simply merge debug into master (or reset master to debug).

月下客 2024-11-15 19:15:01

这是一个原始工作流程

 git stash
 git checkout otherbranch
 git stash apply
 git reset
 git add # interactively? just add the hunks/changes you want to commit
 git commit

,要返回

 git reset --hard # watch it here! make sure you haven't added more changes that you wanted to keep
 git checkout debug
 git stash pop

,您也可以在“此处”提交相关更改,然后将/cherry-pick 推送到主分支上。

Here is a raw workflow

 git stash
 git checkout otherbranch
 git stash apply
 git reset
 git add # interactively? just add the hunks/changes you want to commit
 git commit

And to go back

 git reset --hard # watch it here! make sure you haven't added more changes that you wanted to keep
 git checkout debug
 git stash pop

Alternatively, you might just commit the relevant changes 'here' and push / cherry-pick onto the master branch.

你好,陌生人 2024-11-15 19:15:01

据我所知,你不能。分支切换意味着将位于该分支 HEAD 中的代码版本签入工作副本。

您想要合并您的分支。现在

git checkout master
git merge devel

分支将被同步。如果要合并更改的子集,可以指定一个提交或一系列提交。
另请查看 cherry-pick
例如:

git checkout master
git cherry-pick devel

将 devel 中的最后一次提交合并回 master。

如果您需要合并位于不同主机上的两个分支,请查看 git pull 和 git push。

As far as I know, you can't. Branch switching means checking out into the working copy a version of the code that sits in the HEAD of that branch.

You want to merge your branches. Do

git checkout master
git merge devel

The branches will now be synchronized. If you want to merge a subset of changes, you can specify a commit or a range of commits.
Also take a look at cherry-pick
For example:

git checkout master
git cherry-pick devel

Will merge the last commit in devel back into master.

If you need to merge two branches that sit on different hosts, have a look at git pull and git push.

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