Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Git to automatically pull from current branch when using "git pull"?

Tags:

git

branch

config

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?

like image 876
Brandon Durham Avatar asked Oct 09 '13 18:10

Brandon Durham


People also ask

Does git pull pull from current branch?

A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. In other words: git pull will always merge into the current branch. So you select which branch you want to pull from, and it pulls it into the current branch.

How do you pull changes from a current branch?

Git already only pulls the current branch. If you have branch set up as a tracking branch, you do not need to specify the remote branch. git branch --set-upstream localbranch reponame/remotebranch will set up the tracking relationship. You then issue git pull [--rebase] and only that branch will be updated.

Does git pull only update current branch?

git pull, in contrast, is used with a different goal in mind: to update your current HEAD branch with the latest changes from the remote server. This means that pull not only downloads new data; it also directly integrates it into your current working copy files.

Does git pull pull all remote branches?

git fetch --all and git pull -all will only track the remote branches and track local branches that track remote branches respectively. Run this command only if there are remote branches on the server which are untracked by your local branches. Thus, you can fetch all git branches.


3 Answers

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.

like image 188
batilc Avatar answered Oct 20 '22 04:10

batilc


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
like image 14
Samuel Avatar answered Oct 20 '22 06:10

Samuel


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"
like image 12
Karthik Ramachandran Avatar answered Oct 20 '22 04:10

Karthik Ramachandran