Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `pull` needed after checking out a new branch?

In case of a git-project with several branches, the question is when you checkout a new branch (first time), is a git pull needed ?

$master> git checkout branchA
$branchA> git pull

Note that the idea here is that both commands are executed right after each other (this question is not about when-or-why you should run git pull)

I've tested this, but so far the pull doesn't pull in new commits, but some people claim that the pull is needed. Can someone describe a scenario in which this is indeed needed or maybe break this myth?

like image 729
Jeanluca Scaljeri Avatar asked Jun 18 '15 09:06

Jeanluca Scaljeri


People also ask

What happens when you checkout a new branch?

Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch. Think of it as a way to select which line of development you're working on.

Do I need to git pull every time?

Always Pull Before a Push Before you try to push code out to the repository, you should always pull all the current changes from the remote repository to your local machine. Doing so will ensure that your local copy is in sync with the remote repository.

Does git checkout do a git pull?

Remember, all git pull does is run git fetch and then git merge (or git fetch and then git rebase ). It's the git checkout that is messing with the time-stamps on your work-tree files. Git doesn't really use or need the work-tree: that's for you.


1 Answers

If the branch is already on origin and you do not have a local copy and you check it out, then it will contain all commits anyway so you do not need the pull after. If you already have a local copy of the branch it will checkout that one, and in that case you use git fetch origin to see if there have been any changes and the git pull to get those changes. if your branch is not set up to track the remote branch then you will need to add the branch name at the end of the pull, eg git pull origin branchA

like image 84
bruceyyy Avatar answered Sep 28 '22 01:09

bruceyyy