GitHub Cheatsheet

The Git commands you’ll actually use on a regular basis. Copy-paste friendly examples for the most common workflows. Keep this handy when you’re setting up repos or pushing changes.

Below is a reference for common Git commands and workflows. For more detailed information, see the official Git documentation.

Initial Setup

Once per workstation, you will need to configure Git with your identity and SSH keys. This setup is crucial for committing changes and pushing them to remote repositories.

Configure Your Identity

# Set your username
git config --global user.name "Your Name"

# Set your email
git config --global user.email "[email protected]"

# List your configuration
git config --list

Configure SSH Keys

# Generate a new SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Start the SSH agent
eval "$(ssh-agent -s)"

# Add your SSH key to the agent
ssh-add ~/.ssh/id_ed25519

# Display your public key to copy to GitHub/GitLab
cat ~/.ssh/id_ed25519.pub

Configure GPG Keys for Signing

# Generate a new GPG key
gpg --full-generate-key

# List your GPG keys
gpg --list-secret-keys --keyid-format=long

# Configure Git to use your GPG key
git config --global user.signingkey YOUR_GPG_KEY_ID

# Enable automatic signing for all commits
git config --global commit.gpgsign true

Configure SSH Key for Signing

# Configure Git to use SSH key for signing
git config --global gpg.format ssh

# Set the SSH signing key
git config --global user.signingkey ~/.ssh/id_rsa.pub

# Enable automatic signing for all commits
git config --global commit.gpgsign true

Basic Commands

Below are the basic commands for initializing a repository, cloning, and checking the status of your Git repository.

Check Status and History

# Show working tree status
git status

# Show commit history
git log

# Show commit history with one line per commit
git log --oneline

# Show commit history with a graph
git log --graph --oneline --decorate

# Show changes in a file
git blame filename

Adding and Committing Changes

# Add specific files to staging
git add filename1 filename2

# Add all changed files to staging
git add .

# Add all tracked files with changes
git add -u

# Commit staged changes
git commit -m "Your commit message"

# Add all changes and commit in one command
git commit -am "Your commit message"

# Sign your commit with your GPG key
git commit -S -m "Your signed commit message"

# Amend your last commit
git commit --amend

Working with Remotes

Below are commands for managing the upstream remote repositories, syncing changes, and pushing commits.

Remote Repository Management

# List remote repositories
git remote -v

# Add a remote repository
git remote add origin https://github.com/username/repository.git

# Change URL of existing remote
git remote set-url origin https://github.com/username/new-repository.git

# Remove a remote
git remote remove remote-name

Syncing with Remote Repositories

# Download all changes from remote without merging
git fetch origin

# Download changes and merge from remote repository
git pull origin branch-name

# Pull with rebase instead of merge
git pull --rebase origin branch-name

# Push changes to remote repository
git push origin branch-name

# Push a new local branch to remote
git push -u origin branch-name

# Force push (use with caution!)
git push -f origin branch-name

Branch Management

Below are commands for creating, switching, merging, and deleting branches in your Git repository.

Creating and Switching Branches

# List all branches
git branch

# List all branches including remote branches
git branch -a

# Create a new branch
git branch new-branch-name

# Create and switch to a new branch
git checkout -b new-branch-name

# Switch to an existing branch
git checkout branch-name

# Modern way to switch branches (Git 2.23+)
git switch branch-name

# Create and switch to a new branch (Git 2.23+)
git switch -c new-branch-name

Merging and Rebasing

# Merge a branch into your current branch
git merge branch-name

# Merge without fast-forward
git merge --no-ff branch-name

# Rebase current branch onto another branch
git rebase branch-name

# Interactive rebase for cleaning up commits
git rebase -i HEAD~3  # Rebase last 3 commits

Deleting Branches

# Delete a local branch that has been merged
git branch -d branch-name

# Force delete a local branch
git branch -D branch-name

# Delete a remote branch
git push origin --delete branch-name

Stashing Changes

In Git, the concept of stashing allows you to temporarily save changes that are not ready to be committed. This is useful when you need to switch branches or pull changes without committing your current work.

# Stash current changes
git stash

# Stash with a descriptive message
git stash save "Your stash description"

# List all stashes
git stash list

# Apply most recent stash without removing it
git stash apply

# Apply a specific stash
git stash apply stash@{2}

# Apply and remove the most recent stash
git stash pop

# Remove a specific stash
git stash drop stash@{1}

# Clear all stashes
git stash clear

Advanced Operations

Below are some advanced Git operations, including resolving merge conflicts, reverting changes, tagging, and working with submodules.

Resolving Merge Conflicts

# Abort a merge with conflicts
git merge --abort

# After manually resolving conflicts
git add resolved-file.txt
git commit -m "Merge conflict resolution"

# Use a visual merge tool
git mergetool

Reverting Changes

# Undo staged changes for a specific file
git reset HEAD filename

# Discard changes in working directory
git checkout -- filename

# Reset to a specific commit (soft - keep changes staged)
git reset --soft commit-hash

# Reset to a specific commit (mixed - default, unstage changes)
git reset commit-hash

# Reset to a specific commit (hard - discard all changes)
git reset --hard commit-hash

# Create a new commit that undoes changes
git revert commit-hash

Tagging

# List all tags
git tag

# Create a lightweight tag
git tag tag-name

# Create an annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"

# Push tags to remote
git push origin --tags

# Delete a local tag
git tag -d tag-name

# Delete a remote tag
git push origin --delete tag-name

Git Submodules

A submodule is a repository embedded within another Git repository. This allows you to keep track of external dependencies or libraries. These are used in at least a couple of scenarios:

  1. Themes: your repository may include the theme repository as a submodule. You get the benefit of having the files from the other repository, but those files are not directly in your repository and are not checked-in as part of your repository.
  2. Virtual Forks: if you have a limited, open source project and then you maybe have a private SaaS platform that is based on that open source project, you can use a submodule to include the open source project in your private repository. This way, you can keep the two repositories separate but still have access to the code in both. You “overlay” the open source project with your private code, and you can update the submodule to get the latest changes from the open source project.

So, these are used every day, but when you need this exact functionality, it’s good that this is available.

# Add a submodule
git submodule add https://github.com/username/repo.git path/to/submodule

# Initialize submodules after cloning a repository
git submodule init

# Update submodules to their latest commits
git submodule update

# Update all submodules
git submodule update --remote

Useful Aliases and Configuration

If interested, consider adding these to your ~/.gitconfig file:

[alias]
  st = status
  co = checkout
  ci = commit
  br = branch
  lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
  unstage = reset HEAD --
  last = log -1 HEAD

Best Practices

Here are some best practices to follow when using Git:

  1. Commit often: Make small, focused commits that address a single issue
  2. Write meaningful commit messages: Use a descriptive present tense
  3. Pull before pushing: Integrate others’ changes before pushing yours
  4. Use branches for features/bugfixes: Keep your main branch clean
  5. Sign your commits: Verify your identity with GPG or SSH signing
  6. Keep a clean history: Use rebase to maintain a linear history
  7. Never rewrite public history: Don’t force push to shared branches

This has just been a brief overview of common Git commands. For more detailed information, see the official Git documentation.