Development & Code

Git from the Command Line: The Real Way to Learn It

A straight-talking walkthrough of Git’s most essential CLI commands, what they do, and when to use them.

Note: This guide is written for solo developers working independently with Git. If you’re working in a team or contributing to shared repositories, some practices may differ. A follow-up guide on team workflows, conflict handling, and collaboration best practices is coming soon.


You’ve clicked the buttons, dragged the files, maybe even used GitHub Desktop, but if you want real power and speed, it’s time to get comfortable in the command line. This guide breaks down Git’s most essential commands, how to use them, and what they actually mean.


Why the Command Line Still Matters

  • It’s faster once you learn it
  • It works in any dev environment (even headless servers)
  • It’s what pros, automation, and DevOps tools rely on

Let’s break down the commands you’ll use every day, with clear definitions and real examples.


Before You Start: Install Git

Git does not come with every computer. On Windows it is almost never there by default, which is why a git command can fail before you even begin. Install it first, then everything else in this guide works the same way.

Check what you have. Open a terminal and run:

git --version

If you see a version number, you are ready, skip to Git Configuration. If you get “command not found” or “git is not recognized”, install it for your system below.

Windows

Windows does not include Git. Download the installer from git-scm.com and run it. The default options are fine for most people. It also installs Git Bash, a terminal where every command in this guide works. When it is done, open Git Bash or Windows Terminal and run git --version to confirm.

macOS

Many Macs already have Git. Run git --version. If it is missing, macOS offers to install the Xcode Command Line Tools, which include Git, just click install. If you use Homebrew, brew install git works too. The built-in Terminal app is all you need.

Linux

Install Git with your package manager:

# Debian or Ubuntu
sudo apt install git

# Fedora
sudo dnf install git

# Arch
sudo pacman -S git

Then confirm with git --version.

One thing to know

Once Git is installed, every command in this guide is the same on Windows, macOS and Linux. The only difference is how you open a terminal, and on Windows you will usually use Git Bash or Windows Terminal rather than the old Command Prompt.


Git Configuration

git config

What it does: Sets configuration options like your name, email, editor, and aliases.

Examples:

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

Use --global to apply it everywhere, or leave it off for per-repo settings.


Creating & Cloning Repositories

git init

Initializes a new Git repository in the current folder.

git init

Creates a hidden .git folder that starts tracking changes.


git clone

Copies an existing Git repo to your local system.

git clone https://github.com/user/repo.git

Checking Status & Adding Files

git status

Shows the state of your working directory and staging area.

git status

See what’s changed, what’s staged, and what’s untracked.


git add

Stages files for the next commit.

git add file.txt       # Add one file
git add .              # Add everything in the folder

Committing Changes

git commit

Saves a snapshot of the staged changes.

git commit -m "Your message here"

This is what creates real history. Don’t forget the -m for your message.


Branching & Switching

git branch

Lists, creates, or deletes branches.

git branch           # List branches
git branch feature1  # Create a branch

git switch / git checkout

Switches to another branch.

git switch main

Prefer switch over the older checkout for clarity.


git merge

Combines another branch into your current one.

git merge feature1

Fast-forwards if possible; otherwise creates a merge commit.


Pushing & Pulling

git push

Uploads your commits to the remote repo.

git push origin main

git pull

Fetches and integrates changes from the remote.

git pull

git fetch

Downloads commits but doesn’t apply them.

git fetch

Useful for previewing changes before merging.


Fixing Mistakes

git reset

Unstages or rewinds commits. Be careful!

git reset HEAD~1       # Undo last commit (keep changes)

git restore

Restores file content from a commit or discards local changes.

git restore file.txt

git revert

Creates a new commit that undoes a previous one.

git revert <commit-id>

git stash

Temporarily saves changes without committing.

git stash
git stash pop

Viewing History

git log

Shows commit history.

git log --oneline

git diff

Compares changes.

git diff

git show

Shows details of a commit.

git show <commit-id>

TL;DR Cheat Sheet

CommandPurpose
git initStart a new repo
git cloneCopy a remote repo
git statusCheck changes
git addStage files
git commitSave a snapshot
git pushUpload commits to remote
git pullFetch and merge remote changes
git branchView/create branches
git switchMove between branches
git mergeCombine branches
git logView history
git diffCompare file changes
git stashHide local changes temporarily

Your Turn

Still using Git through buttons and GUIs? No shame, but if you’ve got a favorite CLI trick, alias, or “I broke everything and fixed it with one command” story, drop it in the thread.

Let’s build a proper command-line Git cheatbook, together.


// comments

← all posts more in Development & Code →