Git cheat sheet
Essential git commands.
Published:Git Configuration
# Set global username
git config --global user.name "Your Name"
# Set global email
git config --global user.email "your.email@example.com"
# Verify global configuration
git config --list
# Set local username (only for the current repository)
git config user.name "Your Name"
# Set local email (only for the current repository)
git config user.email "your.email@example.com"
# Verify local configuration
git config --local --list
Creating and Cloning Repositories
# Initialize a new Git repository
git init
# Clone an existing repository
git clone <repository-url>
File Management
# Add files to the staging area
git add <file>
# Add all modified files to the staging area
git add .
# Remove files from the staging area
git reset <file>
# Check the repository status
git status
# Show differences for modified files
git diff
Committing Changes
# Commit files in the staging area
git commit -m "Commit message"
# Commit changes bypassing the staging area
git commit -a -m "Commit message"
Working with Branches
# Create a new branch
git branch <branch-name>
# Switch to an existing branch
git checkout <branch-name>
# Create and switch to a new branch
git checkout -b <branch-name>
# Delete a branch
git branch -d <branch-name>
Merging Branches
# Merge a branch into the current branch
git merge <branch-name>
# Resolving merge conflicts
# (Edit the conflicted files and then)
git add <conflicted-file>
git commit -m "Resolved merge conflict"
Synchronizing with the Remote Repository
# Add a remote repository
git remote add origin <repository-url>
# View remote repositories
git remote -v
# Fetch changes from the remote repository
git fetch
# Merge changes from the remote repository
git pull
# Push changes to the remote repository
git push origin <branch-name>
Git Stash: Save Changes Temporarily
# Save uncommitted changes
git stash
# View the stash list
git stash list
# Retrieve changes from the latest stash
git stash apply
# Retrieve changes from a specific stash
git stash apply stash@{2}
# Retrieve changes and remove the stash
git stash pop
Git Reflog: Recover Lost Commits
# Show the reference log
git reflog
# Recover a specific commit from reflog
git checkout <commit-id>
Git Bisect: Find the Commit that Introduced a Bug
# Start a bisect session
git bisect start
# Mark a "bad" commit (where the bug is present)
git bisect bad <commit-id>
# Mark a "good" commit (where the bug is not present)
git bisect good <commit-id>
# Continue the process with tests until Git finds the problematic commit
git bisect run <test-script>
Git Cherry-Pick: Apply Selected Commits to Another Branch
# Apply a specific commit to the current branch
git cherry-pick <commit-id>
Git Shortlog: Generate a Condensed Commit Log
# Generate a condensed log
git shortlog
# Generate a condensed log sorted by number of commits per author
git shortlog -n -s
Git Blame: Discover Who Modified What
# Show who modified each line of a file
git blame <file>
Git Grep: Search Through Code
# Search for a specific word or phrase in the repository
git grep "keyword"
# Search for a specific word or phrase in commits
git log -S "keyword"
Other Useful Commands
# Show the commit log
git log
# Show a simplified log with branch graphs
git log --oneline --graph --all
# Reset the current branch to the latest commit
git reset --hard
# Restore a specific file to the latest commit
git checkout -- <file>
# Create a tag
git tag <tag-name>
# Delete a tag
git tag -d <tag-name>
Table of Contents
- Git Configuration
- Creating and Cloning Repositories
- File Management
- Committing Changes
- Working with Branches
- Merging Branches
- Synchronizing with the Remote Repository
- Git Stash: Save Changes Temporarily
- Git Reflog: Recover Lost Commits
- Git Bisect: Find the Commit that Introduced a Bug
- Git Cherry-Pick: Apply Selected Commits to Another Branch
- Git Shortlog: Generate a Condensed Commit Log
- Git Blame: Discover Who Modified What
- Git Grep: Search Through Code
- Other Useful Commands