Short answer
If you actually like to be explicit and use the -u
option when necessary,
but just don't want to type the whole:
git push -u origin foo
Then you can use the following alias:
[alias]
push-u = !git push -u origin $(git symbolic-ref --short HEAD)
And simply type:
git push-u
Long answer
Typically, the need for -u
(shorthand for --set-upstream
) is when we have just created a new local branch and commit, and we want to push it upstream. The remote repository doesn't yet have the new branch, so we need to tell git to create and track the remote branch before pushing the commit. This is only necessary for the first push on the branch. Here is a typical scenario:
git checkout -b foo # Create local branch
git commit -m "Foo" # Create local commit
git push -u origin foo # Create and track remote branch, and push commit
git commit -m "Bar" # Create local commit
git push # Push commit
Personally, I do like the need to be explicit with git push -u
when creating the remote branch: it's a pretty significant operation, sharing a whole new branch to the world.
However, I hate that we have to explicitly write git push -u origin foo
. Not only it is a pain to type, but more importantly, it's quite error-prone! It's easy to make a mistake when typing the branch name, and the new remote branch won't have the same name as your local branch! In most cases, really, you want the upstream repository to be origin
, and the upstream branch to have the same name as your local branch.
Therefore, I'm using the following alias in my .gitconfig
, which is a subset of the excellent answer provided by Mark:
[alias]
push-u = !git push -u origin $(git symbolic-ref --short HEAD)
Now, we can do the following, which is still explicit, but less error-prone:
git checkout -b foo # Create local branch
git commit -m "Foo" # Create local commit
git push-u # Create and track remote branch, and push commit
git commit -m "Bar" # Create local commit
git push # Push commit