0

Using git, I often create local branches and then want to push them to the remote (e.g. Github). This requires the -u or --set-upstream flag.

Here is what git outputs without this flag:

$ git checkout -b newbranch
$ git push
fatal: The current branch cross_val has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin newbranch

Is there a way to have this suggestion copied to my prompt? So that I don't have to type it. Something like:

$ git checkout -b newbranch
$ git push
fatal: The current branch cross_val has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin newbranch

$ <tab>
$ git push --set-upstream origin newbranch
1

You could set up an alias that pushes the current branch to the remote.

Configure the alias with the following command:

git config --global alias.rpush '!git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)'

The git rev-parse --abbrev-ref HEAD command returns the name of your current branch. Then run it with:

git rpush

You can choose to give the alias any other name according to your own preference.

0

This won't do exactly what you want, but it will cut down the amount you need to type if you're using bash.

https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

  • I don't think this helps as the git completion scripts are already installed with git. – Julien__ Dec 10 '18 at 17:10
  • Guess it depends on the system. On my linux system it is a separate install and bashrc or profile needs to be updated to take advantage of it. – user103944 Dec 11 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.