Black
Black

Reputation: 20232

Git - update a branch before checking it out

I pushed about 200 commits to the branch master.

On another server the branch A is checked out, but I need to checkout master here.

I could execute git checkout master and then immediately git pull.

But then a old state is going live until git pull is executed to update the branch.


Can I update a branch first and then check it out?

Upvotes: 4

Views: 257

Answers (2)

Alex
Alex

Reputation: 21

git fetch origin master:master
git checkout master

Source: 1177 upticks at last count Merge, update, and pull Git branches without using checkouts

Upvotes: 2

padawin
padawin

Reputation: 4476

After running git fetch, you can force master to change to where origin/master is:

git branch -f master origin/master

Then you can check it out:

git checkout master

Which will be at the state of origin/master and you will not pass by the intermediate state it was before.

Here is a brief example (I am checked out on test and change master to the same place as test):

> git log --graph --oneline
* d97b1f8  (HEAD -> test) - tata (1 second ago)
* e680fb5  - toto (9 seconds ago)
* 4515586  (master) - bar (24 seconds ago)
* e241705  - foo (28 seconds ago)

> git branch -f master test

> git log --graph --oneline
* d97b1f8  (HEAD -> test, master) - tata (9 seconds ago)
* e680fb5  - toto (17 seconds ago)
* 4515586  - bar (32 seconds ago)
* e241705  - foo (36 seconds ago)

Upvotes: 5

Related Questions