Essential Git Commands Cheat Sheet for Developers

Listen to this Post

Featured Image
Whether you’re a beginner or an experienced developer, mastering Git commands is crucial for efficient version control. Below is a comprehensive list of essential Git commands, along with practical examples and advanced tips.

Core Git Commands

1. Initialize a Repository

git init 

Initializes a new Git repository in the current directory.

2. Clone a Repository

git clone <repository-url> 

Creates a local copy of a remote repository.

3. Check Repository Status

git status 

Shows the current state of the working directory and staging area.

4. Add Files to Staging

git add <file> 
git add .  Adds all modified files 

5. Commit Changes

git commit -m "Your commit message" 

6. Push Changes to Remote

git push origin <branch-name> 

7. Pull Latest Changes

git pull origin <branch-name> 

8. Branch Management

git branch  List all branches 
git branch <branch-name>  Create a new branch 
git checkout <branch-name>  Switch to a branch 
git checkout -b <branch>  Create and switch to a new branch 

9. Merging Branches

git merge <branch-name> 

10. Rebase for Clean History

git rebase <branch-name> 

11. Stash Uncommitted Changes

git stash 
git stash pop  Restore stashed changes 

12. View Commit History

git log 
git log --oneline  Compact view 

You Should Know: Advanced Git Commands & Tricks

Undoing Changes

git reset --hard HEAD  Discard all local changes 
git revert <commit-hash>  Revert a specific commit 

Tracking Remote Branches

git fetch --all  Fetch all remote branches 
git remote -v  List remote repositories 

Tagging Releases

git tag -a v1.0 -m "Release version 1.0" 
git push origin --tags 

Finding Changes in Files

git diff  Show unstaged changes 
git diff --cached  Show staged changes 

Rebasing Interactively

git rebase -i HEAD~3  Edit, squash, or reorder commits 

Git Aliases for Faster Workflow

Add these to `~/.gitconfig`:

[bash] 
co = checkout 
br = branch 
ci = commit 
st = status 
lg = log --oneline --graph --decorate 

What Undercode Say

Git is a powerful tool, and mastering it can significantly improve your workflow. Here are additional Linux/Windows commands that complement Git usage:

Linux Commands for Git Users

 Find files modified in the last 24 hours 
find . -type f -mtime -1

Count lines of code in a Git repo 
git ls-files | xargs wc -l

Search for a keyword in Git history 
git log -S "keyword" 

Windows Command Line (PowerShell) for Git

 List modified files 
git status --porcelain | ForEach { $_.Substring(3) }

Open Git log in a GUI viewer 
gitk 

GitHub CLI (Bonus)

gh repo clone <repo> 
gh pr create 
gh issue list 

Expected Output:

By integrating these commands into your daily workflow, you can streamline version control, avoid common mistakes, and collaborate more efficiently. Bookmark this cheat sheet and share it with your team!

🔗 Relevant URLs:

References:

Reported By: Ashsau Tired – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram