Git Cheatsheet (Basics)

April 26, 2020 · View on GitHub

This cheatsheet is a list of our most used Git commands, and it countains useful information for those who are getting started.

It is available in the languages above. Since the translation rely on volunteers, the content between the available languages may vary.

In case you've missed, there's a list of our most used commands and shortcuts in the Terminal for macOS.

English Version

Glossary

KeywordsDescription
gitOpen-source distributed version-control system, used to store code in repositories
GitHub, GitLab and BitbucketPlatform for hosting and collaborating on Git repositories
stagingProposed files/directories that you'd like to commit
commitSaving all staged files/directories to your local repository
branchAn independent line of development, so you can develop features isolated from each other. Master branch is the default.
cloneLocal version of a repository, including all commits and branches
remoteCommon repository on eg. Github that all team members to keep that changes in sync with
forkCopy of a repository owned by a different user
pull requestA method of submitting contributions to a repository
HEADRepresents your current working directory

Configuration

Key/CommandDescription
git config --global user.name [name]Set author name to be used for all commits
git config --global user.email [email]Set author email to be used for all commits
git config color.ui trueEnables helpful colorization of command line output

Core Commands

Key/CommandDescription
git init [directory]Creates new local repository
git clone [repo]Creates local copy of remote repository
git add [directory]Stages specific [directory]
git add [file]Stages specific [file]
git add -AStages all changed files
git add .Stages new and changed files, NOT deleted files
git add -uStages changed and deleted files, NOT new files
git commit -m "[message]"Commit everything that is staged
git statusShows status of changes as untracked, modified or staged

Synchronization of Changes

Key/CommandDescription
git fetchDownloads all history from the remote branches
git mergeMerges remote branch into current local branch
git pullDownloads all history from the remote branch and merges into the current local branch
git pushPushes all the commits from the current local branch to its remote equivalent

Tip: git pull is the combination of git fetch and git merge

Undo Changes

Key/CommandDescription
git checkout -- [file]Replace file with contents from HEAD
git revert [commit]Create new commit that undoes changes made in [commit], then apply it to the current branch
git reset [file]Remove [file] from staging area
git reset --hard HEADRemoves all local changes in working directory
git reset --hard [commit]Reset your HEAD pointer to previous commit and discard all changes since then

Branches

Key/CommandDescription
git branch [branch]Create a new branch
git checkout [branch]Switch to that branch
git checkout [branch] -bCreate and checkout new branch
git merge [branch]Merge [branch] into current branch
git branch -d [branch]Deletes the [branch]
git push origin [branch]Push [branch] to remote
git branchLists local branches
git branch -rLists remote branches
git branch -aLists local and remote branches

History

Key/CommandDescription
git logLists version history for the current branch
git log --author=[name]Lists version history for the current branch from certain author
git log --onelineLists compressed version history for the current branch
git show [commit]Outputs metadata and content changes of the specified commit
git blame [file]Shows who changed what and when in file

Gitignore

You can list files/directories that you want to explicitely exclude from Git in a .gitignore file. This file should be placed at the root of your repository.

It is noted that people usually exclude dependency caches, such as node_modules, system files, such as .DS_Store, among others.

You can see the .gitignore file of this repository or more examples provided by GitHub.

Platforms

The following platforms can be used to host your repositories.

PlatformPrice
GitHubFree
GitLabFree
BitbucketFree

Graphical User Interface (GUI)

Is the command-line interface not for you? Try one of the following clients.

ClientsOperational SystemPrice
GithubMac and WindowsFree
Source TreeMac and WindowsFree
TowerMacOS and Windows$69 to $99

Resources