Git Commands Cheat Sheet
Configuration
Setting up Git
# Set up your name
git config --global user.name "Your Name"
# Set up your email
git config --global user.email "your.email@example.com"
# Check your Git configuration
git config --listRepository
Initialize a New Git Repository
git initClone an Existing Repository
git clone <repository_url>Basic Snapshotting
Check the Status of Your Repository
git statusAdd Changes to the Staging Area
# Add a specific file
git add <file_name>
# Add all files
git add .Commit Changes
# Commit with a message
git commit -m "Your commit message"View Commit History
git log
# View commit history in a single line format
git log --onelineView Changes
# View changes in the working directory
git diff
# View changes between commits
git diff <commit1> <commit2>Branching and Merging
List Branches
git branchCreate a New Branch
git branch <branch_name>Switch to a Branch
git checkout <branch_name>Create and Switch to a New Branch
git checkout -b <branch_name>Merge a Branch into the Current Branch
git merge <branch_name>Delete a Branch
git branch -d <branch_name>Remote Repositories
List Remote Repositories
git remote -vAdd a Remote Repository
git remote add <remote_name> <repository_url>Fetch Changes from a Remote Repository
git fetch <remote_name>Pull Changes from a Remote Repository
git pull <remote_name> <branch_name>Push Changes to a Remote Repository
git push <remote_name> <branch_name>Undoing Changes
Unstage a File
git reset <file_name>Revert Changes in a File
# Discard changes in the working directory
git checkout -- <file_name>Reset to a Previous Commit
# Soft reset (keeps changes in the working directory)
git reset --soft <commit_hash>
# Hard reset (discards changes)
git reset --hard <commit_hash>Stashing
Stash Changes
git stashList Stashes
git stash listApply Stashed Changes
git stash applyDrop a Stash
git stash dropAdditional Commands
Show Commit Details
git show <commit_hash>Tagging
# Create a tag
git tag <tag_name>
# List tags
git tagViewing a Specific File from a Commit
git show <commit_hash>:<file_path>