DEV Community

Cover image for Git beyond push: the commands nobody taught you
Lucas Braun
Lucas Braun

Posted on

Git beyond push: the commands nobody taught you

Everyone learns Git the same way: git init, git add ., git commit -m "first commit", git push.

And then just... stops there.

The problem is that Git has a huge set of commands that make a real difference day-to-day — and almost no beginner tutorial ever mentions them. You only find out about them when something breaks, at 11pm, with an open pull request.

This article is here to change that.


1. git stash — Git's secret pocket

You're in the middle of a feature, the code is half-done, and you get a message: "there's an urgent bug to fix on main". What do you do?

If you try git checkout like that, Git will complain. If you make a commit, you'll pollute your history with a "WIP: saving to switch branches".

The solution is stash:

git stash
Enter fullscreen mode Exit fullscreen mode

This saves all your uncommitted changes to a temporary place and leaves your working directory clean. Fix the bug, come back, and restore:

git stash pop
Enter fullscreen mode Exit fullscreen mode

Want to see what you have saved?

git stash list
Enter fullscreen mode Exit fullscreen mode

You can have multiple stashes and give them names:

git stash push -m "login feature halfway done"
Enter fullscreen mode Exit fullscreen mode

2. git log --oneline — because the default log is terrible

Ever opened a git log and stared at a massive wall of text? There's a much better way:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Output:

a3f1c2e feat: add JWT authentication
9b2d441 fix: correct form validation
3c8e109 chore: update dependencies
Enter fullscreen mode Exit fullscreen mode

Clean, readable, one commit per line.

Want to see branches too?

git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

This draws a visual tree of your branches in the terminal. Looks like something from a hacker movie, but it's just Git.


3. git diff — before committing, see what you actually changed

You know that moment when you type git add . without even checking what changed? Yeah, everyone does it. But the right way is to review first:

git diff
Enter fullscreen mode Exit fullscreen mode

This shows line by line what changed in files that haven't been staged yet.

Already ran git add and want to review before committing?

git diff --staged
Enter fullscreen mode Exit fullscreen mode

Want to compare two specific commits?

git diff abc1234 def5678
Enter fullscreen mode Exit fullscreen mode

Reviewing your diff becomes a habit once you stop accidentally committing garbage.


4. git commit --amend — fixing your last commit

Committed with the wrong message? Forgot to include a file? You don't need to create a new commit just for that:

git commit --amend
Enter fullscreen mode Exit fullscreen mode

This opens your editor with the last commit message for you to edit. Save, close, done.

Want to change the message directly without opening the editor?

git commit --amend -m "feat: add JWT authentication"
Enter fullscreen mode Exit fullscreen mode

Want to include a forgotten file?

git add forgotten-file.py
git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

⚠️ Warning: only use --amend on commits that haven't been pushed to the remote yet. If you've already pushed, you'll run into trouble pushing again.


5. git restore — undoing changes without drama

You changed a file and regretted it. Want to go back to how it was at the last commit?

git restore filename.py
Enter fullscreen mode Exit fullscreen mode

This discards all local changes in that file. No commit, no hassle.

Want to undo a git add (unstage a file)?

git restore --staged filename.py
Enter fullscreen mode Exit fullscreen mode

This doesn't delete your changes, it just removes the file from the staging area. Your edits are still there.


6. git revert — undoing a commit without rewriting history

Made a commit you shouldn't have? The temptation is to use git reset, but there's a problem: it rewrites history, which can cause a mess for teams.

git revert is safer: it creates a new commit that undoes the changes from a previous commit:

git revert abc1234
Enter fullscreen mode Exit fullscreen mode

The history stays intact, you just add a new commit that reverts the damage. Perfect for team environments.


7. git cherry-pick — taking only what you need

Imagine you have a bugfix on a feature branch, but you need to apply that fix to main right now, without merging everything.

cherry-pick takes a specific commit and applies it to another branch:

git cherry-pick abc1234
Enter fullscreen mode Exit fullscreen mode

Switch to the target branch, run this command with the hash of the commit you want, and done. Only that commit gets copied over.


Bonus: aliases that will save you time

Tired of typing git log --oneline --graph --all every time? Create an alias:

git config --global alias.tree "log --oneline --graph --all"
Enter fullscreen mode Exit fullscreen mode

Now just type:

git tree
Enter fullscreen mode Exit fullscreen mode

You can create aliases for any command. Some I use all the time:

git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.unstage "restore --staged"
Enter fullscreen mode Exit fullscreen mode

Summary

Command When to use
git stash Save work in progress to switch context
git log --oneline View history in a readable way
git diff --staged Review what's going into the next commit
git commit --amend Fix the last commit (message or files)
git restore Undo changes in files
git revert Safely undo a commit in a team environment
git cherry-pick Apply a specific commit to another branch

These commands aren't advanced — they're essential. They're what separates someone who "uses Git" from someone who actually works well with it.

Top comments (0)