Skip to content

Instantly share code, notes, and snippets.

@marshallvaughn
Last active January 31, 2023 20:57
Show Gist options
  • Save marshallvaughn/07918d6cea1a42b06e8f990357e8fdf3 to your computer and use it in GitHub Desktop.
Save marshallvaughn/07918d6cea1a42b06e8f990357e8fdf3 to your computer and use it in GitHub Desktop.
Collection of quick-reference snippets for everyday use with git.
# Get the last commit SHA
git log -1 --format='%H' # long sha
git log -1 --format='%h' # short sha

Get the branch name only.

git rev-parse --abbrev-ref HEAD

List changed files.

# Including whitespace changes
git diff --name-only

# README.md
# script.sh
# Excluding whitespace changes
git diff --name-only -w

# README.md

Force pull, basically. Do a hard reset on the current branch from the origin remote. Use with caution!

git reset --hard origin/dev

Get the latest release tag. Find the most recent tag beginning with "release-", followed by any number of digits.

git describe --tags --abbrev=0 --match "release-[0-9]*"
# release-55.11

Move branch from previous name to main. Most often used to replace the now-archaic 'master' name.

git branch -M main

Get latest commit with the tag at $1.

git rev-list -n 1 --tags="$1"

List all git tags in the repo.

git tag -l

Move a file (and, critically, tell git where it went!).

git mv ./oldName.txt ./newName.txt

List all branches.

git branch

List all branches on remote.

git branch --remotes
# OR
git branch -r

# origin/main

Commit messages

  • Commit messages wrap at 50 char on the first line
  • Commit messages wrap at 72 char for each subsequent line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment