Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut equivalent for a git pull from repository to current branch

Tags:

git

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?

like image 275
edA-qa mort-ora-y Avatar asked May 28 '26 20:05

edA-qa mort-ora-y


1 Answers

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\`'
like image 157
VonC Avatar answered May 31 '26 13:05

VonC