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.