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

43 of 43 commands

Basics

11 commands
git init

Create 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 status

See 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 diff

Show changes in tracked files that have not been staged.

git diff --staged

Review the changes staged for the next commit.

git log --oneline --graph --all

Browse a compact graph of commits across all branches.

Branches

6 commands
git branch

List local branches; the asterisk marks the current one.

git branch -a

List 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

7 commands
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-edit

Caution: 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~1

Caution: Move back one commit and keep changes staged. Use on unpublished history.

git reset --hard HEAD

Caution: Discard staged and unstaged changes to tracked files. Untracked files in the way can also be overwritten.

git reflog

Find recent local HEAD positions, for example to locate a commit before a reset.

Remote / Push / Pull

6 commands
git remote -v

List 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 --prune

Update remote-tracking references and remove references to deleted remote branches.

git pull --ff-only

Fetch 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 push

Push the current branch using its configured upstream and push settings.

Stash

6 commands
git stash push -u -m "Work in progress"

Temporarily save tracked changes and untracked files. Ignored files are excluded.

git stash list

List 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 pop

Apply 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

7 commands
git merge <branch>

Integrate the named branch into the current branch.

git merge --abort

Abort 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~3

Caution: Edit, reorder or squash the last three commits interactively. Use for unpublished commits.

git rebase --continue

Continue a paused rebase after resolving conflicts and staging resolved files with git add.

git rebase --abort

Cancel 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.