🧰 Git Commands List – Complete Cheat Sheet with Usage Examples (2025 Guide)
🔍 Introduction – Why Master Git Commands?
Think of Git as your project’s time machine and bodyguard. It lets you save snapshots, work in branches, revert disasters, and collaborate without chaos. Whether you’re coding solo or in a team, knowing your way around Git commands is a superpower. This cheat sheet is your ultimate Git toolkit—updated for 2025.
⚙️ Basic Git Configuration
🔹 Set Global Username and Email
git config --global user.name "John Doe"
git config --global user.email "john@example.com"
Set once, used forever. This identifies your commits across all repos.
🔹 Check Git Settings
git config --list
Shows all current Git configs in one place—great for troubleshooting.
🔹 Set Default Editor
git config --global core.editor "code --wait"
Replace "code --wait"
with your preferred editor (nano
, vim
, etc.).
📁 Repository Setup Commands
🔹 Create a New Repository
git init
Initializes a .git
folder to track version control.
🔹 Clone an Existing Repository
git clone https://github.com/user/repo.git
Copies the repo and its full history to your machine.
✅ Staging and Committing
🔹 Track File Changes
git status
Tells you what’s new, modified, or deleted.
🔹 Stage Files for Commit
git add filename
git add .
Adds specific files or all files to the staging area.
🔹 Commit Changes
git commit -m "Your commit message"
Captures a snapshot of your staged changes.
🌱 Branching Commands
🔹 Create and Switch Branches
git branch new-feature
git checkout new-feature
git switch -c new-feature
New feature? Use a new branch to keep changes isolated.
🔹 List and Delete Branches
git branch # list
git branch -d feature # delete
Housekeeping keeps your repo clean and manageable.
🌐 Working with Remote Repositories
🔹 Add Remote Origin
git remote add origin https://github.com/user/repo.git
Links your local repo to a remote one (like GitHub).
🔹 Push and Pull Changes
git push origin main
git pull origin main
Push sends your work; pull brings remote changes to your local repo.
🔹 Fetch and Merge
git fetch origin
git merge origin/main
Fetches changes without merging—great for reviewing updates.
🔍 Inspecting Repository State
🔹 git status
git status
Real-time status of your repo—what’s staged, what’s not.
🔹 git log
git log --oneline --graph --decorate
Visualize commit history like a timeline.
🔹 git diff
git diff
Compares your working directory with the index or a commit.
⏪ Undoing Changes
🔹 git checkout
git checkout filename
Restores a file to its last committed state.
🔹 git reset
git reset HEAD filename
Unstages a file (but keeps your changes).
🔹 git revert
git revert commit_hash
Creates a new commit that undoes a previous one.
🏷️ Tagging Releases
🔹 Create Lightweight and Annotated Tags
git tag v1.0
git tag -a v1.0 -m "Release version 1.0"
Mark stable versions in your codebase.
🔹 Push and List Tags
git push origin v1.0
git tag
Share your tags and check existing ones.
🧰 Git Stash for Temporary Changes
git stash
git stash pop
Save unfinished work temporarily without committing.
🔀 Git Rebase vs Merge
- Rebase rewrites history for cleaner logs.
- Merge keeps full history but can clutter logs.
git rebase main
git merge main
Choose based on collaboration style and team policy.
🧹 Git Clean and Remove Files
git clean -f # remove untracked files
git clean -fd # remove untracked files and directories
A quick reset for your working directory.
⚡ Helpful Git Shortcuts
Command | Purpose |
---|---|
git commit -am "msg" | Add & commit in one step |
git log --oneline | Simplified commit view |
git diff HEAD~1 | Compare with last commit |
git remote -v | Show remotes |
📌 Summary – Git Commands (2025)
- 🔧 Git commands simplify version control and collaboration.
- 📂 Use
init
,clone
,status
,add
,commit
,push
,pull
daily. - 🌱 Branch and stash for safer, cleaner development.
- 🧹 Clean and tag to keep your repo healthy and stable.
Git is like your project’s command center—learn it once, and it’ll save you hours (and headaches) for years to come.
❓ FAQs – Git Commands (2025)
❓ What is the difference between git pull
and git fetch
?
✅ git pull
= fetch
+ merge
. It updates and merges in one step, while fetch
just grabs changes without merging.
❓ How can I undo my last commit?
✅ Use git reset --soft HEAD~1
to undo the commit but keep changes staged, or --hard
to delete them.
❓ How do I delete a remote branch?
✅ Run git push origin --delete branch_name
.
❓ Can I rename a branch in Git?
✅ Yes. Switch to the branch and run git branch -m new-name
.
❓ How do I check which branch I’m on?
✅ Use git branch
or git status
. The active branch will have an asterisk *
.
Share Now :