1

I use aliases to save time on git commands, one of which is "git push origin master". Problem is, some of my repos use "main" now.

What's the right way to create a zsh alias that means "git push origin main" in some directories and "git push origin master" in others?

cfn
  • 57
  • 5
  • 2
    Does this answer your question? [Git - push current branch shortcut](https://stackoverflow.com/questions/14031970/git-push-current-branch-shortcut) – Kraigolas Mar 10 '21 at 00:51

1 Answers1

3

There are a couple ways to handle this. If your goal is to push the current branch, you can just write HEAD: git push origin HEAD.

If you want to push the default branch, that's a little trickier, since Git doesn't intrinsically have the idea of a default branch. If you want to use the remote's current branch, which on GitHub is the default branch, you can write this:

$ git push origin $(git rev-parse --abbrev-ref origin/HEAD | cut -d/ -f2)

Note that you may need to run git remote set-head -a origin once to set origin/HEAD, and you'll need to run it if the remote branch name changes, but this is the easiest way to automatically pick the right branch (which, admittedly, isn't that easy).

bk2204
  • 31,903
  • 3
  • 22
  • 39