I have a local (never pushed to a remote) feature branch F1 made from master with a few commits. I don't seem to understand a difference between these two actions (F1 is the current branch):
git fetch origin
git rebase master
and
git pull --rebase origin master
My expectation is that they should be equivalent but they aren't - different results are produced.
What's wrong with my thinking?
Git pull rebase is a method of combining your local unpublished changes with the latest published changes on your remote. Let’s say you have a local copy of your project’s main branch with unpublished changes, and that branch is one commit behind the origin/main branch.
In case of a rebase, we use the command git pull --rebase. In a rebase, the unpublished local changes of the local branch are reapplied on top of the published changes of the remote repository. No new commit is created in the rebase case.
A merge commit is created to point to the latest local and remote commits. In case of a rebase, we use the command git pull --rebase. In a rebase, the unpublished local changes of the local branch are reapplied on top of the published changes of the remote repository.
To proceed with the rebase, you will use the git rebase command followed by the name of your target branch—in this case, the dev branch—to move your changes from one branch to another. With that, the rebase is complete! Git has re-written the commits from your feature branch onto the most recent commit on the dev branch.
1. git fetch origin
and git rebase master
will apply changes from F1
to local master
branch. Assume your commit history looks like below at first (the remote master
branch has commit J
in remote):
A---B---C---D---E master
\
F---G---H F1
When you execute git fetch origin
and git rebase master
, even though origin/master
points to J
, it will only rebase F1
branch on the top of local master
branch (commit E
as the graph):
A---B---C---D---E(master)---J origin/master
\
F---G---H F1
2. The command git pull --rebase origin master
will pull changes from remote master
branch at first, then rebase current branch F1
on the top of it:
A---B---C---D---E---J master,origin/master
\
F---G---H F1
In a word, if local master
branch is sync with remote master
branch, these two ways have the same result (rebase F1
branch on the top of master branch). If remote master
branch has new commit(s) which is(are) not exist on local master
branch, the results are different (one rebases on local master
branch, the other rebase on origin/master
branch).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With