55

With our current setup you always have to enter the branch name (ie: git pull origin feature-branch" when doing a pull. I've already made the mistake of pulling from one branch into another, accidentally merging two branches with two very different releases. I'd like to avoid this by configuring Git so that simply typing git pull will pull the current branch you're in.

How do I do this?

Brandon Durham
  • 7,096
  • 13
  • 64
  • 101

8 Answers8

43

I am, too, a fan of typing just git pull and getting all the magic.

You have 2 options:

1) git config --global branch.autoSetupMerge always

This will ensure that whether you checkout a remote branch, or create a new one; the tracking information will be handled automatically by git. Then you will be able to

git clone <some_repo>
git checkout -b <new_branch>
git push
git pull

Note that in order to push without more keywords, you need to set the push option as well. I have set it to matching, but everyone has their preference on that. (git config --global push.default matching)

More info: autosetupmerge defaults to true. When set to true, this lets git to perform tracking when you checkout an already existing branch at the remote. For example, if you do git checkout <branch>, git will handle the tracking info so that you can do git pull while on that branch. However, it will not perform this on branches that you create with -b option. Setting autosetupmerge to always ensures that git handles tracking info all the time.

2) When checking out a new branch, you need to specifically set the branch to pull from origin (aka tracking)

git checkout -b <branch> --track <remote>/<branch>

I find this less useful when the branches are transient. If you rarely create a new branch, you should go with this. However, if you are like me, where only the master branch is persistent and every feature has its own brand new branch, then I find option 1 more useful.

Note that, you do not need to make git configuration --global. You may simply write --local there, and have that setting specific to that repository only.

batilc
  • 691
  • 1
  • 5
  • 16
  • 13
    `git config --global branch.autoSetupMerge always` is the command I've been looking for. Thanks. I don't get why that isn't just default. – sudo Jan 24 '18 at 19:41
  • Notice that `branch.autoSetupMerge` only works when creating a new branch, namely via `git checkout -b ...`: if the branch is already there it does nothing, and `git --set-upstream-to ...` is the (only) solution in that case. – gented Sep 28 '22 at 10:41
  • If u are using shallow clone, `git pull` is different to `git pull origin yourBranch`. – BollMose Oct 28 '22 at 08:15
  • `branch.autoSetupMerge always` is dangerous: It will also set up tracking when you branch from *local* branches, and that's probably not what you wanted. For most people, the default of `branch.autoSetupMerge true` will make sense (setting up tracking if what you are branching from is a remote branch), or `branch.autoSetupMerge simple` (which will only set up tracking if the local branch you are creating has the same name as the remote branch you are branching from). – Tao Jan 13 '23 at 16:06
15

This worked for me:

git branch --set-upstream-to=origin/branch_name branch_name

After doing this I can use the following syntax:

git checkout branch_name
git pull --rebase
git push
Samuel
  • 8,063
  • 8
  • 45
  • 41
12

You can create a tracking branch. From the Git Book (http://git-scm.com/book/en/Git-Branching-Remote-Branches):

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"
Community
  • 1
  • 1
Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53
9

If you have git push configured to automatically figure the remote branch name, then you can pass -u and it will also automatically set the tracking branch.

To setup git push to use a remote branch with the same name:

git config --global push.default current`

Explanation for the -u option from git help push:

-u, --set-upstream
    For every branch that is up to date or successfully pushed, add
    upstream (tracking) reference, used by argument-less git-pull(1) and
    other commands. For more information, see branch.<name>.merge in
    git-config(1).

Assuming your current branch is <branch_name>:

$ git push -u
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Everything up-to-date
ハセン
  • 377
  • 3
  • 5
0

I've needed to sync my repos with the master branch, so I've ended up with that simple bash script that fetch changes and rebase to master branch:

function git_do_rebase_with_master (){
    current=$(pwd)
    echo "Syncing $1 ..."
    cd "$1"
    git fetch origin
    GIT_STASH_MESSAGE="Sync on $(date)"
    echo $GIT_STASH_MESSAGE
    git stash -m"${GIT_STASH_MESSAGE}"
    git rebase origin/master
    (git stash list | grep "${GIT_STASH_MESSAGE}" && git stash pop) || echo "Stash was not applied"
    echo "Completed git sync current branch"
    git log --name-status HEAD^..HEAD --pretty=oneline -1
    echo "Completed syncing of $1 ..."
    cd $current
}


alias sync_repo="git_do_rebase_with_master /path/to/repo" 
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
0

I'm also a fan of the ease of short commands that reduce human error, and it's a shame there isn't a configurable default option for git pull. Unfortunately, the top answer doesn't seem to work on existing checked out branches (a pain if you search for this answer only after pulling a bunch of repos), forcing you to run git branch --set-upstream-to=origin/<branch> <branch> on every repo, for every existing branch.

With a desire to reduce the number of characters typed, you can use a git alias to achieve the shorthand pull on all branches that you want the like named branch on your remote. Here's an example to create an alias for git pu:

git config --global alias.pu '!git pull $(git remote) $(git branch --show-current)'

Now every time you type git pu, it will automatically expand to the full pull command.

The shell expansion is really powerful, and if you prefer to use git pull, you could alternatively create an alias to quickly update the remote branch tracking too, for example git up:

git config --global alias.up '!git branch --set-upstream-to=$(git remote)/$(git branch --show-current) $(git branch --show-current)'
git_driver
  • 131
  • 5
-3

Also, if you were to go into your .gitconfig file and make one little change, you can set it to automatically assume you want to push/pull from the current branch in any project. So open .gitconfig in whatever editor you want. And find the [push] option, set it to default=simple, just like below.

[push]
        default = simple
[pull]
        default = simple

like that. Change pull to simple as well. Both are probably set to current right now. Essentially it's exactly the same option as I posted earlier:

git config --global pull.default current

but I've found it to be a better option. So you could use this same line but change current to simple.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Zaichik
  • 308
  • 1
  • 7
  • 1
    This solution does not work it gives me the following error: error: Malformed value for push.default: simple error: Must be one of nothing, matching, tracking or current. fatal: bad config file line 11 in /home/rebecca/.gitconfig – Rebecca Meritz Mar 28 '14 at 09:46
  • run these from command line. 'git config --global push.default simple' 'git config --global pull.default simple' – Zaichik Apr 03 '14 at 05:35
  • update: it doesn't work in git 2.3 but it does work in git 2.4. – jbustamovej Jun 17 '15 at 12:36
-13

This command should configure git to pull to the current branch ..when run in terminal/iTerm.

git config --global push.default current

I think you can just change "push" to "pull" to get the same effect as "push".

Zaichik
  • 308
  • 1
  • 7