Back to Home

Git Commands Cheat Sheet: Essential Git Commands for Beginners

May 19, 2025
13 min read

Man, ever feel like you’re drowning in code changes? Git is basically your own personal time machine. No joke. Doesn’t matter if you’re just tinkering on solo projects or you’re part of some huge team scattered everywhere, getting a handle on Git will seriously change how you work. This is my rundown of the essential commands to get you tracking, managing, and yeah, protecting your code without pulling your hair out.

So, Why Bother with Git? (Spoiler: It’s a Lifesaver)

Seriously, have you ever deleted a massive chunk of code and instantly wished you could just hit “undo” on life? Or how about trying to work with other people and you’re constantly overwriting each other’s genius? Git totally fixes that crap, and more.

It’s not just some fancy tool; it’s like a safety net for your code. You get:

  • Version control, so every tiny change is tracked.
  • A way to actually collaborate without chaos.
  • Protection! So you don’t lose your work or break everything beyond repair.
  • Freedom to try out wild ideas without nuking your main project.

So, this guide? It’s just the essential Git commands I think will immediately make your coding life a bit smoother. No fluff, promise.

⚙️ First Things First: Getting Git on Your Machine

Okay, before we dive into the commands themselves, you gotta make sure Git is actually installed.

How to Install It

Depends on what you’re running:

Windows:
• Head over to git-scm.com and grab the installer.
• Run that thing. Usually, just clicking “next” through the default options is fine.

macOS:
• If you use Homebrew (you probably should), just pop open a terminal and type: brew install git
• Or, you can download it from git-scm.com too.

Linux:
• For Debian/Ubuntu flavors: sudo apt-get install git
• If you’re on Fedora: sudo dnf install git

Tell Git Who You Are

Alright, once it’s installed, you need to introduce yourself to Git. This name and email will be stamped on all your “commits” (aka saves).

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Oh, quick tip: that --global flag means these settings apply to all your Git projects. If you need different info for a specific project (like a work email vs. personal), just run these commands inside that project’s folder and leave out the --global part.

Okay, Git knows your name. Let’s actually start using it!

📦 Starting a New Project or Grabbing an Existing One

So, there are pretty much two ways you’ll kick things off with Git: either you’re starting a project from scratch, or you’re jumping into one that’s already out there.

Making a Brand New Repository

If you’re starting a fresh project:

# Go to your project's folder first!
cd my-awesome-project

# Tell Git to start tracking this folder
git init

This command makes a little hidden folder called .git. That’s where Git keeps all its tracking info for your project. Kinda magical.

Cloning Something That Already Exists

More often, you might be working on a project that’s already set up, probably on a site like GitHub.

# This copies a remote repository down to your computer
git clone https://github.com/username/repository-name.git

# You can also tell it what to name the folder it creates
git clone https://github.com/username/repository-name.git my-folder

Yeah, you’ll be cloning stuff a LOT if you want to contribute to open-source projects, or just when you need to get a copy of a project that’s already on GitHub, GitLab, Bitbucket, you name it.

🔁 Your Daily Git Grind: The Commands You’ll Use All the Time

These are the commands that’ll become muscle memory pretty quick.

What’s the Status?

First thing I almost always type is:

git status

This tells you what Git sees:

  • Which files it’s tracking (or not tracking).
  • Which files have changes it knows about.
  • What “branch” you’re currently on (more on branches later, they’re super important).

Getting Changes Ready (Staging)

Before you officially “save” your work (which Git calls a “commit”), you have to tell Git which changes you want to save. This is called “staging.” Think of it like putting items in a box before you seal it up to mail.

# Stage one specific file
git add filename.js

# Stage a few files at once
git add file1.js file2.css

# Stage ALL changed files in the current directory and subdirectories
git add .

# Stage parts of files, interactively (this one's pretty cool but takes getting used to)
git add -p

That git add . is super common, but just be aware it adds everything that’s changed. Sometimes you might have test files or notes you don’t want to commit yet.

Saving Your Work (Committing)

Once you’ve staged your changes, it’s time to commit them. And always, always write a good commit message!

# This is the standard way
git commit -m "Add cool new login functionality"

# A shortcut: stage all *tracked* files AND commit in one go
git commit -am "Fix that annoying navigation bug on mobile"

A few words on commit messages, ’cause they matter:

  • Write clear messages that actually describe what you did. Future you will thank you.
  • Use the present tense, like “Add feature” not “Added feature.” It’s a common convention.
  • Keep ’em short but informative. No need for a novel.
  • If your commit relates to a ticket or issue (like in JIRA or GitHub Issues), include the number! E.g., “Fix login redirect (APP-123)”.

Looking at the Past (History)

Wanna see what’s been done in the project?

# Shows the whole commit history
git log

# My favorite for a quick overview (one line per commit)
git log --oneline

# Shows which files were changed in each commit
git log --stat

# Draws a cool little text graph of branches, also with one-line summaries
git log --graph --oneline --all

🌿 Playing with Branches

Branches are a HUGE deal. They let you work on new features, bug fixes, or just experiment without messing up your main, stable codebase. Think of them like parallel universes for your project.

Managing Your Branches

# See all your branches (the one with a * is your current one)
git branch

# Make a new branch
git branch feature-login

# Switch to an existing branch
git checkout feature-login

# Make a new branch AND switch to it in one command (super handy!)
git checkout -b new-shiny-feature

# Delete a branch (usually after you've merged its changes)
git branch -d branch-to-delete

# Force delete a branch (use this if you're sure, even if it's not merged)
git branch -D stubborn-branch-to-delete

Bringing Branches Together (Merging)

Okay, so you built your amazing new feature on its own branch. Now it’s time to merge it back into your main branch (often called main or master).

# First, switch to the branch you want to merge INTO
git checkout main

# Now, merge your feature branch into the current branch (main)
git merge feature-login

# You can also give the merge its own special commit message
git merge feature-login -m "Merging the awesome new login system"

Ugh, Merge Conflicts…
They happen. It’s when Git can’t automatically figure out how to combine changes because you (or someone else) edited the same lines of code in different ways on different branches. Git will freak out a bit and tell you there are conflicts. Here’s the gist of fixing them:

  1. Git will plonk these weird <<<<<<<, =======, and >>>>>>> markers into the conflicted files. These show you the different versions.
  2. You have to open those files, look at the conflicting sections, and manually edit the file to be correct, removing Git’s markers.
  3. Once you’ve fixed a file, you git add it to tell Git it’s resolved.
  4. After all conflicts are resolved and files added, you finish the merge with a git commit.

🌐 Working with a Team (Remote Repositories)

Most of the time, your code doesn’t just live on your computer. It lives on a “remote” server, like GitHub, GitLab, or Bitbucket. This is how you share code and collaborate.

Managing Your Remotes

# See which remotes your project is connected to
git remote -v

# Add a new remote connection (often named 'origin' by default)
git remote add origin https://github.com/yourusername/your-repo.git

# If the URL of your remote changes
git remote set-url origin https://github.com/yourusername/new-repo-url.git

# Remove a remote connection
git remote remove origin

origin is just a common nickname for your main remote. You could call it github or fred if you wanted, but origin is standard.

Keeping in Sync with Remotes

# Download changes from a remote, but DON'T integrate them yet
git fetch origin

# Download changes from 'origin's 'main' branch AND try to merge them into your current branch
git pull origin main

# Upload your local commits on 'main' to the 'origin' remote
git push origin main

# Useful for the first time you push a new local branch to a remote; sets it up to track
git push -u origin main

Okay, what’s the deal with fetch vs. pull? This one trips people up sometimes.

  • fetch (like git fetch origin) just downloads all the new stuff from the remote (e.g., origin). It doesn’t change any of your local files or your current work. It’s safe; it just updates your local Git’s knowledge of what’s on the remote.
  • pull (like git pull origin main) is basically a git fetch followed immediately by a git merge of the remote branch (e.g., origin/main) into your current local branch.
  • When in doubt, I often git fetch first to see what’s new, then decide if/how I want to merge (e.g., git merge origin/main or maybe rebase).

📂 Messing with Files and Folders (Git Style)

Git pays attention to your files, but you sometimes need to tell it specific things.

File Stuff

# See differences between your working files and what's staged
git diff

# See differences between what's staged and your last commit
git diff --staged

# See differences between two specific commits
git diff commit1-hash..commit2-hash

# Remove a file from Git's tracking AND delete it from your computer
git rm filename.txt

# Stop tracking a file but KEEP it on your computer (e.g., if you accidentally added a config file)
git rm --cached filename.txt

Directory Stuff

Git is a bit weird about empty directories – it doesn’t really track them. So if you want an empty directory in your repo, a common trick is to put an empty placeholder file in it, often called .gitkeep.

touch my-empty-directory/.gitkeep
git add my-empty-directory/.gitkeep

And for files or whole directories you never want Git to track (like node_modules/ or log files), you create a file named .gitignore in your project’s root and list patterns in there. Super important.

# Example .gitignore content
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore

⏪ Whoops! Undoing Mistakes

Everyone makes mistakes. It’s coding! The good news is Git is pretty awesome at helping you backtrack.

For Changes You Haven’t Committed Yet

# Throw away changes you made to a specific file since the last commit
git checkout -- filename.js

# Throw away ALL unstaged changes in your current directory (Careful!)
git restore .
# or for older Git versions:
git checkout -- .

# If you staged a file by mistake (git add filename.js) but want to unstage it (keep the changes in the file though)
git restore --staged filename.js
# or for older Git versions:
git reset HEAD filename.js

For Changes You Already Committed

# Oops, typo in my last commit message, or I forgot to add a file! This rewrites the VERY LAST commit.
git commit --amend -m "My new, better commit message"

# To undo a specific past commit by creating a NEW commit that does the opposite. Safe for shared history.
git revert 

# Resetting to a previous state. This can rewrite history, so be CAREFUL if you've shared these commits!
git reset --soft HEAD~1    # Undoes the last commit, but keeps all its changes staged and ready to be re-committed.
git reset HEAD~1           # Undoes the last commit, unstages its changes, but the changes are still in your working files. (This is --mixed, the default)
git reset --hard HEAD~1    # DANGER! Undoes the last commit AND THROWS AWAY all its changes from your working files. They're GONE.

Seriously, BE SUPER CAREFUL with git reset --hard! It permanently deletes those changes from your working directory. If you haven’t pushed them or stashed them, they could be gone for good. Don’t say I didn’t warn ya.

Need to Switch Tasks Quickly? Stash It!

Ever been deep in coding some feature, and BAM! Urgent bug fix needed on another branch? Your current work is a mess, definitely not ready to commit. git stash is your lifesaver here.

# Save your current uncommitted changes (both staged and unstaged) to a temporary holding area
git stash

# See a list of all your stashes
git stash list

# Re-apply the changes from the most recent stash (the stash itself remains)
git stash apply

# Re-apply changes from a specific stash (e.g., the 2nd one in the list, which is stash@{1})
git stash apply stash@{1} # Stashes are numbered like stash@{0}, stash@{1}, etc.

# Re-apply the most recent stash AND remove it from the stash list
git stash pop

# Just delete a specific stash if you don't need it anymore
git stash drop stash@{1}

🧠 Ok, What Else? Beyond These Basics…

Phew! That’s a fair few commands, but honestly, those are the ones you’ll probably be using 90% of the time. Get comfortable with these, and you can handle most everyday Git scenarios. But don’t stop here if you’re curious!

  • Practice! Seriously, the best way to learn is to do. Make a dummy project and just mess around. Break things, then figure out how to fix them.
  • Look at other projects. Go on GitHub, find a project, and look at its git log. You can learn a lot by seeing how others structure their commits and branches.
  • Use Git every day. Even for tiny personal projects. The more you use it, the more natural it becomes.
  • Don’t be scared of mistakes. Git is designed to help you recover. Almost everything is undoable if you know how.’
  • Git Documentation

What to Learn Next?

If you’re feeling adventurous and want to level up your Git game, maybe check out:

  • Interactive rebasing (git rebase -i): For cleaning up your commit history before you share it. Super powerful.
  • Git hooks: Little scripts that can automatically run at certain points in the Git workflow (e.g., before a commit).
  • Git workflows: Different strategies for how teams use branches, like “Git Flow” or “GitHub Flow.”
  • git bisect: This is like black magic for finding exactly which commit introduced a bug.

So, which Git command are you gonna try out first? Keep this list handy. Hope it helps you out! Happy coding!


P.S. If this guide actually made sense and helped you, maybe pass it along to your developer friends? Especially the ones who are still emailing .zip files of their code (we all know one!).

We'd Love to Hear From You!

If you have any feedback, spotted an error, have a question, need something specific, or just want to get in touch; feel free to reach out. Your thoughts help us improve and grow! Contact Us