Skip to main content

Command Palette

Search for a command to run...

Git Cheat Sheet: Essential Quick Reference Guide

Updated
3 min read

Today I compiled a Git Cheat Sheet for quick recall of all the commands I’ve been practicing. Instead of memorizing, I wanted a single document where I can find the most common workflows, along with short explanations and examples.


1. Git Configuration

Before starting, always configure Git with your details:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --list

This ensures commits are linked to your identity.


2. Git Basics (Workflow)

Git follows a 3-step workflow: Working Directory → Staging Area → Local Repository

  • Check status → git status

  • Add file → git add file.txt

  • Add all → git add .

  • Commit → git commit -m "Message"

Note: Files cannot skip staging when moving to local repo.


3. File Recovery

  • Restore deleted (not staged) → git restore file.txt

  • Restore staged → git restore --staged file.txt && git restore file.txt

  • Restore after commit → git checkout HEAD~1 -- file.txt


4. Git Stash

When you want to save uncommitted work without committing:

git stash
git stash list
git stash apply
git stash drop stash@{id}
git stash clear

5. Remote Repositories

  • Clone repo → git clone <repo-url>

  • Add remote → git remote add origin <repo-url>

  • Push changes → git push -u origin main

  • Pull changes → git pull origin main

  • Fetch changes → git fetch


6. Branching

  • List → git branch

  • Create → git branch feature-1

  • Switch → git checkout feature-1

  • Create + Switch → git checkout -b feature-1

  • Rename → git branch -m oldName newName

  • Delete local → git branch -D feature-1

  • Delete remote → git push origin --delete feature-1


7. Merge & Rebase

Merging keeps full history; rebasing creates a linear history.

Merge Example:

git checkout main
git merge feature-1

Rebase Example:

git checkout feature-1
git rebase main

8. Merge Conflicts

When conflicts occur:

  1. Open file → resolve between <<<<<<< and >>>>>>>.

  2. Stage → git add file.txt

  3. Commit → git commit -m "Resolved conflict"


9. .gitignore

Used to exclude files from version control. Example:

*.log
node_modules/
.env

10. GitHub Workflow

Steps to push local code to a new GitHub repo:

git init
git add .
git commit -m "Initial commit"
git remote add origin <repo-url>
git push -u origin main

11. Forking & Pull Requests

  • Fork repo on GitHub.

  • Clone forked repo locally.

  • Push changes to fork.

  • Raise a Pull Request (PR) to the original repository.


12. Useful Commands

  • Commit history → git log --oneline --graph --decorate

  • Show last commit → git show HEAD

  • Undo last commit (keep changes) → git reset --soft HEAD~1

  • Undo last commit (discard changes) → git reset --hard HEAD~1

  • Remove untracked files → git clean -f


Closing Notes

This cheat sheet helps me quickly recall Git commands while working on projects. Git is powerful but can get overwhelming — keeping these short references handy makes it much easier to focus on actual coding and version control.

Documenting DevOps Learning Journey

Part 26 of 40

In this series, I document my day-by-day DevOps learning journey by building projects using AWS, Docker, Kubernetes, Terraform, and CI/CD, and sharing lessons, challenges, and practical insights along the way.

Up next

Learning Jenkins – Part 1

As part of my DevOps journey, I’ve started learning Jenkins, one of the most popular CI/CD tools.I’ll be documenting my learnings in parts — both as a reference for myself and as a guide for others who are starting out. Why Jenkins? In modern develo...

More from this blog

Documenting My DevOps Journey

40 posts