Elie Steinbock
Elie Steinbock

Reputation: 5088

git config settings to automatically create remote branch with same name

I used to have my git set up very nicely where it would automatically push to the remote branch with the same name when I ran git push, and automatically pull when running git pull.

I tried to show a friend how to do this and in the process managed to break my own config. How can I get back to the functionality I'm looking for? I've tried a bunch of configs, but each has its problems.

I currently have this in ~/.gitconfig:

[push]
    default = current
[branch]
    autoSetupMerge = always

But when I create a new branch locally, then commit and push, it says "Everything up-to-date". If I then run git push origin <branch_name> it does push the changes.

These settings don't seem to do the trick either:

[push]
    default = matching
[branch]
    autoSetupMerge = always

Upvotes: 21

Views: 13989

Answers (4)

Arthur de Deus
Arthur de Deus

Reputation: 23

If you want to edit your .gitconfig by hand, something like

[push]
    autoSetupRemote = true

will do the trick.

Upvotes: 2

Alex Vergara
Alex Vergara

Reputation: 2209

You can configure it with git config --global push.default current to make it push the current branch to update a branch with the same name.

But, in 2022, and with a git version git >= 2.37.0, you can specify this in your git config git config --global --add --bool push.autoSetupRemote true and this achieves the same while also setting up the upstream tracking.

Upvotes: 24

user
user

Reputation: 977

You can use following command to instruct git to by default create remote branch with same name (I have used global level, you can decide level based on your requirement:

git config --global push.default current

source

you can use -u option for first time push for new branch, it will help in tracking of new branch and will help while pulling changes. If you did not use -u for first time push, you can use it for later push also. Basically, tracking will start after a push with -u option.

git push -u

without specifying origin and branch name for new branch and push changes.

Upvotes: 17

torek
torek

Reputation: 487755

The main control knob here is indeed push.default and you probably want either current or matching, which have this very-major difference:

  • current means that if you omit the refspec argument(s), your Git should use the branch you are on right now—the one git status would say "on branch ...", or that git branch would print with an asterisk—and try to push that to its upstream counterpart. That is, if the current branch is xyz, the current setting will ask origin to set its xyz branch (your upstream) to the same hash ID as your xyz. This is true even if the upstream for your xyz is their zyx—see upstream below.

  • matching means that if you omit the refspec argument(s), your Git should get a list of branch names from their Git. Then, for each of their branch names—whatever they are; master, develop, zyx, and so on—your Git should see if you also have a branch with the same name, regardless of what your branch's hash ID is. For each matching name, your Git should ask their Git to set their branch to the same hash ID that your Git shows for that name.

The matching setting means that, for instance, if you run:

git push ssh://host.example.com/path/to/repo.git

and they have branches master, develop, and zyx, and you have a master and develop but no zyx, your Git will ask them to set their master and develop to match your master and develop, even if their Git is completely unrelated to your Git.

(If their Git is unrelated to yours, this polite request will certainly fail, so that's not that big a deal.)

You can explicitly ask for matching behavior on any given push, without setting the push.default setting, using:

git push <remote> :

That is, a bare colon : as a refspec means "matching".

The remaining push.default settings, which are nothing, upstream, tracking, and simple, are mostly variants on current:

  • The nothing setting just errors out entirely if you omit a refspec. I used this for a while but found it to be too annoying.
  • The upstream and tracking settings are synonyms—tracking is a deprecated synonym for upstream—and mean use the upstream setting of the current branch to determine the name to send to the other Git. Hence if your forward has origin/reverse as its upstream, you'll ask them to update their reverse.
  • The simple setting mean the same thing as both upstream and current, picking which one to emulate based on where you're git push-ing to: it checks whether the remote you're using is the one for which the remote is set, and if so, behaves like upstream except that it requires that the branch names match. Otherwise, it behaves just like current.

I leave mine set to simple these days, and just run git push -u origin HEAD when pushing a new branch on purpose, and git push with no parameters when pushing to a same-named branch.

Upvotes: 4

Related Questions