Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pull another branch and checkout

Tags:

git

I often need to checkout latest version of another branch:

git checkout anotherbranch
git pull

How can I do that without checking out outdated version of anotherbranch first?

e.g.:

GIVEN

  1. There are branches master and feature1
  2. each branch has one remote called origin and equally named remote branch.
  3. there are no local unpushed commits

WHEN:

  1. I'm on feature1, and somebody merged PR into master

THEN:

How do I checkout latest version of master?

Does following the trick?

git fetch origin master
git checkout master
like image 984
Liero Avatar asked Oct 14 '25 09:10

Liero


1 Answers

One way I deal with this general situation is to work with the remote tracking branch origin/master, rather than working with the local master branch. Let's say that you wanted to merge the latest remote master into feature1 from the feature1 branch. You could achieve that by using:

# from feature1
git fetch origin
git merge origin/master

The git fetch trick above will update all of your remote tracking branches, such that the local origin/master will now reflect whatever latest changes are on the Git repository. You can then use origin/master for whatever purpose you might have.

like image 126
Tim Biegeleisen Avatar answered Oct 16 '25 22:10

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!