A Git Cheat Sheet For That Quick Reminder

A Git Cheat Sheet For That Quick Reminder

Switching from subversion to Git was a paradigm shift that took some getting use to, but looking back now, the switch was worth it. Rather than getting tied to any one Git UI, I wanted to get a grasp of plain old Git and the commands it offers.

With that, I’ve created this Git Cheat Sheet to help along the way.

Cloning a Repository

NotesCommands
clone an existing repositorygit clone "repository-path"
create a new local repositorygit init

 

Branch Management

NotesCommands
list local branchesgit branch
list local and remote branchesgit branch -av
create local branchgit branch "branch-name"
create and checkout local branchgit checkout -b "branch-name"
delete local branch that has had its changes pushed to a remote branch (cannot currently be checked out)git branch -d "branch-name"
delete local branch, ignoring whether or not its changes were pushed to a remote branchgit branch -D "branch-name"
delete local and remote branchgit push origin --delete "branch-name"
checkout branchgit checkout "branch-name"
rename currently checked out local branchgit branch -m "new-branch-name"

 

Handling Changes

NotesCommands
see changes made to local branchgit status
track and stage local changesgit add .
commit staged only with a commit messagegit commit -m "commit-message"
commit unstaged and staged with a commit messagegit commit -a -m "commit-message"
amend changes to previous commit (before pushing)git commit --amend
temporarily stash local changes (resets local branch but preserves changes)git stash
apply previously stashed changes to the current branchgit stash pop

 

Undoing (oops)

NotesCommands
discard all changes in working directorygit reset --hard
reset HEAD to a specific commit and discard local changesgit reset --hard
reset HEAD to a specific commit but preserve local changesgit reset "commit-hash"
discard all changes to specific filegit checkout HEAD
revert commit by applying previous commit changes to HEAD then committinggit revert

 

Tracking History

NotesCommands
show all commits for current HEADgit log
show all commits for current HEAD for given filegit log -p "file"
show all commits that HEAD has pointed togit reflog

 

Fetch, Pull & Push

NotesCommands
grab changes from remote, but don't merge into current HEADgit fetch
grab changes from remote, don't merge, and clean-up remote references (known as pruning, prune)git fetch -p
grab changes and merge into current HEADgit pull
create and track remote branch, push all local changes to itgit push -u "remote" "branch-name"
push all local changes to tracked remote branch for current HEADgit push

 

Merge & Rebase

NotesCommands
merge commits from branch into current HEADgit merge "source-branch-name"
merge commits from source branch into target branchgit merge "source-branch-name" "target-branch-name"
rebase current HEAD onto branch (e.g. make that branch the new base of your changes)git rebase "branch"

 

I'd love to hear your thoughts