224 Advanced git [2d]

Updated . Posted . Visible to the public.

Goals

  • Understand the differences between git diff and git diff --staged (or git diff --cached)
  • Understand the difference between git reset and git reset --hard
  • Use git add -p
  • Use git checkout -p
    • Note you can also rollback partial changes from the line gutter in RubyMine.
  • Use git rebase -i
    • Understand what it means for pushing to a remote, and for working with your colleagues
  • Use git rebase --onto.
  • What does git commit --amend do?
    • Understand what it means for pushing to a remote, and for working with your colleagues
  • Understand the differences between git pull and git pull --rebase
  • Use git revert <commit>
    • What happens? Is the reverted code lost?
  • Use git cherry-pick <commit>
  • Use git reflog
    • When is this useful?
  • Find out how to check out a file from another branch
  • Remember our recommended git workflow for feature branches

Resources

Exercises

While you are working on the movie DB as a single developer, real life applications are usually developed by entire teams. That means that local branches will frequently need to be rebased as they diverge from remote code. These exercises emulate such situations.

You are free to apply better naming conventions to the requested commit messages, they are only kept short here to fit inside the visualization.

Exercise 1: Split up the most recent commit

Head over to https://code.makandra.de/makandra/advanced-git-example-project and fork Show archive.org snapshot the repository to your private space. Ask your mentor to give you access to the repository.

Have a look at these branches and their commits:

masterfeature-documentationnew-git-repodocument-everything

Check out feature-documentation of your fork and rewrite its history into multiple commits using git rebase:

masterfeature-documentationnew-git-repoREADMESetupDocs

The hash of the commit new-git-repo should not change (785951a). Verify this by looking at git log --oneline.

Exercise 2: Split up an earlier commit

Now imagine you are the author of the branch feature-binaries.

masterfeature-binariesnew-git-repodocsadd binariesinit rails

You are now asked to split up the add binaries commit to one commit per binary.

masterfeature-binariesnew-git-repodocsecho binarycurl binaryinit rails

Exercise 3: Rebase onto a diverged feature branch with merge conflicts

masterfeature-nodefeature-echonew-git-repoadd NPMsetup NVMadd echo script

feature-echo was branched of feature-node while it only contained commit "add NPM". It added a new feature on it (add-echo-script).

In the meantime, feature-node was code reviewed, squashed and force pushed. It now looks like this:

masterfeature-nodenew-git-reposetup node

Rebase feature-echo onto feature-node so it has this history:

feature-echonew-git-repo setup nodeadd echo script

The hash of the commit "setup node" should be identical within the two branches. Solve the resulting merge conflicts at the README so that the paragraph about the echo script comes after the introduction to Node.

Henning Koch
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-07-08 18:10)