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

Viewed 34575

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.

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,?)

8 Answers

There are some bad answers here. git doesn't really have a specification for how it should work so the only answer is "how does git do it?". The code is here:

int wt_status_check_rebase(const struct worktree *wt,
               struct wt_status_state *state)
{
    struct stat st;

    if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
        if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
            state->am_in_progress = 1;
            if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
                state->am_empty_patch = 1;
        } else {
            state->rebase_in_progress = 1;
            state->branch = get_branch(wt, "rebase-apply/head-name");
            state->onto = get_branch(wt, "rebase-apply/onto");
        }
    } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
        if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
            state->rebase_interactive_in_progress = 1;
        else
            state->rebase_in_progress = 1;
        state->branch = get_branch(wt, "rebase-merge/head-name");
        state->onto = get_branch(wt, "rebase-merge/onto");
    } else
        return 0;
    return 1;
}

It basically checks if these few files/directories exist (note !stat() means "does the file exist"). am is git am which is for applying patches from a mailbox which I doubt anyone except the Linux developers use.

  • rebase_in_progress: .git/rebase-apply && !.git/rebase-apply/applying || .git/rebase-merge && !.git/rebase-merge/interactive
  • interactive_rebase_in_progress: .git/rebase-merge && .git/rebase-merge/interactive
  • am_in_progress: .git/rebase-apply && .git/rebase-apply/applying

I guess if you want to know if any kind of rebase/am is happening just check if .git/rebase-apply or .git/rebase-merge exist.

I have not seen it stated clearly, so here it is:

during rebasing process, if there is one under way, git status is now sufficient, as it gives information (for reference, I head smaple branches named master and rbBr):

interactive rebase in progress; onto 5f8e534
Last command done (1 command done):
   pick 1b7a450 BRANCH: another comment
No commands remaining.
You are currently rebasing branch 'rbBr' on '5f8e534'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   User.java

no changes added to commit (use "git add" and/or "git commit -a")

This is shown before resolving conflicts, after resolving conflicts it shows:

interactive rebase in progress; onto 5f8e534
Last command done (1 command done):
   pick 1b7a450 BRANCH: another comment
No commands remaining.
You are currently rebasing branch 'rbBr' on '5f8e534'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   User.java

PS C:\my_git_repos\learning_git> git rebase --continue                                                                                                                                                                                       [detached HEAD 9645135] BRANCH: another comment
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/rbBr.

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

Related