Reputation: 21128
Is there a shortcut to pull from the current branch without explicit calling it.
E.g. I've checked out a branch: git checkout mybranch
To pull from the branch I use git pull origin mybranch
. Is it possible to skip the origin mybranch
part? The problem is, that our branches have quite long names like b_searchsystem_grid
.
Maybe something like git pull origin .
(. for current branch).
Upvotes: 4
Views: 1784
Reputation: 608
git pull origin $(git rev-parse --abbrev-ref HEAD)
For git 2.22+:
git pull origin $(git branch --show-current)
Upvotes: 1
Reputation: 3317
I think it's important to not just memorize the command, but to also understand the reason behind this.
Git branches can have an "upstream branch" set, which is the corresponding branch to your local branch on a remote. This upstream branch has to be explicitly declared, because you might have multiple remotes. In that case, git would not know where to push or pull from to when you do git push
or git pull
.
You can list the upstream branches of your branches:
$ git branch -vv
* testbranch 271404b commit message
master ac7a302 [origin/master] add foo
Here you see that the testbranch
branch does not have an upstream branch, so git would not know where to push/pull from. However, the master branch does have an upstream branch (origin/master
), so git does know what to do when we type git pull
.
There are a few ways we can set the upstream branch:
git branch --set-upstream-to origin/branchname
git branch -u origin/branchname
(just a shorthand)git push -u origin branchname
(often more convenient)Upvotes: 2
Reputation: 489608
After you establish the correct defaults, you can run just git pull
with no arguments, though I recommend you split it into git fetch && git merge
if that's what you intend.
First, remember that git pull
just means: Run git fetch
, then run a second Git command. The second command defaults to git merge
, but you can choose git rebase
instead.
The git fetch
command needs to know two things:
Fetch from where?
Importantly, there is a default. The default is the remote associated with the current branch, based on its upstream. Both of these are defined below.
Fetch what?
There is a default here too. Running git fetch
with no arguments means "fetch everything", which is what you will usually want. When git pull
runs git fetch
, it adds some arguments to limit what gets fetched, which may make this fetch slightly faster, but make future fetches slightly slower in the process. (Pay now, or pay later, but since the payment is generally just fractions of seconds, who cares?)
The git merge
or git rebase
command needs to know at least one thing:
And here, too, there is a default. The default is the upstream. When you use git pull
to run the second Git command, it fiddles with this somewhat, but the effect is basically the same, so that you can just run the two Git commands yourself.
Any Git repository can have any number of what Git calls remotes. A remote is primarily a short name for a URL, which acts like a phone number at which some other Git repository can be reached. The most common remote name is origin
.
When you run git clone
to make an initial clone of some other Git repository, you tell git clone
how to reach the other Git, giving it a URL. The clone
step saves that URL under the name origin
.1 Hence origin
becomes the easy way to mention that other Git.
The upstream of a branch is made of two parts:
Typically we just lump these two together, so that the upstream of branch B
is origin/B
. Note that creating a local branch from a remote-tracking name sets the upstream automatically, so that you rarely have to do this at all.2
To set or change the upstream of any given branch, you can use any of several commands:
git branch --set-upstream-to origin/branch branch
(this sets the upstream of branch
to origin/branch
)
git branch --set-upstream-to origin/branch
(this sets the upstream of the current branch, whatever you have checked out, to origin/branch
)
This is usually the one you should use if you have not just created the branch. Typically you'll be on the branch, and it will be time to set its upstream, so you can now set it.
git push -u origin branch
This is what you should use if you've just created the branch. If you just created the branch, so that there is no origin/branch
at all, you have to first create the name branch
in the other Git, which you do with git push
. Now that it exists, and you also have origin/branch
, now you can set the upstream. Using git push -u
will do both at the same time, as a short-cut, and it has the side effect of making git push
know that you meant to create the branch in the other Git, so it skips a bunch of annoying error messages as well.
1You can choose another name, but people rarely do.
2Consider the following sequence:
git clone <url> # creates repository, adds the remote, fetches,
# and checks out master based on origin/master
cd <repository> # enter repo so that you can work in it
git checkout develop # creates develop based on origin/develop
The git clone
step created your master
branch by running, in effect, git checkout master
. That made master
have an upstream, namely origin/master
. The git checkout develop
step does the same thing: finds origin/develop
, uses that to make a new branch name develop
as your develop
, and sets your develop
to have origin/develop
as its upstream.
Upvotes: 6
Reputation: 4821
You can just do git pull
and have it imply origin if you did git push -u origin <branch>
once it'll make all future git pulls based on origin by setting your upstream remote.
Upvotes: 2