Commitment Issues:
Understanding git for better version control and
collaboration
Jacob Adams, UGRC
UGIC 2024
git: Not Just an Unpleasant
Person
Ever wish baking had an undo button?
Photo by Vaibhav Jadhav: https://www.pexels.com/photo/man-preparing-dough-for-
bread-3218467/
All the benets
Undo just about everything
Always have a known-good copy
Try new things without fear
Enable complex collaborations
Share your code easily
https://xkcd.com/1597/
Laying the Foundation
Basic Units of git
Blobs and trees: Storing les and directories
Commit: A snapshot in time
Branches: A named lineage of commits
Repository: A collection of all the branches and commits
git is distributed
Hashes and Immutability
Every commit has a SHA-1 hash
What goes into the hash?
The data itself
Author & committer info
Timestamp
The parent commit
Commits are almost never deleted, just "hidden"
Key takeaway: commits are never modied
You may make a new commit that contains the same blob but the
timestamp will be dierent
The Four Storage Areas
Repository: All the blobs, trees, and commits as binary files in the .git folder
Staging Area/Index: Your proposed next commit
Working Directory/Tree: The current commit "de-blobbed" as a normal folder
and files
HEAD: The currently checked out branch
The same le can exist in all three areas with dierent content
Repository: All your previous work on the file
Index: Changes you've made and staged for a future commit
Working Tree: Current edits that aren't finalized
Basic git Operations
Comitting edits
Branching
Merging
Merge conicts
Life of a le
Committing a le: The bedrock operation
Starting point: previous work was committed
Make some edits
Stage your edits
Commit your edits
Branching
Experiment to your hearts content
Branches are cheap- don't be nervous about using them
Checkout: Switch between branches
Replaces content in working tree, index, and Head with the last
commit in the checked-out branch
Merging: Bringing it all back together
Fast Forward: Simply move master forward
Fast Forward: Simply move master forward
Merge Commit: One commit, two ancestors
Merge Commit: One commit, two ancestors
Conict is inevitable
Merge conict markers
Edit, save, commit, continue
GitHub: A cloud git server
with collaboration tools
Git is distributed: Remotes
Syncing work with fetch, pull, and push
fetch: Download all the commits from the remote that are not in your local repo
pull: Do a fetch and then merge your branch into the latest new commit (usually
just a fast-forward)
push: Send your commits to the remote
Always do a fetch or pull before starting to work locally!
GitHub development model
1. Create repo on GitHub
2. Clone it to your local machine
3. Create a local branch and commit to that
4. Push your branch to GitHub
5. Create Pull Request
6. Rebase and merge into GitHub main branch, delete GitHub branch
7. Pull GitHub main into local main
8. Delete local branch
Pull requests
Issue tracking
Stick a fork in it
Advanced git: Rebase,
Reset, and Recover
Rebase: Rewriting history
Rebase and merge: "Clean" merges
Cherry pick: Just one, please
Reset: The ultimate ctrl-z
Soft: Move HEAD back, leave changes staged
Mixed (defualt): Move HEAD, unstage changes
Hard: Blow everything away
Which reset should I use?
I want to completely abandon my current line of work and pretend it never happened
hard
(or really just checkout a new branch at the last point you want to continue from)
I don't have any work in progress or anything I want to keep, I just want to point my branch at a
dierent commit
hard
I did a commit but I want to go back and change something about my edits without adding an extra
commit in the repo tree
mixed
I did a commit but I want to go back and make and stage more edits in addition to my original edits or
change the commit message
soft
also git commit --amend for just the last commit
Recovery: Commits are loyal friends, always
there when you need them
git reflog
git-graph: include commits mentioned by reflog
Help! I committed a
password!
Check out git-filter-repo
Resources
Stack Exchange
YouTube
Pluralsight
The git docs
The git book