The easiest way to set the upstream branch is to use the “git push” command with the “-u” option for upstream branch. Alternatively, you can use the “–set-upstream” option that is equivalent to the “-u” option. As an example, let's say that you created a branch named “branch” using the checkout command.
Set Upstream If you don't want to push anything, you can also do it using git-branch command. A local branch can track a remote branch using git-branch with long option --set-upstream-to=<upstream> or short option -u <upstream> . The command sets up branchname 's tracking information.
Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.
No, you must make a commit before you can push.
You can configure it with git config
using git config --global push.default current
.
Docs: https://git-scm.com/docs/git-config/#Documentation/git-config.txt-pushdefault
Since I don't think this is possible using git config, here is what you can do in bash:
[[ $(git config "branch.$(git rev-parse --abbrev-ref HEAD).merge") = '' ]] && git push -u || git push
If the current branch has a remote tracking branch, it calls git push
otherwise it calls git push -u
Note: the fact that the new default push policy "simple
" relies on a branch having an upstream one means that:
setting an upstream branch is viewed as a voluntary step, not an hidden automated one
When "
git push [$there]
" does not say what to push, we have used the traditional "matching" semantics so far (all your branches were sent to the remote as long as there already are branches of the same name over there).We will use the "
simple
" semantics that pushes the current branch to the branch with the same name, only when the current branch is set to integrate with that remote branch.
There is a user preference configuration variable "push.default
" to change this.
So building up from mechanicalfish's answer, you can define an alias, with the right double quotes ("
) escaped (\"
):
git config alias.pu "![[ $(git config \"branch.$(git rev-parse --abbrev-ref HEAD).merge\") = '' ]] && git push -u || git push"
git pu origin
Sc0ttyD proposes in the comments the following alias:
alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'
In multiple lines:
alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] &&
git push -u origin $(git symbolic-ref --short HEAD) ||
git push'
I've had the same problem. I've found this alias (.gitconfig)
[alias]
track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"
Usage: git track
once per new branch (currently checked out). Then just push as normal :)
The answers by @VonC and @Frexuz are helpful, but both of their solutions produce an error for me. Using both of their answers, I cobbled together something that works for me:
[alias]
pu = ![[ $(git config "branch.$(git symbolic-ref --short HEAD).merge") = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push
This results in executing either git push -u origin $BRANCHNAME
or git push
, depending on whether its upstream (property branch.$BRANCHNAME.merge
) is defined.
Entering this alias on the command line will require escape codes, so it's probably easiest to use an editor to insert into the correct file ($HOME/.gitconfig
(global), .git/config
(local), or /etc/gitconfig
(system) )
I solved this issue by using this simple Bash script. It won't work on existing branches, but if you create all of your branches with this function, you'll always have your upstream branch set automatically.
function con { git checkout -b $1 && git push --set-upstream origin $1; }
The $1 represents the first argument you pass after con
so it's just like doing:
git checkout -b my-new-branch && git push -u my-new-branch
...by just doing this:
con my-new-branch
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With