Version control / Git
Git
cheat sheet.
Start a repository, work with branches, and put things right when a commit goes wrong. Your everyday Git reference.
- Commands
- 43
- Categories
- 6
Basics
git initCreate a Git repository in the current directory.
git clone <url>Clone a repository, including its history.
git config --global user.name "Your Name"Set the author name for future commits across your repositories.
git config --global user.email "you@example.com"Set the author email for future commits across your repositories.
git statusSee the current branch, staged changes and untracked files.
git add <file>Stage the changes in a file for the next commit.
git add .Stage additions, modifications and deletions under the current directory. Review before committing.
git commit -m "Describe the change"Create a commit from the staged changes.
git diffShow changes in tracked files that have not been staged.
git diff --stagedReview the changes staged for the next commit.
git log --oneline --graph --allBrowse a compact graph of commits across all branches.
Branches
git branchList local branches; the asterisk marks the current one.
git branch -aList local and remote-tracking branches.
git switch -c <branch>Create a branch and switch to it.
git switch <branch>Switch to an existing local branch.
git switch -Switch back to the previously checked-out branch.
git branch -d <branch>Delete a local branch after Git checks it is merged into its upstream, or HEAD if no upstream exists.
Undo changes
git restore --staged <file>Unstage a file while keeping its working-tree changes.
git restore <file>Caution: Discard unstaged changes in a tracked file, restoring it from the index. Discarded edits cannot be recovered by Git.
git commit --amend --no-editCaution: Replace the latest commit, including staged changes, keeping its message. Use on unpublished commits.
git revert <commit>Create a new commit that undoes a regular commit. Suitable for shared history.
git reset --soft HEAD~1Caution: Move back one commit and keep changes staged. Use on unpublished history.
git reset --hard HEADCaution: Discard staged and unstaged changes to tracked files. Untracked files in the way can also be overwritten.
git reflogFind recent local HEAD positions, for example to locate a commit before a reset.
Remote / Push / Pull
git remote -vList remotes and their fetch and push URLs.
git remote add origin <url>Add a remote called origin to a repository that does not already have one.
git fetch --pruneUpdate remote-tracking references and remove references to deleted remote branches.
git pull --ff-onlyFetch and update the current branch only if it can fast-forward. Stop if histories have diverged.
git push -u origin <branch>Push a branch and set its upstream for future pull and push commands.
git pushPush the current branch using its configured upstream and push settings.
Stash
git stash push -u -m "Work in progress"Temporarily save tracked changes and untracked files. Ignored files are excluded.
git stash listList saved stashes, newest first.
git stash show -p --include-untracked stash@{0}Inspect the latest stash as a patch, including saved untracked files.
git stash apply stash@{0}Apply the latest stash and keep the saved entry. Conflicts may need resolving.
git stash popApply the latest stash and remove it if application succeeds. Conflicts leave the stash saved.
git stash drop stash@{0}Caution: Remove the latest stash entry. Make sure its changes are no longer needed.
Merge / Rebase
git merge <branch>Integrate the named branch into the current branch.
git merge --abortAbort a conflicted merge. Start merges with a clean working tree so local changes can be restored reliably.
git rebase <branch>Caution: Replay current-branch commits on the named branch. This rewrites history; use for unpublished commits.
git rebase -i HEAD~3Caution: Edit, reorder or squash the last three commits interactively. Use for unpublished commits.
git rebase --continueContinue a paused rebase after resolving conflicts and staging resolved files with git add.
git rebase --abortCancel a rebase and return to the state before it began.
git cherry-pick <commit>Apply the changes from a regular commit as a new commit on the current branch.