0

I have a set of git aliases that I use both in my personal life and also at my professional positions. Because of long-standing convention in git usage, I've hardcoded master as a default "main" branch in many of these aliases.

However, I happen to be working at an institution where the convention is to use main as the main branch. This means a lot of my aliases break when I try to use them, because I've hard-coded an assumption of a branch called master, and and ensuant usage thereof.

Instead of changing the instances of master in my aliases file, and maintaining separate versions of that alias file for different situations, I wonder if I can instead use a variable in my alias definitions, one that I could set in my .gitconfig. Something like:

$ cat .gitconfig

...
[variables]
  DEFAULT_BRANCH = main

[include]
  path = myaliases.git

$ cat myaliases.git

bclean = "!f() { git branch --merged ${1-DEFAULT_BRANCH} | grep -v " ${1-DEFAULT_BRANCH}$" | xargs -r git branch -d; }; f"

Instead of

bclean = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -r git branch -d; }; f"
user151841
  • 17,377
  • 29
  • 109
  • 171

1 Answers1

0

You cannot do this directly with a Git alias (as in the parts that Git expands). You can do it with a shell alias (as in parts that the shell expands, either through a direct shell alias, or through a Git alias that invokes the shell with !).

The comment from Kraigolas that suggests using a symbolic name is also a good proposition for some cases: just make master a symbolic name for main in these repositories. The only normal symbolic name is HEAD, though, and symbolic names have certain weirdnesses about them: for instance, it used to be the case that an attempt to delete a symbolic name, deleted instead its target.

Much more directly, you can use [include] directives. For any repository where you want your aliases to use main, simply include a (sub-)version of your alias file that replaces master with main. Of course that's what you said you don't want to do—but how often do you edit these aliases? You can write a little script that generates the alternate file from your "main" config file (or should I call it your "master" file?), and run that whenever you make an edit. Note that you need only override the aliases with later aliases via inclusion:

[include]
    path = ~/.gitconfig.main

As for a script that massages the aliases in ~/.gitconfig, there would be several ways to tackle this. My preference would be to move the master-related aliases and have ~/.gitconfig include ~/.gitconfig.master itself so that the script is just:

sed -e s/master/main/ < ~/.gitconfig.master > ~/.gitconfig.main

However, you could get fancy and write something that runs git config --global --list, extracts the alias.* entries, and so on. This gets difficult because you then have to deal with quoting.

Here's a silly example:

$ grep silly ~/.gitconfig
        silly = rev-parse master
$ cat ~/.gitconfig.main
[alias]
        silly = rev-parse main

Running git silly in a repository without the include section runs git rev-parse master. Running git silly in a repository with the include section runs git rev-parse main.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Could you add some fleshed-out example code or commands to your answer? – user151841 Jun 28 '21 at 01:44
  • "how often do you edit these aliases? " Not often, but really the issue is I don't want to juggle alias files between work, which uses `main`, and elsewhere, where `master` is common – user151841 Jun 28 '21 at 01:45
  • 1
    There's no need to *juggle files*. You just add `[include] path = ~/.gitconfig.main` at the end of your `.git/config` in a repository where you want the `main`-ized aliases. – torek Jun 28 '21 at 01:50