Git is an essential tool for developers, but with so many commands available, it's easy to get overwhelmed. This guide covers 30 Git commands that you'll actually use in your daily workflow, from setting up a repository to managing branches and resolving conflicts.


🔹 Getting Started with Git

1️⃣ Initialize a Repository

git init

Creates a new Git repository in the current directory.

2️⃣ Clone a Repository

git clone

Downloads a copy of a remote repository to your local machine.

3️⃣ Check Repository Status

git status

Shows changes that have been staged, unstaged, or untracked.

4️⃣ Add Files to Staging Area

git add 
git add .

Stages a specific file or all files for the next commit.

5️⃣ Commit Changes

git commit -m "Commit message"

Saves changes in the local repository with a descriptive message.


🔹 Branching & Merging

6️⃣ Create a New Branch

git branch

Creates a new branch without switching to it.

7️⃣ Switch to a Branch

git checkout 
# or (Git 2.23+)
git switch

Moves to an existing branch.

8️⃣ Create & Switch to a New Branch

git checkout -b 
# or (Git 2.23+)
git switch -c

Creates and switches to a new branch in one step.

9️⃣ Merge a Branch

git checkout main
git merge

Merges changes from another branch into the current branch.

🔟 Delete a Branch

git branch -d

Deletes a branch that has been merged.


🔹 Working with Remote Repositories

1️⃣1️⃣ Add a Remote Repository

git remote add origin

Links your local repo to a remote repository.

1️⃣2️⃣ View Remote Repositories

git remote -v

Lists remote repositories associated with the project.

1️⃣3️⃣ Fetch Changes from Remote

git fetch

Downloads new changes without merging them.

1️⃣4️⃣ Pull Changes from Remote

git pull

Fetches changes and merges them into your local branch.

1️⃣5️⃣ Push Changes to Remote

git push origin

Uploads local commits to a remote repository.


🔹 Undoing & Fixing Mistakes

1️⃣6️⃣ Undo the Last Commit (Keep Changes)

git reset --soft HEAD~1

Moves the last commit back to the staging area.

1️⃣7️⃣ Undo the Last Commit (Discard Changes)

git reset --hard HEAD~1

Completely removes the last commit and changes.

1️⃣8️⃣ Unstage a File

git reset

Removes a file from the staging area without deleting it.

1️⃣9️⃣ Revert a Commit

git revert

Creates a new commit that undoes a specific commit.

2️⃣0️⃣ Stash Changes (Save Without Committing)

git stash

Temporarily saves changes without committing them.

2️⃣1️⃣ Apply Stashed Changes

git stash pop

Restores the last stashed changes and removes them from the stash list.

2️⃣2️⃣ Drop Stashed Changes

git stash drop

Deletes the last stashed changes without applying them.


🔹 Viewing History & Comparing Changes

2️⃣3️⃣ View Commit History

git log

Shows a list of commits with details.

2️⃣4️⃣ View a File’s Change History

git log --

Displays all commits that modified a specific file.

2️⃣5️⃣ Show Differences Between Commits

git diff

Compares changes between the working directory and the last commit.

2️⃣6️⃣ Show Changes in a Specific File

git diff

Displays changes made to a specific file.

2️⃣7️⃣ Compare Two Branches

git diff ..

Shows the differences between two branches.


🔹 Collaboration & Advanced Commands

2️⃣8️⃣ Rebase a Branch

git rebase

Moves commits from one branch on top of another for a cleaner history.

2️⃣9️⃣ Cherry-Pick a Commit

git cherry-pick

Applies a specific commit to the current branch.

3️⃣0️⃣ Squash Commits Before Merging

git rebase -i HEAD~

Combines multiple commits into one before merging.


🎯 Final Thoughts

These 30 Git commands will help you manage your workflow, collaborate effectively, and fix mistakes with confidence. Whether you're working on personal projects or contributing to a team, mastering Git will save you time and frustration.


Follow Me

Thank you for reading my blog. 🚀 You can follow me on GitHub and connect on Twitter