DEV Community

Michal Bryxí
Michal Bryxí

Posted on

Simplify pushing to git

I think everyone using git from command line knows this scenario:

1) Code, code, code, ...
2) Hmm, better save my progress.

$ git checkout -b myawesomefeature
$ git add .
$ git commit -m 'It works!'
$ git push
fatal: The current branch myawesomefeature has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin myawesomefeature

3) !@#$%^&

And then you just have to manually type the lengthy command above to be able to push.

After doing this quite a few times I found out, that there is a better way. You can tell git to automatically set the upstream for you by adjusting following config:

git config --global push.default current

After this, simple git push call will push current branch to a remote branch of the same name.

But before setting this, it's a good idea to read the documentation and understand what the different options do:

  • 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.
  • upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).
  • simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

Discussion (10)

Collapse
koffeinfrei profile image
Alexis Reigel

And then you just have to manually type the lengthy command above to be able to push.

Instead of having to type git push --set-upstream origin myawesomefeature the following shorter version works as well (i.e. the branch name can be omitted, it defaults to the current one, and the flag --set-upstream has a short version -u):

git push origin -u
Collapse
jpgrefaldo profile image
John Paul Grefaldo

I'm afraid that didn't work for me.

Collapse
koffeinfrei profile image
Alexis Reigel

Can you elaborate a bit? What's the error message?

Thread Thread
jpgrefaldo profile image
John Paul Grefaldo

same as above post,

$ git push
fatal: The current branch myawesomefeature has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin myawesomefeature
Thread Thread
koffeinfrei profile image
Alexis Reigel

I'm confused. That error is expected if you just issue git push. My suggestion was to use git push origin -u (instead of the longer version git push --set-upstream origin myawesomefeature).

Thread Thread
jpgrefaldo profile image
John Paul Grefaldo • Edited

Sorry I've no idea, I've tried.
Alt text of image

when

git config --global push.default upstream

what's your config?

Thread Thread
jpgrefaldo profile image
John Paul Grefaldo

From what I understand, I think it will only work if it was push successfully before or branch exist in remote?
https://git-scm.com/docs/git-push#Documentation/git-push.txt--u
ref: git-scm.com/docs/git-push#Document...

Thread Thread
koffeinfrei profile image
Alexis Reigel

what's your config?

I haven't set that config, I use the default (i.e. push.default=simple). For me git push origin -u works.

Setting git config --global push.default upstream I get the same error as you.

The OP suggested to use git config --global push.default current though, which has the desired effect (i.e. git push sets the remote branch as the tracking branch).

Collapse
jessekphillips profile image
Jesse Phillips

I'm less concerned that there is a long command to type and more concerned with:

$ git add .

That is too SVN like and partial commits are usually more appropriate.

Collapse
michalbryxi profile image
Michal Bryxí Author

Don't disagree, but that's just not point of this post.