3

I have (amongst others) git aliases:

  • grm: git rebase master
  • grim: git rebase -i master

The development community as a whole is moving away from the use for master for the default branch, in favour of main. (For reasons relating to historical associations)

Unfortunately the community isn't doing that uniformly, and I frequently switch between projects, so I'd have to keep editing my aliases.

Is there any way to write the alias so that it interacts with "whichever of master or main exists in this repo"? (Happy to have it assume that only one of the 2 exists, locally)


Re: Suggested answer. The problem is not "I as a human don't know which is in use in this repo"; That's trivial to identify by glancing at the history. It's that my *alias* doesn't know.

I'd assumed that the solution would be something that "tries both options" in some way, but a solution that uses the linked question to determine the correct answer directly and then uses that answer would also work.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • Does this answer your question? [git - how to get default branch?](https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch) – jonrsharpe Nov 30 '21 at 10:34
  • @jonrsharpe, uhhh ... I suppose that could be the first step, yes ... I don't know enough bash to know how/whether I can use that to then run `git rebase `, though? – Brondahl Nov 30 '21 at 10:41
  • offtopic: I like `grim` acronym it is so gloomy :). – Marek R Nov 30 '21 at 11:11

2 Answers2

2

Use one of the answers from git - how to get default branch? to write an alias which looks up the correct branch name, then use that inside other aliases:

# ~/.gitconfig
[alias]
    main-branch-name = "!f() { git symbolic-ref refs/remotes/origin/HEAD | sed \"s@^refs/remotes/origin/@@\" ; }; f"
    rm = ! git rebase $(git main-branch-name)

Rich
  • 15,048
  • 2
  • 66
  • 119
0

I would approach this issue this way:

git rev-parse main >/dev/null 2>/dev/null && git rebase main || git rebase master

If there is main branch use it otherwise try with master.

It should be less lagging then use of git remote show origin.

Marek R
  • 32,568
  • 6
  • 55
  • 140