COMMANDS

Gitcheatsheet

From your first repository to branches, remotes, and undoing a change.

Download .txt ↓

68 commands

Create or clone a repository

Start a repository in this directory

Change
git init

Use this for a project that is not already a Git repository.

Or clone an existing repository

Change
git clone REPOSITORY_URL

Creates a new project directory. Use this instead of git init when the project already has a remote repository.

Enter the project directory

Change
cd PROJECT_DIRECTORY

After cloning, enter the directory that Git created.

Set your commit name for this repository

Change
git config user.name "YOUR NAME"

Run inside the repository.

Set your commit email for this repository

Change
git config user.email "YOU@example.com"

This email is recorded in new commits.

Check, stage & commit

Check repository status

View
git status

Shows the branch, staged files, unstaged changes, and untracked files.

Review unstaged changes

View
git diff

Shows changes to tracked files; new untracked files are not included.

Stage a file

Change
git add -- FILE

Add the file content you want in the next commit.

Review staged changes

View
git diff --staged

Check exactly what the next commit will contain.

Commit staged changes

Change
git commit -m "Describe the change"

Saves the staged changes in repository history.

Show a compact status

View
git status --short

Work with branches

List local branches

View
git branch

Create and switch to a branch

Change
git switch -c BRANCH

Creates a new local branch.

Switch to an existing branch

Change
git switch BRANCH

Uncommitted changes can prevent switching.

Merge a branch into your current branch

Change
git merge BRANCH

Switch to the receiving branch first. Conflicts may need resolution.

Connect & sync a remote

Show configured remotes

View
git remote -v

Connect a remote to a local repository

Change
git remote add origin REPOSITORY_URL

For a repository created with git init. Cloning normally adds origin for you.

Check the current branch name

View
git branch --show-current

Use this name in place of BRANCH when pushing.

Push and track a branch

Change
git push -u origin BRANCH

Uploads commits and sets the upstream. Review the destination and content first.

Fetch remote changes

Change
git fetch origin

Updates remote-tracking references without merging into your branch.

List local and remote branches

View
git branch --all

Update by fast-forward only

Change
git pull --ff-only

Requires an upstream. Refuses an update that would need a merge commit.

Push new commits

Change
git push

Uses the configured upstream.

Read commit history

Recent commits

View
git log --oneline --graph -15

Review the last commit

View
git show --stat HEAD

Find the last change to each line

View
git blame -- FILE

Undo & recover

Unstage a file

Change
git restore --staged -- FILE

Keeps the working file. Requires an existing commit to restore from.

Discard unstaged edits

Discard
git restore -- FILE

Replaces tracked working content with the staged version. Uncommitted edits can be lost.

Undo a committed change

Change
git revert COMMIT

Creates an inverse commit; conflicts may need resolution.

Find earlier HEAD positions

View
git reflog -15

Useful for finding commits after a mistaken branch move.

Stash unfinished work

Stash tracked and untracked edits

Change
git stash push -u -m "Pause current work"

Ignored files are not included. Review the files before stashing.

List saved stashes

View
git stash list

Inspect a saved stash

View
git stash show -p stash@{0}

Restore without removing the stash

Change
git stash apply stash@{0}

May conflict with current edits.

Restore and remove a successfully applied stash

Change
git stash pop

Conflicts can leave the stash available.

Remove one saved stash

Delete
git stash drop stash@{0}

Deletes this saved entry. Confirm you no longer need it.

Reshape local history

Stage selected hunks

Change
git add -p -- FILE

Read each hunk and choose y or n.

Amend the last commit without editing its subject

Change
git commit --amend --no-edit

Stages must already contain the intended correction. Replaces the commit.

Undo the last commit, keeping its changes staged

Change
git reset --soft HEAD~1

Moves the branch back; use on your own unpublished history.

Rebase current work onto a branch

Change
git rebase BASE_BRANCH

Replays commits and can stop at conflicts.

Choose, reorder, or squash recent commits

Change
git rebase -i HEAD~3

Edit the todo list for three local commits; commit IDs change.

Move only a selected commit range

Change
git rebase --onto NEW_BASE OLD_BASE BRANCH

Replays BRANCH commits after OLD_BASE onto NEW_BASE.

Create a correction for an earlier commit

Change
git commit --fixup=COMMIT

Stage the correction first. Pair with autosquash.

Autosquash local correction commits

Change
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash BASE

Bash example. BASE is excluded from the replay; use the parent of the earliest target.

Continue a resolved rebase

Change
git rebase --continue

Stage resolved files first.

Cancel a rebase

Change
git rebase --abort

Returns to the state from before this rebase.

Copy a selected commit onto your branch

Change
git cherry-pick COMMIT

Can conflict; copies the change with a new commit ID.

Stage a branch as one combined change

Change
git merge --squash BRANCH

Does not create a commit or merge parent; review and commit afterward.

Investigate and recover

Inspect recent reference movements

View
git reflog --date=iso

Local reflogs are not permanent backups.

Preserve a recovered commit on a new branch

Change
git branch recovered COMMIT

Creates a reference without replacing your current branch.

Find changes to a string occurrence count

View
git log -S "STRING" --oneline -- FILE

Find diffs matching a pattern

View
git log -G "REGEX" -p -- FILE

Start searching for a regression

Change
git bisect start BAD_COMMIT GOOD_COMMIT

Temporarily checks out candidate revisions.

Test each bisect candidate automatically

Change
git bisect run sh ./check.sh

The checker returns 0 for good, 1–127 except 125 for bad, and 125 to skip.

Finish the bisect session

Change
git bisect reset

Returns to the branch or revision from before bisect.

Search tracked file contents

View
git grep -n "PATTERN"

Inspect authorship of lines

View
git blame -L 10,25 -- FILE

Files, tags, and working copies

Rename a tracked file

Change
git mv -- OLD_NAME NEW_NAME

For a case-only rename on Windows, use an intermediate name.

Stop tracking while keeping the local file

Change
git rm --cached -- FILE

Commit the removal and use an appropriate ignore rule.

Record executable permission

Change
git update-index --chmod=+x -- SCRIPT

Changes the Git index even when filesystem mode support differs.

Inspect recorded file modes

View
git ls-files --stage

Create an annotated release tag

Change
git tag -a TAG -m "Release description"

Creates a local tag; it is not automatically pushed.

List tag details

View
git show TAG

Add another working directory

Change
git worktree add ../WORKTREE BRANCH

The branch must be available for checkout.

Inspect all working directories

View
git worktree list

Save a patch for a local edit

Change
git diff > change.patch

Untracked files are not included.

Check whether a patch applies

View
git apply --check change.patch

Apply a reviewed patch

Change
git apply change.patch

Changes working files; does not commit them.