270

I'm a noob in Git, and trying to learn the difference between git pull vs git rebase. Can someone provide an example when to use which option since I feel that both serve the same purpose.

3

3 Answers 3

286

git pull and git rebase are not interchangeable, but they are closely connected.

git pull fetches the latest changes of the current branch from a remote and applies those changes to your local copy of the branch. Generally this is done by merging, i.e. the local changes are merged into the remote changes. So git pull is similar to git fetch & git merge.

Rebasing is an alternative to merging. Instead of creating a new commit that combines the two branches, it moves the commits of one of the branches on top of the other.

You can pull using rebase instead of merge (git pull --rebase). The local changes you made will be rebased on top of the remote changes, instead of being merged with the remote changes.

Atlassian has some excellent documentation on merging vs. rebasing.

Sign up to request clarification or add additional context in comments.

6 Comments

So what's actual diffrence between putting feature on top of master and merging feature with master , in the end both are mixed aren't they ?
The content of the resulting commit is indeed indistinguishable. However, the commit tree looks a bit different. The reason I prefer rebasing over merging is that if you have conflicts, you resolve them commit-wise instead of all during the single merge commit. Also, is results in a cleaner more linear commit tree, which makes it easier to walk through.
When you will check the commit history, if you have used Git Pull, it will show you an extra merge commit, while in git rebase, that wont be there. Git Rebase is a cleaner approach from the context of git history.
So the only different is history related only...? if I wont these merged changes to be in a separate commit on "rewrite/recommit" the commits
So in a way pull is copy-paste and rebase is cut-paste?
|
94

git-pull - Fetch from and integrate with another repository or a local branch GIT PULL

Basically you are pulling remote branch to your local, example:

git pull origin master 

Will pull master branch into your local repository

git-rebase - Forward-port local commits to the updated upstream head GIT REBASE

This one is putting your local changes on top of changes done remotely by other users. For example:

  • You have committed some changes on your local branch for example called SOME-FEATURE
  • Your friend in the meantime was working on other features and he merged his branch into master

Now you want to see his and your changes on your local branch. So then you checkout master branch:

git checkout master 

then you can pull:

git pull origin master 

and then you go to your branch:

git checkout SOME-FEATURE 

and you can do rebase master to get lastest changes from it and put your branch commits on top:

git rebase master 

I hope now it's a bit more clear for you.

5 Comments

You do not need to checkout master. Stay on your branch and do: git fetch then git rebase origin/master.
By checking out the local master and then pulling origin master will update your local master also. This is one of the best practice should be used
so what diffrence if we merging master instead of rebase
@OmarEssamEl-Din "merge" always creates new commit. So you will see one extra merge commit in git commit history. While rebase doesn't create any new commit.
Best answer so far! I just followed this answer and my butt got saved
60

In a nutshell :

Git Merge:

Merges your local changes and remote changes, and that will create another commit history record

Git Rebase:

Put your changes above all new remote changes, and rewrite commit history, so your commit history will be much cleaner than git merge. Rebase is a destructive operation. That means, if you do not apply it correctly, you could lose committed work and/or break the consistency of other developer's repositories.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.