Git Cheatsheets for Quick Reference

Last updated on 2026-05-01 | Edit this page

Git Cheatsheets for Quick Reference


Here is the cheat sheet created by Carpentry @ UCSB for this workshop. You can print or save it as a PDF directly from this page!

Git Cheat Sheet

Version control is like an unlimited ‘undo’. It allows many people to work in parallel, collaborate and share their work.

Setting Up Git - Once per computer

Configure user information for all local repositories:

$ git config --global user.name "[name]"
$ git config --global user.email "[email]"
$ git config --global init.defaultBranch main

Creating a Repository

Git stores all of its repository data in the .git directory.

Turn an existing directory into a git repository:

$ git init

Tracking Changes

Files can be stored in a project’s working directory (which users see), the staging area (where the next commit is being built up, after git add) and the local repository (where commits are permanently recorded, after git commit).

Show the status of a repository:

$ git status

Put files in the staging area:

$ git add [file]

Save staged content as a new commit in the local repository:

$ git commit -m "[descriptive message]"

Exploring History

Display differences in file compared to a previous commit:

$ git diff [commit] [file]

Restore version of a file from last commit:

$ git restore [file]

Restore version of a file from a previous commit:

$ git restore --source [commit] [file]

Ignoring Things

The .gitignore file is a text file that tells Git which files or folders to track and which to ignore in the repository.

Remotes in GitHub / Collaborating

First, you’ll need to set up SSH to authenticate with GitHub. A local Git repository can be connected to one or more remote repositories in GitHub (or other hosting platforms).

Clone (download) a GitHub repository:

$ git clone [url]

Copy changes from a local repository to a remote repository:

$ git push

Copy changes from a remote repository to a local repository:

$ git pull

Branches

Branches allow parallel work without affecting the main codebase. Each branch is a parallel snapshot until merged.

See current branches:

$ git branch

Switch to a different branch:

$ git switch [branch-name]

Create a new branch:

$ git switch -c [branch-name]

Merge a specified branch’s history into current branch:

$ git merge [branch]

Delete the specified branch:

$ git branch -d [branch-name]

Pull requests (PR) on GitHub enable review and discussion before merging.

Forks

A fork is your own copy of another GitHub repository (the “upstream” repo). You can make changes freely even if you don’t have write access to the upstream repo.

To see current remote repositories being tracked:

$ git remote -v

Add a new remote being tracked, maybe the upstream repo:

$ git remote add [remote-name] [url]

You can contribute by creating a pull request from your fork to the upstream repository.

Conflicts

Conflicts occur when two or more people change the same lines of the same file. Git highlights conflicts so that they can be resolved. Defining a workflow with your team is the first step to avoid them.

Open Science / Licensing / Citation / Hosting

Open scientific work is more useful and accelerates discovery. The LICENSE.md file indicates how the contents of the repo may be used. Add a CITATION file to explain how you want your work cited.

Huang, Daisie (2015). How Git works: a cartoon.

Other resources


  • Printable Git cheatsheets in several languages made by GitHub are available here (English version). More material is available from the GitHub training website.
  • Here is a great refresher for Bash commands from EDS 221 - Scientific Programming Essentials
  • An interactive one-page visualisation about the relationships between workspace, staging area, local repository, upstream repository, and the commands associated with each (with explanations).
  • Both resources are also available in other languages (e.g. Spanish, French, and more).
  • “Happy Git and GitHub for the useR” is an accessible, free online book by Jenny Bryan on how to setup and use Git and GitHub with specific references on the integration of Git with RStudio and working with Git in R.
  • Open Scientific Code using Git and GitHub - A collection of explanations and short practical exercises to help researchers learn more about version control and open source software.

Glossary


changeset
A group of changes to one or more files that are or will be added to a single commit in a version control repository.
commit
To record the current state of a set of files (a changeset) in a version control repository. As a noun, the result of committing, i.e. a recorded changeset in a repository. If a commit contains changes to multiple files, all of the changes are recorded together.
conflict
A change made by one user of a version control system that is incompatible with changes made by other users. Helping users resolve conflicts is one of version control’s major tasks.
HTTP
The Hypertext Transfer Protocol used for sharing web pages and other data on the World Wide Web.
merge
(a repository): To reconcile two sets of changes to a repository.
protocol
A set of rules that define how one computer communicates with another. Common protocols on the Internet include HTTP and SSH.
remote
(of a repository) A version control repository connected to another, in such way that both can be kept in sync exchanging commits.
repository
A storage area where a version control system stores the full history of commits of a project and information about who changed what, when.
resolve
To eliminate the conflicts between two or more incompatible changes to a file or set of files being managed by a version control system.
revision
A synonym for commit.
SHA-1
SHA-1 hashes is what Git uses to compute identifiers, including for commits. To compute these, Git uses not only the actual change of a commit, but also its metadata (such as date, author, message), including the identifiers of all commits of preceding changes. This makes Git commit IDs virtually unique. I.e., the likelihood that two commits made independently, even of the same change, receive the same ID is exceedingly small.
SSH
The Secure Shell protocol used for secure communication between computers.
timestamp
A record of when a particular event occurred.
version control
A tool for managing changes to a set of files. Each set of changes creates a new commit of the files; the version control system allows users to recover old commits reliably, and helps manage conflicting changes made by different users.