reducing activity
reducing activity

Reputation: 2266

How can I specify default branch for pulling from remote - while I have one remote configured already as a default?

I have setup of my personal fork with:

master branch mirroring master branch in upstream repo and fork that is my main branch including my changes - and other feature/WIP/testing/testcase branches as usual.

master branch has a default configured remote origin/master where my repo lives and where I push update master.

But I am pulling from upstream remote.

How can I configure git so I can:

Now git pull upstream is resulting in

Because this is not the default configured remote for your current branch

specify default branch for a non-default remote for pull may seem to be a duplicate but is not - "This does not answer the question, since this only works for the default remote (origin)!"

The same Specify default pull branch for a given remote - it allows setting only one

Upvotes: 0

Views: 628

Answers (2)

torek
torek

Reputation: 489818

If you're willing to ditch git pull entirely, here's how to automate the job.

Step 1: write yourself a new Git command. Let's call it up for now. This command is a simple script, which we'll see at the bottom of this posting. To make it an executable Git command, make it an executable script named git-up and put this somewhere such that your $PATH will find it. Running git up will now run your git-up script.

Step 2: in each repository in which you want this to happen, run:

git config branch.<name>.up origin/<other-name>

where <name> is the name of the branch you want your script to update, and origin/<other-name> is the remote-tracking name in your Git repository that will be updated by git fetch.

Step 3: replace git pull with git up.

Now for the script. We'll need it to be executable (remember to chmod +x!), and run by a shell such as /bin/sh or /bin/bash, so:

#! /bin/sh

The script should:

  • figure out which branch we are on right now
  • locate the branch.name.up setting: error if not set
  • run git fetch
  • run git merge $setting

These steps are accomplished with:

branch=$(git symbolic-ref --short HEAD) || exit
up=$(git config --get branch.$branch.up) || {
    echo "no name configured for git-up for $branch" 1>&2
    exit 1
}
git fetch && git merge $up

This is all untested, so test each part and make any necessary fixes.

Upvotes: 0

LeGEC
LeGEC

Reputation: 52176

You can setup a remote so that it has different push and pull urls, e.g : git fetch origin would fetch from upstream, git push origin would push to fork,
however I don't know of a stock configuration option to specify two distinct branch names depending on the remote.

IMHO the easiest way would be to have the branches in your fork repo follow the same names as the upstream repo, so that you can push/pull to branch main in both cases.

One other generic option is : you can define aliases to run the appropriate commands.


Here is how to set up origin with two distinct urls :

# you can have distinct protocols for both urls :
git remote set-url origin https://upstream.repo
git remote set-url --push origin ssh://fork.repo

With your origin remote set up like this, if you set the upstream branch for your local master branch to origin/main :

git branch -u origin/main master

the default action for :

  • git push will be to push your local branch to main on fork
  • git pull will be to fetch anbd merge branch main from upstream

Upvotes: 1

Related Questions