Git rebase 可能的错误

Bre*_*min 3 git

我正在使用git 版本 2.23.0,这是 MAC 的最新版本,我想我在 git rebase 中发现了一个错误,除非我弄错了。错误不在功能中,而在显示消息中。

让我们使用以下脚本将我们的 git 历史记录为:

  #!/bin/bash

  git init .
  echo "10" >> 1.txt && git add . && git commit -m "1"

  # Add 2 commits to master
  echo "3" >> 1.txt && git commit -am "m3"
  echo "2" >> 1.txt && git commit -am "m2"


  #checkout topic branch
  git checkout -b topic HEAD~2
  echo "1" >> 1.txt && git commit -am "t1"
  echo "2" >> 1.txt && git commit -am "t2"
  echo "1" >> 1.txt && git commit -am "t3"
  echo "2" >> 1.txt && git commit -am "t4"


  #checkout small_topic
  git checkout -b small_topic HEAD~2
  echo "1" >> 1.txt && git commit -am "s1"
  echo "2" >> 1.txt && git commit -am "s2"

  git checkout topic
  git merge small_topic
  echo "1" >> 1.txt && git commit -am "t5"
  echo "2" >> 1.txt && git commit -am "t6"

  #Show graph
  git log --oneline --all --decorate --graph
Run Code Online (Sandbox Code Playgroud)

历史将是这样的——

  * ea3543d (HEAD -> topic) t6
  * b57cbbc t5
  *   2d5e7d3 Merge branch 'small_topic' into topic
  |\  
  | * c94bb3b (small_topic) s2
  | * 7dab544 s1
  * | 37ae0d9 t4
  * | b667871 t3
  |/  
  * 6486a67 t2
  * 490f6d3 t1
  | * 84d8343 (master) m2
  | * f8c8abc m3
  |/  
  * 3018ae2 1
Run Code Online (Sandbox Code Playgroud)

我们主要有 2 个分支机构 - mastertopic. HEAD 指向topic现在。在topic我们创建了一个分支small_topic,其会从主题,并最终创造了被合并到它。

我们想将主题重新设置为 master。我们跑git reabse -i master topic。现在我们将看到这条我认为是错误的消息!

  pick 490f6d3 t1
  pick 6486a67 t2
  pick b667871 t3
  pick 37ae0d9 t4
  pick 7dab544 s1
  pick c94bb3b s2
  pick b57cbbc t5
  pick ea3543d t6

  # Rebase 84d8343..ea3543d onto ea3543d (8 commands)
Run Code Online (Sandbox Code Playgroud)

[请注意,您的 SHA1 将与我的不同]

可能的错误:将 84d8343..ea3543d 重新绑定到 ea3543d

为什么我们看到了onto ea3543d?不应该onto 84d8343吗?但是,84d8343仅在执行此 rebase 时。

另外,有趣的是 - 如果没有small_topic分支,则消息没问题。只有当有一个从分支创建并合并到要重新定位的分支时(如small_topictopic),只有我看到了这一点。

这是一个错误吗?

tor*_*rek 5

是的,如果您要重新定位到 master,它应该说onto masteronto 84d8343

你的命令是git rebase -i master topic.

这意味着首先,git checkout topic,然后开始一个git rebase -i master

This in turn means that your target (--onto argument) is defaulted from your upstream; and your upstream (commit-limiter) argument is master. So the onto should be either master (the name) or its hash ID.

This appears to be a bug in the code that generates the comments. The list of commits, whether expressed as master..topic or 84d8343..ea3543d, is correct, but the onto target is not. The occurrence of the bug is particularly weird, but probably a consequence of rewriting git rebase from shell script (which is much easier to get right) into C.