Home > OS >  git alias that treats `main` and `master` the same
git alias that treats `main` and `master` the same

Time:11-30

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.

CodePudding user response:

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)

CodePudding user response:

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.

  • Related