# 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](https://nvie.com/posts/a-successful-git-branching-model/)
- "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](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
1. Install [pivotal workstation setup](https://github.com/pivotal/workstation-setup)
1. Clone the workshop repository
```bash
git clone https://github.com/simon-parker/git-workshop
cd git-workshop
```
---
## 🛠 Reading `git log` output
```bash
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
```bash
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!
```bash
cd puzzles/checkout-them-all
source init
```
---
## 🛠 Creating commits
```bash
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
```bash
git checkout -b new-branch
```
The new branch will appear at `HEAD`
---
## 🤔 Puzzle Time! (5 minutes)
```bash
cd puzzles/branch-gardening
source init
```
---
## 🛠 Rebasing: a fork in the road
What situation led to this branching?
```graphviz
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
```bash
git checkout feature-branch
git rebase master
```
```graphviz
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**!
```bash
git checkout master
git merge feature
```
```graphviz
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**.
```bash
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)
```bash
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?
```bash
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!
```bash
cd puzzles/day-in-the-life
source init
```
---
## 🎉 Thank you!
🙏 In the spirit of continuous improvement, <a href="mailto:simon@parker.run?subject=Git (Happy Path) Feedback&body=One thing I learned was: %0A%0AOne way the workshop can be improved in the future: ">please send me an e-mail</a> 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
- [Trunk-Based Development](https://trunkbaseddevelopment.com/)
- [Git Documentation](https://git-scm.com/)
- [Pivotal Workstation Setup](https://github.com/pivotal/workstation-setup)
- [Explain Shell](https://explainshell.com/explain?cmd=git+log+--oneline+--graph+--decorate+--all)
{"metaMigratedAt":"2023-06-15T05:15:49.947Z","metaMigratedFrom":"Content","title":"Opinionated Git","breaks":true,"contributors":"[{\"id\":\"fde7702e-69a3-479d-ab92-f66e20acf544\",\"add\":17792,\"del\":12226}]"}