3

When I want to push changes on a branch that already has upstream branch, git push is all I need. But when there's no current upstream branch, I have to type out git push -u $BRANCH_NAME.

I create new branches all the time (for every feature that I work on, so it's about one every two hours or so). I also make typos sometimes. It gets irritating. How can I create an alias that would automatically resolve to git push or git push -u $BRANCH_NAME and wouldn't require me to type out branch name?

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • Besides [chelmertz's answer](https://stackoverflow.com/a/60877164/1256452), you can make an alias (pick some name for it) that runs `git push origin -u HEAD`. Your Git resolves the symbolic name `HEAD` to the current branch, so this works for any branch, after which the upstream is now set. – torek Mar 27 '20 at 00:13

1 Answers1

6

To get your wanted behavior, put this into your ~/.gitconfig:

[push]
    default = current

(as usual, $ git config --global push.default current does this for you.)

There's an explanation of the options of push.default here:

current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

-- https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • This is great, seriously, and I will use it but I'm still looking for a way to set an alias for a command that will set the upstream branch. Any ideas? – Laurence Lord Mar 02 '23 at 10:18
  • @LaurenceLord here's a guide on creating git aliases: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases If you want an alias in e.g. bash, you could add @torek's suggestion like this `alias gp="git push origin -u HEAD"` to ~/.bashrc – chelmertz Jul 27 '23 at 21:14