Skip to content

Instantly share code, notes, and snippets.

@wasulabenjamin
Last active January 1, 2025 13:44
Show Gist options
  • Save wasulabenjamin/f46bf83f4d26d02e10dcb3e122d65736 to your computer and use it in GitHub Desktop.
Save wasulabenjamin/f46bf83f4d26d02e10dcb3e122d65736 to your computer and use it in GitHub Desktop.
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.

GITHUB GIT CHEAT SHEET

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.

INSTALLATION AND GUIs

With platform specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.

  • GitHub for Windows
  • GitHub for Mac

For Linux and Solaris platforms, the latest release is available on the official Git web site.

  • Git for All Platforms

SETUP

Configuring user information used across all local repositories

# Sets a name that is identifiable for credit when review version history
git config --global user.name “[firstname lastname]”

# Sets an email address that will be associated with each history marker
git config --global user.email “[valid-email]”

# Sets our default branch to main
git config --global init.default branch main

# Sets automatic command line coloring for Git for easy reviewing
git config --global color.ui auto

# Ensures line endings are left unchanged when files are checked out or committed
git config --global core.autocrlf false

SETUP AND INIT

Configuring user information, initializing and cloning repositories

# Initialize an existing directory as a Git repository
git init

# Retrieve an entire repository from a hosted location via URL
git clone [url]

STAGE AND SNAPSHOT

Working with snapshots and the Git staging area

# Shows modified files in working directory, staged for your next commit
git status

# Adds a specified file as it looks now to your next commit (stage)
git add [file]

# Adds all files as they look now to your next commit (stage)
git add .

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

# Difference of what is changed but not staged
git diff

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

# Commits your staged content as a new commit snapshot
git commit -a -m “[descriptive message]”

BRANCH AND MERGE

Isolating work in branches, changing context, and integrating changes

# Lists your branches, an * will appear next to the currently active branch
git branch

# Creates a new branch at the current commit
git branch [branch-name]

# Switches to another
git switch [branch-name]

# Creates and switches to another branch
git switch -c [branch-name]

# Merges the specified branch’s history into the current one
git merge -m "[descriptive message ie; Merge branch_B back to main]" [branch-name]

# Deletes the specified branch
git branch -d [branch-name]

# Shows all commits in the current branch’s history
git log

INSPECT AND COMPARE

Examining logs, diffs and object information

# Shows the commit history for the currently active branch
git log

# Shows the commits on branchA that are not on branchB
git log branchB..branchA

# Shows the commits that changed file, even across renames
git log --follow [file]

# Shows the difference of what is in branchA that is not in branchB
git diff branchB...branchA

# Shows any object in Git in human-readable format
git show [SHA]

TRACKING PATH CHANGES

Versioning file removes and path changes

# Deletes the file from project and stage the removal for commit
git rm [file]

# Changes an existing file path and stage the move
git mv [existing-path] [new-path]

# Shows all commit logs with indication of any paths that moved
git log --stat -M

IGNORING PATTERNS

To preventing unintentional staging or commiting of files save a file with desired paterns as .gitignore with either direct string matches or wildcard globs.

SHARE AND UPDATE

Retrieving updates from another repository and updating local repos

# Adds a git URL as an alias
git remote add [alias] [url]

# Fetches down all the branches from that Git remote
git fetch [alias]

# Merges a remote branch into your current branch to bring it up to date
git merge [alias]/[branch]

# Transmits local branch commits to the remote repository branch
git push [alias] [branch]

# Fetches and merge any commits from the tracking remote branch
git pull

REWRITE HISTORY

Rewriting branches, updating commits and clearing history

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

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

TEMPORARY COMMITS

Temporarily store modified, tracked files in order to change branches

# Saves modified and staged changes
git stash

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

# Writes working from top of stash stack
git stash pop

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

Updated: 01 JAN 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment