Helper-KitCheatsheets

Git Cheatsheet

Git commands for commits, branches, remotes, stash, tags and undoing mistakes, with risky commands clearly marked.

Git Cheatsheet · Helper-Kit

Words in <angle brackets> are placeholders: replace them, including the brackets.

52 entries

Setup

Set your name and email

Saved once per computer and attached to every commit you make.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Set the default branch name

New repositories start on main instead of master.

git config --global init.defaultBranch main

Show your settings

List every setting and the file it comes from.

git config --list --show-origin

Create a shortcut (alias)

After this, git st runs git status.

git config --global alias.st status

Start a project

Create a repository

Turn the current folder into a Git repository.

git init

Clone a repository

Download a repository and its full history.

git clone <url>
git clone <url> <folder-name>

Shallow clone

Download only the latest commit. Faster for big repositories.

git clone --depth 1 <url>

Daily work

Check status

See changed, staged and untracked files.

git status
git status -s

Stage changes

Choose what goes into the next commit.

git add <file>
git add .
git add -p

git add -p lets you pick changes piece by piece.

Commit

Save the staged changes with a message.

git commit -m "Describe the change"

Stage and commit tracked files

Commit all changes to files Git already tracks.

git commit -am "Describe the change"

New (untracked) files are skipped. Add them with git add first.

See what changed

Unstaged changes, staged changes, or the difference between two branches.

git diff
git diff --staged
git diff main..<branch>

Rename or move a file

Move the file and stage the change in one step.

git mv <old-name> <new-name>

Stop tracking a file

Remove a file from Git but keep it on disk.

git rm --cached <file>

Branches

List branches

Local branches, or local and remote branches.

git branch
git branch -a

Create and switch to a branch

Start new work on its own branch.

git switch -c <branch>

Older equivalent: git checkout -b <branch>

Switch branch

Move to an existing branch. git switch - goes back to the previous one.

git switch <branch>
git switch -

Rename the current branch

Give the branch you are on a new name.

git branch -m <new-name>

Merge a branch

Bring another branch’s commits into the current branch.

git merge <branch>

Rebase onto another branch

Replay your commits on top of another branch for a straight history.

git rebase main

Avoid rebasing commits that other people have already pulled.

Delete a branch

Careful

Delete a merged branch. -D deletes it even if it isn’t merged.

git branch -d <branch>
git branch -D <branch>

With -D, commits that exist only on that branch are lost.

Copy one commit to this branch

Apply a single commit from somewhere else.

git cherry-pick <commit>

Remotes

List remotes

Show remote names and URLs.

git remote -v

Add a remote

Connect a local repository to one on GitHub, GitLab and so on.

git remote add origin <url>

Fetch

Download new commits without changing your files.

git fetch
git fetch --prune

--prune removes local references to branches deleted on the remote.

Pull

Fetch and merge the remote branch into yours.

git pull
git pull --rebase

Push

Upload your commits. -u links the branch so plain git push works next time.

git push
git push -u origin <branch>

Force push safely

Careful

Overwrite the remote branch after a rebase or amend.

git push --force-with-lease

Refuses if someone else pushed in the meantime. Plain --force does not check.

Delete a remote branch

Careful

Remove a branch from the remote.

git push origin --delete <branch>

Undo

Discard changes to a file

Careful

Put a file back to how it was at the last commit.

git restore <file>

Uncommitted changes to that file are lost.

Unstage a file

Take a file out of the next commit but keep your edits.

git restore --staged <file>

Change the last commit

Fix the message, or add forgotten staged files to the last commit.

git commit --amend
git commit --amend --no-edit

Only amend commits you have not pushed yet.

Undo the last commit, keep changes

Remove the commit; its changes stay staged.

git reset --soft HEAD~1

Undo a pushed commit

Create a new commit that reverses an old one. Safe for shared branches.

git revert <commit>

Throw away all local changes

Careful

Reset everything to the last commit.

git reset --hard HEAD

All uncommitted changes are lost and cannot be recovered.

Delete untracked files

Careful

Preview first with -n, then delete with -f.

git clean -n
git clean -fd

Find lost commits

Every position HEAD has been at, including after a bad reset.

git reflog

Get a commit back with git switch -c <branch> <commit>.

History

Show history

Full log, one line per commit, or a graph of all branches.

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

History of one file

Commits that changed a file, with the changes.

git log -p -- <file>

Search commit messages

Find commits whose message contains some text.

git log --grep="login"

Show one commit

The message and changes of a single commit.

git show <commit>

Who changed each line

Show the last commit and author for every line of a file.

git blame <file>

Stash

Stash your changes

Put unfinished work aside and get a clean working folder.

git stash
git stash -u -m "work in progress"

-u also stashes untracked files.

List stashes

See what is saved.

git stash list

Bring a stash back

pop applies and removes it; apply keeps it in the list.

git stash pop
git stash apply stash@{1}

Delete a stash

Careful

Remove one stash, or all of them.

git stash drop stash@{0}
git stash clear

Tags

Create a tag

Mark a release. -a creates an annotated tag with a message.

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

Push tags

Tags are not pushed with commits by default.

git push origin v1.0.0
git push --tags

List and delete tags

Careful

List tags, delete one locally, then on the remote.

git tag
git tag -d v1.0.0
git push origin --delete v1.0.0

.gitignore

Ignore files

Add patterns to a .gitignore file in the repository.

node_modules/
.env
*.log
dist/

Keep an exception

A leading ! un-ignores a file.

*.env
!.env.example

Why is a file ignored?

Show which rule is ignoring it.

git check-ignore -v <file>

💡 Search matches titles, descriptions and the commands themselves. Copy puts the exact text on your clipboard; Print gives a clean sheet without menus or buttons. Searching happens in your browser; nothing is sent anywhere.

About This Cheatsheet

Git keeps the history of your project, so you can see what changed, work on features in separate branches and undo mistakes. This cheatsheet lists the commands you use most, grouped the way you work: setup, daily work, branches, remotes, undoing changes, history, stash, tags and .gitignore. Commands that delete work or rewrite history, such as git reset --hard, git clean and force pushes, are marked Careful and explain what you lose. Where Git has a newer, clearer command (git switch and git restore), that is shown first, with the older form in a note. Use the search box to find a command by what it does ("undo", "rename") or by name, copy it with one click, and replace the <placeholders> with your own values.

Quick Tips

  • Run git status before and after anything risky, so you know exactly what will change.
  • Commit small, focused changes with messages that say why, not just what.
  • Use git revert, not git reset, to undo commits other people may already have.
  • Prefer git push --force-with-lease over --force: it refuses to overwrite someone else's work.
  • Lost a commit after a reset? git reflog usually still has it.

Frequently Asked Questions

How do I undo the last commit?

If you have not pushed it, git reset --soft HEAD~1 removes the commit and keeps your changes staged. If you have pushed it, use git revert <commit> to add a new commit that reverses it.

What is the difference between git fetch and git pull?

git fetch downloads new commits but leaves your files alone. git pull fetches and then merges (or rebases) them into your current branch.

Should I use git switch or git checkout?

Both work. git switch (for branches) and git restore (for files) were added in Git 2.23 to split up the many jobs of git checkout, and are easier to read.

What is the difference between Git and GitHub?

Git is the version control tool on your computer. GitHub, GitLab and Bitbucket are websites that host Git repositories and add features like pull requests.

Was this tool helpful?

More in this category