3

In git I'm using the pull command to merge from a different repository, for example:

git pull ../mortoray-main/ mortoray-main

This will fetch and merge the mortoray-main branch from the other repository into the current branch in the current repository.

What I'm looking for is how to setup the equivalent shortcut that I can just do this:

git pull mortoray

In my gitconfig I added this:

[remote "mortoray"]
    url = ../mortoray-main/
    fetch = mortoray-main

The problem is now that when I run git pull mortoray I get the error: "You asked to pull from the remote 'mortoray', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line."

How does one configure the remote to get the shortcut to work as the original pull command?


Note that instead of 'git pull mortoray', the following works, but is of course not a shortcut:

git fetch mortoray
git merge FETCH_HEAD

This is exactly what the following does without using a "remote":

git pull ../mortoray-main/ mortoray-main

So the question is about the shortcut, I want a pull command which does the fetch and merge for me as the original pull command does:

git pull mortoray

Is there any configuration that would make this shortcut work as intended?

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

3 Answers3

3

You should make sure your branch is tracking your mortoray-main branch of the mortoray repo:

git branch --set-upstream <your_current_branch> mortoray-main/mortoray

(as detailed in "How do you make an existing git branch track a remote branch?")
Then the git pull mortoray should now the branch to rebase when fetching mortoray-main/mortoray branch.


The OP eda-qa-mort-ora-y adds:

This isn't a shortcut then, since I'll need to continually switch the tracking branch.
That is:

  • this branch doesn't permanently track that remote branch, I'll be merging from different branches at different times.
  • Also, this branch is actually already tracking a remote branch, which I'd like to keep as such

Regarding a shortcut, in order to reference the current branch, you can take inspiration from "git push current branch", which has idea also valid for a git pull, declaring in the .bashrc:

get_git_branch() {
  echo `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
}
alias gpull='git pull mortoray-main `get_git_branch`'

If you want to add parameters for the upstream repo (ie not pulling always from mortoray-main, and for a specific local branch whose name differs from the upstream branch)

alias gpull='git pull ${1:-origin} $2:`get_git_branch`'

then gpull mortoray-main mortoray should fetch the right branch and rebase your current branch.

The OP edA-qa mort-ora-y mentions:

I don't want to specify the local or remote branch name -- I'll always be pulling from the branch called mortoray-main into whatever happens to be my local branch.

So this alias should work then:

gpull='git pull mortoray mortoray:`get_git_branch\`'
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This isn't a shortcut then, since I'll need to continually switch the tracking branch. That is, this branch doesn't permanently track that remote branch, I'll be merging from different branches at different times. Also, this branch is actually already tracking a remote branch, which I'd like to keep as such. – edA-qa mort-ora-y Dec 01 '11 at 09:14
  • @edA-qamort-ora-y then you need to specify your local branch in the `git pull`: `git pull mortoray-main yourBranch` – VonC Dec 01 '11 at 09:18
  • I know that works, that's essentially my first command (except the URL can be added as a remote, a slight gain). This means there _no_ way to create the intended shortcut using a one-line git command. – edA-qa mort-ora-y Dec 01 '11 at 09:27
  • @edA-qamort-ora-y: I have updated my answer to address your last point. – VonC Dec 01 '11 at 09:28
  • I think your alias is using the wrong ref, since you need to specify the remote branch name, not the local one. If I do want to use a shell alias/function I could just use the two-step process using FETCH-HEAD (which I added to the question). I'm trying to avoid an approach since I have to use multiple platforms (like windows) which will have different shells. – edA-qa mort-ora-y Dec 01 '11 at 09:34
  • @edA-qamort-ora-y: but I thought the all issue was to avoid to name the local branch? As for the refspec used by the git pull in my alias, I have fixed it. I think a shell/alias function is your best option, and is portable accross OS (on Windows, the msysgit implementation uses a standard bash shell). – VonC Dec 01 '11 at 09:42
  • I don't want to specify the local or remote branch name -- I'll always be pulling from the branch called `mortoray-main` into whatever happens to be my local branch. – edA-qa mort-ora-y Dec 01 '11 at 09:45
  • @edA-qamort-ora-y: ok so the alias alias `gpull='git pull mortoray mortoray:\`get_git_branch\`'` will do that for you. – VonC Dec 01 '11 at 10:35
0

I believe your question has already been answered here. You need that in you .git/config:

[branch 'mortoray-main']
    remote = mortoray                 # set mortoray as default remote for this branch
    merge  = refs/heads/mortoray-main # set mortoray-main as default remote branch to merge
Community
  • 1
  • 1
KL-7
  • 46,000
  • 9
  • 87
  • 74
  • I don't have a local branch called `mortoray-main`. The `pull` command pulls into the current branch and that is the behaviour I want to duplicate via shortcut. – edA-qa mort-ora-y Dec 01 '11 at 08:39
0

EDITED: I see you have already rejected this - I think I know what you're trying to do, and you can use an alias...

Essentially what you've got to do is add that remote branch with the 'remote' that you had in the first post. At this point you should be able to git fetch and you'll have an mortoray/mortoray-main branch in the repo (this is the remote one).

The process from here is to git fetch && git merge whilst sitting on the branch you wish to merge it into. This can be turned into an alias in your .git/config or ~/.gitconfig file. Should be something like (you may or may not need the git part before the two commands):

alias pullmortoray = git pull && git merge

Then you type (with tabcompletion) git pullmortoray, and voila!

DISREGARD BELOW!!! for posterity in case someone's already commented.

You've actually almost got it in your question - the remote is correct, you just need to set your local branch up to track the ../mortoray/mortoray-main branch.

git branch --set-upstream <current-branch> mortoray/mortoray-main

do this for each branch you wish to track the mortoray-main branch with, and then a simple git pull will do the trick.

MattJenko
  • 1,208
  • 8
  • 11