DevOps · Version Control · Git · Collaboration

Git
Cheatsheet

A quick reference guide for version control using Git — covering setup, basic workflow, branching, undoing changes, viewing history, and working with remote repositories.

Tool: Git
Type: Distributed VCS
Level: Beginner → Advanced
Sections: 7
Git Documentation GitHub Docs

 What is Git?

Git is the world's most widely used distributed version control system (DVCS), created by Linus Torvalds in 2005 for Linux kernel development. Unlike centralised VCS, every developer has a full copy of the repository — including its entire history — on their local machine.

What it does: Git tracks changes to files over time, allowing you to revert to previous states, compare changes, branch into parallel lines of development, and merge work from multiple contributors safely.

Where it is used: Every modern software team uses Git — from solo developers to organisations with thousands of contributors. It underpins GitHub, GitLab, Bitbucket, and Azure DevOps. It is mandatory knowledge for developers, DevOps engineers, and data scientists alike.

2005
Created by Torvalds
DVCS
Architecture
Free
GPL-2.0 License
90%+
Dev Market Share
🔍

1. Setup & Configuration

Configure Git globally before your first commit. These settings are saved in ~/.gitconfig.

# Set your identity (required for commits)
git config --global user.name "Kenneth Nweke"
git config --global user.email "[email protected]"

# Set default branch name to main
git config --global init.defaultBranch main

# Set default editor
git config --global core.editor "code --wait"   # VS Code
git config --global core.editor "vim"           # Vim

# Set line ending handling
git config --global core.autocrlf input         # macOS/Linux
git config --global core.autocrlf true          # Windows

# Useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"

# View all config
git config --list
git config --global --list

2. Basic Workflow

The core Git cycle: init/clone → stage → commit → push.

# Initialise a new repo
git init                            # create .git in current dir
git init my-project                 # create new dir + init

# Clone an existing repo
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder  # rename
git clone --depth 1 https://github.com/user/repo.git  # shallow clone

# Check status
git status                          # what's changed / staged
git status -s                       # short format

# Stage changes
git add file.txt                    # stage specific file
git add .                           # stage all changes
git add -p                          # interactively stage hunks

# Commit
git commit -m "feat: add login page"
git commit -am "fix: correct typo"  # stage tracked files + commit
git commit --amend                  # edit last commit
git commit --amend --no-edit        # amend without changing message

# Push
git push                            # push to tracked remote
git push origin main                # push branch to remote
git push -u origin main             # set upstream and push
git push --force-with-lease         # safe force push

3. Branching & Merging

# List branches
git branch                          # local branches
git branch -r                       # remote branches
git branch -a                       # all branches
git branch -v                       # with last commit

# Create & switch
git checkout -b feature/login       # create + switch (classic)
git switch -c feature/login         # create + switch (modern)
git switch main                     # switch branch
git checkout main                   # switch branch (classic)

# Rename & delete
git branch -m old-name new-name     # rename branch
git branch -d feature/login         # delete (safe — merged only)
git branch -D feature/login         # force delete
git push origin --delete feature/login  # delete remote branch

# Merge
git merge feature/login             # merge into current branch
git merge --no-ff feature/login     # always create merge commit
git merge --squash feature/login    # squash into single commit
git merge --abort                   # abort conflicting merge

# Rebase
git rebase main                     # rebase current branch onto main
git rebase -i HEAD~3                # interactive rebase last 3 commits
git rebase --abort                  # abort rebase
git rebase --continue               # continue after resolving conflicts

4. Undoing Changes

SituationCommand
Unstage a file (keep changes)git restore --staged file.txt
Discard working dir changesgit restore file.txt
Undo last commit (keep changes)git reset --soft HEAD~1
Undo last commit (unstage changes)git reset HEAD~1
Undo last commit (discard changes)git reset --hard HEAD~1
Revert a commit (safe — new commit)git revert abc1234
Remove untracked filesgit clean -fd
Stash uncommitted changesgit stash
Restore stashgit stash pop
# Stash workflow
git stash                           # stash all uncommitted changes
git stash push -m "WIP: login form" # stash with message
git stash list                      # view all stashes
git stash pop                       # apply latest stash + drop it
git stash apply stash@{2}           # apply specific stash
git stash drop stash@{0}            # delete stash
git stash clear                     # delete all stashes

# Cherry-pick (apply specific commit to current branch)
git cherry-pick abc1234

5. Viewing History & Diffs

# git log
git log                             # full history
git log --oneline                   # compact one line per commit
git log --oneline --graph --all     # visual branch graph
git log --oneline -10               # last 10 commits
git log --author="Kenneth"          # filter by author
git log --since="2 weeks ago"       # filter by date
git log -- file.txt                 # history of specific file
git log --stat                      # show files changed per commit
git log --follow file.txt           # track file through renames

# git diff
git diff                            # unstaged changes
git diff --staged                   # staged vs last commit
git diff main..feature              # diff between branches
git diff abc1234 def5678            # diff between commits

# Show specific commit
git show abc1234                    # show commit details + diff
git show HEAD                       # show latest commit
git show HEAD~2:file.txt            # show file contents 2 commits ago

# Find when a bug was introduced
git bisect start
git bisect bad                      # current commit is buggy
git bisect good abc1234             # this commit was good
git bisect reset                    # end bisect

6. Remote Repositories

# View remotes
git remote -v                       # list remotes with URLs
git remote show origin              # detailed info about origin

# Add / rename / remove remotes
git remote add origin https://github.com/user/repo.git
git remote rename origin upstream
git remote remove upstream
git remote set-url origin https://github.com/user/new-repo.git

# Fetch vs Pull
git fetch                           # download changes WITHOUT merging
git fetch origin                    # fetch from origin
git pull                            # fetch + merge (or rebase)
git pull --rebase                   # fetch + rebase (cleaner history)
git pull origin main                # pull specific branch

# Push
git push origin main
git push --all origin               # push all branches
git push origin v1.0.0              # push a tag
git push --tags                     # push all tags

7. Tags, .gitignore & Useful Shortcuts

# Tags
git tag                             # list all tags
git tag v1.0.0                      # lightweight tag
git tag -a v1.0.0 -m "Release 1.0" # annotated tag
git tag -a v1.0.0 abc1234          # tag a specific commit
git tag -d v1.0.0                   # delete local tag
git push origin --tags              # push all tags

# .gitignore — ignore files from tracking
# Create: echo "node_modules/" >> .gitignore
*.log           # ignore all .log files
node_modules/   # ignore directory
.env            # ignore env file
*.pyc           # ignore Python bytecode
.DS_Store       # ignore macOS files

# Useful one-liners
git shortlog -sn                    # commit count by author
git log --all --full-history -- "**file.txt" # find deleted file
git grep "TODO"                     # search in tracked files
git count-objects -vH               # repo size

📚 Further Learning