如何快速向前分行而不切换到他们? [英] How do I fast foward branches without switching to them?

查看:111
本文介绍了如何快速向前分行而不切换到他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在我的 master 分支上,我的舞台很脏。我不想提交,并且我已经有太多的存储空间来存储我的更改(或者我太懒惰了吗?)。

我想快速向前移动一些分支而不切换到它们(请记住,我的舞台很脏)。

我怎么能快速 optim 手机?或 optim 到 master (我当前的分支)?



图片: http://i.stack.imgur.com/WUPeq.png

解决方案

注意:分支名称仅仅是一个指向(单个)提交的指针。我们倾向于将其视为分支 - 整个提交链 - 是通过将每个提交工作交给其父代来实现的。父母只记录在提交中,而不记录在任何分支名称中。这可能有助于将分支名称视为粘贴到某个特定提交的便签。我不能在文本中绘制它,所以我将它绘制为箭头(< - )。



将图像翻译为文本,旋转图像,做一两个假设,截断一点以适应这个答案,你会看到:

  o ... 
/
--o - M - o - o - o - o - o - o - o - o --o - o - o< - mobile
/
- o< - optim,origin / optim

(这里更新的东西是向右的,而不是上面的,并且各种提交消息的文本被省略。)

在以下情况发生快进: 1




  • 分支,即 HEAD 是对分支名称的符号引用

  • 你要求git与另一个提交合并SHA1 ID;通常通过分支名称)

  • ,并且合并提交是直的提前分支现在的位置,也就是说,可以简单地沿着一些后代链接滑动分支名称到达那里。 (更确切地说, HEAD commit是待合并提交的祖先。)


由于 optim 指向一个提交,其单个后代是合并提交 M ,并且该提交对于右侧的每个 o 都有一个后代,git可以沿该链向前滑动分支名称 optim

因此:

  $ git checkout optim; git merge mobile 

会将标签上下滑动到M,然后进一步右移到 mobile 点数,给出:

  o ... 
/
--o - M - o - o - o - o - o - o - o - o - o - o - o < - mobile,optim
/
--- o< - origin / optim

如果你希望这样做是手动的,没有做任何事情,甚至没有在分支上,你可以告诉git请重新指出分支名称 optim 定位与名称 mobile 相同的提交:

  $ git分支-f优化手机



-f (或 - force )标志表示继续并更改现有标签的目标提交,而不是出错。



这个命令不检查操作是否是快速操作,前锋。 (当然,纯粹的 git merge 只会检查是否执行快进合并或真正合并。但是,您可以询问 git merge - ff-only ,即如果操作不是快速出错)






1 取和推也应用相同种类的快进的概念,所以这是何时,而不是只有当。


I currently am on my master branch, and my stage is dirty. I don't want to commit and I already have too many stashes to stash my changes (or am I too lazy ?).

I would like to move fast foward some branches without switching to them (remember, my stage is dirty).

How could I fastfoward optim to mobile ? or optim to master (my current branch) ?

image: http://i.stack.imgur.com/WUPeq.png

解决方案

Note: a branch name is merely a pointer to a (single) commit. The thing we tend to think of as "the branch"—the whole chain of commits—is found by working from each commit backwards to its parent(s). The parents are recorded only in the commits themselves, not in any branch names. It may help to think of branch names as sticky-notes that are pasted on to some particular commit. I can't draw that in text so I draw it below as an arrow (<--) instead.

Translating your image to text, rotating it, making an assumption or two, and truncating a bit to fit nicely in this answer, you're showing:

                                        o...
                                       /
--o--M--o--o--o--o--o--o--o--o--o--o--o    <-- mobile
    /
---o                                       <-- optim, origin/optim

(Here "newer" stuff is towards the right, instead of above, and the text of the various commit messages is omitted.)

A "fast forward" occurs when:1

  • you are "on a branch", i.e., HEAD is a symbolic reference to a branch name
  • you ask git to merge with another commit (by name or SHA1 ID; usually by branch-name)
  • and, the "merge with" commit is "straight ahead" of where the branch is now, i.e., it's possible to simply slide the branch name along some set of "descendent" links to get there. (More precisely, the HEAD commit is an ancestor of the "to-be-merged" commit.)

Since optim points to a commit whose single descendent is the merge commit M, and that commit has one descendent for each little o to the right, git can slide the branch name optim forward along that chain of commits.

Hence:

$ git checkout optim; git merge mobile

would slide the label up-and-right to M, then further right to where mobile points, giving:

                                        o...
                                       /
--o--M--o--o--o--o--o--o--o--o--o--o--o    <-- mobile, optim
    /
---o                                       <-- origin/optim

If you want to do that "manually", without doing anything else and without even being "on" that branch, you can just tell git "please re-point the branch name optim to target the same commit as the name mobile:

$ git branch -f optim mobile

The -f (or --force) flag says to go ahead and change an existing label's target-commit, rather than erroring out.

This command does not check that the operation is a fast-forward. (Of course plain git merge only checks for that to decide whether to do a fast-forward merge, or a "real" merge. However, you can ask git merge to do --ff-only, i.e., error out if the operation is not a fast-forward.)


1Fetch and push also apply the same kind of "fast forward" notion, so this is "when" but not "only when".

这篇关于如何快速向前分行而不切换到他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆