Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git alias that treats `main` and `master` the same

Tags:

git

alias

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.

like image 236
Brondahl Avatar asked Oct 24 '25 09:10

Brondahl


1 Answers

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)

like image 149
Rich Avatar answered Oct 27 '25 02:10

Rich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!