Ultimate Git Cheat Sheet

Master Git effortlessly with this ultimate cheat sheet, your perfect guide to version control wizardry!

ultimate-git-cheat-sheet

The Ultimate Git Cheat Sheet

Git is an essential tool for version control in software development. This cheat sheet will guide you through basic commands, undoing changes, rewriting history, branching and merging, pushing and pulling changes, stashing modifications, and tracking paths. Let's dive in!

Basic Commands

Initialize and Clone Repositories

Create an empty Git repo in the specific directory:

git init <directory>

Clone repo located at the <repo> defined. Can be either local or remote via HTTP or SSH:

git clone <repo>

Configuration

Set the author name that would be used for all commits in the current repo:

git config user.name <name>

Stage and Commit Changes

Add the directory/file on your next commit:

git add <directory>/<file>

Commit your staged content as the new commit snapshot with a message:

git commit -m "<message>"

Status and Logs

Show modified files in the working directory:

git status

Show all commits in the current branch's history:

git log

Show the difference of what's changed but not staged:

git diff

Undo Changes

Undoing the Doing

Undo all the changes made in the <commit> by creating a new commit and apply to the current branch:

git revert <commit>

Remove the file from the staging area but leave the working directory unchanged. Unstage a file without overwriting any changes:

git reset <file>

Remove the unwanted files from your working directory:

git clean

More Changes

Rewrite, Reset, Rebase

Apply any commits of the current branch ahead of the specified one:

git rebase <branch>

Show a log of changes to the local repository's HEAD:

git reflog

Reset the staging area to match the most recent commit but leave the working directory unchanged:

git reset

Reset both the staging area and the working directory to match. Delete uncommitted changes, and all commits after <commit>:

git reset --hard <commit>

Committing Changes

Branch and Merge

List all your branches:

git branch

Create a new branch at the current commit:

git branch <branch-name>

Switch to another branch and check it out into your working directory:

git checkout <branch-name>

Merge the specified branch's history into the current one:

git merge <branch>

Push and Pull

Remote Operations

Add a Git URL with a name as the alias:

git remote add <alias> <url>

Fetch all the branches from the Git remote:

git fetch <alias>

Merge a remote branch into your current branch to bring it up to date:

git merge <alias>/<branch>

Push the branch commits to the repository. Create the named branch if it doesn't exist:

git push <alias> <branch>

Fetch and merge any commits from the tracking remote branch:

git pull

Temporary Commits

Stashing Changes

Save modified and staged changes:

git stash

List stack-order of stashed file changes:

git stash list

Discard changes from the stash stack:

git stash drop

Inspect and Compare

Logs and Diffs

Show all commits in the current branch's history:

git log

Show the commits on branchA that are not on branchB:

git log branchB..branchA

Show the commits that changed a file, including file renames:

git log --follow <file>

Show the diff of what's in branchA compared to branchB:

git diff branchB...branchA

Tracking Path

Versioning File Removes and Path Changes

Delete the file from the project and stage the removal for commit:

git rm <file>

Change the current file path and stage the move for commit:

git mv <current-path> <new-path>

Show all commit logs with an indication of paths that have been moved:

git log --stat -M

Follow Us:

Stay updated with our latest tips and tutorials by subscribing to our YouTube Channel.


DevTools99

Developer Co-Team

Let us make it easier for developers throughout the world to breathe. Let's keep things simple. Let's take it easy.