CTCaliberTek
Cheat Sheets

Git Cheat Sheet: Everyday Commands, Branching, and Undoing Mistakes

Published October 16, 2026·Updated October 29, 2026·7 min read
Like SaveLog in to like, save, and track progress.

Introduction

Git tracks changes to your code so you can collaborate and recover from mistakes. This cheat sheet covers the commands you use every day, including the safer ways to undo things. For a guided path, see the DevOps lessons on this site.

Prerequisites

  • Git installed

Step-by-Step Guide

Step 1: Set up and start

Configure your identity once, then create or clone a repository.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

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

Step 2: Stage and commit

Stage the changes you want, then commit them with a clear message.

git add file.txt
git add .                      # stage everything in the folder
git add -p                     # choose changes interactively
git commit -m "Add login form validation"
git commit --amend             # fix the last commit (not yet pushed)
git diff                       # unstaged changes
git diff --staged              # staged changes
git log --oneline --graph --decorate

Step 3: Branches

Create a branch for each piece of work. switch changes branches, and switch -c creates one.

git branch                     # list
git switch -c feature/search   # create and switch
git switch main
git merge feature/search
git branch -d feature/search   # delete a merged branch
git rebase main                # replay your commits on top of main (not on shared branches)

Step 4: Remotes

Fetch to see remote changes safely, and pull to fetch and merge.

git remote -v
git remote add origin https://github.com/user/repo.git
git fetch
git pull
git push -u origin feature/search   # first push of a branch
git push

Step 5: Undo safely

Prefer commands that do not rewrite shared history. Use revert for commits already pushed.

git restore file.txt               # discard unstaged changes in a file
git restore --staged file.txt      # unstage a file
git revert <commit>                # new commit that undoes a commit (safe for shared branches)
git reset --soft HEAD~1            # undo last commit, keep changes staged
git reset --hard HEAD~1            # DANGER: discards the commit and the changes
git reflog                         # find lost commits and recover them

Step 6: Stash, tags, and ignoring files

Stash saves work in progress, tags mark releases, and .gitignore keeps files like secrets and build output out of the repository.

git stash
git stash list
git stash pop
git tag v1.0.0
git push origin v1.0.0

# .gitignore
node_modules/
.env
*.log
dist/

Step 7: Resolve a merge conflict

Conflicts show markers in the file. Edit the file to keep what you want, remove the markers, stage it, and continue.

<<<<<<< HEAD
your version
=======
their version
>>>>>>> feature/search

git add file.txt
git commit                        # completes the merge
git merge --abort                 # or cancel the merge

FAQs

I committed a secret. Is deleting it in a new commit enough?
No. The secret stays in history. Revoke and replace the secret immediately, then clean the history if needed.

Summary

Stage and commit small changes, branch for each task, fetch before you pull, undo with restore and revert, use reflog to recover, and never commit secrets.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.