Search code examples
gitrebasegit-rebase

git: how can I easily tell if I'm in the middle of a rebase


I'm writing a bash one-liner where I want to do something automatically every time I run git rebase --continue inside a while [ true ] loop. When it stops, if I'm still rebasing, I will do X, then I will ask rebase to continue again. Is there an easy easy to tell if I'm still rebasing? I tried with if [ ! -f .git/REBASE_HEAD ]; then break; fi but the file is still present after rebase is finished. I see that .git/rebase-merge is present while rebasing and gone after it's done. Can I use it as my tip?


Solution

  • We can see how git-prompt.sh detects being in a rebase.

        if [ -d "$g/rebase-merge" ]; then
            if [ -f "$g/rebase-merge/interactive" ]; then
                r="|REBASE-i"
            else
                r="|REBASE-m"
            fi
        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
            elif [ -f "$g/MERGE_HEAD" ]; then
                r="|MERGING"
            elif __git_sequencer_status; then
                :
            elif [ -f "$g/BISECT_LOG" ]; then
                r="|BISECTING"
            fi
    

    And there are additional statuses in __git_sequencer_status.

    Because the Git directory is not always .git, the script uses git rev-parse --git-dir to get the git directory ($g above). Then you can see from the above which directories and files inside correspond to what actions.

    Boiling that down, you're looking for one of two directories.

    • $g/rebase-merge
    • $g/rebase-apply