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"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
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"New repositories start on main instead of master.
git config --global init.defaultBranch mainList every setting and the file it comes from.
git config --list --show-originAfter this, git st runs git status.
git config --global alias.st statusTurn the current folder into a Git repository.
git initDownload a repository and its full history.
git clone <url>
git clone <url> <folder-name>Download only the latest commit. Faster for big repositories.
git clone --depth 1 <url>See changed, staged and untracked files.
git status
git status -sChoose what goes into the next commit.
git add <file>
git add .
git add -pgit add -p lets you pick changes piece by piece.
Save the staged changes with a message.
git commit -m "Describe the change"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.
Unstaged changes, staged changes, or the difference between two branches.
git diff
git diff --staged
git diff main..<branch>Move the file and stage the change in one step.
git mv <old-name> <new-name>Remove a file from Git but keep it on disk.
git rm --cached <file>Local branches, or local and remote branches.
git branch
git branch -aStart new work on its own branch.
git switch -c <branch>Older equivalent: git checkout -b <branch>
Move to an existing branch. git switch - goes back to the previous one.
git switch <branch>
git switch -Give the branch you are on a new name.
git branch -m <new-name>Bring another branch’s commits into the current branch.
git merge <branch>Replay your commits on top of another branch for a straight history.
git rebase mainAvoid rebasing commits that other people have already pulled.
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.
Apply a single commit from somewhere else.
git cherry-pick <commit>Show remote names and URLs.
git remote -vConnect a local repository to one on GitHub, GitLab and so on.
git remote add origin <url>Download new commits without changing your files.
git fetch
git fetch --prune--prune removes local references to branches deleted on the remote.
Fetch and merge the remote branch into yours.
git pull
git pull --rebaseUpload your commits. -u links the branch so plain git push works next time.
git push
git push -u origin <branch>Overwrite the remote branch after a rebase or amend.
git push --force-with-leaseRefuses if someone else pushed in the meantime. Plain --force does not check.
Remove a branch from the remote.
git push origin --delete <branch>Put a file back to how it was at the last commit.
git restore <file>Uncommitted changes to that file are lost.
Take a file out of the next commit but keep your edits.
git restore --staged <file>Fix the message, or add forgotten staged files to the last commit.
git commit --amend
git commit --amend --no-editOnly amend commits you have not pushed yet.
Remove the commit; its changes stay staged.
git reset --soft HEAD~1Create a new commit that reverses an old one. Safe for shared branches.
git revert <commit>Reset everything to the last commit.
git reset --hard HEADAll uncommitted changes are lost and cannot be recovered.
Preview first with -n, then delete with -f.
git clean -n
git clean -fdEvery position HEAD has been at, including after a bad reset.
git reflogGet a commit back with git switch -c <branch> <commit>.
Full log, one line per commit, or a graph of all branches.
git log
git log --oneline
git log --oneline --graph --allCommits that changed a file, with the changes.
git log -p -- <file>Find commits whose message contains some text.
git log --grep="login"The message and changes of a single commit.
git show <commit>Show the last commit and author for every line of a file.
git blame <file>Put unfinished work aside and get a clean working folder.
git stash
git stash -u -m "work in progress"-u also stashes untracked files.
See what is saved.
git stash listpop applies and removes it; apply keeps it in the list.
git stash pop
git stash apply stash@{1}Remove one stash, or all of them.
git stash drop stash@{0}
git stash clearMark 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"Tags are not pushed with commits by default.
git push origin v1.0.0
git push --tagsList tags, delete one locally, then on the remote.
git tag
git tag -d v1.0.0
git push origin --delete v1.0.0Add patterns to a .gitignore file in the repository.
node_modules/
.env
*.log
dist/A leading ! un-ignores a file.
*.env
!.env.exampleShow 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.
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.
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.
git fetch downloads new commits but leaves your files alone. git pull fetches and then merges (or rebases) them into your current branch.
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.
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.