Menu

Tools

Git Commands Tutorial

Master 30 essential Git commands every developer should know

30 commands

git config

Setup

What it does

Sets configuration values like username, email, and editor. Use --global for user-wide settings.

Example

$ git config --global user.name "John Doe"

Output

(sets username globally)

git init

Setup

What it does

Initializes a new Git repository in the current directory. Creates a hidden .git folder.

Example

$ git init

Output

Initialized empty Git repository in /project/.git/

git clone

Setup

What it does

Creates a copy of a remote repository on your local machine. Downloads all history and branches.

Example

$ git clone https://github.com/user/repo.git

Output

Cloning into 'repo'...
Receiving objects: 100%

git remote

Setup

What it does

Manages remote repository connections. Use -v to see URLs, add to link new remotes.

Example

$ git remote -v

Output

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

git status

Basic

What it does

Shows the working tree status - modified files, staged changes, and untracked files.

Example

$ git status

Output

On branch main
Changes not staged for commit:
  modified: index.js

git add

Basic

What it does

Stages file changes for commit. Use . for all files, -p for interactive staging.

Example

$ git add .

Output

(stages all changes)

git commit

Basic

What it does

Records staged changes to the repository. Use -m for inline message, -a to auto-stage tracked files.

Example

$ git commit -m "Add new feature"

Output

[main abc1234] Add new feature
 2 files changed, 50 insertions(+)

git push

Basic

What it does

Uploads local commits to a remote repository. Use -u to set upstream for first push.

Example

$ git push origin main

Output

Enumerating objects: 5, done.
Writing objects: 100%

git pull

Basic

What it does

Fetches and integrates changes from remote. Combines fetch and merge in one command.

Example

$ git pull origin main

Output

Updating abc1234..def5678
Fast-forward
 2 files changed

git fetch

Basic

What it does

Downloads objects and refs from remote without merging. Safe way to check remote changes.

Example

$ git fetch origin

Output

From https://github.com/user/repo
 * branch main -> FETCH_HEAD

git diff

Basic

What it does

Shows changes between commits, working tree, or staged changes. Use --staged for staged diff.

Example

$ git diff

Output

-old line
+new line

git stash

Basic

What it does

Temporarily saves uncommitted changes. Use pop to restore, list to see all stashes.

Example

$ git stash

Output

Saved working directory and index state WIP on main: abc1234

git branch

Branch

What it does

Lists, creates, or deletes branches. Use -d to delete, -a to show all including remote.

Example

$ git branch feature/login

Output

(creates new branch)

git checkout

Branch

What it does

Switches branches or restores files. Use -b to create and switch in one command.

Example

$ git checkout -b feature/auth

Output

Switched to a new branch 'feature/auth'

git switch

Branch

What it does

Modern way to switch branches (Git 2.23+). Use -c to create new branch.

Example

$ git switch main

Output

Switched to branch 'main'

git merge

Branch

What it does

Joins two or more development histories together. Use --no-ff to preserve merge commit.

Example

$ git merge feature/login

Output

Merge made by 'recursive' strategy.
 3 files changed

git rebase

Branch

What it does

Reapplies commits on top of another base. Use -i for interactive mode to squash/edit.

Example

$ git rebase main

Output

Successfully rebased and updated refs/heads/feature.

git cherry-pick

Branch

What it does

Applies changes from specific commits. Useful for moving single commits between branches.

Example

$ git cherry-pick abc1234

Output

[main def5678] Pick commit message
 1 file changed

git remote add

Remote

What it does

Adds a new remote repository connection with a shortname.

Example

$ git remote add upstream https://github.com/original/repo.git

Output

(adds remote)

git push -u

Remote

What it does

Pushes and sets upstream tracking. Future pushes can just use git push.

Example

$ git push -u origin feature/auth

Output

Branch 'feature/auth' set up to track remote branch.

git pull --rebase

Remote

What it does

Fetches remote and rebases local commits on top. Keeps history linear.

Example

$ git pull --rebase origin main

Output

Successfully rebased and updated refs/heads/main.

git push --force

Remote

What it does

Force pushes, overwriting remote history. Use with caution! Prefer --force-with-lease.

Example

$ git push --force-with-lease origin feature

Output

+ abc1234...def5678 feature -> feature (forced update)

git reset

Undo

What it does

Unstages files or moves HEAD. --soft keeps changes staged, --hard discards everything.

Example

$ git reset HEAD~1

Output

Unstaged changes after reset:
M index.js

git revert

Undo

What it does

Creates a new commit that undoes a previous commit. Safe for shared history.

Example

$ git revert abc1234

Output

[main def5678] Revert "Add feature"
 1 file changed

git restore

Undo

What it does

Restores working tree files (Git 2.23+). Use --staged to unstage files.

Example

$ git restore --staged index.js

Output

(unstages file)

git clean

Undo

What it does

Removes untracked files from working directory. Use -n for dry run, -f to force.

Example

$ git clean -fd

Output

Removing temp/
Removing debug.log

git commit --amend

Undo

What it does

Modifies the most recent commit. Can change message or add forgotten files.

Example

$ git commit --amend -m "Better message"

Output

[main abc1234] Better message
 Date: Sat Jan 11

git log

Inspect

What it does

Shows commit history. Use --oneline for compact view, --graph for branch visualization.

Example

$ git log --oneline -5

Output

abc1234 Add feature
def5678 Fix bug
ghi9012 Initial commit

git show

Inspect

What it does

Displays detailed information about a commit, including the diff.

Example

$ git show abc1234

Output

commit abc1234
Author: John

+new code added

git blame

Inspect

What it does

Shows who last modified each line of a file. Great for finding when bugs were introduced.

Example

$ git blame index.js

Output

abc1234 (John 2026-01-11) const app = express();

💡 Pro Tip

Use git help <command> to see detailed help for any command.

Example: git help commit

About Git Commands

Quick reference for Git version control commands. From basic commits to advanced rebasing, find the command you need with syntax and examples. Perfect for daily development work.

Git is essential for modern development but has a steep learning curve. This cheat sheet covers everyday commands plus the less common ones you'll need when things go wrong—with clear examples.

How to use Git Commands

1

Search for what you want to do (e.g., 'undo commit').

2

Or browse by category (basics, branching, undoing).

3

View the command syntax.

4

Click to copy the command.

5

Paste into your terminal.

Examples

Basic workflow

The commands you'll use every day:

git status            # Check what changed
git add .             # Stage all changes
git commit -m "msg"   # Commit with message
git push              # Push to remote
git pull              # Get remote changes

Branching

Working with branches:

git branch            # List branches
git branch feature    # Create branch
git checkout feature  # Switch to branch
git checkout -b fix   # Create and switch
git merge feature     # Merge into current

Undoing changes

When you need to go back:

git restore file.txt      # Discard changes
git reset HEAD~1          # Undo last commit (keep changes)
git reset --hard HEAD~1   # Undo last commit (lose changes)
git revert abc123         # Create undo commit

Features

30+ essential Git commands
Grouped by workflow
Syntax with common options
Copy commands instantly
Links to Git documentation
Searchable by name or action

When to use this

  • •Daily development workflow
  • •Learning Git version control
  • •Recovering from mistakes
  • •Branch and merge operations
  • •Collaborating with teams
  • •Code review workflows

Common questions