1

I'd like to use just:

git push

or at most:

git push origin

rather than having to first check which is the active branch with git branch --list, then type:

git push origin <branch>

Here and here I've seen that if I do this once:

git push -u origin <branch>

then from then on I can use just git push for that branch. However, I don't want to do this "setup" step for each branch, because it's extra work that I also may forget to do.

Why is this "setup" step necessary and can I avoid it?

Also, if I do the "setup" step (push -u) for a branch that doesn't exist yet (neither on my PC nor on the server), does that remove the need to do git checkout -b <branch> first?

Related post: Almost the same question has been posted here but it doesn't ask for a way to do away with the requirement, it only asks why the requirement is there.

Community
  • 1
  • 1
Stefan Monov
  • 11,332
  • 10
  • 63
  • 120

3 Answers3

2

Take a look at https://git-scm.com/docs/git-config#git-config-pushdefault

Setting push.default=current will allow you to create a new branch with git checkout -b ... and then just git push to create a matching branch on your remote repo. If you have multiple remotes then I think you'll need to specify one e.g. git push origin.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • Thanks, looks good. It seems like `simple` mode (which I've used till now since it's the default) does something similar to `current` mode. I find it difficult to understand the docs on it, since it refers to `upstream` mode which I also don't understand well (e.g. I'm confused by the phrase "**usually** integrated there). Could you explain why I can't just use `simple` mode for what I want? – Stefan Monov Dec 23 '16 at 20:18
  • I think `simple` won't create branches for you (part of the `refuse to push if the upstream branch’s name is different from the local one` check). That forces you to be more aware of when you're using a remote repo and which one you use but also requires that you be explicit about when you want to create a remote branch. It looks like the difference between these modes comes up a lot here, see http://stackoverflow.com/questions/13751319/git-push-current-vs-push-upstream-tracking and many similar questions. – Jonah Dec 23 '16 at 21:56
1

You probably don't use multiple remotes, therefore it seems unnecessary to you to specify which remote to use.

But, when using the forking scenario (common on e.g. GitHub or BitBucket), you usually use two remotes, origin and upstream (names may differ), and you need to specify for each branch to which remote it belongs (even if it's usually the origin).

It may be also possible to have a local branch named differently to a remote one, but I'm yet waiting to meet such a need.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

You can continue the way you push.

  1. Push the active branch: git push or git push origin
  2. Push all the branches: git push --all

You can also not to set the step push -u. This step only creates a relationship between local branch the remote branch (or we call remote tracking branch). After set upstream, you can find the relation by git branch -vv. But it not effect the way you push.

When you want to push a new create branch, it’s not necessary to switch to the branch. You can directly use git push origin branch or git push -u origin branch.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74