Git Commands: Complete List with Examples

By Git Commands · Updated · 9 min read · Checked against the official Git docs

Git commands are the instructions you type in a terminal to control Git: git, then a command like commit, then options. This page lists the 107 you'll actually use, grouped by task, each with a one-line explanation you can copy.

The 7 Git commands you'll use every day

git status → git add . → git commit -m "msg" → git pull → git push, plus git switch -c for new branches and git log --oneline to read history.

Setup & Config commands

Tell Git who you are and how it should behave. Most of these run once per machine.

git config --global user.name "Your Name"

Set the name attached to your commits.

Beginner
git config --global user.email "you@example.com"

Set the email attached to your commits.

Beginner
git config --global init.defaultBranch main

Make new repos start on a branch called main.

Beginner
git config --global core.editor "code --wait"

Use VS Code as the editor for commit messages.

Beginner
git config --list --show-origin

Show every setting Git is using and the file it came from.

Intermediate
git config --global alias.st status

Create a shortcut, so git st runs git status.

Intermediate
git config --global pull.rebase true

Make git pull rebase instead of merge by default.

Intermediate
git version

Show which version of Git is installed.

$ git version
git version 2.56.1
Beginner
git help <command>

Open the manual page for any command.

Beginner
git help -a

List every Git command installed on your machine.

Intermediate

Start a Repository commands

Create a new repo or copy an existing one.

git init

Turn the current folder into a Git repository. Full guide →

$ git init
Initialized empty Git repository in /projects/app/.git/
Beginner
git init <folder>

Create a new folder and make it a repository.

Beginner
git clone <url>

Download a full copy of a remote repo, history included.

$ git clone https://github.com/user/app.git
Beginner
git clone <url> <folder>

Clone into a folder name of your choice.

Beginner
git clone --depth 1 <url>

Clone only the latest snapshot. Much faster for big repos.

Intermediate
git clone --branch <name> <url>

Clone and check out a specific branch right away.

Intermediate

Stage & Commit commands

The daily loop: check what changed, stage it, save it.

git status

See which files are changed, staged or untracked.

$ git status
On branch main
Changes not staged for commit:
  modified:   index.html
Beginner
git status -s

Short, one-line-per-file status.

Intermediate
git add <file>

Stage one file for the next commit.

Beginner
git add .

Stage every change in the current folder and below.

Beginner
git add -A

Stage all changes in the whole repo, deletions included.

Beginner
git add -p

Pick which pieces of a file to stage, hunk by hunk.

Intermediate
git commit -m "message"

Save staged changes as a new commit.

$ git commit -m "Add login form"
[main 3f2a1c9] Add login form
 1 file changed, 24 insertions(+)
Beginner
git commit -am "message"

Stage all tracked files and commit in one step.

Beginner
git commit --amend

Fix the last commit's message or add forgotten files to it. Full guide →

Intermediate
git commit --amend --no-edit

Add staged changes to the last commit, keep its message.

Intermediate
git rm <file>

Delete a file and stage the deletion.

Beginner
git rm --cached <file>

Stop tracking a file but keep it on disk.

Intermediate
git mv <old> <new>

Rename or move a file and stage the change.

Beginner

Branching & Merging commands

Work on features in isolation, then bring them back together.

git branch

List local branches. The current one has a *.

Beginner
git branch -a

List local and remote branches.

Beginner
git branch <name>

Create a new branch but stay where you are.

Beginner
git switch <name>

Move to another branch.

Beginner
git switch -c <name>

Create a new branch and switch to it.

$ git switch -c feature/login
Switched to a new branch 'feature/login'
Beginner
git switch -

Jump back to the branch you were on before.

Intermediate
git checkout <name>

Older way to switch branches. Still works everywhere.

Beginner
git checkout -b <name>

Older way to create and switch to a new branch.

Beginner
git branch -m <new-name>

Rename the current branch.

Intermediate
git merge <branch>

Merge another branch into the one you're on.

Beginner
git merge --no-ff <branch>

Merge and always create a merge commit.

Intermediate
git merge --abort

Cancel a merge that has conflicts and go back.

Intermediate
git rebase <branch>

Replay your commits on top of another branch.

Intermediate
git rebase -i HEAD~3

Edit, squash, reorder or drop your last 3 commits.

Advanced
git rebase --continue

Carry on after fixing a rebase conflict.

Intermediate
git rebase --abort

Cancel a rebase and go back to how things were.

Intermediate
git branch -d <name>

Delete a branch that's already merged.

Beginner
git branch -D <name>

Force-delete a branch, merged or not.

IntermediateDestructive

Remote Repositories commands

Sync your work with GitHub, GitLab, Bitbucket or any server.

git remote -v

Show which remotes are connected and their URLs.

Beginner
git remote add origin <url>

Connect your local repo to a remote one.

Beginner
git remote set-url origin <url>

Change the URL of an existing remote.

Intermediate
git remote remove <name>

Disconnect a remote.

Intermediate
git fetch

Download new commits from the remote without changing your files. Full guide →

Beginner
git fetch --prune

Fetch and clean up branches deleted on the remote.

Intermediate
git pull

Fetch and merge remote changes into your current branch. Full guide →

Beginner
git pull --rebase

Fetch and rebase instead of merging. Keeps history straight. Full guide →

Intermediate
git push

Upload your commits to the remote.

Beginner
git push -u origin <branch>

Push a new branch and set it to track the remote.

$ git push -u origin feature/login
Beginner
git push origin --delete <branch>

Delete a branch on the remote.

IntermediateDestructive
git push --force-with-lease

Force-push, but refuse if someone else pushed first.

AdvancedDestructive

Inspect & Compare commands

Read history and see exactly what changed.

git log

Show the commit history of the current branch.

Beginner
git log --oneline

One line per commit.

Beginner
git log --oneline --graph --all

Compact, visual history of every branch.

$ git log --oneline --graph --all
* 3f2a1c9 (HEAD -> main) Add login form
* 9b8d7e1 Initial commit
Intermediate
git log -p <file>

Show every change ever made to one file.

Intermediate
git log --author="name"

Show only commits by one person.

Intermediate
git log -S "text"

Find commits that added or removed some text.

Advanced
git diff

Show unstaged changes.

Beginner
git diff --staged

Show what's staged and about to be committed.

Beginner
git diff <branch1>..<branch2>

Compare two branches.

Intermediate
git show <commit>

Show one commit: message, author and changes.

Beginner
git blame <file>

See who last changed each line of a file.

Intermediate
git shortlog -sn

Count commits per author.

Intermediate
git grep "text"

Search tracked files for some text. Faster than plain grep.

Intermediate
git describe --tags

Name the current commit relative to the nearest tag.

Advanced

Undo Changes commands

Every mistake has a fix. Pick the one that matches where you are.

git restore <file>

Throw away unstaged changes to a file.

BeginnerDestructive
git restore --staged <file>

Unstage a file but keep the edits.

Beginner
git restore --source=<commit> <file>

Bring back a file as it was in an older commit.

Intermediate
git reset --soft HEAD~1

Undo the last commit, keep its changes staged. Full guide →

Intermediate
git reset HEAD~1

Undo the last commit, keep its changes unstaged. Full guide →

Intermediate
git reset --hard HEAD~1

Delete the last commit and its changes. Full guide →

IntermediateDestructive
git reset --hard origin/main

Make your branch exactly match the remote. Local work is lost. Full guide →

AdvancedDestructive
git revert <commit>

Make a new commit that reverses an old one. Safe on shared branches.

Intermediate
git reflog

List everywhere HEAD has been. Your rescue rope for 'lost' commits. Full guide →

Advanced
git clean -n

Preview which untracked files would be deleted.

Intermediate
git clean -fd

Delete untracked files and folders.

IntermediateDestructive

Stash, Tags & Cherry-pick commands

Park unfinished work, mark releases, copy single commits.

git stash

Save uncommitted changes and clean the working tree. Full guide →

Beginner
git stash -u

Stash untracked files too. Full guide →

Intermediate
git stash push -m "note"

Stash with a label so you can find it later.

Intermediate
git stash pop

Bring back the latest stash and remove it from the list. Full guide →

Beginner
git stash apply stash@{1}

Apply a specific stash but keep it in the list.

Intermediate
git stash drop

Delete the latest stash.

IntermediateDestructive
git tag

List all tags.

Beginner
git tag -a v1.0.0 -m "Release"

Create an annotated release tag.

Intermediate
git push --tags

Upload your tags to the remote.

Intermediate
git cherry-pick <commit>

Copy one commit from another branch onto this one.

Intermediate

Advanced & Power Tools commands

Less common, but they save hours when you need them.

git bisect start

Start a binary search to find the commit that introduced a bug.

$ git bisect start
$ git bisect bad
$ git bisect good v1.0.0
Advanced
git worktree add <path> <branch>

Check out a second branch in another folder at the same time.

Advanced
git submodule add <url>

Include another repository inside this one.

Advanced
git submodule update --init --recursive

Download all submodules after cloning.

Advanced
git sparse-checkout set <folder>

Only check out part of a huge repo.

Advanced
git archive -o app.zip HEAD

Export the current snapshot as a zip file, without .git.

Advanced
git format-patch -1 <commit>

Turn a commit into a patch file you can email.

Advanced
git apply <file.patch>

Apply a patch file to your working tree.

Advanced
git rerere

Remember how you fixed a conflict and reuse the fix next time.

Advanced
git gc

Clean up and compress the repository database.

Advanced
git fsck

Check the repository for corruption.

Advanced
git maintenance start

Schedule background tasks that keep big repos fast.

Advanced

No command matches those filters.

How a Git command is built

Every Git command follows the same pattern:

git <command> [options] [arguments]
PartExampleWhat it does
CommandcommitThe action Git should take.
Short option-mOne dash, one letter. Can often be combined, like -am.
Long option--amendTwo dashes, a full word. Easier to read in scripts.
Argument"Fix typo", main, index.htmlWhat the command acts on: a message, branch, file or commit.

Commits can be referred to by their hash (3f2a1c9), a branch name (main), or relative to HEAD, your current position: HEAD~1 means "one commit before this one". That's why so many undo fixes use HEAD~1.

Where to type Git commands

  • Windows: Git Bash, installed with Git for Windows. PowerShell and Command Prompt work too once Git is installed.
  • macOS: the Terminal app. Run git --version; if Git isn't installed, macOS offers to install it.
  • Linux: any terminal. Install with your package manager, for example sudo apt install git.
  • VS Code and other editors: open the built-in terminal and type the same commands.

Run commands from inside your project folder. Git looks for the hidden .git folder to know which repository you mean. New to all this? Read what Git is and how it works first.

Git commands FAQ

What are Git commands?

Git commands are instructions you type in a terminal to control Git, the version control system. Each one starts with git, followed by a command name such as commit or push, then options and arguments.

How many Git commands are there?

Git ships with roughly 150 commands, which you can list with git help -a. About 40 are everyday "porcelain" commands; the rest are low-level "plumbing" commands used by scripts and by Git itself.

What are the basic Git commands every beginner should know?

git init, git clone, git status, git add, git commit, git push and git pull. Those seven cover creating a repository, saving changes and syncing them with a remote.

Where do I type Git commands?

In a terminal. On macOS and Linux, use the built-in Terminal app. On Windows, use Git Bash (installed with Git for Windows), PowerShell or Command Prompt. Code editors like VS Code also have a built-in terminal.

How do I see help for a single Git command?

Run git help <command> for the full manual page, or git <command> -h for a short summary of its options.

Is Git the same as GitHub?

No. Git is the version control tool that runs on your computer. GitHub is a website that hosts Git repositories and adds features like pull requests. The same Git commands work with GitHub, GitLab and Bitbucket.