Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - update a branch before checking it out

Tags:

git

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?

like image 696
Black Avatar asked Oct 16 '25 07:10

Black


1 Answers

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)
like image 125
padawin Avatar answered Oct 17 '25 20:10

padawin