{ ILoveJS }

Git Commands Cheatsheet

git

Essential Git commands for daily development workflow.

8 sections · 86 items

Setup

Set Username
git config --global user.name "Your Name"

Configure the author name to be used with your commits globally

bash
git config --global user.name "John Doe"
Set Email
git config --global user.email "email@example.com"

Configure the email address to be used with your commits globally

bash
git config --global user.email "john@example.com"
List Config
git config --list

Display all Git configuration settings for the current repository

bash
git config --list --show-origin
Initialize Repository
git init [directory]

Create a new Git repository in the current or specified directory

bash
git init my-project
Initialize Bare Repository
git init --bare [directory]

Create a bare repository without a working directory for server use

bash
git init --bare project.git
Clone Repository
git clone <url> [directory]

Download a repository and its entire version history

bash
git clone https://github.com/user/repo.git
Shallow Clone
git clone --depth <depth> <url>

Clone only the specified number of commits for faster downloads

bash
git clone --depth 1 https://github.com/user/repo.git
Clone Specific Branch
git clone -b <branch> <url>

Clone a repository and checkout a specific branch

bash
git clone -b develop https://github.com/user/repo.git

Staging

Check Status
git status

Show the working tree status including staged and unstaged changes

bash
git status -s
Add File
git add <file>

Add a specific file to the staging area

bash
git add index.js
Add All Files
git add .

Add all new and modified files in current directory to staging

bash
git add .
Add by Pattern
git add <pattern>

Add files matching a pattern to the staging area

bash
git add *.js
Interactive Add
git add -p [file]

Interactively choose hunks of patch to add to staging

bash
git add -p src/app.js
Unstage File
git reset HEAD <file>

Remove a file from the staging area but keep changes in working directory

bash
git reset HEAD index.js
Unstage All
git reset HEAD

Unstage all files while preserving changes in working directory

bash
git reset HEAD
Discard Changes
git checkout -- <file>

Discard changes in working directory for a specific file

bash
git checkout -- index.js
View Unstaged Diff
git diff

Show changes between working directory and staging area

bash
git diff src/app.js
View Staged Diff
git diff --staged

Show changes between staging area and last commit

bash
git diff --staged
Diff Between Commits
git diff <commit1> <commit2>

Show changes between two specific commits

bash
git diff HEAD~2 HEAD

Committing

Commit
git commit -m "message"

Record staged changes to the repository with a message

bash
git commit -m "Add user authentication"
Commit All Tracked
git commit -am "message"

Stage all tracked modified files and commit in one step

bash
git commit -am "Fix login bug"
Commit with Editor
git commit

Open the default editor to write a detailed commit message

bash
git commit
Amend Last Commit
git commit --amend

Modify the most recent commit message or add staged changes to it

bash
git commit --amend -m "Updated message"
Amend Without Edit
git commit --amend --no-edit

Add staged changes to last commit without changing the message

bash
git commit --amend --no-edit
Empty Commit
git commit --allow-empty -m "message"

Create a commit with no changes useful for triggering CI builds

bash
git commit --allow-empty -m "Trigger build"
Revert Commit
git revert <commit>

Create a new commit that undoes changes from a specified commit

bash
git revert abc1234
Revert Without Commit
git revert -n <commit>

Revert changes but do not automatically create a commit

bash
git revert -n HEAD~3

Branching

List Branches
git branch

List all local branches in the repository

bash
git branch -a
Create Branch
git branch <branch-name>

Create a new branch at the current commit

bash
git branch feature/login
Delete Branch
git branch -d <branch-name>

Delete a branch that has been fully merged

bash
git branch -d feature/login
Force Delete Branch
git branch -D <branch-name>

Force delete a branch even if not fully merged

bash
git branch -D experimental
Rename Branch
git branch -m <old> <new>

Rename a branch from old name to new name

bash
git branch -m old-name new-name
Checkout Branch
git checkout <branch-name>

Switch to an existing branch

bash
git checkout develop
Create and Checkout
git checkout -b <branch-name>

Create a new branch and switch to it immediately

bash
git checkout -b feature/auth
Switch Branch
git switch <branch-name>

Switch to an existing branch using modern syntax

bash
git switch main
Create and Switch
git switch -c <branch-name>

Create a new branch and switch to it using modern syntax

bash
git switch -c feature/api
Merge Branch
git merge <branch-name>

Merge specified branch into the current branch

bash
git merge feature/login
Merge No Fast-Forward
git merge --no-ff <branch>

Create a merge commit even if fast-forward is possible

bash
git merge --no-ff feature/login
Abort Merge
git merge --abort

Abort the current merge operation and restore pre-merge state

bash
git merge --abort
Rebase
git rebase <branch>

Reapply commits on top of another base branch

bash
git rebase main
Interactive Rebase
git rebase -i <commit>

Interactively edit reword squash or reorder commits

bash
git rebase -i HEAD~3
Cherry Pick
git cherry-pick <commit>

Apply changes from a specific commit to current branch

bash
git cherry-pick abc1234

Remote

List Remotes
git remote -v

Show all configured remote repositories with URLs

bash
git remote -v
Add Remote
git remote add <name> <url>

Add a new remote repository with a shortname

bash
git remote add origin https://github.com/user/repo.git
Remove Remote
git remote remove <name>

Remove a remote repository reference

bash
git remote remove origin
Rename Remote
git remote rename <old> <new>

Rename a remote repository reference

bash
git remote rename origin upstream
Show Remote Info
git remote show <name>

Display detailed information about a remote repository

bash
git remote show origin
Fetch
git fetch <remote>

Download objects and refs from remote without merging

bash
git fetch origin
Fetch All
git fetch --all

Fetch from all configured remote repositories

bash
git fetch --all --prune
Pull
git pull <remote> <branch>

Fetch from remote and merge into current branch

bash
git pull origin main
Pull Rebase
git pull --rebase <remote> <branch>

Fetch and rebase current branch on top of remote branch

bash
git pull --rebase origin main
Push
git push <remote> <branch>

Upload local branch commits to remote repository

bash
git push origin main
Push Set Upstream
git push -u <remote> <branch>

Push and set remote branch as upstream for current branch

bash
git push -u origin feature/login
Push Force
git push --force

Force push overwriting remote history use with caution

bash
git push --force-with-lease origin feature
Delete Remote Branch
git push <remote> --delete <branch>

Delete a branch from the remote repository

bash
git push origin --delete feature/old

History

View Log
git log

Show commit history for the current branch

bash
git log --oneline -10
Log One Line
git log --oneline

Show condensed commit history with one line per commit

bash
git log --oneline --graph
Log Graph
git log --graph --all

Show commit history as ASCII graph for all branches

bash
git log --graph --all --decorate
Log with Diff
git log -p

Show commit history with full diff for each commit

bash
git log -p -2
Log by Author
git log --author="name"

Filter commit history by author name or email

bash
git log --author="John"
Log by Date
git log --since="date" --until="date"

Filter commits within a date range

bash
git log --since="2024-01-01" --until="2024-12-31"
Log File History
git log -- <file>

Show commit history for a specific file

bash
git log --follow -- src/app.js
Show Commit
git show <commit>

Display detailed information and diff for a specific commit

bash
git show abc1234
Blame
git blame <file>

Show who last modified each line of a file with commit info

bash
git blame -L 10,20 src/app.js
Bisect Start
git bisect start

Start binary search to find the commit that introduced a bug

bash
git bisect start
Bisect Bad
git bisect bad [commit]

Mark a commit as bad containing the bug

bash
git bisect bad HEAD
Bisect Good
git bisect good [commit]

Mark a commit as good not containing the bug

bash
git bisect good abc1234
Bisect Reset
git bisect reset

End bisect session and return to original branch

bash
git bisect reset

Stashing

Stash Changes
git stash

Save uncommitted changes and revert working directory to clean state

bash
git stash
Stash with Message
git stash push -m "message"

Stash changes with a descriptive message for identification

bash
git stash push -m "WIP: login feature"
Stash Including Untracked
git stash -u

Stash changes including untracked files

bash
git stash -u
List Stashes
git stash list

Show all stashed changesets

bash
git stash list
Apply Stash
git stash apply [stash]

Apply stashed changes without removing from stash list

bash
git stash apply stash@{0}
Pop Stash
git stash pop [stash]

Apply stashed changes and remove from stash list

bash
git stash pop
Show Stash
git stash show [stash]

Show summary of changes in a stash

bash
git stash show -p stash@{0}
Drop Stash
git stash drop [stash]

Remove a specific stash from the list

bash
git stash drop stash@{1}
Clear All Stashes
git stash clear

Remove all stashed entries

bash
git stash clear

Tags

List Tags
git tag

List all tags in the repository

bash
git tag -l "v1.*"
Create Lightweight Tag
git tag <tagname>

Create a lightweight tag pointing to current commit

bash
git tag v1.0.0
Create Annotated Tag
git tag -a <tagname> -m "message"

Create an annotated tag with a message and metadata

bash
git tag -a v1.0.0 -m "Release version 1.0.0"
Tag Specific Commit
git tag <tagname> <commit>

Create a tag pointing to a specific commit

bash
git tag v0.9.0 abc1234
Show Tag
git show <tagname>

Display information about a specific tag

bash
git show v1.0.0
Push Tag
git push <remote> <tagname>

Push a specific tag to remote repository

bash
git push origin v1.0.0
Push All Tags
git push <remote> --tags

Push all local tags to remote repository

bash
git push origin --tags
Delete Local Tag
git tag -d <tagname>

Delete a tag from local repository

bash
git tag -d v1.0.0
Delete Remote Tag
git push <remote> --delete <tagname>

Delete a tag from remote repository

bash
git push origin --delete v1.0.0

Related Content