Git Cheat Sheet

BACK: Navigation

Undoing a local commit

git commit -m "I committed something I wasn't supposed to, oops" oops

git reset HEAD~ I think this resets the HEAD back to where it is on the remote (ie all the local changes you just made)?

Resetting Local Changes

git reset <SHA> preserves working directory while removing the commit history

git reset --hard <SHA> holy crap those changes were garbage, remove everything (commits and local changes) since that SHA.

Typo in commit message

git commit -m "fxied the world"

git commit --amend

New Local Branch, Pushing to Public

git branch new_branch

git checkout new_branch && <make_changes>

git push origin new_branch

Undo Public Change

git revert <SHA> creates a commit which undos the change.

Deleting Outdated Local References

When public branches get deleted, the local repo doesn’t know. (ie git push origin \t\t shows a bunch of references.) git remote prune origin

WTF I was on master this whole time?

git branch new_branch Creates new branch

git reset --hard origin/master Deletes everything since origin/master

git checkout new_branch Switch to that new branch

Someone made changes to master while I was adding stuff too..

git checkout master && git pull origin master

git checkout feature

git rebase master Finds the common ancestor and then makes applies a “merge” while keeping all the local changes.

Just want some of the changes from one commit

git cherry-pick <SHA> Adds the commit from that SHA into your current branch git cherry-pick -n <SHA> Adds the files but doesn’t automatically commit them.

Grab a branch from someone else’s fork

git remote add <username><username>/<repo>.git git fetch <username> git checkout <branch>