Changes between Version 7 and Version 8 of WorkingWithGit


Ignore:
Timestamp:
Aug 20, 2016, 8:48:09 PM (8 years ago)
Author:
neverpanic (Clemens Lang)
Comment:

Explain updating your local copy and the pitfalls of merging and/or rebasing

Legend:

Unmodified
Added
Removed
Modified
  • WorkingWithGit

    v7 v8  
    8383
    8484== Common `git` tasks & notes about MacPorts' Subversion export ==
     85=== Fetching the latest changes ===
     86Git's equivalent to `svn update` is a little more complicated due to Git's distributed nature. Most of the complexity is not visible if you do not have commits in your working copy that have not been pushed yet. If both the local and the remote repository have changes (git calls them "diverged"), you will run into one of Git's core principles: Every commit has (at least) one parent commit, i.e. the commit history forms a directed acyclic graph.
     87
     88==== Background knowledge ====
     89
     90A picture is worth a thousand words:
     91{{{
     92 A --- B --- C ---- R1 ---- R2 ---- R3  <= origin/master
     93              \
     94               +--- L1 ---- L2          <= master
     95}}}
     96A, B and C are commits that are both in your local and in the remote repository. R1-3 are commits that have been pushed into the remote repository "origin"'s master branch while you were working. L1 and L2 are commits you prepared locally on your master branch. Git offers two different ways to bring R1-3 into your local branch:
     97
     98===== Merging =====
     99A merge commit, created by `git merge`, is a commit that has multiple parents. If no conflict occurs, merge commits do not usually have a diff attached (i.e. they do not modify files). On conflict, merge commits contain the diff that resolves the conflict. In pictures:
     100{{{
     101 A --- B --- C ---- R1 ---- R2 ---- R3   <= origin/master
     102              \                      \
     103               +--- L1 ---- L2 ------ M  <= master
     104}}}
     105The new commit M is the merge commit and can be pushed back to origin. This preserves the information that work was done in parallel, but unfortunately tends to mess up the history graph. See the attached screenshot of a commit history that always merges. To avoid this, you can instead rebase your changes.
     106
     107===== Rebasing =====
     108Rebasing commits rewrites their parent commit IDs and avoids the need for a merge commit. Running `git rebase origin/master` will take all commits in your local working copy that are not yet pushed and attach them after the end of `origin/master`, which yields this picture:
     109{{{
     110 A --- B --- C ---- R1 ---- R2 ---- R3   <= origin/master
     111                                      \
     112                                       L1' ---- L2'  <= master
     113
     114}}}
     115Note that L1 and L2 have been modified by this operation; their commit IDs changed because of that. This new state can be pushed back to origin without the need for a merge commit, and the history graph will stay linear. '''We recommend that all developers rebase their changes rather than merge when conflicts occur during pushing.'''
     116
     117==== Putting the background knowledge into production ====
     118First, get all new commits from the remote repository using `git fetch <remote-name>`, where `<remote-name>` identifies the repository from which you want to fetch and defaults to "origin":
     119{{{
     120git fetch
     121}}}
     122Then, rebase your local changes (if any) on top of any new changes in the remote repository and fix any conflicts that occur:
     123{{{
     124git rebase origin/master
     125}}}
     126
     127Because these two operations are very common, Git offers a shorthand for them:
     128{{{
     129git pull --rebase
     130}}}
     131
     132'''Warning:''' `git pull` without the `--rebase` flag is a shorthand for `git fetch && git merge origin/master`, which will automatically create a merge commit if it thinks that's necessary.
     133
    85134=== Commit messages === #commitmessages
    86135WIP
     136
    87137=== Repository split === #reposplit
    88138WIP