Opinionated Git

The Happy Path


🎯 Goals

  • ✍️ Establish guiding principles for Git usage
  • 🛠 Introduce tools aligned with these principles
  • 🤔 Have fun solving puzzles!

🙀 Anti-goals

  • A complete overview of the Git API or internals
  • An explanation of more complex branching models, such as git flow
  • "Advanced" git-fu and how to get out of common git conundrums (planning a separate workshop for this)

✍️ Guiding Principles

  • master is the source of truth and should be production-ready at all times
  • feature branches track work in progress and should be short-lived
  • git log tells the story of our product and should be linear

🏁 Let's get started!

  1. Make sure git is installed on your machine
  2. Install pivotal workstation setup
  3. Clone the workshop repository
git clone https://github.com/simon-parker/git-workshop
cd git-workshop

🛠 Reading git log output

git log --oneline --decorate --all --graph # aka `git lola`
  • HEAD is your current location in the repository
  • (master, branch) show your local branches/tags.
  • (origin/master, origin/branch) show your remote tracking branches/tags.

🛠 Moving about

git checkout <commitish>

<commitish> can be a number of things, including:

  • git checkout <commit-hash>
  • git checkout <branch-or-tag-name>
  • git checkout <commitish>~1
  • git checkout <commitish>~

🤔 Puzzle Time! (5 minutes)

Try to solve the puzzle as efficiently as you can!

cd puzzles/checkout-them-all
source init

🛠 Creating commits

git add . 
git add "src"
git add "*.sh" 
git add "*/test/*" 

git commit -m "commit message"
git commit --amend -m "amended commit"

🛠 Creating new branches

git checkout -b new-branch

The new branch will appear at HEAD


🤔 Puzzle Time! (5 minutes)

cd puzzles/branch-gardening
source init

🛠 Rebasing: a fork in the road

What situation led to this branching?

digraph hierarchy {
                nodesep=0.5 // increases the separation between nodes
                node [shape=box, style=rounded]
                //rankdir=LR;
                
                master, feature [style="square", color="red"]
                
                
                feature -> "feature commit" -> "shared commit"
                master -> "master commit" -> "shared commit"
}

🛠 Rebasing: linear history

git checkout feature-branch
git rebase master
digraph hierarchy {
                nodesep=0.5 // increases the separation between nodes
                node [shape=box, style=rounded]
                //rankdir=LR;
                
                master, feature [style="square", color="red"]
                
                
                feature -> "feature commit" -> "master commit" -> "shared commit"
                master -> "master commit"
}

✍️ Rebasing makes our history easy to understand by making it linear!


🛠 Merging

✍️ We are not done until our changes are on master, the source of truth!

git checkout master
git merge feature
digraph hierarchy {
                nodesep=0.5 // increases the separation between nodes
                node [shape=box, style=rounded]
                //rankdir=LR;
                
                master, feature [style="square", color="red"]
                
                
                feature, master -> "feature commit" -> "master commit" -> "shared commit"
}

🛠 Merging: cleaning up

✍️ Once our changes are on our source of truth, we should delete our short-lived feature branch.

git checkout master
git merge feature
git push master
git branch -D feature     # delete local branch
git push --delete feature # delete remote branch

# delete any remote tracking branches 
# others have deleted with git push --delete
git fetch -p 

🤔 Puzzle Time! (10 minutes)

cd puzzles/rebase-madness
source init

🎁 Bonus: The - shortcut

Like bash, Git will treat a - as an alias for your previous location in the tree

It works with git checkout, git rebase and git merge!


🤔 What does this do?

git checkout feature-branch
# ... do some work
git checkout master
git pull -r

git checkout -
git rebase -
git checkout -
git merge -

git push

🤔 Puzzle Time! (10 minutes)

It's time to bring together everything we learned and go through a full day in the life of a pair!

cd puzzles/day-in-the-life
source init

🎉 Thank you!

🙏 In the spirit of continuous improvement, please send me an e-mail with the following feedback:

  • One thing you learned during this workshop
  • One way it can be improved in the future

😎 That's all, folks! 😎


📝 References

Select a repo