promisepreston
12/10/2019 - 6:49 PM

Git Cheat Sheet

Git Cheat Sheet

Glossary

EntitiesDescription
branchlightweight movable pointer to a commit
clonelocal version of a repository, including all commits and branches
commitA Git object, a snapshot of your entire repository compressed into a SHA
gitAn open source, distributed version-control system
GitHubA platform for hosting and collaborating on Git repositories
forkA copy of a repository owned by a different user
HEADRepresenting the most current commit of the repository.
indexRepository committed status.
masterThe primary branch of all your repositories.
pull requestA place to compare and discuss the differences introduced on a branch with reviews, comments, integrated tests, and more
remoteA common repository on GitHub that all team member use to exchange their changes. It is often named origin.
.gitignore fileSometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore.

Common Commands and Description

CommandDescription
git add .Stage all file changes, ready for commit.
git add -fStage all file changes including ignored files, ready for commit.
git add --allStage all file changes, ready for commit.
git add <directory>Stage all changes in <directory> for the next commit.
git add <file>Stage <file> in its current state for the next commit.
git branchList all the branches in the local repository.
git branch -aList all the branches in the local and remote repository.
git branch -d <branch>Delete a branch named <branch>.
git branch -m <branch>Rename a branch named <branch>.
git branch <branch>Create a new branch named <branch>.
git blame <file>List the change dates and authors for <file>.
git checkout <branch>Switch to an existing branch named <branch>.
git checkout <tag>Switch to an existing tag named <tag>.
git checkout -b <branch>Create and switch to a new branch named <branch>.
git checkout -- .Undo local modifications to all files.
git checkout -- <file>Undo local modifications to <file>.
git clean -dfDelete all untracked files and directories.
git clean -fDelete all untracked files.
git clean -nShows which files would be removed from working directory.
git clone <url>Clone a remote repository into a newly created directory.
git clone <url> <directory>Clone a remote repository into a newly created directory and name the directory <directory>.
git clone -l <path/to/repo>Clone a local repository into a newly created directory.
git clone -b <url> <name>Clone a remote repository into a newly created directory and change the branch HEAD to <name>.
git clone -o <url> <name>Clone a remote repository into a newly created directory and name the remote <name>.
git clone --no-tags <url>Clone a remote repository into a newly created directory without any tags.
git commit -am "<message>"Commit all tracked files changes to versioned history, and use <message> as the commit message.
git commit -m "<message>"Commit all staged files changes to versioned history, and use <message> as the commit message.
git commit --amendReplace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git config user.name <name>Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user.
git config --global alias. <alias-name> <git-command>Create shortcut for a Git command. E.g. alias.glog log --graph --oneline will set git glog equivalent to git log --graph --oneline.
git config --global color.ui autoEnable helpful colorization of command line output.
git config --global user.email <email>Define the author email to be used for all commits by the current user.
git config --global user.name <name>Define the author name to be used for all commits by the current user.
git config --global --editOpen the global configuration file in a text editor for manual editing.
git config --system core.editor <editor>Set text <editor> to be used by commands for all users on the machine (e.g., vi).
git diffShow the file changes that are unstaged.
git diff HEADShow the difference between staged and unstaged file changes.
git diff --cachedShow the difference between file changes that are staged and the last commit.
git diff --stagedShow the difference between file changes that are staged, but not yet committed.
git diff <branch1> <branch2>Show the difference between <branch1> and <branch2> file changes.
git diff <commit1> <commit2>Show the difference between <commit1> and <commit2> file changes.
git fetch <name>Fetch file changes of all the branches from the <name> repository.
git fetch <name> <branch>Fetch file changes of a specific <branch> from the <name> repository.
git fetch --prune <name>Remove remote references that were removed from the <name> repository.
git initInitialize an existing directory as a Git repository.
git init <directory>Create empty Git repo in specified directory.
git logDisplay the entire commit history using the default format.
git log <branch>..List commits that are present on current branch and not merged into <branch.
git log ..<branch>List commits that are present on <branch> and not merged current branch`
git reflogList operations (like checkouts, commits etc.) made on local repository.
git log <since>..<until>Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
git log -<limit>Limit number of commits by <limit>. E.g. git log -5 will limit to 5 commits.
git log -pDisplay the full diff of each commit.
git log -- <file>Only display commits that have the specified <file>.
git log --follow <file>Lists version history for a file, including renames.
git log --author=”<pattern>Search for commits by a particular author.
git log --graph --decorateList commits in a text based graph with references labels.
git log --grep=”<pattern>Search for commits with a commit message that matches <pattern>.
git log --onelineList each commit in a single line.
git log --statInclude which files were altered and the relative number of lines that were added or deleted from each of them.
git log --stat -MShow all commit logs with indication of any paths that moved
git merge <branch>Merge <branch> into the current branch.
git merge <branch> <name>Merge <branch>from the <name> repository into the current branch.
git mv <existing-path> <new-path>Change an existing file path and stage the move.
git pull <name>Fetch the specified remote’s copy of current branch and merge it into the local copy. git pull is a combination of git fetch and git merge.
git pull --tags <name>Fetch the remote tags and merge them into the local copy.
git pull --rebase <name>Fetch the remote’s copy of current branch and rebase it into the local copy.
git push <name> <branch>Push the branch to <name>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git push <name> --allPush all local branches to the specified <name>.
git push <name> --forceForce the git push even if it results in a non-fast-forward merge.
git push <name> --tagsPush all local tags to the specified <name>.
git push -u <name> <branch>Push local branch to remote repository.
git rebase <branch>Apply any commits of the current branch onto <branch>.
git rebase -i <branch>Interactively rebase current branch onto <branch>.
git reflogShow a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.
git remote -vShow remote name and url.
git remote add <name> <url>Add a remote named <name> for the repository at <url>.
git remote get-url --all <name>Retrieve all the URLs for a remote.
git remote get-url --push <name>Retrieve only the push URLs for a remote.
git remote prune <name>Delete stale references associated with <name>.
git remote prune --dry-run <name>Report what branches associated with <name> will be pruned, but do not actually prune them.
git remote remove <name>Remove the remote named <name>.
git remote rename <old> <new>Rename the remote named <old> to <new>.
git remote set-branches <name> <branch>Change the list of branches tracked by the named remote.
git remote set-branches --add <name> <branch>Add to the list of branches tracked by the named remote.
git remote set-head <name> <branch>Set the default branch for the named remote.
git remote set-head -a <name>Query the named remote to determine its HEAD.
git remote set-head -d <name>Delete the default branch for the named remote.
git remote set-url <name> <newurl> <oldurl>Change URLs for the remote.
git remote set-url --push <name> <newurl> <oldurl>Change only push URLs for the remote.
git remote set-url --add <name> <newurl>Add URLs for the remote.
git remote set-url --add --push <name> <newurl>Add only push URLs for the remote.
git remote set-url --delete <name> <url>Delete URLs for the remote.
git remote set-url --delete [--push] <name> <url>Delete only push URLs for the remote.
git remote show <name>Give some information about the remote <name> and its mapping to local.
git remote update <name>Fetch updates for <name>.
git remote update <remote>)Fetch updates for remote <groups> in the repository as defined by remotes.
git remote show <name>Show remote branches and their mapping to local.
git resetReset staging area to match most recent commit, but preserve file changes locally.
git reset <commit>Reset the staging area to match <commit>, but preserve file changes locally.
git reset <file>Unstage <file> from the staging area, but preserve <file> changes locally.
git reset --hardReset both the staging area and the local repository to match most recent commit.
git reset --hard <commit>Reset both the staging area and the local repository file changes to match <commit>.
git revert <commit>Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.
git rm <file>Delete <file> from local repository and stage the removal for commit.
git show <commit>Show the file changes for <commit>.
git show <commit>:<file>Show the file changes for <commit> and/or <file>.
git stashSave modified and staged file changes temporarily.
git stash dropClear stach without applying it into local repository.
git stash listList stack-order of stashed file changes.
git stash popApply saved stash content into local repository and clear stash.
git stash save "<stash>" && git stashSave changes to a <stash>.
git statusList which files are staged, unstaged, and untracked.
git tagList all tags.
git tag <tag>Tag the current commit.
git tag -a <tag> -m "<message>"Create a new tag named <tag> with the commit <message>.
git tag -d <name>Remove a tag from local repository.