Utility Tool

Git Cheat Sheet

Complete interactive reference of Git commands with search and copy.

62 commands across 8 categories

git init

Initialize a new Git repository in the current directory

git clone <url>

Clone a remote repository to your local machine

git config --global user.name "<name>"

Set your global Git username

git config --global user.email "<email>"

Set your global Git email address

git config --list

List all Git configuration settings

git status

Show the current status of the working directory and staging area

git add <file>

Stage a specific file for the next commit

git add .

Stage all changes in the current directory for the next commit

git commit -m "<message>"

Commit staged changes with a descriptive message

git commit -am "<message>"

Stage all tracked files and commit in one step

git diff

Show unstaged changes between working directory and staging area

git diff --staged

Show changes that are staged for the next commit

git rm <file>

Remove a file from the working directory and stage the removal

git mv <old> <new>

Rename or move a file and stage the change

git branch

List all local branches

git branch <name>

Create a new branch with the given name

git branch -d <name>

Delete a local branch (safe delete)

git branch -D <name>

Force delete a local branch

git checkout <branch>

Switch to a different branch

git checkout -b <name>

Create and switch to a new branch in one step

git switch <branch>

Switch to a branch (modern alternative to checkout)

git switch -c <name>

Create and switch to a new branch (modern)

git branch -m <old> <new>

Rename a branch

git merge <branch>

Merge the specified branch into the current branch

git merge --no-ff <branch>

Merge with a merge commit (no fast-forward)

git merge --abort

Abort the current merge and restore the previous state

git cherry-pick <commit>

Apply changes from a specific commit onto the current branch

git rebase <branch>

Rebase the current branch onto the specified branch

git rebase -i <commit>

Interactive rebase to edit, squash, or reorder commits

git remote -v

List all remote repositories with their URLs

git remote add <name> <url>

Add a new remote repository

git remote remove <name>

Remove a remote repository

git fetch <remote>

Download objects and refs from a remote repository

git pull <remote> <branch>

Fetch and merge changes from a remote branch

git push <remote> <branch>

Push local commits to a remote repository

git push -u origin <branch>

Push and set upstream tracking for a new branch

git push --force

Force push, overwriting remote history (use with caution)

git stash

Temporarily store modified tracked files

git stash list

List all stashed changes

git stash pop

Apply the most recent stash and remove it from the stash list

git stash apply

Apply the most recent stash without removing it

git stash drop

Discard the most recent stash

git stash clear

Remove all stashed entries

git stash branch <name>

Create a branch from a stash

git log

Show the full commit history

git log --oneline

Show commit history in compact one-line format

git log --graph

Show commit history as an ASCII graph

git log -n <count>

Show only the last N commits

git log --author="<name>"

Filter commits by author

git log --since="<date>"

Show commits since a specific date

git shortlog

Show commit counts by author

git show <commit>

Show details of a specific commit

git blame <file>

Show who last modified each line of a file

git reset <file>

Unstage a file while keeping the changes

git reset --soft <commit>

Reset to a commit, keeping changes staged

git reset --mixed <commit>

Reset to a commit, keeping changes unstaged

git reset --hard <commit>

Reset to a commit, discarding all changes (dangerous)

git revert <commit>

Create a new commit that undoes a previous commit

git checkout -- <file>

Discard changes in a working directory file

git restore <file>

Restore a file in the working directory (modern)

git clean -fd

Remove untracked files and directories

git reflog

Show a log of where HEAD has been (recovery tool)

Git Cheat Sheet: Essential Commands and Best Practices

Getting Started with Git

Git is the most widely used version control system in the world, trusted by millions of developers to manage code changes, collaborate on projects, and maintain a complete history of their work. Created by Linus Torvalds in 2005 for the development of the Linux kernel, Git has become the de facto standard for version control in software development. Unlike earlier centralized version control systems, Git is distributed—every developer has a complete copy of the entire repository history on their local machine, enabling offline work, fast operations, and robust branching and merging capabilities.

Getting started with Git requires just a few steps. First, install Git on your system—it is available for Windows, macOS, and Linux. Then configure your identity, which Git attaches to every commit you make, using the git config command. With your identity set, you can either initialize a new repository with git init or clone an existing one with git clone. From there, the basic workflow is simple: make changes to your files, stage them with git add, commit them with git commit, and share them with git push. This cycle of edit, stage, commit, and push forms the backbone of daily Git usage.

Understanding Git's three main areas is fundamental to using it effectively. The working directory is where you edit your files. The staging area (also called the index) is where you prepare changes before committing—think of it as a preview of your next commit. The repository itself stores all your committed snapshots. Files move from the working directory to the staging area with git add, and from the staging area to the repository with git commit. This two-step process gives you fine-grained control over what goes into each commit, allowing you to create clean, logical units of change that tell a coherent story about how your project evolved.

Essential Git Commands

While Git has over 150 commands, most developers use only about 15 to 20 on a daily basis. The essential commands fall into four categories: snapshot creation, branching and merging, remote collaboration, and inspection. Snapshot commands include git status to see what has changed, git add to stage changes, git commit to record snapshots, and git diff to view changes. These form the core workflow that you will use dozens of times each day, and mastering them is the first step toward Git proficiency.

Branching commands are what make Git truly powerful. A branch is simply a lightweight, movable pointer to a commit. Creating branches in Git is instantaneous, and switching between them is nearly as fast. This encourages a workflow where you create branches freely for features, experiments, and bug fixes without worrying about the overhead. The git branch command manages branches, git checkout or git switch moves between them, and git merge integrates changes from one branch into another. Understanding how branches work—and how merge conflicts arise and are resolved—is central to effective Git usage.

Remote commands enable collaboration by connecting your local repository to shared repositories on platforms like GitHub, GitLab, and Bitbucket. The git remote command manages these connections, git fetch downloads changes without integrating them, git pull fetches and merges in one step, and git push uploads your commits. When working with a team, you will typically pull changes at the start of each work session, create a branch for your task, commit your changes, and push your branch for review through a pull request. This collaborative workflow, supported by these essential commands, is how modern software teams coordinate their work across time zones and continents.

Git Workflow Best Practices

Writing good commit messages is one of the most impactful Git practices. A well-written commit message explains what changed and why, providing invaluable context for anyone reading the project history—including your future self. The conventional format starts with a short summary line of 50 characters or less, written in the imperative mood ("Add feature" not "Added feature"). If more explanation is needed, leave a blank line and write a detailed body that explains the motivation for the change, any side effects, and references to related issues. This disciplined approach transforms your commit log from a cryptic timeline into a readable narrative of your project's evolution.

Branching strategies vary by team size and project complexity, but the principle of isolated development is universal. The feature branch workflow, where each new feature or bug fix is developed on its own branch and merged via pull request, is the most widely adopted approach. This keeps the main branch stable, enables code review, and makes it easy to track and revert changes. Git Flow is a more structured variant with dedicated branches for development, release, and hotfix, suited for projects with scheduled releases. Trunk-based development, where developers commit to the main branch frequently with feature flags, is favored by teams practicing continuous deployment.

Regular housekeeping keeps your repository healthy and your team productive. This includes deleting branches that have been merged, keeping your local repository up to date with regular fetches, and cleaning up untracked files. Avoid committing large binary files, sensitive data like passwords and API keys, or generated files that can be rebuilt from source—use a .gitignore file to exclude these. When you accidentally commit something you should not have, use git rm --cached to remove it from tracking without deleting the local file. Regular maintenance of your repository prevents it from becoming bloated and confusing, ensuring that Git remains a helpful tool rather than a source of frustration.

Advanced Git Techniques

Interactive rebase is one of Git's most powerful features for maintaining a clean project history. With git rebase -i, you can reorder commits, squash multiple commits into one, edit commit messages, and split commits into smaller units. This is particularly valuable before merging a feature branch into the main branch, as it allows you to present a clean, logical sequence of changes rather than the messy reality of how the code was actually written. However, interactive rebase rewrites history, so it should only be used on commits that have not yet been shared with others through push. The golden rule of rebasing is never rebase public commits.

Git stash is a lifesaver when you need to switch contexts mid-task. Suppose you are working on a feature and an urgent bug report comes in—you do not want to commit half-finished work, but you need a clean working directory to work on the bug. Git stash saves your current changes and reverts the working directory to the last commit, allowing you to switch branches and address the bug. When you are ready to resume your feature, git stash pop restores your saved changes. You can have multiple stashes, apply them selectively, and even create branches from stashes, making this a versatile tool for managing interrupted workflows.

The reflog is Git's safety net—a journal that records every time the tip of a branch is updated, including operations like commits, checkouts, merges, rebases, and resets. Even after a seemingly destructive operation like git reset --hard, the reflog still contains a reference to the previous state, allowing you to recover lost commits. This makes the reflog an essential recovery tool. To use it, run git reflog to see the history, identify the commit you want to return to, and use git reset --hard to move back to that point. Combined with git fsck for finding dangling objects, the reflog ensures that in Git, almost nothing is truly lost—a comforting fact for developers who occasionally make mistakes with forceful commands.