Git Command Cheatsheet: Master Version Control Like a Pro

Listen to this Post

Featured Image
Git isn’t just a version control system—it’s your project history, collaboration toolkit, and recovery parachute. Below is an expanded guide with practical commands, examples, and workflows to enhance your Git expertise.

1️⃣ Git Basics

 Initialize a new Git repository 
git init

Clone a remote repository 
git clone https://github.com/user/repo.git

Check the status of files 
git status

Stage a specific file 
git add filename.txt

Stage all changes 
git add .

Commit changes with a message 
git commit -m "Initial commit" 

You Should Know:

  • Use `git config –global user.name “Your Name”` to set your Git username.
    – `git config –global user.email “[email protected]”` ensures commits are attributed correctly.

2️⃣ Branching & Merging

 List all branches 
git branch

Create and switch to a new branch 
git checkout -b feature-branch

Switch to an existing branch 
git checkout main

Merge a branch into the current branch 
git merge feature-branch

Delete a branch (safe) 
git branch -d feature-branch

Force delete an unmerged branch 
git branch -D feature-branch 

You Should Know:

– `git fetch` retrieves remote branches without merging.
– Resolve merge conflicts with `git mergetool` or manually editing conflicted files.

3️⃣ Undo & Reset

 Undo last commit but keep changes staged 
git reset --soft HEAD~1

Completely discard the last commit and changes 
git reset --hard HEAD~1

Discard changes in a specific file 
git checkout -- filename.txt

Revert a specific commit (creates a new undo commit) 
git revert abc1234 

You Should Know:

– `git reflog` helps recover lost commits.
– `git reset –hard` is destructive—use cautiously!

4️⃣ Remote Repos

 List remote repositories 
git remote -v

Add a new remote 
git remote add origin https://github.com/user/repo.git

Push changes to a remote branch 
git push -u origin main

Pull latest changes (fetch + merge) 
git pull

Force push (use with caution) 
git push --force 

You Should Know:

– `git fetch` updates remote tracking branches without altering your working directory.
– Use `git remote remove origin` to unlink a remote repo.

5️⃣ Logs & Diffs

 View commit history (compact) 
git log --oneline

View changes in working directory 
git diff

See changes in a specific commit 
git show abc1234

Track changes between branches 
git diff main..feature-branch 

You Should Know:

– `git log –graph –oneline –decorate` visualizes branch history.
– `git blame filename.txt` identifies who changed each line.

6️⃣ Stash & Clean

 Temporarily save uncommitted changes 
git stash

Reapply stashed changes 
git stash pop

List all stashes 
git stash list

Delete untracked files and directories 
git clean -fd 

You Should Know:

– `git stash apply` reapplies a stash without removing it from the list.
– `git clean -n` previews files to be deleted before running the actual command.

What Undercode Say

Git is a lifeline for developers, enabling seamless collaboration and version control. Mastering these commands ensures efficiency in managing codebases, recovering from mistakes, and contributing to team projects.

Expected Output:

A streamlined Git workflow with fewer errors, faster debugging, and better collaboration.

Prediction:

As DevOps and CI/CD pipelines evolve, Git will remain the backbone of version control, with more integrations for AI-assisted conflict resolution and automated commit messaging.

🔗 Further Reading:

References:

Reported By: Surajdevx Git – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram