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 initCreates a new Git repository in the current directory.
2️⃣ Clone a Repository
git cloneDownloads a copy of a remote repository to your local machine.
3️⃣ Check Repository Status
git statusShows 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 branchCreates a new branch without switching to it.
7️⃣ Switch to a Branch
git checkout
# or (Git 2.23+)
git switchMoves to an existing branch.
8️⃣ Create & Switch to a New Branch
git checkout -b
# or (Git 2.23+)
git switch -cCreates and switches to a new branch in one step.
9️⃣ Merge a Branch
git checkout main
git mergeMerges changes from another branch into the current branch.
🔟 Delete a Branch
git branch -dDeletes a branch that has been merged.
🔹 Working with Remote Repositories
1️⃣1️⃣ Add a Remote Repository
git remote add originLinks your local repo to a remote repository.
1️⃣2️⃣ View Remote Repositories
git remote -vLists remote repositories associated with the project.
1️⃣3️⃣ Fetch Changes from Remote
git fetchDownloads new changes without merging them.
1️⃣4️⃣ Pull Changes from Remote
git pullFetches changes and merges them into your local branch.
1️⃣5️⃣ Push Changes to Remote
git push originUploads local commits to a remote repository.
🔹 Undoing & Fixing Mistakes
1️⃣6️⃣ Undo the Last Commit (Keep Changes)
git reset --soft HEAD~1Moves the last commit back to the staging area.
1️⃣7️⃣ Undo the Last Commit (Discard Changes)
git reset --hard HEAD~1Completely removes the last commit and changes.
1️⃣8️⃣ Unstage a File
git resetRemoves a file from the staging area without deleting it.
1️⃣9️⃣ Revert a Commit
git revertCreates a new commit that undoes a specific commit.
2️⃣0️⃣ Stash Changes (Save Without Committing)
git stashTemporarily saves changes without committing them.
2️⃣1️⃣ Apply Stashed Changes
git stash popRestores the last stashed changes and removes them from the stash list.
2️⃣2️⃣ Drop Stashed Changes
git stash dropDeletes the last stashed changes without applying them.
🔹 Viewing History & Comparing Changes
2️⃣3️⃣ View Commit History
git logShows 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 diffCompares changes between the working directory and the last commit.
2️⃣6️⃣ Show Changes in a Specific File
git diffDisplays 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 rebaseMoves commits from one branch on top of another for a cleaner history.
2️⃣9️⃣ Cherry-Pick a Commit
git cherry-pickApplies 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