Git Wizardry
Artificial Perfection
Hello World
Matt Fields
Systems Programmer / Administrator
Identity and Web Services
Office of Information Technology
Wizards
“A Wizard is never late, nor is he early.
He arrives precisely when he means to.”
A Common Comparison
The upper echelon of programmers are often jokingly referred to as “Wizards”, seemingly making the impossible happen and writing perfect code that lesser mortals dream to one day achieve.
How do they do it?!?
Magic
Use the Book of Spells to warp reality and rewrite time!
Bag of Holding, Spell Sculpting!
Well let’s learn some Magic!
Rewrite history to fix mistakes before they happen!
Move ideas and reorganize thoughts!
Remove clutter and mess and present the illusion of perfection!
Our Spell Book
Add Patch - Cantrip
Amend Commit - Level 1
Stash - Level 2
Cherry-pick - Level 3
Rebase - Level 4
Filter-Repo - Level 5
Add Patch
git add -p
Launches an interactive prompt to stage small chunks of changes, or leave them for a later commit.
Fantastic way to visually review changes and ensure everything makes sense to add into a commit.
Amend Commit
git commit --amend [-C HEAD]
Modifies the most recent commit by adding the currently staged changes, and optionally modifies the commit message. Use “-C HEAD” to skip editing the message.
Useful when you just need to add a small change into a commit, or need to slightly modify the commit message.
Stash
git stash / git stash pop
Stores all unstaged changes into the stash, which can later be restored to the current working directory.
Very useful when working on something and you need to switch branches to something else for a moment. Can stash multiple times for maximum multi-tasking.
Cherry-Pick
git cherry-pick <ref>
Plucks an individual commit from another branch/tag/commit and append it to the end of the current branch.
Useful when you have a change in one branch that needs to be incorporated into another branch, but the change hasn’t been merged into your mainline branch yet.
Bonus Action: Merge Conflicts
Getting comfortable with resolving merge conflicts will help a lot while manipulating history.
Often times moving a commit around to a different order will create a merge conflict that must be resolved to continue.
Keeping changes small helps avoid massively complicated merge conflicts, and they can be handled pretty quickly and painlessly.
Rebase
git rebase [-i] <ref>
Rewrites all commits since the given reference to the end of the graph. Optionally interactively decide which commits should be modified, combined, or deleted from the history.
The bread and butter for cleaning up history and reorganizing commits. Very powerful, but can cause damage if used carelessly. Better to practice in another branch at first.
Filter-Repo
git filter-repo <...>
The nuclear cannon of rewriting history. Provides a quick and easy way to extract folders into their own repository, fully delete files from history, and much much more.
Absolutely destructive if used carelessly, but enables things not possible with a simple rebase.
https://github.com/newren/git-filter-repo/
Awesome Combos
Turn This…
Into This…
Warning: Never Modify Known Reality
Mainline branches are sacred, and their history is immutable.
NEVER rebase, amend, or otherwise alter the history of a mainline branch unless you know exactly what you’re doing.
Modifying history on unpushed branches is fine and encouraged! You can’t break anything for anyone else because no one else knows those branches exist yet.
Modifying history on pushed, unmerged branches is acceptable as long as everyone working on that branch knows to expect changes.
Safely Force-Push Branch
git push origin <branch> --force-with-lease
Prevents overriding changes which came in since you checked out the branch.
If the push is aborted, try rebasing against the upstream and try again.
Live Examples!
Why Bother?
General Recommendations
Questions?
GitHub Migration
Migration from github.ncsu.edu to Enterprise Cloud