Essential Git Commands Reference
git config Configure Git parameters
git clone Clone repository
git init Initialize repository
git status Check local repository status
git remote Manage remote repositories
git branch Manage branches
git checkout Switch branches
git cherry-pick Apply commit
git add Stage changes
git commit Commit staged changes
git fetch Fetch latest changes
git merge Merge branches
git diff Compare differences
git pull Pull changes
git push Push changes
git log View commit history
git reset Reset commits
git revert Revert commits
git tag Manage tags
git mv Move/rename files
git rm Remove files
git config
Configure Git settings and parameters.
Git uses three configuration files:
- Repository-level configuration: Located at
.git/.gitconfigin your repository, only affects that specific repository. - Global configuration: On Mac systems at
~/.gitconfig, on Windows atC:\Users\, affects all repositories for the current user.\.gitconfig - System-level configuration: Found in the Git installation directory's
etcfolder asgitconfig, affects all users on the system.
# View configuration settings
# --local: repository level, --global: global level, --system: system level
$ git config <--local | --global | --system> -l
# View currently active configuration
$ git config -l
# Edit configuration file
# --local: repository level, --global: global level, --system: system level
$ git config <--local | --global | --system> -e
# Add configuration setting
# --local: repository level, --global: global level, --system: system level
$ git config <--local | --global | --system> --add <name> <value>
# Get specific configuration value
$ git config <--local | --global | --system> --get <name>
# Remove configuration setting
$ git config <--local | --global | --system> --unset <name>
# Configure user information for commits
$ git config --global user.name <username>
$ git config --global user.email <email>
# Adjust Git buffer size for large commits
# Buffer size in bytes, e.g., 524288000 (500MB)
$ git config --global http.postBuffer
# Enable colored output for status and diff commands
$ git config --global color.ui true
# Configure password caching (default: 15 minutes)
$ git config --global credential.helper cache
# Set password cache duration in seconds
$ git config --global credential.helper 'cache --timeout=<duration>'
# Store passwords permanently
$ git config --global credential.helper store
git clone
Clone a remote repository to your local machine.
# Clone repository, creates a directory with same name as repository
$ git clone
# Clone repository to specified directory
$ git clone
# Clone specific branch, defaults to master
$ git clone -b
git init
Initialize a new Git repository in the current directory. This creates a .git directory.
# Initialize repository in current directory
$ git init
git status
Check the status of your local repository.
# View detailed repository status
$ git status
# View status in short format
# Shows two columns: status indicator and filename
# Status indicators: A (added), M (modified), D (deleted), ?? (untracked)
$ git status -s
git remote
Manage remote repositories.
# List all remote repositories
$ git remote
# List remote repositories with URLs
$ git remote -v
$ git remote --verbose
# Add a new remote repository
$ git remote add <alias> <url>
# Rename a remote repository
$ git remote rename
# Remove a remote repository
$ git remote remove <alias>
# Update a remote repository's URL
$ git remote set-url <alias>
git branch
Manage Git branches.
# List all local branches, current branch marked with *
$ git branch
# List branches with last commit information
$ git branch -v
# Create a new branch based on the latest commit
$ git branch
# Rename a branch
# If original branch not specified, renames current branch
$ git branch -m []
# Force rename branch
$ git branch -M []
# Delete a branch
$ git branch -d
# Force delete a branch
$ git branch -D
git checkout
Checkout command, used for switching between branches or restoring files.
# Switch to an existing branch
$ git checkout
# Create and switch to a new branch, preserving commit history
# Equivalent to running "git branch" and "git checkout"
$ git checkout -b
# Create and switch to a new orphan branch (no commit history)
$ git checkout --orphan
# Restore a file from the last commit
# New files and staged files are unaffected
$ git checkout
git cherry-pick
Apply an existing commit to the current branch.
# Apply a specific commit to the current branch
$ git cherry-pick
git add
Add file changes to the staging area. When you run git commit, Git will commit the staged changes.
# Add specific file to staging area
$ git add
# Add all modified and deleted files to staging area
$ git add -u []
$ git add --update []
# Add all modified, deleted, and new files to staging area
# Omit to stage current directory
$ git add -A []
$ git add --all []
# Interactively select changes to stage
$ git add -i []
$ git add --interactive []
git commit
Commit staged changes to the local repository.
# Commit staged changes, opens editor for commit message
$ git commit
# Commit staged changes with a message
$ git commit -m ""
# Commit all modified and deleted files
# Excludes untracked files, equivalent to "git add -u" first
$ git commit -a -m ""
# Amend the last commit
$ git commit --amend
git fetch
Download objects and refs from a remote repository to your local tmp branch.
# Fetch all branches from remote repository
$ git fetch
# Fetch specific branch from remote repository
$ git fetch
git merge
Merge changes from one branch into another.
# Merge specified branch into current branch
$ git merge
git diff
Compare differences between versions.
# Compare working directory with staging area
$ git diff
# Compare staging area with last commit
$ git diff --cached
$ git diff --staged
# Compare working directory with last commit
$ git diff HEAD
# Show changes since specific commit
$ git diff
# Compare differences between two branches
$ git diff
# Show changes on each branch since they diverged
$ git diff ...
git pull
Fetch changes from a remote repository and merge them into the current branch. This is equivalent to running git fetch followed by git merge.
# Fetch and merge changes from remote repository
$ git pull
git push
Push local commits to a remote repository.
# Push local branch to remote branch
$ git push :
# Delete remote branch
$ git push :
$ git push --delete
git log
View commit history.
# Show all commit history
$ git log
# Show history from first commit to specified commit
$ git log
# Show specified number of recent commits
$ git log -<number>
git reset
Reset the current HEAD to a specified state.
# Reset staging area, files remain unchanged
# Equivalent to un-staging files added with "git add"
# No commit ID defaults to current HEAD
$ git reset []
$ git reset --mixed []
# Move HEAD to specified commit, files unchanged
$ git reset
$ git reset --mixed
# Move HEAD to specified commit and stage changes
# Equivalent to "git reset --mixed" followed by "git add"
$ git reset --soft
# Move HEAD to specified commit and discard changes
$ git reset --hard
git revert
Create a new commit that undoes changes from a previous commit. All commits before the target commit are preserved.
# Create a new commit to undo specified commit
$ git revert
git tag
Manage Git tags.
# List all tags
$ git tag
# Create lightweight tag pointing to a commit
$ git tag []
# Create annotated tag with description
$ git tag -a -m "" []
# Switch to tag (creates detached HEAD)
$ git checkout
# Show tag information
$ git show
# Delete tag
$ git tag -d
# Push tag to remote repository
$ git push
# Push all tags to remote repository
$ git push --tags
git mv
Move or rename files and directories.
# Move or rename file or directory
$ git mv <source> <destination>
git rm
Remove files and directories from the repository.
# Remove file from tracking and delete from working directory
$ git rm
# Remove directory from tracking and delete from working directory
$ git rm -r
# Remove file from tracking but keep in working directory
$ git rm --cached
Common Git Scenarios
Remove remote branches that no longer exist locally
When collaborating with others, remote branches may be deleted from the remote repository but still appear when running git branch --all locally. Use these commands to clean up:
# Use pull with prune option
$ git pull -p
# Equivalent commands
$ git fetch -p
$ git fetch --prune origin