/git-cheat-sheet

A collection of some of the most useful Git commands

MIT LicenseMIT

Git Cheat Sheet

A collection of some of the most useful Git commands

Table of Contents

Settings

There are 3 configuration levels:

  • User-specific settings. Options set with the --global flag are stored in ~/.gitconfig
  • Repository-specific settings. Options set with or without the --local flag are stored in <repo>/.git/config
  • System-wide settings. Options set with the --system flag are stored in $(prefix)/etc/gitconfig

Setting Configs

Set repository option.

git config [<config-level>] [--add] name value

Adds a new line to the option without altering any existing values.

--add name value

Create an alias for git commands.

alias.* '<command>'

A boolean to make git-blame show email in output.

blame.showEmail value

Set up default git branch sorting criteria. Possible values: authordate, numparent, refname, upstream

branch.sort value

A boolean to make git-clean do nothing unless given -f, -i or -n. Possible values: true, false

clean.requireForce value

Turn on/off all Git’s colored terminal output. Possible values: true, false, auto.

color.ui value

Set up default text editor.

core.editor value

Makes git case sensitive. Possible values: true, false.

core.ignorecase value

Sets a diff algorithm. Possible values: default, histogram, minimal, myers, patience.

diff.algorithm value

Use the specified name for the initial branch in the newly created repository. If not specified, fall back to the default name (currently master, but this is subject to change in the future; the name can be customized via the init.defaultBranch configuration variable).

init.defaultBranch value

Specify the style in which conflicted hunks are written out to working tree files upon merge. The default is "merge", which shows a <<<<<<< conflict marker, changes made by one side, a ======= marker, changes made by the other side, and then a >>>>>>> marker. An alternate style, "diff3", adds a ||||||| marker and the original text before the ======= marker.

merge.conflictstyle value

Set up your custom merge resolution and diff tools.

merge.tool value

Defines the action git push should take. Possible values: nothing, matching, upstream, current, simple (safest option and is well-suited for beginners).

push.default value

When set to true, automatically create a temporary stash entry before the operation begins, and apply it after the operation ends. Possible values: true, false.

rebase.autostash value

Show full by default diff when using git stash show. Possible values: true, false.

stash.showPatch value

Show files which are not currently tracked by Git. Directories which contain only untracked files, are shown with the directory name only. Showing untracked files means that Git needs to lstat() all the files in the whole repository, which might be slow on some systems. So, this variable controls how the commands displays the untracked files.

status.showUntrackedFiles value

Set username.

user.name value

Set email.

user.email value

Open the configuration file in a text editor for manual editing.

-e
--edit

Getting Configs

Get repository option.

git config [<config-level>] [--show-origin] [--get] name

Augment the output of all queried config options with the origin type (file, standard input, blob, command line) and the actual origin (config file path, ref, or blob id if applicable).

--show-origin

Get repository options.

git config [<config-level>] [--show-origin] -l --list

Augment the output of all queried config options with the origin type (file, standard input, blob, command line) and the actual origin (config file path, ref, or blob id if applicable).

--show-origin

List all variables set in config file, along with their values.

-l
--list

Removing Configs

Remove repository options.

git config [<file-option>] --unset name

Remove the line matching the key from config file.

--unset name

Setting Up Repository

Initializing Repository

Create an empty Git repository or reinitialize an existing one.

git init [--template=<template_directory>] [--bare] [directory]

Specify the directory from which templates will be used.

--template=<template_directory>

Create a bare repository.

--bare

Cloning Repository

Clone a repository into a current directory if not specified.

git clone [-o <name>] [-b <name>]
          [--depth <depth>] [--single-branch]
          [--recursive]
          [--template=<template_directory>]
          <repository> [<directory>]

Instead if using the remote name origin to keep track of the upstream repository, use .

-o <name>
--origin <name>

Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD, point to branch instead.

-b <name>
--branch <name>

Clone only history leading up to the main branch or the one specified by -b option.

--single-branch

Clone repository and all its submodules recursively.

--recursive

Clone the repository and apply template.

--template=<template_directory>

Clone the repository and only clone the history of commits specified by the option depth. If depth is equal to one, only the most recent commit is included.

--depth=<depth>

Saving Changes

Adding Files

Add file contents to the index.

git add [-n | --dry-run] [-f | --force] [-u | --update]
        [-i | --interactive] [-p | --patch]
        [<pathspec>…]

Do not actually add files; only show which ones would be added.

-n
--dry-run

Allow adding otherwise ignored files.

-f
--force

Add to index modified and deleted tracked files but it will never stage new files.

-u
--update

Begin an interactive staging session that lets you choose portions of a file to add to the next commit.

You can select one of the following options and type return:

  • y - stage this hunk
  • n - do not stage this hunk
  • q - quit; do not stage this hunk nor any of the remaining ones
  • a - stage this hunk and all later hunks in the file
  • d - do not stage this hunk nor any of the later hunks in the file
  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • j - leave this hunk undecided, see next undecided hunk
  • J - leave this hunk undecided, see next hunk
  • k - leave this hunk undecided, see previous undecided hunk
  • K - leave this hunk undecided, see previous hunk
  • s - split the current hunk into smaller hunks
  • e - manually edit the current hunk
  • ? - print help
-p
--patch

Staging only untracked files.

List of available subcommands.

  • Type a (for "add untracked")
  • Then * (for "all")
  • Then q (to quit)
-i
--interactive

Add modified files to index.

git add $(git ls-files --modified)

Removing Files

Remove file from the working directory.

git rm [-n | --dry-run] [-r] [--] <pathspec>

Do not actually remove the files, just show if they exist in the index.

-n
--dry-run

Remove recursively directory from the working directory.

-r

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).

--

Remove a file from index but not from disk.

--cached

Remove files from the working directory.

git rm $(git ls-files --deleted)

Renaming Files

Move or rename a file, a directory, or a symlink.

git mv <source> <destination>

Alternate way to move or rename a file, directory or symlink.

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

Committing Files

Record changes to the repository.

git commit [-a | --all] [-m <msg> | --message <msg>]
           [-v | --verbose] [-n | --dry-run] [-o | --only]
           [--allow-empty] [--author=<author>]

Stage all modified and deleted paths, but new files you have not told git about are not affected.

-a
--all

Use the given message as the commit message.

-m <msg>
--message <msg>

Use the given message as the commit message and override the author name used in the commit.

--author=<author>

Allow recording an empty commit with no file changes.

--allow-empty

Show unified diff of all file changes.

-v
--verbose

Do not create a commit, but show a list of paths that are to be committed.

-n
--dry-run

Skip staging area and commit files, this only works with tracked files.

-o
--only

Stashing Files

Stash away changes to dirty working directory.

git stash [-p | --patch] [-k | --[no-]keep-index] [-u | --include-untracked]

Interactively select hunks from diff between HEAD and the working tree to stashed.

-p
--patch

Stash only unstaged changes.

-k
--keep-index

All changes already added to the index are undone.

--no-keep-index

Save local changes including untracked files.

-u
--include-untracked

List the stashes stored in the stack.

git stash list

Show the changes recorded in the stash as a diff. Latest stash is used if stash is not provided.

git stash show [-p | --patch] [<stash>]

Show the changes recorded in the stash as a diff in patch format.

-p
--patch

Save local changes to a new stash with message.

git stash save [<message>]

Apply the changes recorded in the stash.

git stash apply [<stash>]

Remove and apply a single stashed state from the stack.

git stash pop

Creates and checks out a new branch named starting from the commit at which the stash was originally created, applies the changes recorded in stash to the new working tree and index, then drops the stash. Latest stash is used if stash is not provided.

git stash branch <name> [<stash>]

Remove a single stashed state from the stack.

git stash drop [<stash>]

Remove all the stashed states.

git stash clear

Inspecting Changes

Display State

Show working directory status.

git status [-s | --short] [-u [<mode>] | --untracked-files [<mode>]] [--ignored] [--] [<pathspec>…]

Give the output in short format.

-s
--short

Show untracked files.

The mode parameter is used to specify the handling of untracked files. It is optional: it defaults to all.

The possible options are:

  • no - Show no untracked files.
  • normal - Shows untracked files and directories.
  • all - Also shows individual files in untracked directories.
-u [<mode>]
--untracked-files

Status of ignored files.

--ignored

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).

--

Display Objects

Show details of the object (blobs, trees, tags and commits).

git show [-w | --ignore-all-space]
         [--name-only] [--name-status]
         [--stat] [--pretty | --format=<format>]
         [<object>…]

Ignore white space when comparing lines.

-w
--ignore-all-space

Show only names of changed files without the diffs.

--name-only

Show only names and status of changed files without the diffs.

--name-status

Generate diffstat instead of patch.

--stat

Show details of an object with pretty format.

--pretty <object>
--format=<format> <object> 

Show details of commit via commit message pattern matching.

git show :/<commit>

Show details of a file on a object.

git show <object>:<file>

Export file to path.

git show <branch>:<file> > <path>

Logging

Show commit logs.

git log [-p | --patch] [[-n | --max-count] <number>]
        [--follow] [--first-parent]
        [--oneline]
        [--stat] [--shortstat] [--summary]
        [--[no-]merges] [--author=<pattern>] [--grep=<pattern>]
        [<pathspec>…]

Generate diff in patch format for a specific file.

-p
--patch

Limit the number of commits to output.

-n
--max-count

Show changes over time for a specific file even if the file was renamed.

--follow

Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge.

--first-parent

Condense each commit to a single line.

--oneline

Show statistics for files modified in each commit.

--stat

Display only the changed/insertions/deletions line from the --stat command.

--shortstat

Generate condensed summary of extended header information.

--summary

Display only merge commits.

--merges

Display commits without merge commits.

--no-merges

Search for commits by a particular author. The argument can be a plain string or a regular expression.

--author=<pattern>

Search for commits with a commit message. The argument can be a plain string or a regular expression.

--grep=<pattern>

Get a list of commits made in the last two weeks.

git log --since='2 weeks'

Show only commits that occur in the range.

git log <since>..<until>

Match regexps ignoring case.

git log [-i <regex> | --regexp-ignore-case <regex>]

Maximum number of commits to display.

git log [-n <number> | --max-count <number>]

Show what every contributor has been getting up to across all branches.

git log --all --oneline --no-merges

Show changes since two weeks.

git log --no-merges --raw --since='2 weeks ago'

Generate a changelog.

git log --oneline --no-merges <last tag>..HEAD

Check which changes you’re about to pull.

git log --oneline --no-merges HEAD..<remote>/<branch>

Review what you’re about to push.

git log --oneline --no-merges <remote>/<branch>..HEAD

View complex logs.

git log --graph --all --decorate --stat --date=iso

Summarize commits of current branch.

git shortlog [--all] [--no-merges] [-s] [-n]

Summarize commits of all refs.

--all

Summarize commits and supress the ones with more than one parent (merge commits).

--no-merges

Summarize commits and suppress commit message.

-s
--summary

Sort according to number of commits.

-n
--numbered

Number of commits of each contributor and ensures that merge commits aren’t being counted.

git shortlog --all --no-merges -sn 

Display Differences

Show changes between commits, commit and working tree, etc.

git diff [<options>] [<object>]

Show changes between two arbitrary commits.

git diff [<options>] <commit> <commit> [--] [<path>…]

This is synonymous to the previous form. If on one side is omitted, it will have the same effect as using HEAD instead.

git diff [<options>] <commit>..<commit> [--] [<path>…]

This form is to view the changes on the branch containing and up to the second , starting at a common ancestor of both . You can omit any one of , which has the same effect as using HEAD instead.

git diff [<options>] <commit>..<commit> [--] [<path>…]

Ignore white space when comparing lines.

-w
--ignore-all-space

Show staged changes against your HEAD.

--cached
--staged

Show inline word diff.

--word-diff

Look for differences whose added or removed line matches the given pattern.

-G <pattern>

Blaming

Show what revision and author last modified each line of a file.

git blame [-n | --show-number] [-s]
          [-e | --show-email] [-w]
          [-L <range>] [-M] [-C]
          <file>

Show the line number in the original commit (Default: off).

-n
--show-number

Suppress the author name and timestamp from the output.

-s

Show the author email instead of author name (Default: off).

-e
--show-email

Ignore whitespace when comparing the parent’s version and the child’s to find where the lines came from.

-w

Restrict the output to the requested line range.

-L <starting-line>,<ending-line>

Detect moved or copied lines within in the same file. This will report the original author of the lines instead of the last author that moved or copied the lines.

-M

Detect lines that were moved or copied from other files. This will report the original author of the lines instead of the last author that moved or copied the lines.

-C

Listing Files

Show information about files in the index and the working directory.

git ls-files [-t] [-v]
             (--[cached | deleted | others | ignored | stage | modified])*
             (-[c | d | o | i | s | m])*
             [--exclude-standard]

Show cached files.

-c
--cached

Show deleted files.

-d
--deleted

Show all untracked or ignored files.

-o
--others

Show only ignored files in the output. When showing files in the index, print only those matched by an exclude pattern. When showing "other" files, show only those matched by an exclude pattern.

-i
--ignored

Show staged contents (mode bits, object name and stage number).

-s
--stage

Show modified files.

-m
--modified

This option identifies the file status with the following tags.

You can select one of the following options and type return:

  • H - cached
  • S - skip-worktree
  • M - unmerged
  • R - removed/deleted
  • C - modified/changed
  • K - to be killed
  • ? - other
-t

Similar to -t, but use lowercase letters for files that are marked as assume unchanged.

-v

Add the standard Git exclusions: .git/info/exclude, .gitignore in each directory, and the user’s global exclusion file.

--exclude-standard

Show all untracked files.

--others --exclude-standard

Show all ignored files.

--others --ignored --exclude-standard

Undoing Changes

Reverting Files

Return the state of your unstaged files as they were before.

git checkout (-p | --patch) [--] [<pathspec>…]

Interactively select hunks in diff.

-p
--patch

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).

--

This turns the file that resides in the working directory into an exact copy of the one from object (commit, branch, etc).

git checkout [<object>] [<pathspec>…]

Update all files in the working directory to match the specified commit. This will put you in a detached HEAD state.

git checkout <commit> .

This turns the file that resides in the working directory into an exact copy of the one from stash@{n} and adds it to the staging area.

git checkout stash@{n} <file>

The contents from a specific side of the merge can be checked out of the index.

git checkout [--ours | --theirs] [<tree-ish>]

Gives the version from the branch that holds your work that is being rebased.

--theirs

Gives the version from the branch the changes are rebased onto.

--ours

Reverting Commit

Generate a new commit that undoes all of the changes introduced in the commit.

git revert [-e | --edit] [--no-edit] [-n | --no-commit]  <commit>

This is a default option and doesn't need to be specified. This option will open the configured system editor and prompts you to edit the commit message prior to committing the revert.

-e
--edit

This is the inverse of the -e option. The revert will not open the editor.

--no-edit

Passing this option will prevent git revert from creating a new commit that inverses the target commit. Instead of creating the new commit this option will add the inverse changes to the Staging Index and Working Directory.

-n
--no-commit

Sequencer subcommands.

git revert (--continue | --skip | --abort | --quit)

Continue the operation in progress using the information in .git/sequencer. Can be used to continue after resolving conflicts in a failed cherry-pick or revert.

--continue

Skip the current commit and continue with the rest of the sequence.

--skip

Skip the current commit and continue with the rest of the sequence.

--abort

Skip the current commit and continue with the rest of the sequence.

--quit

Revert initial commit.

git update-ref -d HEAD

Delete ref.

git update-ref -d <ref>

Revert merge commit. The -m followed by the 1 indicates that we want to keep the parent side of the merge (the branch we are merging into).

git revert -m 1 <commit>
           --mainline

Unstaging Staged File

Unstage all files and leave the working directory unchanged.

git reset [-p | --patch] [<pathspec>]

Select diff hunks to remove from the index.

-p
--patch

Unstage all files and leave the working directory unchanged.

git reset HEAD

Restore specified path in the working tree.

git restore --staged <file>

Resetting Current Commit

Reset current HEAD to the specified state.

git reset [--soft | --mixed | --hard | --keep] [<commit>]

Do not reset the index file nor the working directory.

--soft

Reset the index but not the working directory (default).

--mixed

Reset the index and working directory to the given commit.

--hard

Like --hard, but keep local working directory changes.

--keep

Get back to where things were a period of time ago (e.g. when you screw up a merge).

git reset --hard master@{"300 minutes ago"}

Removing Untracked Files

Remove untracked files from working tree.

git clean [-d] [-f | --force] [-i | --interactive] [-n | --dry-run] [-x | -X] <path>

Normally, when no is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant.

-d

Required when clean.requireForce is true (default).

-f
--force

Only show what would and what would not be removed.

-n
--dry-run

Show what would be done and clean files interactively.

-i
--interactive

Don’t use the standard ignore rules, but still use the ignore rules given with -e options from the command line.

-x

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

-X

Rewriting History

Fix Up Commit

Record changes.

git commit [--amend] [--no-edit]
           [--author=<author>] [--reset-author]
           [--reuse-message=<commit>]

Add forgotten files and edit the last commit's message.

--amend

Use the selected commit message without launching an editor.

--no-edit

Override the author name used in the commit.

--author="Name <your_email@youremail.com>"

The author of the resulting commit now belongs to the committer.

--reset-author

Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

--reuse-message=<commit>

Rebase Commits

Reapply commits on top of another base tip.

git rebase [-i | --interactive] [--autostash] [--root] [<branch>]

Rebase the current branch onto branch.

-i
--interactive

Automatically stashes any local changes made to your working copy before rebasing and reapplies them after the rebase is completed.

--autostash

Rebase to the root commit of your current branch.

--root

It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolve any such merge failure and run an option.

git rebase (--continue | --abort)

After resolving the conflict manually and updating the index with the desired resolution, you can continue the rebasing process.

--continue

Abort the rebase operation and reset HEAD to the original branch. If was provided when the rebase operation was started, then HEAD will be reset to . Otherwise HEAD will be reset to where it was when the rebase operation was started.

--abort

Copy Commits

Apply the changes introduced by some existing commits.

git cherry-pick (--continue | --skip | --quit | --abort)

Continue the operation in progress using the information in .git/sequencer. Can be used to continue after resolving conflicts in a failed cherry-pick or revert.

--continue

Skip the current commit and continue with the rest of the sequence.

--skip

End revert or cherry-pick sequence.

--quit

Cancel revert or cherry-pick sequence.

--abort

Commits to cherry-pick.

git cherry-pick <commit>

Introduce particular commits from one branch onto a different branch.

git cherry-pick <branch>

Synchronize

Remotes

List remote names.

git remote [-v | --verbose]

Show remote url after name.

-v
--verbose

Create a new connection to a remote repository. HTTP is an easy way to allow anonymous, read-only access to a repository. For read-write access, you should use SSH instead.

git remote add <remote> <url>

Remove a remote and all associated tracking branches.

git remote rm <remote>

Change URL for a remote.

git remote set-url <remote> <url>

Retrieves the URLs for a remote.

git remote get-url <remote>

Rename a remote and update all associated tracking branches.

git remote rename <old-remote> <new-remote>

Show information about a given remote.

git remote show <remote>

Fetching

Fetch objects and refs from repository. It has absolutely no affect on local development work because it doesn’t force to merge changes. When no remote is specified, by default the origin remote will be used.

git fetch [-p | --prune] [<remote> [<refspec>…]]

Remove any remote-tracking references that no longer exist on the remote.

-p
--prune

Fetch objects and refs from all remotes.

git fetch --all

Merge remote branch into local branch without having to checkout local branch first.

git fetch origin <remote-branch>:<local-branch>

Merge local branch into local branch master without having to checkout master first. Here . means to use the local repository as the remote.

git fetch . <remote-branch>:<local-branch>

Fetch from all remotes, not just one.

git remote update

Pulling

Fetch from and integrate with another repository or a local branch.

git pull [-r | --rebase] [<remote> [<refspec>…]]

Rebase the current branch on top of the upstream branch after fetching.

-r
--rebase

Pushing

Update remote refs along with associated objects.

git push [--all | --mirror | --tags] [-n | --dry-run]
         [-f | --force] [--force-with-lease[=<refname>[:<expect>]]]
         [-u | --set-upstream]
         [<remote> [<refspec>…]]

Push all branches (i.e. refs under refs/heads/).

--all

Push all refs under refs/heads/ refs/remotes, and refs/tags/ and delete non-existing refs.

--mirror

All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the command line.

--tags

Do everything except actually send the updates.

-n
--dry-run

Usually, the push command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

-f
--force

Usually, the push command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This option overrides this restriction if the current value of the remote ref is the expected value. "git push" fails otherwise.

--force-with-lease

Push a local branch and set the remote as upstream.

-u
--set-upstream

Make the head of the branch point at your personal history, ignoring any changes that may have occurred in parallel with yours.

git push -f <remote> <commit>:<local-branch>
         --force

Branches

Create Branches

Create new branch.

git branch [-t | --track | --no-track] <branch> [<start-point>]

When creating a new branch, set up configuration entries to mark the start-point branch as "upstream" from the new the branch.

--track

Do not set up "upstream" configuration.

--no-track

Create new branch.

git checkout [[-b | -B] <new-branch> [-t | --track | --no-track] <start-point>]

Causes a new branch to be created as if git-branch were called and then checked out.

-b
-B

Rename Branches

Rename a branch and the corresponding reflog.

git branch (-m | -M | --move) [<oldbranch>] <newbranch>

Rename a branch and the corresponding reflog.

-m
-M
--move

Delete Branches

Delete a branch.

git branch (-d | -D) [-r] <branch>…​

Delete the specified branch if it doesn't have unmerged changes.

-d
--delete

Delete the specified branch even if it has unmerged changes.

-D

Delete remote branch from repository.

git push <remote> --delete <branch>

Delete remote branch from repository.

git push <remote> :<branch>

Delete local branches that have been merged into the current branch.

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

List Branches

List local branches.

git branch [-v | -vv | --verbose]
           [--sort=<key>]
           [(--merged | --no-merged) [<object>]]
           [--contains [<commit]] [--no-contains [<commit>]]
           [(-r | --remotes) | (-a | --all)]

Show SHA-1 and commit subject line for each head.

-v
-vv
--verbose

List local branches, ordered by least recent commit.

--sort=committerdate

List local branches, ordered by most recent commit.

--sort=-committerdate

Only list branches which are fully contained by object.

--merged

Do not list branches which are fully contained by object.

--no-merged

Only list branches which contain the specified commit (HEAD if not specified).

--contains

Only list branches which don’t contain the specified commit (HEAD if not specified).

--no-contains

List only remote branches.

-r
--remotes

List both remote and local branches.

-a
--all

List local branches, ordered by most recent commit.

git for-each-ref --sort=-committerdate refs/heads/

Show branches and their commits.

git show-branch

Merge Branches

Join two or more development histories together

git merge [--ff | --no-ff] [--squash] [<commit>…]

Specifies how a merge is handled when the merged-in history is already a descendant of the current history.

--ff

Generate a merge commit (3-way merge) even if the merge resolved as a fast-forward.

--no-ff

Takes all the commits from a branch, squash them into 1 commit, and merge it with another branch.

--squash

Merge the last checked out branch.

git merge -

Sequencer subcommands.

git merge (--continue | --abort | --quit)

Continue merge operation after a git merge stops due to conflicts.

--continue

Restore the original branch and abort the merge operation.

--abort

Forget about the current merge in progress.

--quit

Track Branches

Make local branches to track branches.

git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branch>]

Branch will start tracking remote branch.

-u
--set-upstream-to

Remove the upstream information. If no branch is specified it defaults to the current branch.

git branch --unset-upstream [<branch>]

Switch to Branch

Switch to a different branch.

git checkout <branch>

Quickly jump back to previous branch.

git checkout -

Quickly jump back to previous branch.

git checkout @{-1}

Debugging

Binary Search

Use binary search to find the commit that introduced a bug.

git bisect (start | bad | good | run | skip | reset | visualize | log | replay | terms)

Reset bisection state and start a new bisection.

git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]

If you would like to use your own terms instead of "bad"/"good", you can choose any names you like (except existing bisect subcommands like reset, start, …​) by starting the bisection using.

--term-{new,bad}=<term>

If you would like to use your own terms instead of "new"/"old", you can choose any names you like (except existing bisect subcommands like reset, start, …​) by starting the bisection using.

--term-{old,good}=<term>

Mark current or given revision as bad.

bad

Mark current or given revision as good.

good

If you have a script that can tell if the current source code is good or bad. Note that the script should exit with code 0 if the current source code is good/old, and exit with a code between 1 and 127 (inclusive), except 125, if the current source code is bad/new.

run <cmd>...

If the code doesn’t run or the feature isn’t reachable.

skip

Finish bisection search and return to the given branch (or master).

reset

See the currently remaining suspects in gitk, issue the following command during the bisection process (the subcommand view can be used as an alternative to visualize).

visualize

Show the log of the current bisection. If you discover that you made a mistake in specifying the status of a revision, you can save the output of this command to a file, edit it to remove the incorrect entries, and then issue a reset followed by a replay command to return to a corrected state.

log

Replay a bisection log.

replay <logfile>

Get a reminder of the currently used terms.

terms [--term-good | --term-bad]

Get a reminder of the good term.

--term-good

Get a reminder of the bad term.

--term-bad

Tagging

Create tag object signed with GPG.

git tag [-a] [(-m | --mesage) <msg>] <tagname> [<commit> | <object>]

Create annotated tag.

-a

Use the given tag message.

-m
--message

Create lightweight tag.

git tag <tagname>

List tags in alphabetical order.

git tag

List tags.

git tag [-n] [-l | --list] [<pattern>…​]

Search for tags with a particular pattern.

-l
--list

Show tag message.

-n

Delete local tag.

git tag (-d | --delete) <tagname>…​

Delete remote tag.

git push <remote> :<tagname>

By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them.

git push <remote> <tag>

All tags under refs/tags are pushed to the remote repository.

git push <remote> --tags

Push missing annotated tags reachable from the pushed refs.

git push --follow-tags

Show the most recent tag on the current branch.

git describe --tags --abbrev=0

Submodules

Add given repository as a submodule.

git submodule add <url> [<path>]

Initialize submodules recorded in the index by setting the submodule name in .git/config.

git submodule init [<path>…​]

Update the registered submodules to match what the superproject expects by cloning missing submodules and updating the working tree of the submodules.

git submodule update [--init] [--remote] [<path>…​]

Initialize all submodules for which "git submodule init" has not been called so far before updating.

--init

Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

--remote

Deinitialize submodule from repository.

git submodule deinit [-f | --force] (--all | <path>…​)

The submodule’s working tree will be removed even if it contains local modifications.

-f
--force

Unregister all submodules.

--all

Show the status of the submodules.

git submodule status [<path>…​]

Prints out a summary of the difference between the submodule's HEAD and the one recorded in main repository.

git submodule summary [<path>…​]

Copy the new configuration from .gitmodules to .git/config

git submodule sync [<path>…​]

Evaluate shell command in each checked-out submodule.

git submodule foreach <command>

Refs and the Reflog

Hashes

Get the hash of object.

git rev-parse <object>

Get the hash of first commit.

git rev-list --max-parents=0 HEAD

Get the hash of the original base.

git merge-base <local-branch> master

Check whether or not the argument object is a valid reference.

git rev-parse --verify <object>

Get symbolic name of object.

git rev-parse --abbrev-ref <object>

Get symbolic name of object.

git symbolic-ref --short <object>

Get symbolic name of object.

git rev-name <object>

Refs

A ref is an indirect way of referring to a commit. You can think of it as a user-friendly alias for a commit hash. Refs are stored as normal text files in the .git/refs directory. To explore the refs in one of your repositories, navigate to .git/refs. The heads directory defines all of the local branches in your repository. Each filename matches the name of the corresponding branch, and inside the file you’ll find a commit hash. This commit hash is the location of the tip of the branch.

Output information on each ref (bisect, heads, remotes or tags).

git for-each-ref refs/heads/

Show the last 10 local branches you recently worked on, sorted by the time that we were last working there.

git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"

List references in a local repository.

git show-ref

List only refs/tags in a local repository.

git show-ref --tags

Special Refs

In addition to the refs directory, there are a few special refs that reside in the top-level .git directory. These refs are created and updated by Git when necessary. They are listed below:

  • HEAD – The currently checked-out commit/branch.
  • FETCH_HEAD – The most recently fetched branch from a remote repo.
  • ORIG_HEAD – A backup reference to HEAD before drastic changes to it.
  • MERGE_HEAD – The commit(s) that you’re merging into the current branch with git merge.
  • CHERRY_PICK_HEAD – The commit that you’re cherry-picking.

Relative Refs

The ~ character lets you reach parent commits. For example, the following displays the grandparent of HEAD.

git show HEAD~2

When working with merge commits, things get a little more complicated. Since merge commits have more than one parent, there is more than one path that you can follow. For 3-way merges, the first parent is from the branch that you were on when you performed the merge, and the second parent is from the branch that you passed to the git merge command. If you want to follow a different parent, you need to specify which one with the ^ character. For example, if HEAD is a merge commit, the following returns the second parent of HEAD.

git show HEAD^2

Reflog

The reflog is Git’s safety net. It records almost every change you make in your repository, regardless of whether you committed a snapshot or not. You can think of it as a chronological history of everything you’ve done in your local repo. Using git reset it is then possible to change back to the commit it was before.

git reflog

Show the reflog with relative date information (e.g. 2 weeks ago).

git reflog --relative-date

Others

Run as if git was started in given path.

git -C <path> <command>

The Git repository browser.

git gitk <file>

Instantly browse your working repository in gitweb.

git instaweb --httpd=webrick

Create backup with your repository’s files.

git archive --format=zip > <name>.zip

Create backup with your repository’s files.

git archive --format=zip -o <name>.zip <object>
                         --output=<name>.zip

Create backup with your repository’s files.

git archive | gzip > <name>.tar.gz

Export branch with history to a file.

git bundle create <file> <branch-name>

Import from a bundle.

git clone repo.bundle <repo-dir> -b <branch-name>

Count unpacked number of objects and their disk consumption.

git count-objects --human-readable

Remove files that are listed in the .gitignore but still on the repository.

git ls-files -i --exclude-from=.gitignore | xargs git rm --cached

Getting Help

Display help information about git.

git help <command>

Display help information about git.

git <command> --help

Get git version.

git --version

Print lines matching a pattern.

git grep <pattern>

List ignored files.

git check-ignore *

Determine which pattern is causing a particular file to be ignored.

git check-ignore -v <path>

FINDING COMMITS NOT MERGED UPSTREAM Find commits yet to be applied to upstream.

git cherry <branch>

Find commits yet to be applied to upstream with additional information.

git cherry -v <branch>
           --verbose

IGNORING CHANGES TO A TRACKED FILE Temporarily ignore changes.

git update-index --assume-unchanged <file>

Track changes again.

git update-index --no-assume-unchanged <file>

SHOWING ALL COMMITS TO BE MERGED Show all commits in the current branch yet to be merged to local-branch

git cherry -v <local-branch>

// TODO

git cherry -v <local-branch> <branch-to-be-merged>

// TODO

git log <branch-to-be-merged> ^<local-branch>