Mansplaining git

Heavily based off of Oh Shit, Git! by Katie-Sylor Miller. I read the comic version by Julia Evans.

Fire emoji times 3Fire emoji times 3

part 1: git fundamentals

wtf is a commit

Every git commit has an id like 3f29abcd233fa, also called a SHA (“Secure Hash Algorithm”). No matter how many weird things you do with git, checking out a SHA will always give you the exact same code. It’s like saving your game so that you can go back if you die.

How to go back in (commit) history: git checkout 3f29ab // go back to the changes at the time of the 3f29ab commit

wtf is a branch

A branch in git is a pointer to a commit SHA

master —> 2e9fab awesome-feature —> 3bafea fix-typo —> 9a9a9a

Understanding what a branch is will make it WAY EASIER to fix your branches when they’re broken: you just need to figure out how to get your branch to point at the right commit again!

3 main ways to change the commit a branch points to: git reset COMMIT_SHA will point the branch at COMMIT_SHA git pull will point the branch at the same commit as the remote branch git commit will point the branch at the new commit

Try these for yourself. Answer key: the same commit as the remote branch, COMMIT_SHA, the new commit

HEAD is just a text file**

**Run cat git/HEAD to see the current HEAD.

In git you always have some commit checked out. HEAD is a pointer to that commit.

show what’s changed since 16 commits ago: git diff HEAD~16

squash a bunch of commits together git rebase -i HEAD~16

UNDO UNDO UNDO UNDO: reset branch to 16 commits ago git reset --hard HEAD~16

where do commits come from?

Every commit (except the first one!) has a parent commit!

HEAD always refers to the current commit you have checked out, and HEAD^ is its parent. So if you want to go look at the code from the previous commit, you can run git checkout HEAD^

git log shows you all the ancestors of the current commit

part 2: how to un fuck up

Most mistakes you make with git can be fixed. If you’ve ever committed your code, you can get it back

except for..

//WIP Here are the dangerous git commands: the ones that throw away uncommitted work.

⚠️git reset --hard COMMIT

⚠️git clean

⚠️git checkout BRANCH FILE

I need to change the message on my last commit

Just run: git commit –amend

Then edit the commit message & save

Never have I ever … committed and then needed to make one small change

  1. Make your change
  2. Add your files with git add
  3. Run: git commit --amend --no-edit

Never have I ever … accidentally committed to the wrong branch

  1. Check out the correct branch git checkout correct-branch

  2. Add the commit you wanted (git log wrong-branch) to it git cherry-pick COMMIT-ID

  3. Delete the commit from the wrong branch

git checkout wrong-branch
git reset –hard HEAD^

⚠️git reset –hard Before running,

  1. git status to make sure there aren’t uncommitted changes
  2. git stash to save them if there are

Never have I ever … committed something to master that should have been on a brand new branch

  1. Make sure you have master checked out `git checkout master
  2. Create the new branch git branch my-new-branch
  3. Remove the unwanted commit from master
git status
git reset –hard HEAD~
  1. Check out the new branch! git checkout my-new-branch

Never have I ever … had a merge conflict ?!

tl;dr use VS Code.

Acknowledgements: This guide is based on the excellent Oh Shit, Git! zine. If you found this post at all helpful, go buy it now. It has cute drawings. And it’s only $12 for what is the most friendly yet comprehensive guide to Git on planet Earth. If you hate cute drawings, you can find the original post on https://ohshitgit.com/.


Date
January 10, 2023