Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A list of my most used git commands for reference
// Create a new branch from the current branch which has uncommitted work
git checkout -b <branch_name>
// Fetch all latest versions of remote & branches
git fetch -all
// Delete a local branch (forces delete with -D rather than -d )
$ git branch -D <local-branch>
// See what files were changed in a commit
git log remote_name/branch_name --raw
// See the history of a specific file over time across commits
git log --follow -p -- file_name
// Switch to a different commit - (creating a new branch and using that commit to populate it so further commits could be used)
git checkout -b my_new_branch xxxxx(commit hash)
// This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32
// Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:
git checkout -b old-state 0d1d7fc32
// Revert to a previous commit
// This will destroy any local modifications.
// Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
// This will unstage all files you might have staged with git add:
git reset
// Revert all uncommitted changes
git reset --hard HEAD
// Merge one branch into another - checkout the branch you want to merge into ie: main and then run
git merge _branchname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment