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:
- Efficiency: Searching through official documentation or Stack Overflow takes time. Git Memo provides the most "reached-for" commands in one place.
- Accuracy: Mistakes in Git can lead to lost work or "detached HEAD" states. Having the exact syntax reduces the risk of typos in critical commands.
- Mental Offloading: Developers can focus on architecture and logic rather than memorizing every niche Git flag.
- Onboarding: It serves as an excellent resource for junior developers or those switching from other version control systems (like SVN or Mercurial).
- Disaster Recovery: When a mistake happens (e.g., committing to the wrong branch), Git Memo provides the safe "undo" paths to rectify the situation without Panic.
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.
- Locate the Configuration section.
- Replace
[name]with your actual name and[email]with your professional email address. - Run the commands in your terminal.
Initializing a Project
- Navigate to your project folder in the terminal.
- Use
git initto transform the directory into a Git repository. - Alternatively, use
git clone [url]to pull down an existing project from a remote host like GitHub or GitLab.
Managing Commits
- Once you have made changes, use
git commit -am "[message]"to stage and commit all tracked changes in one go. - If you realize you forgot a small detail right after committing, use
git commit --amend --no-editto "tuck" those changes into the previous commit.
Fixing Mistakes
- Wrong Message? Use
git commit --amendto open your editor and change the last commit's description. - Wrong Code? Use
git reset HEAD~1to keep your files but remove the commit record, allowing you to fix the code and re-commit. - 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
- 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.
- Abusing
--hardReset: Using--hardwhen you actually meant to keep your changes. Always try a standard reset first. - Amending Pushed Commits: Never use
git commit --amendon a commit that has already been pushed to a shared remote. This rewrites history and will cause conflicts for your teammates. - Committing Large Binaries: Forgetting to set up a
.gitignorebefore runninggit initand adding all files. - 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
- Regex Memo: Often, developers need to search through Git logs or files using complex patterns. The Regex Memo tool in this suite is a perfect companion.
- Diff Tools: While Git Memo helps you commit, a Diff tool helps you see what you are committing before you pull the trigger.
- Gitignore Generator: A vital companion for new repositories to prevent sensitive or unnecessary files from being tracked.
- GitHub/GitLab/Bitbucket: The remote platforms where you will likely
pushthe commits you manage using these commands. - SSH Key Generator: Essential for secure, password-less communication with your Git remotes.