如何知道是否正在进行 git rebase?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3921409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:16:38  来源:igfitidea点击:

How to know if there is a git rebase in progress?

gitgit-rebase

提问by Olivier Verdier

When I start a git rebase -i, I can issue commands like git rebase --continue, or git rebase --abort. Those commands only work if a rebase is in progress.

当我启动 a 时git rebase -i,我可以发出诸如git rebase --continue, 或 之类的命令git rebase --abort。这些命令仅在 rebase 正在进行时才起作用。

How can I know if there is a rebase in progress?

我如何知道是否有正在进行的变基?

(I would greatly appreciate some details on how rebase works internally; what does git do to a repo that gives it the "rebase in progress" status,?)

(我非常感谢有关 rebase 如何在内部工作的一些细节;git 对一个 repo 做了什么,使其处于“rebase in progress”状态,?)

采纳答案by VonC

For one thing, there is a ORIG_HEADin place during a rebase (but that is not limited to the rebase command)

一方面,在 rebase 期间有一个ORIG_HEADin place(但这不仅限于 rebase 命令)

But you can also look at the 2010 Git 1.7.0 git-rebase.shscriptitself (which is as "internal" as you can get ;) ).
Lines like those can give you another clue:

但是您也可以查看 2010 Git 1.7.0git-rebase.sh脚本本身(它是您所能获得的“内部”;))。
像这样的行可以给你另一个线索:

dotest="$GIT_DIR"/rebase-merge
test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || die "No rebase in progress?"

sabgentoncomments:

萨布金顿评论

  • The folder rebase-applyseems to appear with rebase,
  • but a folder rebase-mergeshows up only with with rebase -i.
  • 该文件夹rebase-apply似乎与rebase
  • 但文件夹rebase-merge只显示 with rebase -i

And hippyalso comments, in 2017, that:

嬉皮士意见,在2017年,即:

The coding guidelines discourage the usage of -o(see Documentation/CodingGuidelines), so the correct way now (2017, but also since 2011, Git 1.7.6)is:

编码指南不鼓励使用-o(参见Documentation/CodingGuidelines),因此现在(2017 年,但也是自 2011 年以来,Git 1.7.6)的正确方法是:

(test -d ".git/rebase-merge" || test -d ".git/rebase-apply") || die "No rebase in progress?"

Jelabysuggests in the comments:

Jelaby在评论中建议:

(test -d "$(git rev-parse --git-path rebase-merge)" || \
 test -d "$(git rev-parse --git-path rebase-apply)" )

This correctly handles worktrees and unusual or non-standard layouts that don't have a .gitdirectory, and also allows you to run this test from a subdir of the working directory.

这可以正确处理没有.git目录的工作树和异常或非标准布局,并且还允许您从工作目录的子目录运行此测试。

That is because the git rev-parse --git-path <path>: does resolve "$GIT_DIR/<path>".

那是因为git rev-parse --git-path <path>: 确实解析了 " $GIT_DIR/<path>"。

And Elizandro - SparcBRadds in the comments:

Elizandro - SparcBR增加的评论

Could also redirect the error to null:

(test -d "$(git rev-parse --git-path rebase-merge)" || test -d "$(git rev-parse --git-path rebase-apply) 2>/dev/null"

也可以将错误重定向到 null:

(test -d "$(git rev-parse --git-path rebase-merge)" || test -d "$(git rev-parse --git-path rebase-apply) 2>/dev/null"


Git 2.6+ (Q3 2015) will print more information during a rebase:

Git 2.6+(2015 年第三季度)将在 rebase 期间打印更多信息:

See commit 592e412, commit 84e6fb9(06 Jul 2015), commit 84e6fb9(06 Jul 2015), and commit df25e94, commit 05eb563(30 Jun 2015) by Guillaume Pagès (gitster).
(Merged by Junio C Hamano -- gitster--in commit 178d2c7, 03 Aug 2015)

提交592e412提交84e6fb9(2015年7月6日),提交84e6fb9(2015年7月6日),以及提交df25e94提交05eb563(二○一五年六月三十○日)由纪尧姆页(gitster
(由Junio C gitsterHamano合并-- --commit 178d2c7,2015 年 8 月 3 日)

status: give more information during rebase -i

git statusgives more information during rebase -i, about the list of commands that are done during the rebase.
It displays:

  • the last two commands executed and
  • the next two lines to be executed.

It also gives hints to find the whole files in .gitdirectory.

status: 期间提供更多信息 rebase -i

git status提供了更多信息rebase -i,关于在变基期间完成的命令列表。
它显示:

  • 执行的最后两个命令和
  • 接下来要执行的两行。

它还提供了在.git目录中查找整个文件的提示。



Trying and detect the promptwon't work with Git 2.26+, as shown in commit 6d04ce7

尝试检测提示不适用于 Git 2.26+,如提交 6d04ce7所示

"git rebase" has learned to use the merge backend (i.e. the machinery that drives "rebase -i") by default, while allowing "--apply" option to use the "apply" backend (e.g. the moral equivalent of "format-patch piped to am").
(The rebase.backendconfiguration variable can be set to customize.)

git rebase”已经学会rebase -i默认使用合并后端(即驱动“ ”的机器),同时允许“ --apply”选项使用“ apply”后端(例如“ ”的道德等价物format-patch piped to am)。
rebase.backend配置变量可以设置为自定义。)

See commit 10cdb9f, commit 2ac0d62, commit 8295ed6, commit 76340c8, commit 980b482, commit c2417d3, commit 6d04ce7, commit 52eb738, commit 8af14f0, commit be50c93, commit befb89c, commit 9a70f3d, commit 93122c9, commit 55d2b6d, commit 8a997ed, commit 7db00f0, commit e98c426, commit d48e5e2(15 Feb 2020), and commit a9ae8fd, commit 22a69fd(16 Jan 2020) by Elijah Newren (newren).
(Merged by Junio C Hamano -- gitster--in commit 8c22bd9, 02 Mar 2020)

提交10cdb9f提交2ac0d62提交8295ed6提交76340c8提交980b482提交c2417d3提交6d04ce7提交52eb738提交8af14f0提交be50c93提交befb89c提交9a70f3d提交93122c9提交55d2b6d提交8a997ed提交7db00f0提交e98c426提交 d48e5e2(2020 年 2 月 15 日),并提交 a9ae8fd提交 22a69fd(2020 年 1 月 16 日)作者:Elijah Newren ( newren)
(由Junio C gitsterHamano合并-- --8c22bd9 提交中,2020 年 3 月 2 日)

git-prompt: change the prompt for interactive-based rebases

In the past, we had different prompts for different types of rebases:

REBASE: for am-based rebases
REBASE-m: for merge-based rebases
REBASE-i: for interactive-based rebases

It's not clear why this distinction was necessary or helpful; when the prompt was added in commit e752019("Improve bash prompt to detect various states like an unfinished merge", 2007-09-30, Git v1.5.5-rc0), it simply added these three different types.
Perhaps there was a useful purpose back then, but there have been some changes:

  • The merge backend was deleted after being implemented on top of the interactive backend, causing the prompt for merge-based rebases to change from REBASE-mto REBASE-i.
  • The interactive backend is used for multiple different types of non-interactive rebases, so the "-i" part of the prompt doesn't really mean what it used to.
  • Rebase backends have gained more abilities and have a great deal of overlap, sometimes making it hard to distinguish them.
  • Behavioral differences between the backends have also been ironed out.
  • We want to change the default backend from am to interactive, which means people would get "REBASE-i" by default if we didn't change the prompt, and only if they specified --amor --whitespaceor -Cwould they get the "REBASE" prompt.
  • In the future, we plan to have "--whitespace", "-C", and even "--am" run the interactive backend once it can handle everything the am-backendcan.

For all these reasons, make the prompt for any type of rebase just be "REBASE".

git-prompt: 更改基于交互的 rebase 的提示

过去,我们对不同类型的 rebase 有不同的提示:

REBASE: for am-based rebases
REBASE-m: for merge-based rebases
REBASE-i: for interactive-based rebases

不清楚为什么这种区分是必要的或有帮助的。当在 commit e752019(“改进 bash 提示以检测各种状态,如未完成的合并”,2007-09-30,Git v1.5.5-rc0)中添加提示时,它只是添加了这三种不同的类型。
也许当时有一个有用的目的,但发生了一些变化:

  • 合并后端在交互式后端之上实现后被删除,导致基于合并的 rebases 的提示从 更改REBASE-mREBASE-i.
  • 交互式后端用于多种不同类型的非交互式变基,因此-i提示的“ ”部分并不真正意味着它过去的含义。
  • Rebase 后端获得了更多的能力并且有很多重叠,有时很难区分它们。
  • 后端之间的行为差​​异也已消除。
  • 我们希望从上午默认后端更改为交互式,这意味着人们将获得“ REBASE-i”在默认情况下,如果我们不改变提示,只有当他们指定--am--whitespace-C他们将获得“ REBASE”的提示。
  • 将来,我们计划让“ --whitespace”、“ -C”甚至“ --am”运行交互式后端,一旦它可以处理所有事情am-backend

出于所有这些原因,将任何类型的 rebase 的提示设置为“ REBASE”。



Since Git 2.17 (March 2018), you also have:

自 Git 2.17 (March 2018) 起,您还拥有:

git rebase --show-current-patch

It shows the content of .git/REBASE_HEADduring an interactive rebase which can be paused during conflict.

它显示.git/REBASE_HEAD可在冲突期间暂停的交互式变基期间的内容。

回答by Jakub Nar?bski

You can also check how such detection is done in __git_ps1function in contrib/completion/git-prompt.sh, which can be used for git-aware bash prompt:

您还可以在__git_ps1函数中contrib/completion/git-prompt.sh检查此类检测是如何完成的,该函数可用于 git-aware bash 提示:

                if [ -f "$g/rebase-merge/interactive" ]; then
                        r="|REBASE-i"
                        b="$(cat "$g/rebase-merge/head-name")"
                elif [ -d "$g/rebase-merge" ]; then
                        r="|REBASE-m"
                        b="$(cat "$g/rebase-merge/head-name")"
                else
                        if [ -d "$g/rebase-apply" ]; then
                                if [ -f "$g/rebase-apply/rebasing" ]; then
                                        r="|REBASE"
                                elif [ -f "$g/rebase-apply/applying" ]; then
                                        r="|AM"
                                else
                                        r="|AM/REBASE"
                                fi

回答by Ed Cashin

If there's an interactive rebase in progress, this will tell you where you are in the process:

如果正在进行交互式 rebase,这将告诉您在此过程中的位置:

$ cat .git/rebase-merge/done 
pick 786139e lrg
edit 668b8a6 ktio
$ 

Right now I'm editing the “ktio” patch in an interactive rebase.

现在我正在一个交互式 rebase 中编辑“ktio”补丁。

If there's no rebase going on, it will look like this:

如果没有进行变基,它将如下所示:

$ cat .git/rebase-merge/done 
cat: .git/rebase-merge/done: No such file or directory
$ 

回答by Alexander Bird

From a bash command line:

从 bash 命令行:

ls `git rev-parse --git-dir` | grep rebase

That will return exit code 0 (success) if there is a rebase folder, and it will output the rebase folder to STDOUT. If you are notin the middle of a rebase, then it will output nothing and return non-0 exit code. So you could even do something like this:

如果有 rebase 文件夹,这将返回退出代码 0(成功),并将 rebase 文件夹输出到 STDOUT。如果您不在rebase 中间,那么它将不输出任何内容并返回非 0 退出代码。所以你甚至可以做这样的事情:

ls `git rev-parse --git-dir` | grep rebase || echo no rebase

回答by Rory O'Kane

If you have EasyGit, eg statuswill tell you:

如果你有EasyGiteg status会告诉你:

$ eg status
(Not currently on any branch.)
(YOU ARE IN THE MIDDLE OF A INTERACTIVE REBASE; RUN 'eg help topic middle-of-rebase' FOR MORE INFO.)
Changes ready to be committed ("staged"):
    modified:   .gitmodules
    renamed:    config_loader.rb -> code/config_loader.rb
Newly created unknown files:
    vendor/
(YOU ARE IN THE MIDDLE OF A INTERACTIVE REBASE; RUN 'eg help topic middle-of-rebase' FOR MORE INFO.)

In a colored terminal, the notification is very prominent:

在彩色终端中,通知非常突出:

<code>eg status</code>middle-of-rebase demonstration screenshot

<code>eg status</code>middle-of-rebase 演示截图

(eg help topic middle-of-rebasedisplays the documentation “How to resolve or abort an incomplete rebase”.)

eg help topic middle-of-rebase显示文档“如何解决或中止不完整的变基”。)

回答by BILL

I am using this command is_rebase=$(git status | grep "rebasing" | wc -l)

我正在使用这个命令 is_rebase=$(git status | grep "rebasing" | wc -l)