git cheat sheet

This cheat sheet features the most important and commonly used Git commands for easy reference

Vinayak Hegde
4 min readJan 9, 2024

Git is the free and open source distributed version control system that’s responsible for everything GitHub related that happens locally on your computer. This cheat sheet features the most important and commonly used Git commands for easy reference.

Concise Git cheat sheet with some commonly used commands:

Basic Commands

# Initialise a Repository
git init

# Clone a Repository
git clone <repository-url>

# View Changes
git status

# Add Changes to Staging
git add <file(s)>

# unstage a file while retaining the changes in working directory
git reset [file]

# diff of what is changed but not staged
git diff

# diff of what is staged but not yet commited
git diff --staged

# Commit Changes
git commit -m "Commit message"

# View Commit History
git log

Branching

# Create a New Branch
git branch <branch-name>

# Switch to a Branch
git checkout <branch-name>

# Create and Switch to a New Branch
git checkout -b <new-branch-name>

# Merge Branches
git merge <branch-name>

Remote Repositories

# Add a Remote Repository
git remote add origin <remote-url>

# Set a new Remote Repository
git remote set-url origin

# Check the Current Remote Origin
git remote -v

# Push Changes to Remote
git push -u origin <branch-name>

# Pull Changes from Remote
git pull origin <branch-name>

# Fetch Changes from Remote
git fetch origin

Undoing Changes

# Discard Changes in Working Directory
git checkout -- <file(s)>

# Undo Last Commit (Keep Changes)
git reset HEAD~1

# Undo Last Commit (Discard Changes)
git reset --hard HEAD~1

# apply any commits of current branch ahead of specified one
git rebase [branch]

# clear staging area, rewrite working tree from specified commit
git reset --hard [commit]

Global Config

The git config --global command is used to set or get configuration options that apply globally to a user. These configurations are stored in a file located at ~/.gitconfig. The --global flag ensures that the configuration changes are applied to the global Git configuration file, affecting all repositories on the user's machine.

# Set Global Configuration Syntax
git config --global <key> <value>

# Configure User Information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Get Global Configuration Syntax
git config --global --get <key>

# View Global User Information
git config --global --get user.name
git config --global --get user.email

# Unset Global Configuration Syntax
git config --global --unset <key>

# Unset Global user email
git config --global --unset user.email

# View Git Configuration
git config --global --list

# Set Default Editor (e.g., VSCode)
git config --global core.editor "code --wait"

# Create a Git Alias (e.g., co for checkout)
git config --global alias.co checkout

# View Global Aliases
git config --global --get-regexp alias

# Enable Credential Caching (e.g., for 1 hour)
git config --global credential.helper 'cache --timeout=3600'

# Enable Colored Output
git config --global color.ui true

# Set Default Branch Name (e.g., to "main")
git config --global init.defaultBranch main

# Set Line Ending Conversion (Windows)
git config --global core.autocrlf true

# Branch configuration
# Set the Default Branch Name for New Repositories
git config --global branch.autosetuprebase always
# Set Default Push Mode for New Repositories
git config --global push.default current
# Get the Default Push Mode
git config --global --get branch.push.default
# List All Global Branch Configurations
git config --global --get-regexp branch
# Unset the Default Push Mode
git config --global --unset branch.push.default

# Push configuration
# Set the Default Push Mode
git config --global push.default current
# Set the Default Push Mode to Simple
git config --global push.default simple
# Get the Default Push Mode
git config --global --get push.default
# List All Global Push Configurations
git config --global --get-regexp push
# Unset the Default Push Mode
git config --global --unset push.default
# automatically setup remote tracking
git config --global push.autoSetupRemote true # then `git push`
# or
git config --global push.default current # then `git push -u`

# Pull configuration
# Set the Default Pull Mode
git config --global pull.default current
# Set the Default Pull Mode to Simple
git config --global pull.default simple
# Get the Default Pull Mode
git config --global --get pull.default
# List All Global Pull Configurations
git config --global --get-regexp pull
# Unset the Default Pull Mode
git config --global --unset pull.default

Temporary Commits

git stash a mechanism that allows you to temporarily save changes without committing them.

# Save modified and staged changes
git stash

# list stack-order of stashed file changes
git stash list

# write working from top of stash stack
git stash pop

# discard the changes from top of stash stack
git stash drop

# reapply stashed changes
git stash apply stash@{1}

--

--

Vinayak Hegde

Dad, Husband, Son, Brother, Coder (mostly JavaScript and python), micro-blogger