Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rewriting GIT history

mbie
March 03, 2015

Rewriting GIT history

mbie

March 03, 2015
Tweet

More Decks by mbie

Other Decks in Programming

Transcript

  1. RUBY TALKS #1
    REWRITING GIT HISTORY
    Created by Mateusz Bielec

    View full-size slide

  2. AGENDA
    Merging strategies
    Rebase
    Re og

    View full-size slide

  3. MERGING STRATEGIES
    fast-forward
    recursive
    octupus (<1%)

    View full-size slide

  4. FAST-FORWARD
    ➜ git:(master) git checkout -b git-readme
    ➜ git:(git-readme) echo "Git is cool" > GIT.md
    ➜ git:(git-readme) ✗ git add GIT.md
    ➜ git:(git-readme) ✗ git commit -m "Added GIT.md"
    ➜ git:(master) git merge git-readme
    Updating 578bc0a..dc327af
    Fast-forward
    GIT.md | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 GIT.md
    ➜ git:(master) git log --oneline
    dc327af Added GIT.md
    578bc0a Added readme

    View full-size slide

  5. RECURSIVE
    ➜ git:(master) git checkout -b svn-readme
    ➜ git:(svn-readme) echo "SVN ...is not so good" > SVN.md
    ➜ git:(svn-readme) ✗ git add SVN.md
    ➜ git:(svn-readme) ✗ git commit -m "Added SVN.md"
    ➜ git:(master) echo "Git is really cool" > GIT.md
    ➜ git:(master) ✗ git commit -am "Updated GIT.md"
    ➜ git:(master) git merge svn-readme
    Merge made by the 'recursive' strategy.
    SVN.md | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 GIT.md
    ➜ git:(master) git log --oneline --graph
    * 48337c6 Merge branch 'svn'
    |\
    | * abf12f9 Added SVN.md
    * | a7aebe5 Updated GIT.md
    |/
    * dc327af Added GIT.md

    View full-size slide

  6. GIT PULL
    git fetch
    git merge

    View full-size slide

  7. GIT PULL
    ➜ git:(master) git pull
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From https://gitlab.com/mbie/ruby-talks-advanced-git
    a947eed..a8d4a70 master -> origin/master
    Merge made by the 'recursive' strategy.
    README.md | 2 +-
    ➜ git:(master) git log --oneline --graph
    * 7ed530a Merge branch 'master' of https://gitlab.com/mbie/ruby-talks
    |\
    | * a8d4a70 Update Readme.md
    * | b05c794 Updated GIT.md
    |/

    View full-size slide

  8. POLLUTED GIT HISTORY
    ➜ git:(master) git log --oneline
    04e38c8 pulled
    ce8ea84 Merge branch 'develop' of github.com:user/repo into develop
    15a4ae7 Merge branch 'feature'

    View full-size slide

  9. REBASE
    Move commits from current branch to temporary branch
    Run all commits from master (one by one)
    Run all commits from temporary branch (one by one)

    View full-size slide

  10. REBASE IN PRACTICE
    ➜ git:(master) git log --oneline --graph --all --decorate
    * 78492df (HEAD, master) Added CHANGELOG.md
    * 2cc346f Fixed typo in Readme.md
    | * 4541394 (update-repositories) Updated SVN.md
    | * 75a03ad Updated GIT.md
    |/
    * 4e6a918 Updated GIT.md
    ➜ git:(update-repositories) git rebase master
    First, rewinding head to replay your work on top of it...
    Applying: Updated GIT.md
    Applying: Updated SVN.md
    ➜ git:(master) git merge update-repositories
    Updating 78492df..7a8c3df
    Fast-forward
    ➜ git:(master) git log --oneline --graph --all --decorate
    * 7a8c3df (HEAD, update-repositories, master) Updated SVN.md
    * 852bd73 Updated GIT.md
    * 78492df Added CHANGELOG.md
    * 2cc346f Fixed typo in Readme.md
    * 4e6a918 Updated GIT.md

    View full-size slide

  11. REBASE WHEN PULLING
    git fetch & git rebase
    git pull --rebase
    Automatically
    git config branch.branch-name.rebase
    true
    git config branch.autosetuprebase always

    View full-size slide

  12. RESOLVING CONFLICTS WITH REBASE
    git rebase --continue
    git rebase --skip
    git rebase --abort

    View full-size slide

  13. INTERACTIVE REBASE
    Rewrites history
    remove
    squash
    rename
    ...and more
    Freedom
    ... but risky

    View full-size slide

  14. GIT REBASE -I
    ➜ git:(master) git rebase -i
    ...
    # Rebase f0002d3..7a8c3df onto f0002d3
    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commits log message
    # x, exec = run command (the rest of the line) using shell
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.

    View full-size slide

  15. REWRITING HISTORY
    ➜ git:(master) git log --oneline
    fcdac61 Added spec for awesome feature
    a497447 Added awesome feature
    7a8c3df Updated SVN.md
    ➜ git:(master) git rebase -i HEAD~3
    pick 7a8c3df Updated SVN.md
    pick a497447 Added awesome feature
    pick fcdac61 Added spec for awesome feature
    pick 7a8c3df Updated SVN.md
    reword fcdac61 Added spec for awesome feature
    pick a497447 Added awesome feature
    ➜ git:(master) git log --oneline
    018a2bd Added awesome feature
    6358738 Added awesome spec for awesome feature
    7a8c3df Updated SVN.md

    View full-size slide

  16. REFLOG
    Only locally
    Updates when HEAD is changed
    pulling branches
    switch between branches
    add new commits
    ... and much more

    View full-size slide

  17. REFLOG IN PRACTICE
    ➜ git:(master) git reset --hard 567e4f1
    ➜ git:(master) git log --oneline
    519a057 Added awesome spec for awesome feature
    7a8c3df Updated SVN.md
    ➜ git:(master) git reflog
    519a057 HEAD@{0}: reset: moving to 519a057
    567e4f1 HEAD@{1}: rebase -i (finish): returning to refs/heads/master
    567e4f1 HEAD@{2}: rebase -i (pick): Added awesome feature
    519a057 HEAD@{3}: rebase -i (reword): Added awesome spec for awesome feature
    eb6edf3 HEAD@{4}: rebase -i (reword): Added spec for awesome feature
    7a8c3df HEAD@{5}: checkout: moving from master to 7a8c3df

    View full-size slide

  18. REFLOG IN PRACTICE
    ➜ git:(master) git reset --hard HEAD@{1}
    ➜ git:(master) git log --oneline
    567e4f1 Added awesome feature
    519a057 Added awesome spec for awesome feature
    7a8c3df Updated SVN.md
    ➜ git:(master) git reflog
    567e4f1 HEAD@{0}: reset: moving to HEAD@{1}
    519a057 HEAD@{1}: reset: moving to 519a057
    567e4f1 HEAD@{2}: rebase -i (finish): returning to refs/heads/master

    View full-size slide

  19. SUMMARY
    Two the most used strategies for merging branches
    Pulling with merging pollutes history with merge commits
    Rebase cleans history
    Interactive rebase helps rewrite entire history
    Re og may save our skin

    View full-size slide

  20. THANK YOU FOR
    LISTENING
    ANY QUESTIONS?

    View full-size slide