Skip to main content

Git Cheats

Cheat sheet for Git commands.

info

This page was automatically generated from a navi cheat file available at difranca/navi-cheats.
Navi is an interactive cheatsheet tool for the command-line. To learn more about it, visit denisidoro/navi.


Configโ€‹

Configure Git across all local repositories.

CommandDescription
git config --global user.name {name}Set global Git user name
git config --global user.email {email}Set global Git user email
git config --global color.ui autoSet automatic Git CLI coloring

Setupโ€‹

Initialize and clone repositories.

CommandDescription
git initInitialize a Git repository
git clone {repository}Clone a Git repository default branch to current directory
git clone -b {branch_name} {repository} {clone_directory}Clone a Git repository branch
git rev-parse --show-toplevelGet local repository top level directory

Stage & Changesโ€‹

Work with stage and unstaged changes.

CommandDescription
git statusShow modified and staged files
git add .Stage all files
git add {file}Stage a file
git reset {file}Unstage a file
git reset .Unstage all files
git diffShow diff of unstaged files
git diff --stagedShow diff of staged files
git commit -m {message}Commit staged changes
git commit --date="{date}" -m "{message}"Commit staged changes modifying the author date

Branchโ€‹

Work with branches

CommandDescription
git branchList branches
git branch {name}Create a new branch at the current commit
git checkout {branch}Switch to another branch

Log & Compareโ€‹

View logs and diffs in branches.

CommandDescription
git logShow commit history for active branch
git log {branch}..{second_branch}Show commits on first branch that are not on the second one
git log --follow {file}Show the commits that changed a file
git show {commit_sha}Show a commit object details

Updateโ€‹

Retrieve updates from repositories and push changes.

CommandDescription
git remote -vView all remote for a Git repository
git remote add {name} {url}Add a remote for a Git repository
git remote rename {remote_name} {new_name}Renames a remote for a Git repository
git remote remove {remote_name}Remove a remote for a git repository
git fetch {remote_name}Fetch all branches from Git remote
git pullPull commits from remote
git merge {branch}Merge specified branch into current branch
git merge --abortRollback to pre-merge state
git pushPush active branch commits to remote
git push -u {remote_name} {current_branch}Push local branch commits that does not have upstream to remote
git push {remote_name} {current_branch}:{branch}Push local branch commits to another branch

Rewriteโ€‹

Rewrite commits history.

CommandDescription
git commit --amendAmend the last commit with staged changes
git commit --amend -m {message}Amend the last commit message
git rebase {branch}Apply commits from specified branch ahead of current branch
git reset --hard HEAD~{number_of_commits}Reset commits going back to a number of commits before HEAD (destroy changes)
git reset --soft HEAD~{number_of_commits}Reset commits going back to a number of commits before HEAD (keep changes)
git reset --hard {commit_sha}Reset commits going back to a specific commit (destroy changes)
git reset --soft {commit_sha}Reset commits going back to a specific commit (keep changes)

Tagโ€‹

Create and manage tags.

CommandDescription
git tagList tags
git tag {tag_name}Create a lightweight tag
git tag -a {tag_name} -m {message}Create an annotated tag
git tag -a {tag_name} {commit_sha} -m {message}Create a tag on a specific commit
git tag -d {tag}Delete a local tag
git push origin {tag}Push a tag to remote
git push origin --tagsPush all tags to remote
git push origin --delete {tag}Delete a remote tag

Cherry-pickโ€‹

Apply commits from other branches.

CommandDescription
git cherry-pick {commit_sha}Apply a commit to the current branch
git cherry-pick --no-commit {commit_sha}Apply a commit without committing (stage changes only)
git cherry-pick --abortAbort a cherry-pick in progress

Temporaryโ€‹

Temporarily store modified files

CommandDescription
git stashSave modifications for later use and rollback to last the last commit
git stash listList saved stashes
git stash popApply stashed changes into the current branch (discard saved stash)
git stash applyApply stashed changes into the current branch (keep saved stash)
git stash dropDiscard saved stash

GitHubโ€‹

Interact with GitHub using gh CLI and quick links.

CommandDescription
https://github.com/pulls?&q=author:\{user\}+is:open+is:prSee all open pull requests of a user on Github
gh pr listList pull requests for current repository
gh pr createCreate a pull request for the current branch
gh pr view {pr_number}View a pull request in the terminal
gh pr checkout {pr_number}Checkout a pull request locally
gh issue listList issues for current repository
gh issue createCreate a new issue
gh issue view {issue_number}View an issue in the terminal
gh repo clone {repository}Clone a GitHub repository
gh browseView current repository in browser