Michael Durrant
Michael Durrant

Reputation: 96484

Do I need to git pull main and not just git fetch in order to rebase another branch off latest main?

I am working on a branch called branchA. main has been updated by another and pushed to the remote.

Can I:

git co branchA # Make sure to be on branchA
git fetch
git rebase main

without merging the remote main changes into my local main branch with git pull because rebase goes against the remote copy or do I need to do:

git fetch
git checkout main
git pull # to get the changes applied to local main
git checkout branchA
git merge main

Upvotes: 0

Views: 59

Answers (2)

Mureinik
Mureinik

Reputation: 311348

You don't need to pull in order to rebase. You could rebase on top of the remote main branch you've fetched by referring to it with the origin/ prefix instead of rebasing on top of your local main branch:

git checkout branchA # Make sure to be on branchA
git fetch # origin is implied
git rebase origin/main

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521269

Your first rebase approach should be using the remote tracking branch origin/main, not the local main, as the latter will not be updated by doing a fetch alone:

git checkout branchA
git fetch origin
git rebase origin/main

The same logic applies to the version which does a merge:

git checkout branchA
git fetch origin
git merge origin/main

If you want to use the local main branch, then you would have to update that specifically, in a separate step:

git checkout main
git pull origin main

Upvotes: 1

Related Questions