DEV Community

Cover image for 30 Git Commands Every Developer Should Know 🔥
Mohamed Ibrahim
Mohamed Ibrahim

Posted on

2

30 Git Commands Every Developer Should Know 🔥

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
Enter fullscreen mode Exit fullscreen mode

Creates a new Git repository in the current directory.

2️⃣ Clone a Repository

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

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

3️⃣ Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

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

4️⃣ Add Files to Staging Area

git add <file>
git add .
Enter fullscreen mode Exit fullscreen mode

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

5️⃣ Commit Changes

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

Saves changes in the local repository with a descriptive message.


🔹 Branching & Merging

6️⃣ Create a New Branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates a new branch without switching to it.

7️⃣ Switch to a Branch

git checkout <branch-name>
# or (Git 2.23+)
git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Moves to an existing branch.

8️⃣ Create & Switch to a New Branch

git checkout -b <branch-name>
# or (Git 2.23+)
git switch -c <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates and switches to a new branch in one step.

9️⃣ Merge a Branch

git checkout main
git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Merges changes from another branch into the current branch.

🔟 Delete a Branch

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Deletes a branch that has been merged.


🔹 Working with Remote Repositories

1️⃣1️⃣ Add a Remote Repository

git remote add origin <repository-url>
Enter fullscreen mode Exit fullscreen mode

Links your local repo to a remote repository.

1️⃣2️⃣ View Remote Repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

Lists remote repositories associated with the project.

1️⃣3️⃣ Fetch Changes from Remote

git fetch
Enter fullscreen mode Exit fullscreen mode

Downloads new changes without merging them.

1️⃣4️⃣ Pull Changes from Remote

git pull
Enter fullscreen mode Exit fullscreen mode

Fetches changes and merges them into your local branch.

1️⃣5️⃣ Push Changes to Remote

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Uploads local commits to a remote repository.


🔹 Undoing & Fixing Mistakes

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

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Moves the last commit back to the staging area.

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

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Completely removes the last commit and changes.

1️⃣8️⃣ Unstage a File

git reset <file>
Enter fullscreen mode Exit fullscreen mode

Removes a file from the staging area without deleting it.

1️⃣9️⃣ Revert a Commit

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that undoes a specific commit.

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

git stash
Enter fullscreen mode Exit fullscreen mode

Temporarily saves changes without committing them.

2️⃣1️⃣ Apply Stashed Changes

git stash pop
Enter fullscreen mode Exit fullscreen mode

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

2️⃣2️⃣ Drop Stashed Changes

git stash drop
Enter fullscreen mode Exit fullscreen mode

Deletes the last stashed changes without applying them.


🔹 Viewing History & Comparing Changes

2️⃣3️⃣ View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

Shows a list of commits with details.

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

git log -- <file>
Enter fullscreen mode Exit fullscreen mode

Displays all commits that modified a specific file.

2️⃣5️⃣ Show Differences Between Commits

git diff
Enter fullscreen mode Exit fullscreen mode

Compares changes between the working directory and the last commit.

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

git diff <file>
Enter fullscreen mode Exit fullscreen mode

Displays changes made to a specific file.

2️⃣7️⃣ Compare Two Branches

git diff <branch-1>..<branch-2>
Enter fullscreen mode Exit fullscreen mode

Shows the differences between two branches.


🔹 Collaboration & Advanced Commands

2️⃣8️⃣ Rebase a Branch

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

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

2️⃣9️⃣ Cherry-Pick a Commit

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Applies a specific commit to the current branch.

3️⃣0️⃣ Squash Commits Before Merging

git rebase -i HEAD~<number>
Enter fullscreen mode Exit fullscreen mode

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

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay