Git Command Cheat Sheet
Where am I?
Section titled “Where am I?”git statusShows changed files, staged files, branch name, and whether there is anything to commit.
Download a repository
Section titled “Download a repository”git clone https://github.com/organization/repository-name.gitCopies a GitHub repository to your computer.
See the GitHub connection
Section titled “See the GitHub connection”git remote -vShows the remote repository URL.
Bring down the latest work
Section titled “Bring down the latest work”git pullGets the latest commits from GitHub.
Create a branch
Section titled “Create a branch”git checkout -b branch-nameor:
git switch -c branch-nameCreates and switches to a new branch.
Switch branches
Section titled “Switch branches”git checkout mainor:
git switch mainSwitches to another branch.
List branches
Section titled “List branches”git branchShows local branches. The current branch has an asterisk.
Stage a file
Section titled “Stage a file”git add filenamePrepares a file for the next commit.
Stage all current changes
Section titled “Stage all current changes”git add .Prepares all changed files in the current folder and below.
Use carefully. Beginners should often prefer staging specific files.
Commit staged changes
Section titled “Commit staged changes”git commit -m "short message"Saves staged changes as a local checkpoint.
Push a branch the first time
Section titled “Push a branch the first time”git push -u origin branch-nameSends a new branch to GitHub and connects the local branch to the remote branch.
Push after the first time
Section titled “Push after the first time”git pushSends local commits to GitHub.
View commit history
Section titled “View commit history”git logShows previous commits.
For a shorter view:
git log --onelineBeginner safety commands
Section titled “Beginner safety commands”git statusRun this often.
git diffShows unstaged line-by-line changes.
git diff --stagedShows staged line-by-line changes.
Commands to ask for help before using
Section titled “Commands to ask for help before using”Beginners should ask for help before using:
git reset --hardgit push --forcegit rebasegit clean -fdThese commands can discard work or rewrite history if used incorrectly.