Git cheatsheet

Git is a decentralized version management software. With this cheatsheet, you will have quick access to the most common git commands.

Enable JavaScript to use the interactive tool. The guide below explains how it works.

Git Memo: A Comprehensive Guide to Essential Git Commands

1. What the Tool Does

Git Memo is a curated, high-density reference guide designed to provide developers with immediate access to the most critical Git commands. Rather than serving as a verbose tutorial, it acts as a "cheat sheet" or a memory aid for both foundational and advanced version control operations. The tool categorizes commands into logical groups—Configuration, Getting Started, Commits, Error Correction, and Miscellaneous—ensuring that even under the pressure of a fast-paced development cycle, the right syntax is only a glance away.

In essence, Git Memo bridges the gap between basic Git knowledge and the practical, daily reality of managing a codebase. It covers everything from setting up your identity to performing surgical "undo" operations on your commit history.

2. Why Someone Uses It

Git is notoriously powerful but also possesses a steep learning curve and a syntax that can occasionally be unintuitive. Even seasoned engineers often find themselves searching for the specific flags needed to amend a commit or hard-reset a branch to its remote state.

Key Reasons for Use:

3. Step-by-Step Instructions

Setting Up Your Environment

Before you can effectively use Git, you must identify yourself. This ensures that every commit you make is correctly attributed to you.

  1. Locate the Configuration section.
  2. Replace [name] with your actual name and [email] with your professional email address.
  3. Run the commands in your terminal.

Initializing a Project

  1. Navigate to your project folder in the terminal.
  2. Use git init to transform the directory into a Git repository.
  3. Alternatively, use git clone [url] to pull down an existing project from a remote host like GitHub or GitLab.

Managing Commits

  1. Once you have made changes, use git commit -am "[message]" to stage and commit all tracked changes in one go.
  2. If you realize you forgot a small detail right after committing, use git commit --amend --no-edit to "tuck" those changes into the previous commit.

Fixing Mistakes

  1. Wrong Message? Use git commit --amend to open your editor and change the last commit's description.
  2. Wrong Code? Use git reset HEAD~1 to keep your files but remove the commit record, allowing you to fix the code and re-commit.
  3. Total Disaster? Use git reset --hard origin/[branch-name] to wipe your local changes and match the remote server exactly.

4. Examples

Scenario A: Starting a New Feature

# Initialize the repo
git init

# Set identity for this specific repo (if different from global)
git config user.name "John Doe"
git config user.email "john@example.com"

# First commit
git add .
git commit -m "Initial commit"

Scenario B: Amending a Commit

Imagine you committed your code but forgot to remove a console.log.

# Remove the log in your editor
# ... edit file ...

# Add the change
git add .

# Merge into the last commit without changing the message
git commit --amend --no-edit

Scenario C: Moving from 'master' to 'main'

Many modern repositories use main as the default branch.

git branch -m master main

5. FAQs

Q: What is the difference between git reset --soft and git reset --hard? A: --soft (or the default, which is similar to what HEAD~1 does) keeps your work in the staging area. --hard completely deletes your local changes. Use --hard with extreme caution.

Q: Does git commit -am add new files? A: No. The -a flag only stages files that Git is already tracking. For brand-new files, you must use git add [filename] first.

Q: How do I undo a git reset --hard? A: It's difficult. If you haven't run a garbage collection, you might find the lost commit using git reflog.

Q: Can I use Git Memo for GUI applications? A: Git Memo focuses on the CLI (Command Line Interface). However, understanding these commands will help you understand what your GUI (like GitKraken or Sourcetree) is doing under the hood.

6. Common Mistakes

  1. Forgetting to Set Global Config: Committing without a name/email often results in "Author Unknown" or uses your system's default username, which is unprofessional in a shared repo.
  2. Abusing --hard Reset: Using --hard when you actually meant to keep your changes. Always try a standard reset first.
  3. Amending Pushed Commits: Never use git commit --amend on a commit that has already been pushed to a shared remote. This rewrites history and will cause conflicts for your teammates.
  4. Committing Large Binaries: Forgetting to set up a .gitignore before running git init and adding all files.
  5. Vague Commit Messages: Using messages like "fix" or "updates". While Git Memo shows the how, the what (your message) should be descriptive.

7. Use Cases

The Freelancer

Working on multiple projects for different clients? Use the configuration section to ensure your identity matches the client's expectations for each repository.

The Open Source Contributor

When contributing to a project, you'll often need to clone a repo, make changes, and perhaps amend your commits based on code review feedback. Git Memo provides the exact syntax for these frequent operations.

The "Oops" Moment

We've all done it: committed a secret key or a typo. The "I’ve made a mistake" section is the most visited part of the memo, providing the path to a clean history.

The Repository Migration

Moving an old project to a new host? Use the "Miscellaneous" section to rename branches or the "Getting Started" section to re-initialize your workflow.

8. Related Tools

All tools

Guides and articles