Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain "git pull --rebase" in simple terms?

I think I understand git pull and this is how I explain it in, what I call, "simple terms":

  1. Generally speaking, git pull is about merging a "remote" branch into a "local" branch.
  2. In more detail, git uses the content of the "remote" branch to "update" / "modify" content of the "local" branch.
  3. In even more detail, if a file has been modified in the "local" branch but not in the "remote" branch, then after the merge, the content of the file will be the same as the content in the "local" branch. The opposite is also true. If a file was modified on the "remote" branch but not in the "local" branch, the content will be taken from the "remote" branch.
  4. If a file was modified in both branches ("local" and "remote") than git will try to take modifications from both branches. If the changes happen on different places of the file, both changes will be applied and be present in the content of the file after the merge.
  5. If the changes happen on the same place we have what is know as a "merge conflict" and I am not going to touch this case for simplicity.
  6. As a result of the merge, we modify the "local" repository and therefore we need to "commit".

Now I want to get the same kind of explanation for git pull --rebase. I do not want to use such terms as "head", "index", "fetch", "upstream" because these terms / concept only confuse beginners like me. I know that I need to learn these "advanced" concepts and I do it by reading tutorials but for now, as a part of my learning process, I want to understand git pull --rebase.

ADDED

I think at some point I heard the following explanation. By git pull --rebase. When we merge, we do it not in a "symmetric" way, as described above. Instead, we first "forget" the changes in the "local" repository and apply only the changes from the "remote" repository. By doing that we basically "copy" the remote repository as it is. After that we apply the changes from the "local" repository on top. However, it is still not clear to me what exactly it means. In particular, what "on top" means.

like image 976
Roman Avatar asked Dec 02 '17 09:12

Roman


People also ask

What does pull rebase mean in git?

Git pull rebase is a method of combining your local unpublished changes with the latest published changes on your remote.

What is git rebase vs pull?

Git pull downloads the newest changes from the remote repository and applies the changes to your local repository. Generally, git pull is git fetch and git merge. Rebasing on the other hand can be a replacement for git merge .

What exactly git pull does?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.


1 Answers

I see two things that could be clarified: You are focusing on the state of a file in the two branches, but a better way to consider what is going on is in terms of the changesets that have occurred. The second issue is that git pull is shorthand for two operations: git fetch, and git merge. Yes, you write that you "don't want to use words like fetch", but that's not an "advanced concept". If you want to understand what's going on, you need to start there.

  • git fetch essentially informs the local repo of changes that it did not know about.

  • git merge unifies the newly arrived changes with your local changes.

The catch is that if things have been happening on both repos without synchronization, they may have diverged:

... b--o--o--o--o  (remote)
     \
      x--x--x      (local)

The above shows time left to right; the rightmost point is the most recent. So the newly arrived changes are modifications to an older state of the files, the one marked "b".

  • git pull, i.e. plain git merge, will merge the most recent state of the two branches as best as it can.

  • git pull --rebase will pretend that your changes were made not to the state marked "b", but to the most current remote state. In other words it will try to rewrite history so that it looks like this:

    ... b--o--o--o--o              (remote)
                     \
                      x--x--x      (local)
    

That's the difference. One consequence is that if you don't rebase, the history of your repo contains some states (which you can rewind to in the future, if you want) where the "x" changes were applied but the "o" changes are absent. After rebasing, there is no such place in the repository.

like image 179
alexis Avatar answered Oct 03 '22 10:10

alexis