All articles
4 min read

A Comprehensive Guide to Basic Git Usage

A practical Git reference covering setup, commits, branches, remotes, stash, rebase, cherry-pick, and the most important commands for everyday development.

Git is the standard distributed version control system. This guide covers the commands and workflows used in real projects — from initial setup through branching, rebasing, and recovering from mistakes.

Installation and Configuration

sudo apt install git   # Ubuntu/Debian
brew install git        # macOS

git --version

Configure your identity (stored in ~/.gitconfig):

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "nano"    # default editor
git config --global color.ui auto

Creating and Cloning Repositories

# Initialize a new repo
git init
git init my-project   # creates a new directory

# Clone an existing repo
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git    # via SSH
git clone https://github.com/user/repo.git local-name   # custom directory name

Staging and Committing

git status                   # show working tree status
git add file.txt             # stage a file
git add .                    # stage all changes
git add -p                   # interactively stage hunks

git commit -m "Add feature X"
git commit -am "Fix bug"     # stage all tracked files and commit

git diff                     # changes not yet staged
git diff --staged            # changes staged for commit

Working with Branches

git branch                   # list local branches
git branch -a                # list all branches (including remote)
git branch feature-login     # create a branch
git checkout feature-login   # switch to a branch
git checkout -b feature-login   # create and switch in one step
git switch -c feature-login     # modern syntax (Git 2.23+)

git merge feature-login      # merge into current branch
git branch -d feature-login  # delete merged branch
git branch -D feature-login  # force delete unmerged branch

Viewing History

git log                      # full history
git log --oneline            # compact view
git log --oneline --graph    # with branch graph
git log --oneline -10        # last 10 commits
git log --follow file.txt    # history of a specific file

git show abc1234             # show a specific commit
git diff main..feature       # compare two branches

Stashing Work in Progress

Stash lets you save uncommitted work temporarily and come back to it later:

git stash                    # stash current changes
git stash push -m "WIP: login form"  # with a label

git stash list               # see all stashes
git stash pop                # apply latest stash and remove it
git stash apply stash@{1}   # apply a specific stash (keep it in list)
git stash drop stash@{0}    # delete a specific stash
git stash clear              # delete all stashes

Remote Repositories

git remote -v                # list remotes
git remote add origin https://github.com/user/repo.git

git fetch                    # download remote changes without merging
git pull                     # fetch + merge
git pull --rebase            # fetch + rebase (cleaner history)

git push origin main
git push -u origin main      # set upstream (first push)
git push --force-with-lease  # force push safely (fails if remote changed)

Rebasing

Rebase rewrites commit history by replaying commits on top of another branch — useful for keeping a clean, linear history:

# Rebase current branch onto main
git rebase main

# Interactive rebase: squash, reorder, edit last 5 commits
git rebase -i HEAD~5

In the interactive editor, common actions:

  • pick — keep the commit
  • squash / s — merge into previous commit
  • reword / r — keep the commit but edit its message
  • drop / d — discard the commit

Don't rebase commits that have been pushed to a shared remote branch. Rebase is for local history cleanup before sharing.

Cherry-Pick

Apply a specific commit from another branch:

git cherry-pick abc1234         # apply one commit
git cherry-pick abc1234..def567  # apply a range of commits

Useful for backporting a bug fix to an older release branch.

Undoing Changes

# Unstage a file
git restore --staged file.txt

# Discard working tree changes
git restore file.txt

# Undo the last commit (keep changes staged)
git reset --soft HEAD~1

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

# Undo the last commit (discard changes — destructive)
git reset --hard HEAD~1

# Safely undo a commit that's already been pushed (creates a new commit)
git revert abc1234

The Reflog (Recovery Tool)

git reflog records every change to HEAD — a lifeline if you accidentally delete a branch or reset too far:

git reflog             # show recent HEAD movements
git reset --hard HEAD@{3}   # recover to a specific point
git checkout -b recovered HEAD@{2}   # recover a branch

The .gitignore File

Create .gitignore at the project root to exclude files from tracking:

# Dependencies
node_modules/
venv/

# Build output
dist/
*.pyc
__pycache__/

# Environment files
.env
.env.local

# OS files
.DS_Store
Thumbs.db

Find templates for common languages at github.com/github/gitignore.

Useful Aliases

Add to ~/.gitconfig:

[alias]
    st = status
    co = checkout
    lg = log --oneline --graph --decorate
    undo = reset HEAD~1

Glossary

| Term | Meaning | |---|---| | Repository | A directory tracked by Git | | Commit | A snapshot of the project at a point in time | | Branch | A moveable pointer to a commit | | HEAD | The currently checked-out commit | | Remote | A copy of the repository on another machine | | Staging area | Where changes are prepared before committing | | Merge | Combine branch history | | Rebase | Replay commits on top of another branch | | Stash | Temporary storage for uncommitted changes |