Knowledge Base > Development & Code > Git from the Command Line

Git from the Command Line [Part 4 of 7]

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. We cover team workflows in Part 7.


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.


Git Configuration

git config

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

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 and 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 and 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 and 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 and 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

Command Purpose
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