Commits
reset
Resets the current branch HEAD to a given state. Use with caution.
# Reset to a specific commit, unstage changes, and delete changes from the working directory
git reset --hard [<commit>]
# Reset to a specific commit, but keep unstaged & working directory changes
git reset --soft [<commit>]
# Reset HEAD to origin (main branch in this example)
git fetch origin
git reset --hard origin/main
revert
Creates a new commit at HEAD that reverts all changes from the commit passed to revert
. With the --no-edit
option, we can skip opening the commit message editor and, instead, use the original commit message prepended with the word “Revert”.
git revert --no-edit [<commit>]
--amend
Amends the most recent commit. Most commonly, this is used to change the commit message, but it can also be used to add additional (staged) changes to the most recent commit. Changes the commit SHA.
# Amend commit message for most recent commit
git commit --amend -m "<new_message>"
# Amend the latest commit without changing the commit message
git commit --amend --no-edit
Branches
--unset-upstream
Removes upstream tracking of a branch. If no branch is specified it defaults to the current branch.
git branch --unset-upstream [<branch>]
fetch --prune
Remove references to remote branches that no longer exist. I prefer to setting fetch.prune
to true
in my .gitconfig over this, though.
git fetch --prune
Logs
--pretty=short --graph
Minimizes the commit details and displays a text-based graphical representation of the git history in the output.
git log --pretty=short --graph
rev-parse
Fetch the current HEAD commit SHA.
git rev-parse HEAD
Fetch the current HEAD branch name.
git rev-parse --abbrev-ref HEAD