Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase local vs git pull --rebase origin

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?

like image 898
UserControl Avatar asked Jul 04 '17 12:07

UserControl


People also ask

What is pull REBASE in Git?

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.

How do I REBASE a git repository?

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.

What is the difference between merge commit and REBASE in Git?

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.

How do I REBASE from one branch to another?

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 Answers

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).

like image 198
Marina Liu Avatar answered Oct 01 '22 22:10

Marina Liu