Git Cheat Sheet
This cheat sheet features the most important and commonly used git commands for easy reference. Git Tips and Tricks (A collection of useful tips and tricks for Git)
External Resources
Table of contents
- Configurations
- Creating and Managing Repositories
- Working with Files
- Committing Changes
- Branching
- Merging and Rebasing
- Stashing
- Viewing History
- Undoing Changes
- Synchronizing with Remote
- Tags
- Collaboration
- Advanced Commands
- Hooks
- Troubleshooting
- Rewriting History
- Advanced Stashing
- Key Differences: Apply vs. Pop
- Additional Tips for History Rewriting
Configurations
Setup & Configuration
git config --global user.name "Your Name" # Set your name
git config --global user.email "you@example.com" # Set your email
git config --list # List all settings
git config --global core.editor "code --wait" # Set default editor (e.g., VS Code)
git config --global core.autocrlf true # Handle line endings
Creating and Managing Repositories
Initialize a Repository
git init # Create a new local repository
git clone <url> # Clone a remote repository
Check Repository Status
git status # Check status of files
Working with Files
Adding and Removing Files
git add <file> # Stage a specific file
git add . # Stage all changes in the current directory
git rm <file> # Remove a file from working directory and stage it
git rm --cached <file> # Remove file from staging but keep in working directory
Viewing Changes
git diff # View unstaged changes
git diff --staged # View staged changes
Committing Changes
Basic Commit
git commit -m "Your commit message" # Commit staged changes
git commit --amend # Amend the last commit
Skipping Staging Area
git commit -a -m "Your commit message" # Add all tracked files and commit
Branching
Basic Branch Commands
git branch # List branches
git branch <branch_name> # Create a new branch
git checkout <branch_name> # Switch to a branch
git checkout -b <branch_name> # Create and switch to a new branch
git branch -d <branch_name> # Delete a branch
git branch -D <branch_name> # Force delete a branch
Merging and Rebasing
Merging
git merge <branch_name> # Merge a branch into the current branch
Resolving Merge Conflicts
# Edit the conflicting files, then:
git add <conflicted_file>
git commit
Rebasing
git rebase <branch_name> # Rebase onto another branch
git rebase --continue # Continue rebase after conflict
git rebase --abort # Abort rebase
Stashing
Saving Temporary Changes
git stash # Stash current changes
git stash list # List stashes
git stash apply # Apply the most recent stash
git stash pop # Apply the stash and delete it
git stash drop # Delete the stash
Viewing History
Log Commands
git log # View commit history
git log --oneline # Compact commit history
git log --graph --decorate # Visualize branch history
git log -p # Show changes in each commit
Undoing Changes
Discard Changes
git restore <file> # Discard changes in working directory
git restore --staged <file> # Unstage a file
Resetting Commits
git reset --soft <commit_hash> # Reset to a previous commit (keep changes staged)
git reset --mixed <commit_hash> # Reset to a previous commit (unstage changes)
git reset --hard <commit_hash> # Reset to a previous commit (discard changes)
Synchronizing with Remote
Managing Remotes
git remote add origin <url> # Add a remote repository
git remote -v # List remotes
git remote remove <name> # Remove a remote
Fetch, Pull, and Push
git fetch # Download changes from the remote
git pull # Fetch and merge changes from remote
git push # Push changes to remote repository
git push -u origin <branch_name> # Push branch and set upstream
Tags
Working with Tags
git tag # List all tags
git tag <tag_name> # Create a new tag
git tag -a <tag_name> -m "Message" # Create an annotated tag
git push origin <tag_name> # Push a tag to remote
git push origin --tags # Push all tags to remote
Collaboration
Viewing and Working on Changes
git blame <file> # Show who changed each line of a file
git show <commit_hash> # Show details of a commit
Resolving Conflicts
# After resolving conflicts:
git add <file>
git commit
Advanced Commands
Clean Untracked Files
git clean -f # Remove untracked files
git clean -fd # Remove untracked files and directories
Cherry Picking
git cherry-pick <commit_hash> # Apply a specific commit to the current branch
Squashing Commits
git rebase -i HEAD~<number_of_commits> # Squash commits interactively
Hooks
Common Hooks
# Located in .git/hooks/
pre-commit # Script that runs before a commit
pre-push # Script that runs before a push
Troubleshooting
Unfinished Operations
git merge --abort # Abort a merge
git rebase --abort # Abort a rebase
git cherry-pick --abort # Abort a cherry-pick
Fix a Broken Push
git push --force # Force push changes
Rewriting History
Modifying the Most Recent Commit
git commit --amend # Amend the last commit (e.g., update message or files)
git commit --amend --no-edit # Amend without changing the commit message
Changing the Message of an Older Commit
- Start an interactive rebase:
git rebase -i HEAD~<number_of_commits> - Mark the commit to edit by replacing
pickwithedit. - Modify the commit:
git commit --amend - Continue the rebase:
git rebase --continue
Squashing Commits (Combining Commits)
- Start an interactive rebase:
git rebase -i HEAD~<number_of_commits> - Change
picktosquash(ors) for the commits you want to merge. - Edit the commit message when prompted.
Removing a Commit from History
git rebase -i HEAD~<number_of_commits> # Mark the commit with `drop` to remove it
Reverting Changes in a Commit
git revert <commit_hash> # Create a new commit that undoes a specific commit
Rewriting History Across the Entire Repository
Use with caution!
git filter-branch --tree-filter 'rm -rf <file_or_directory>' HEAD
Force Push to Rewrite Remote History
git push --force # Push rewritten history to remote
Advanced Stashing
Save Changes to Stash
git stash # Save changes to stash
git stash save "Message" # Save with a message
List Stashes
git stash list # Show all stashes
Apply or Pop Stashes
git stash apply # Apply the latest stash (without removing it)
git stash apply stash@{2} # Apply a specific stash
git stash pop # Apply and remove the latest stash
git stash pop stash@{2} # Apply and remove a specific stash
Stashing Specific Files
git stash push -m "Message" <file> # Stash specific files
View Stash Contents
git stash show -p # Show changes in the most recent stash
git stash show -p stash@{2} # Show changes in a specific stash
Create a Branch from Stash
git stash branch <branch_name> # Create a new branch with the stash applied
Drop or Clear Stashes
git stash drop # Remove the latest stash
git stash drop stash@{2} # Remove a specific stash
git stash clear # Remove all stashes
Key Differences: Apply vs. Pop
git stash apply:Applies the stash but keeps it in the stash list.git stash pop:Applies the stash and removes it from the stash list.
Additional Tips for History Rewriting
Remove Sensitive Data from History
If a file with sensitive data (e.g., passwords.txt) was committed:
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch passwords.txt' \
--prune-empty --tag-name-filter cat -- --all
Then force-push to update the remote:
git push --force --all
If you need additional details on any command, use:
git help <command>