Back

10 Git Commands Every Developer Should Know

10 Git Commands Every Developer Should Know

Git commands form the backbone of every modern development workflow. Whether you’re pushing code to production or collaborating with a distributed team, these ten commands will cover most of your daily work. Here’s what you actually need to know for a modern Git workflow in 2025.

Key Takeaways

  • Master git status and git diff to understand your repository state before making changes
  • Use git switch and git restore instead of the older git checkout for clearer, more focused commands
  • Know git reflog as your safety net for recovering lost commits and undoing mistakes
  • Keep your Git installation current to benefit from security patches and new features

Checking Your Repository State

git status

Before doing anything else, git status tells you where things stand. It shows modified files, staged changes, and untracked files in your working directory.

git status

Run this frequently. It prevents surprises before commits and helps you understand what Git sees.

git diff

While status shows which files changed, diff shows what changed inside them.

git diff              # unstaged changes
git diff --staged     # staged changes

Review your changes before committing. This catches mistakes early.

Staging and Committing

git add

Staging prepares changes for your next commit. You control exactly what goes into each snapshot.

git add filename.js   # stage one file
git add .             # stage everything

For more control, use git add -p to stage specific hunks within files—useful when you’ve made multiple unrelated changes.

git commit

Commits create permanent snapshots in your project history.

git commit -m "Fix authentication timeout bug"

Write clear messages. If you need to fix your last commit message or add forgotten changes, use git commit --amend.

Working with Remotes

git clone

Clone downloads a repository and its full history to your machine.

git clone https://github.com/user/repository.git

git pull

Pull fetches changes from the remote and merges them into your current branch.

git pull origin main

Note: git pull combines git fetch and git merge. If you want to inspect remote changes before merging, run git fetch first.

git push

Push uploads your local commits to the remote repository.

git push origin main

Only committed changes get pushed. Uncommitted work stays local.

Branching and History

git switch

For branch switching, git switch is the modern approach—introduced in Git 2.23 and now the recommended command over the older git checkout for this purpose.

git switch feature-branch      # switch branches
git switch -c new-feature      # create and switch

Git switch vs checkout: checkout still works but handles too many unrelated tasks (switching branches, restoring files, detaching HEAD). switch does one thing clearly. Use it.

git restore

Similarly, git restore handles file restoration—another task previously overloaded onto checkout.

git restore filename.js           # discard working directory changes
git restore --staged filename.js  # unstage a file

This separation makes Git’s mental model clearer for developers learning the tooling in 2025.

git log

Understanding your project history matters. git log shows the commit history of your current branch.

git log --oneline --graph    # compact view with branch structure

The --oneline flag keeps output readable. Add --graph to visualize branching and merging.

Recovery with git reflog

When things go wrong—and they will—git reflog is your safety net. It records every HEAD movement, even commits that no longer appear in git log.

git reflog

Accidentally reset to the wrong commit? Deleted a branch? reflog helps you find and recover lost work. It’s not a daily command, but knowing it exists saves hours of panic.

A Note on Workflow Choices

Teams often debate merge vs rebase. Both integrate changes but differ in how history looks afterward. Merge preserves the full branch structure. Rebase creates a linear history. Neither is universally correct—choose based on your team’s preferences and project needs.

Also worth noting: keep your Git installation reasonably current. Recent versions have addressed security advisories worth patching.

Conclusion

These commands—status, diff, add, commit, clone, pull, push, switch, log, and reflog—cover the Git essentials for developers doing day-to-day work. Master these first. The more advanced commands become useful once this foundation is solid.

Start using switch and restore if you haven’t already. They reflect how Git commands are designed in 2025: focused, predictable, and easier to reason about.

FAQs

Git fetch downloads changes from a remote repository but does not merge them into your local branch. Git pull combines fetch and merge in one step, automatically integrating remote changes into your current branch. Use fetch when you want to review changes before merging them.

Use git reset --soft HEAD~1 to undo your last commit while keeping all changes staged. If you want the changes unstaged but still in your working directory, use git reset HEAD~1 instead. Both approaches preserve your work while removing the commit from history.

Use git switch for all branch-related operations like switching branches or creating new ones. Git switch was introduced in Git 2.23 specifically to handle branching, while checkout remains overloaded with multiple unrelated functions. Switch provides clearer intent and reduces confusion.

Run git reflog to find the commit hash where your branch pointed before deletion. Then recreate the branch using git branch branch-name commit-hash. Reflog tracks all HEAD movements for about 90 days by default, making recovery possible even after accidental deletions.

Gain control over your UX

See how users are using your site as if you were sitting next to them, learn and iterate faster with OpenReplay. — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.

OpenReplay