A series of exercises to help understand the git object model, and explore some of the cool features it provides.
For a simple explanation of the git object model, see http://www.gitguys.com/topics/all-git-object-types-blob-tree-commit-and-tag/.
Each exercise is a different branch of this repository. The relevant portions of the initial state of the exercises are included in the Samples folder in master. Each exercise has a number of changes to the repository described, which you will add as a single commit. Draw the change you expect to see, then use git cat-file -p
to check your work.
In this first exercise, we'll change a single file and see how it changes the git object graph. Be sure to write down your expected state before using cat-file to check your work.
- Checkout the simple-edit branch
- Open 'file-to-edit.txt' in your editor of choice
- Use
git commit -am
to add your changes to your repository - Starting from the diagram in simple-edit.png, draw the changes you just made to the git object graph
- Check your diagram, using
git cat-file simple-edit
to look at the commit at the head of the simple-edit branch
In this exercise, we'll rename a single file without making any other edits. Once again, make sure to write down how you expect the object state to change before using cat-file to check the results.
- Checkout the simple-rename branch
- Rename 'file-to-rename.txt' to 'renamed.txt'
- Use
git add -A
and thengit commit -m
to add your changes to your repository - Starting from the diagram in simple-rename.png, draw the changes you just made to the git object graph
- Check your diagram, using
git cat-file simple-rename
to inspect the commit at the head of the simple-rename branch
In this exercise, we'll change a single file in a deeply nested sub-directory.
- Checkout the directory-edit branch
- Edit deep/sample/directory/structure/file.txt
- Use
git commit -am
to add your changes to your repository - Starting from the diagram in directory-edit.png, draw the changes you just made to the git object graph
- Check your diagram, using
git cat-file directory-edit
to inspect the commit at the head of the directory-edit branch
Consider the performance implications of the way git handles directories. What benefits and drawbacks does this cause?
In this exercise, we'll perform a simple merge with no conflicts. Merging is one of two strategies git supports for combining the changes from two different branches.
- Checkout the simple-merge branch
- Use
git merge simple-merge-a
to bring in the changes from the simple-merge-a branch - Starting from the diagram in simple-merge.png, draw the changes you just made to the git object graph
- Mark which commits are at the head of both simple-merge and simple-merge-a after the merge
- Check your diagram, using
git cat-file simple-merge
to inspect the commit at the head of the simple-merge branch
In this exercise, we'll perform a merge with a simple merge conflict. Before starting, make sure your mergetool is configured in git.
- Checkout the merge-conflicts branch
- Use
git merge merge-conflicts-a
to bring in the changes from the merge-conflicts a branch - Use
git mergetool
to resolve the merge conflicts (the final file should read 'a man, a plan, a canal, panama' - Use
git commit
to add your merged file to your repository - Starting from the diagram in merge-conflicts.png, draw the changes you just made to the git commit object graph
- Mark which commits are at the head of both merge-conflicts and merge-conflicts-a
- Check your diagram
In this exercise, we'll perform a simple rebase with no conflicts. Rebase is the other of the two strategies for sharing work across branches.
- Checkout the simple-rebase branch
- Use
git rebase rebase-master simple-rebase
to add the changes from simple-rebase to the rebase-master branch - Starting from the diagram in simple-rebase.png, draw the changes you just made to the git object graph.
- Mark which commits are at the head of both rebase-master and simple-rebase
- Check your diagram
- What will happen if you use
git merge simple-rebase
on rebase-master?
In this exercise, we'll use interactive rebase to reduce the number of commits in a local branch before applying them to master. This allows you to take advantage of git's ability to quickly make local commits to save state, while limiting the number of commits that you share.
- Checkout the simple-squash branch
- Use
git rebase -i HEAD~3
- Change 'pick' to 'squash' for all the commits except the first
- Save and quit the editor
- Save and quit the modified commit message editor
- Starting from the diagram in simple-squash.png, draw the changes you just made to the git object graph.
- Check your diagram
Interactive rebase works the same way when rebasing on to a different branch.