Git Commands You Must Know

If your entire Git workflow consists of git add ., git commit, and git push, you are treating Git like a simple cloud backup system. Git is infinitely more powerful than that.

In a professional environment, you will make mistakes. You will commit code to the wrong branch, you will need to quickly switch branches while in the middle of a messy feature, and you will need to cleanly integrate updates from your team. Here are the advanced commands senior developers use daily.

1. Git Stash: The Lifesaver

Imagine you are halfway through writing a complex new feature. Suddenly, your manager tells you there is a critical bug in production that you need to fix immediately on the main branch. You can't switch branches because your current code is half-finished and broken. You don't want to commit broken code. What do you do?

You use Stash. It temporarily shelves (hides) your uncommitted changes, leaving your working directory clean.

# 1. Hide your current messy changes
git stash

# 2. Switch to main and fix the bug
git checkout main
# ... fix bug ...

# 3. Go back to your feature branch
git checkout feature-branch

# 4. Bring your messy code back out of the stash
git stash pop

2. Git Rebase: The Clean History

When you want to pull new updates from the main branch into your feature branch, you usually use git merge. However, merging creates ugly "merge commits" that clutter the project history.

Rebase is the professional alternative. It rewrites your commit history, making it look as if you wrote your feature exactly on top of the latest version of the main branch.

# Make sure you are on your feature branch
git checkout feature-branch

# Rebase your branch onto the latest main
git rebase main

# If there are conflicts, fix them, then run:
git add .
git rebase --continue

3. Git Cherry-Pick

Sometimes a coworker writes a brilliant commit on their branch, and you need only that specific commit in your branch, without merging their entire unfinished feature. You can "cherry-pick" it.

# Find the commit hash (e.g., a1b2c3d4) from their branch
git log

# Apply that exact commit to your current branch
git cherry-pick a1b2c3d4

4. Fixing Mistakes (Amend)

You just typed git commit -m "Fixed login button" but then you realized you forgot to save one of the files! Instead of making a stupid "Oops forgot a file" commit, you can amend the previous one.

# Save the missing file, then stage it
git add .

# Add it to the PREVIOUS commit seamlessly
git commit --amend --no-edit

The Golden Rule of Git

Never rewrite public history. Commands like rebase and amend physically change the history of your commits. You should ONLY use these commands on your local branches that have not been pushed to the remote server yet. If you rebase a branch that someone else is working on, you will destroy their local repository.

Mini Task: Time Travel

  1. Open a dummy repository in your terminal.
  2. Run git reflog. This is Git's secret diary. It records every single action you take, even if you delete a branch or do a hard reset.
  3. Find an old state, and use git reset --hard HEAD@{2} to magically time travel back to it.

Best External Resources

MSMAXPRO

Written by MSMAXPRO

Professional web developer and security enthusiast crafting modern digital experiences. Follow me for more tutorials and roadmaps.