Git Installation and Cheatsheet

Install Git

You can install the git binary using Homebrew.

brew install git

Configuring User Information

Used across all local repositories:

Set a name that is identifiable for credit when reviewing version history:

git config --global user.name "[firstname lastname]"

Set an email address associated with each history marker:

git config --global user.email "[valid-email]"

Enable automatic command-line coloring for easy review:

git config --global color.ui auto

Initializing and Cloning Repositories

Create a new local repository:

git init [project name]

Retrieve an entire repository via URL:

git clone <project url>

Branch and Merge

Isolating work in branches, changing context, and integrating changes:

List branches:

git branch

Create a new branch:

git branch [branch-name]

Switch to another branch:

git checkout

Merge branch history:

git merge [branch]

Show all commits:

git log

Share and Update

Fetch and merge commits:

git pull

Add a git URL as an alias:

git remote add [alias] [url]

Fetch all branches:

git fetch [alias]

Merge a remote branch:

git merge [alias]/[branch]

Push commits to a remote repository:

git push [alias] [branch]

Stage and Snapshot

Show modified files:

git status

Stage a file:

git add [file]

Unstage a file:

git reset [file]

View diffs:

git diff
git diff --staged

Commit staged content:

git commit -m "[descriptive message]"

Tagging Commits

List all tags:

git tag

Create a tag:

git tag [name] [commit sha]

Create an annotated tag:

git tag -a [name] [commit sha]

Remove a tag:

git tag -d [name]

Saving Work Temporarily

Save changes:

git stash

List stashed changes:

git stash list

Apply stash:

git stash pop

Discard stash:

git stash drop

Inspect and Compare

Show commit history:

git log

Compare branches:

git log branchB..branchA

Show file changes across renames:

git log --follow [file]

View branch differences:

git diff branchB...branchA

Show Git object details:

git show [SHA]