DEV Community

Cover image for Top Git Commands Every Developer Should Know
Vrushik Visavadiya
Vrushik Visavadiya

Posted on

Top Git Commands Every Developer Should Know

Git is one of the most powerful tools in a developer’s toolkit, yet it can feel intimidating to beginners. But don’t worry! In this blog, we’ll break down Git in a way that makes it easy to understand while also sharing some pro tips to boost your workflow.

Why Should You Care About Git?

If you’ve ever worked on a project that needed to keep track of changes, collaborate with others, or roll back to a previous version, Git is your best friend. It helps developers:

✅ Track changes efficiently
✅ Collaborate seamlessly
✅ Prevent the nightmare of lost code
✅ Experiment without breaking everything

Let’s dive into Git step by step.

Getting Started with Git

First, install Git if you haven’t already:

# For Debian-based systems (Ubuntu, etc.)
sudo apt install git

# For macOS
brew install git

# For Windows
download from https://git-scm.com/
Enter fullscreen mode Exit fullscreen mode

After installation, configure your identity

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Now, let’s go hands-on with some essential Git commands!


Essential Git Commands (With Real-Life Scenarios!)

1. Initializing a Git Repository

Ever wanted to track your project changes? Run:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a Git repository in your project folder.

2. Checking the Status

Find out what’s happening in your repo:

git status
Enter fullscreen mode Exit fullscreen mode

It tells you which files are modified, staged, or untracked.

3. Adding Files to Staging

When you make changes, you need to tell Git to track them:

git add file.txt  # Add a specific file
git add .         # Add all changed files
Enter fullscreen mode Exit fullscreen mode

4. Committing Changes

Once files are staged, save them with a meaningful message:

git commit -m "Added a new feature"
Enter fullscreen mode Exit fullscreen mode

Pro tip: Write clear commit messages! Future-you will thank you.

5. Pushing Changes to GitHub

To share your work, push it to a remote repository:

git remote add origin https://github.com/yourusername/repository.git
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

6. Pulling Changes (Syncing with Others)

If you’re working in a team, always pull before making new changes:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

This prevents merge conflicts!


Advanced Git Features

1. Stashing Changes

If you need to switch branches but don’t want to commit yet, stash your changes:

git stash
Enter fullscreen mode Exit fullscreen mode

To apply stashed changes later:

git stash apply
Enter fullscreen mode Exit fullscreen mode

2. Interactive Rebase

Rewriting commit history for a clean and readable log:

git rebase -i HEAD~3  # Modify last 3 commits
Enter fullscreen mode Exit fullscreen mode

3. Bisecting to Find Bugs

Efficiently locate the commit that introduced a bug:

git bisect start
git bisect bad  # Mark current commit as bad
git bisect good commit_hash  # Mark a known good commit
Enter fullscreen mode Exit fullscreen mode

Git will help you find the faulty commit!

4. Cherry-Picking Commits

Apply a specific commit from one branch to another:

git cherry-pick commit_hash
Enter fullscreen mode Exit fullscreen mode

5. Signing Commits for Security

Secure your commits with GPG signatures:

git commit -S -m "Secure commit"
Enter fullscreen mode Exit fullscreen mode

Avoiding Common Git Mistakes

🚫 Forgetting to Pull Before Pushing – Always run git pull first!
🚫 Not Using Branches – Keep your main branch clean and use feature branches.
🚫 Messy Commit Messages – Write clear messages like: git commit -m "Fix login bug".
🚫 Deleting Files Without Git – Use git rm filename to properly remove files.


Pro Git Tips for Developers

🚀 Use Aliases for Speed – Save time with shortcuts:

git config --global alias.st status
git config --global alias.cm "commit -m"
Enter fullscreen mode Exit fullscreen mode

Now you can run git st instead of git status!

🚀 Undo the Last Commit – If you mess up, don’t panic:

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

This keeps your changes but removes the commit.

🚀 Rebase Instead of Merge – Keep history clean with:

git pull --rebase origin main
Enter fullscreen mode Exit fullscreen mode

Top comments (0)