# Useful Git commands

Beyond the basic add, commit, push and pull, there are several Git commands that you'll use regularly while working with branches. This page covers the most important ones.

# Understanding git status

git status is your best friend — use it constantly to understand the state of your project. Here are the most common outputs you'll see:

Clean working tree (nothing to commit):

$ git status
On branch jj-change-home
nothing to commit, working tree clean
Copied!
1
2
3

Untracked files (new files Git doesn't know about yet):

$ git status
On branch jj-change-home
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        contact.html
        css/contact.css
Copied!
1
2
3
4
5
6

Staged changes (ready to commit):

$ git status
On branch jj-change-home
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html
        new file:   contact.html
Copied!
1
2
3
4
5
6

Modified files (changed but not yet staged):

$ git status
On branch jj-change-home
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
        modified:   index.html
Copied!
1
2
3
4
5

TIP

Always check git status before committing to make sure you're on the right branch and you're committing the right files!

# Visualizing history with git log

Use git log with special flags to get a compact overview of your commits and branches:

$ git log --oneline --graph --all
Copied!
1

This shows a visual tree of all branches and their commits:

* a1b2c3d (HEAD -> jj-change-home) feat: add contact form
* d4e5f6a fix: correct header padding
| * 7g8h9i0 (origin/main, main) Merge pull request #3
|/
* j1k2l3m feat: add about page
* n4o5p6q Initial commit
Copied!
1
2
3
4
5
6

Some useful variations:

Command What it shows
git log --oneline Compact one-line-per-commit view
git log --oneline -5 Only the last 5 commits
git log --oneline --graph --all Visual branch tree with all branches
git log --oneline --graph Visual tree of the current branch only

TIP: Make it a habit

Before starting work each day, run:

$ git status            # What branch am I on? Any uncommitted changes?
$ git log --oneline -5  # What were the last 5 commits?
Copied!
1
2

# Saving uncommitted work with git stash

Sometimes you need to switch branches but you have uncommitted changes that aren't ready to commit yet. If you try to switch, Git may refuse or carry your changes to the other branch — both are problematic.

git stash temporarily saves your uncommitted changes and gives you a clean working tree:

# Save your current changes
$ git stash

# Your working tree is now clean — you can safely switch branches
$ git switch main

# ... do whatever you need on main ...

# Switch back to your feature branch
$ git switch jj-change-home

# Restore your saved changes
$ git stash pop
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13

WHEN TO USE `git stash`

  • You need to quickly switch branches but aren't ready to commit yet
  • A teammate asks you to urgently review their pull request
  • You realize you've been working on the wrong branch (stash → switch → pop)

Don't use stash as a long-term storage — commit your work instead. Stashes can be forgotten and lost!

# Undoing changes with git restore

Made changes you want to throw away? git restore lets you discard or unstage changes:

Discard changes in a file (revert to the last commit):

$ git restore index.html
Copied!
1

Unstage a file (keep changes, but remove from staging):

$ git restore --staged index.html
Copied!
1

WARNING

git restore permanently discards your uncommitted changes. There is no undo! Make sure you really want to throw away those changes before running it.

# Checking differences with git diff

Want to see exactly what changed in your files before committing?

See unstaged changes (what you've modified but not yet added):

$ git diff
Copied!
1

See staged changes (what you've added but not yet committed):

$ git diff --staged
Copied!
1

TIP

Running git diff before git add is a good habit — it helps you review your changes and avoid committing unintended code.

Last Updated: 2/11/2026, 11:46:56 AM