Cheatsheet
Note: For all git commands, you can run git [command] --help in a terminal to read more about the command.
Everyday Commands
git clone [remote_url]
Clones a remote repository to your local machine so you can make edits. Cloning is needed once to set up
a new repository, while git pull (see below) should be run frequently to sync local and remote repositories.
See this guide for more info about cloning.
git pull
Syncs remote changes to your local repository. You should pull new changes frequently to avoid challenges integrating your edits with your team members. See Troubleshooting for resolving merge conflicts.
git status
Prints helpful information about the current status of the local git repository such as your current branch, whether it is synced with the remote repository, and any modified files.
File Status (untracked, unstaged, staged)
Within a git repository, files can be untracked (not monitored by git), unstaged or staged.
- Untracked files are not recorded or managed by git. You must add them with
git add [file]if you need to track them. - Unstaged files are tracked files that have been modified since the last time they were committed.
- Staged files are modified files that have been added to the group of files that will be associated with the next commit you make.
git branch
Lists the local branches. Add the -a flag to list both local and remote branches - useful listing
team member's branches you might want to test. Use the -r flag to only view
remote branchs (useful for fetching a collaborator's branch from GitHub).
git switch [-c] [branch] vs “checkout”
Switches to specified branch and optionally creates that branch if it does not already exist if the -c flag is added.
Best practice is to run git switch main and then git pull before creating a new branch.
Switching to main and pulling changes before making a new branch helps ensure your branch can be successfuly
merged back into the main branch.
Note: Many tutorials will still use git checkout instead of git switch. The latter is the modern method
recommended by the maintainers of git.
git add [files]
Adds local files to a list of "staged" files that will be included in the next git commit. You must specify a space separated list of one or more files and/or directories as the last argument.
git restore --staged [file]
Removes one or more local files or directories from the list of "staged" files that will be included in the
next git commit. This command can undo or modify git add.
git commit [-m "message"]
Commits the changes to the staged files. You should commit to a non-main branch.
If the -m "your message" option is passed, you can add your commit message in one command.
git push [-u origin]
Pushes local committed changes to the remote (GitHub). The -u origin option is only needed on the first push from a
new local branch if you didn't enable auto-setup of remote branches (see git config below).
Initial Setup
git config
Recommended commands to run once on a new machine to align with best practices and set defaults.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global init.defaultBranch main
git config --global push.autoSetupRemote true
git config --list
git init
Not used in our recommended workflow. Initializes a local git repository. Not needed if you are cloning a remote repository from GitHub.
See git clone above.
For a more detailed cheat-sheet, see git-scm.com/cheat-sheet