Comprehensive Documentation

Git & GitHub
Complete Reference Guide

Everything you need to master version control — from your very first commit to advanced team workflows. Designed for beginners looking to learn and professionals who need a quick reference.

10 Categories
81+ Commands
Copy & Use Instantly

Getting Started

Brand new to Git? Start here. These commands will help you install, configure, and understand the basics of version control.

What is Git?

Git is the world's most popular version control system. It lets you track every change you make to your code, collaborate with other developers, and safely experiment with new ideas without breaking your project. Think of it as an "undo button" for your entire codebase.

Pro TipGit is installed on most systems. Run the command below to verify.
Check your installed Git version
$git --version

First-Time Configuration

Before you start using Git, you need to tell it who you are. Your name and email will be attached to every commit (save point) you create. This is how your teammates will know who made each change.

Pro TipYou only need to do this once. The --global flag means these settings apply to all your projects.
Set your display name for commits
$git config --global user.name "Your Name"
Set your email for commits
$git config --global user.email "you@example.com"
Set 'main' as the default branch name
$git config --global init.defaultBranch main
Review all your current settings
$git config --list

Creating Projects

Every project starts somewhere. Learn how to create a brand new repository or download an existing one.

Initialize a New Repository

When you start a new project from scratch, you need to initialize a Git repository. This creates a hidden .git folder that tracks all your changes from this point forward.

Pro TipRun this inside your project folder. It won't affect any existing files.
Create a new Git repository in current folder
$git init
Create a new project folder with Git already set up
$git init my-project

Clone an Existing Repository

If a project already exists on GitHub (or another remote server), you can download the entire project — including its full history — using the clone command.

Pro TipThis is the most common way to start working on a team project.
Download a full copy of a remote repository
$git clone https://github.com/user/repo.git
Clone into a specific folder name
$git clone https://github.com/user/repo.git my-folder
Clone only the latest version (faster)
$git clone --depth 1 https://github.com/user/repo.git

Day-to-Day Workflow

These are the commands you will type every single day. Master them and Git becomes effortless.

Checking the Status

Before you do anything in Git, it's a good habit to check your status. This tells you which files have been modified, which are staged (ready to commit), and which are untracked (new files Git doesn't know about yet).

See a summary of all changes in your project
$git status
Show a compact one-line-per-file summary
$git status -s

Staging Changes

Before committing, you need to 'stage' your changes. Staging means selecting exactly which changes you want to include in your next save point. This gives you fine-grained control over your project history.

Pro TipThink of staging as putting items in a shopping cart before checking out.
Stage a specific file
$git add index.html
Stage an entire folder
$git add src/
Stage everything that has changed
$git add .
Interactively choose specific lines to stage
$git add -p

Committing Changes

A commit is a permanent save point in your project's history. Every commit has a unique ID and a message describing what changed. Good commit messages make your project's history easy to understand.

Pro TipWrite your commit messages in the imperative mood: 'Add feature' not 'Added feature'.
Create a commit with a message
$git commit -m "Add user login page"
Stage all tracked files and commit in one step
$git commit -am "Fix header styling"
Edit the message or content of your last commit
$git commit --amend

Viewing History

Your project's history is a powerful record of every change ever made. You can browse it, search it, and even travel back in time to any point.

View the full commit history
$git log
Show each commit as a single line (compact)
$git log --oneline
Beautiful visual graph of all branches
$git log --oneline --graph --all
View the full details of a specific commit
$git show abc1234
Show unstaged changes line by line
$git diff
Show staged changes that are ready to commit
$git diff --staged

Branching & Merging

Branches let you work on new features, bug fixes, or experiments in isolation — without affecting the main codebase.

Working with Branches

A branch is like a parallel universe for your code. You can create a branch, make changes, and merge them back when you're ready. The main branch stays safe while you experiment freely.

Pro TipCreate a branch for every new feature or bug fix. This is called 'feature branching'.
List all local branches
$git branch
List all branches including remote ones
$git branch -a
Create a new branch called 'feature/navbar'
$git branch feature/navbar
Switch to an existing branch
$git checkout feature/navbar
Create AND switch to a new branch in one step
$git checkout -b feature/navbar
Modern way to switch branches (Git 2.23+)
$git switch feature/navbar
Delete a branch after it's been merged
$git branch -d feature/navbar

Merging Branches

Once your work on a branch is complete, you merge it back into the main branch. Git intelligently combines the changes. If two people edited the same line, Git will ask you to resolve the conflict manually.

Pro TipAlways pull the latest changes from main before merging to avoid conflicts.
First, switch to the branch you want to merge INTO
$git checkout main
Merge the feature branch into main
$git merge feature/navbar
Cancel a merge if there are too many conflicts
$git merge --abort

Remote & Collaboration

Git becomes truly powerful when you connect it to a remote server like GitHub. These commands let you share your work and collaborate with others.

Connecting to Remote

A 'remote' is a version of your project hosted on the internet (like GitHub). When you clone a repository, the remote is set up automatically. For projects you initialized locally, you need to add it manually.

View your current remote connections
$git remote -v
Connect your local project to GitHub
$git remote add origin https://github.com/user/repo.git
Disconnect from a remote
$git remote remove origin

Pushing & Pulling

Push sends your local commits to the remote server so others can see your work. Pull downloads the latest changes from the remote and merges them into your local branch.

Pro TipAlways pull before you push to avoid conflicts with your teammate's work.
Upload your commits to the remote main branch
$git push origin main
Push a new branch to remote for the first time
$git push -u origin feature/login
Download and merge remote changes into your branch
$git pull origin main
Download remote changes without merging (safer)
$git fetch origin

Undoing Mistakes

Made a mistake? Don't panic. Git has powerful tools to undo almost anything, from a simple typo to an entire commit.

Unstaging & Discarding

If you accidentally staged a file or want to throw away recent changes, these commands can help you go back without losing your commit history.

Warninggit checkout -- <file> permanently discards changes. There is no undo for this.
Unstage a file (keep the changes in your editor)
$git reset HEAD index.html
Discard all changes to a specific file
$git checkout -- index.html
Modern way to discard changes (Git 2.23+)
$git restore index.html
Modern way to unstage a file
$git restore --staged index.html

Reverting & Resetting Commits

If you need to undo an entire commit, you have two options: revert (safe — creates a new commit that undoes the changes) or reset (destructive — rewrites history).

WarningNever use git reset --hard on commits that have already been pushed to a shared branch.
Safely undo a commit by creating a new reversal commit
$git revert abc1234
Undo last commit but keep changes staged
$git reset --soft HEAD~1
Undo last commit and unstage the changes
$git reset --mixed HEAD~1
Completely erase the last commit and all its changes
$git reset --hard HEAD~1

Stashing

Need to quickly switch branches but have uncommitted work? Stash lets you save your current changes temporarily and come back to them later.

Using the Stash

The stash is like a clipboard for your code. It saves your current work-in-progress and gives you a clean working directory. When you're ready, you can pop the changes right back.

Pro TipStashing is perfect for when you need to switch branches to fix an urgent bug.
Save all uncommitted changes to the stash
$git stash
Stash with a descriptive message
$git stash save "login page work-in-progress"
See all your saved stashes
$git stash list
Apply the latest stash and remove it from the list
$git stash pop
Apply a specific stash without removing it
$git stash apply stash@{1}
Delete a specific stash
$git stash drop stash@{0}
Delete ALL stashes (use with caution)
$git stash clear

Advanced Techniques

Ready to level up? These advanced commands give you surgical precision over your commit history and workflow.

Interactive Rebase

Rebase lets you rewrite your commit history. You can squash multiple messy commits into one clean commit, reorder them, rename them, or even delete specific commits entirely.

WarningNever rebase commits that have been pushed to a shared branch — it rewrites history.
Interactively edit the last 5 commits
$git rebase -i HEAD~5
Move your branch's commits on top of the latest main
$git rebase main
Cancel a rebase if something goes wrong
$git rebase --abort

Cherry-Pick

Cherry-pick lets you copy a specific commit from one branch and apply it to another. This is useful when you only need one particular change, not an entire branch merge.

Apply a specific commit to your current branch
$git cherry-pick abc1234
Apply changes without auto-committing
$git cherry-pick abc1234 --no-commit

Tagging Releases

Tags mark important points in your project's history, like version releases. Unlike branches, tags don't change — they're permanent bookmarks.

Create a lightweight tag
$git tag v1.0.0
Create an annotated tag with a message
$git tag -a v1.0.0 -m "First stable release"
List all tags
$git tag
Push a tag to the remote server
$git push origin v1.0.0
Push all tags at once
$git push origin --tags

Inspection & Debugging

When things go wrong, these commands help you investigate, compare, and find exactly where a bug was introduced.

Comparing Changes

The diff command shows you exactly what has changed, line by line. This is essential for code reviews and understanding what happened between two points in your history.

Compare the last 3 commits
$git diff HEAD~3..HEAD
Compare two branches
$git diff main..feature/login
Show a summary of changed files and lines
$git diff --stat

Blame & Bisect

Who changed this line? When was a bug introduced? Git's blame and bisect tools help you answer these detective questions.

Show who last modified each line of a file
$git blame index.html
Start a binary search to find the commit that introduced a bug
$git bisect start
Mark a commit as 'good' (bug-free)
$git bisect good abc1234
Mark the current commit as 'bad' (has the bug)
$git bisect bad

Cleanup & Maintenance

Keep your repository clean and organized with these housekeeping commands.

Cleaning Up

Over time, your working directory can accumulate untracked files (build artifacts, temp files, etc.). The clean command helps you remove them.

Warninggit clean is destructive. Always use -n (dry run) first to preview what will be deleted.
Preview which untracked files would be deleted
$git clean -n
Delete untracked files and directories
$git clean -fd
Run garbage collection to optimize the repository
$git gc

Using .gitignore

The .gitignore file tells Git which files and folders to completely ignore. This is essential for keeping sensitive data, build artifacts, and dependencies out of your repository.

Pro TipCreate a .gitignore file before your first commit. Visit gitignore.io for templates.
Ignore the node_modules folder
$node_modules/
Ignore environment variable files
$.env
Ignore all .log files
$*.log
Ignore build output folders
$dist/