Git Commands
Essential Git commands for version control workflows, including advanced stash operations and SSH configuration.
Stash Operations
Git stash temporarily stores modified tracked files, allowing you to switch branches without committing.
Quick Reference
| Task | Command |
|---|---|
| Stash one file | git stash push -m "message" -- file.txt |
| Stash untracked files | git stash push -m "message" -u |
| Apply with staged state | git stash apply --index |
| Apply specific stash | git stash apply stash@{0} |
| List stashes | git stash list |
| Show stash contents | git stash show -p stash@{0} |
| Drop stash | git stash drop stash@{0} |
| Clear all stashes | git stash clear |
Basic Stash Commands
bash
# Stash current changes
git stash
# Stash with message
git stash push -m "Work in progress on feature X"
# Stash including untracked files
git stash push -u
# Stash including untracked and ignored files
git stash push -aStash Specific Files
bash
# Stash single file
git stash push -m "Update config" -- config.json
# Stash multiple files
git stash push -m "Update configs" -- config.json settings.yml
# Stash files matching pattern
git stash push -m "Stash tests" -- tests/*.test.jsApplying Stashes
bash
# Apply most recent stash (keep stash)
git stash apply
# Apply and remove most recent stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Apply stash and restore staged state
git stash apply --indexManaging Stashes
bash
# List all stashes
git stash list
# Show stash changes
git stash show
# Show detailed stash changes
git stash show -p
# Show specific stash
git stash show -p stash@{1}
# Create branch from stash
git stash branch feature-branch stash@{0}
# Drop specific stash
git stash drop stash@{0}
# Clear all stashes
git stash clearStash Examples
bash
# Stash before switching branches
git stash push -m "WIP: user authentication"
git checkout main
# Later, return and apply stash
git checkout feature-auth
git stash pop
# Stash only staged changes
git stash push --staged -m "Only staged changes"
# Stash everything except specific file
git stash push -m "Everything but config" -- . ":(exclude)config.json"SSH Configuration
Generate SSH Key
bash
# Generate RSA key (4096 bits recommended)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Generate Ed25519 key (modern, more secure)
ssh-keygen -t ed25519 -C "your_email@example.com"
# Specify custom file location
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github -C "your_email@example.com"Add SSH Key to Agent
Linux/Mac:
bash
# Start SSH agent
eval "$(ssh-agent -s)"
# Add key to agent
ssh-add ~/.ssh/id_ed25519
# Add key with custom location
ssh-add ~/.ssh/id_ed25519_githubWindows (PowerShell):
powershell
# Start SSH agent service
Start-Service ssh-agent
# Add key to agent
ssh-add C:\Users\YourName\.ssh\id_ed25519Add SSH Key to GitHub
bash
# Copy public key to clipboard (Linux)
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# Copy public key to clipboard (Mac)
pbcopy < ~/.ssh/id_ed25519.pub
# Copy public key to clipboard (Windows PowerShell)
Get-Content ~/.ssh/id_ed25519.pub | clip
# Or just display it
cat ~/.ssh/id_ed25519.pubThen:
- Go to GitHub Settings → SSH and GPG keys
- Click "New SSH key"
- Paste your public key
- Give it a descriptive title
Test SSH Connection
bash
# Test GitHub connection
ssh -T git@github.com
# Test GitLab connection
ssh -T git@gitlab.com
# Test with verbose output
ssh -vT git@github.comMultiple SSH Keys
Create ~/.ssh/config:
# GitHub account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
# Work GitHub account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
# GitLab account
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlabUsage:
bash
# Clone using default github.com config
git clone git@github.com:user/repo.git
# Clone using work config
git clone git@github-work:company/repo.gitCommon Git Commands
Repository Setup
bash
# Initialize repository
git init
# Clone repository
git clone https://github.com/user/repo.git
# Clone with SSH
git clone git@github.com:user/repo.git
# Clone specific branch
git clone -b develop https://github.com/user/repo.gitBasic Workflow
bash
# Check status
git status
# Add files
git add file.txt
git add .
git add -A
# Commit
git commit -m "Commit message"
# Commit with detailed message
git commit -m "Short description" -m "Detailed explanation"
# Amend last commit
git commit --amend
# Push to remote
git push origin main
# Pull from remote
git pull origin mainBranching
bash
# List branches
git branch
git branch -a # Include remote branches
# Create branch
git branch feature-name
# Switch branch
git checkout feature-name
git switch feature-name # Modern syntax
# Create and switch
git checkout -b feature-name
git switch -c feature-name # Modern syntax
# Delete branch
git branch -d feature-name
git branch -D feature-name # Force delete
# Rename branch
git branch -m old-name new-nameMerging and Rebasing
bash
# Merge branch into current
git merge feature-name
# Merge with no fast-forward
git merge --no-ff feature-name
# Rebase current branch onto main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Abort merge/rebase
git merge --abort
git rebase --abortRemote Management
bash
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL (HTTPS to SSH)
git remote set-url origin git@github.com:user/repo.git
# Remove remote
git remote remove origin
# Fetch from remote
git fetch origin
# Prune deleted remote branches
git fetch --pruneViewing History
bash
# View commit history
git log
# Compact log
git log --oneline
# Graph view
git log --graph --oneline --all
# Show commits by author
git log --author="John"
# Show commits in date range
git log --since="2 weeks ago"
# Show changes in commit
git show <commit-hash>
# Show file history
git log -p file.txtUndoing Changes
bash
# Discard changes in working directory
git checkout -- file.txt
git restore file.txt # Modern syntax
# Unstage file
git reset HEAD file.txt
git restore --staged file.txt # Modern syntax
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert commit (create new commit)
git revert <commit-hash>Tags
bash
# List tags
git tag
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tags to remote
git push origin v1.0.0
git push origin --tags # Push all tags
# Delete tag
git tag -d v1.0.0
git push origin :refs/tags/v1.0.0 # Delete remote tagAdvanced Operations
Cherry Pick
bash
# Apply commit from another branch
git cherry-pick <commit-hash>
# Cherry pick multiple commits
git cherry-pick <commit1> <commit2>
# Cherry pick without committing
git cherry-pick -n <commit-hash>Searching
bash
# Search in files
git grep "search term"
# Search in specific file type
git grep "search term" -- "*.js"
# Search commit messages
git log --grep="bug fix"
# Find when line was changed
git blame file.txtCleaning
bash
# Remove untracked files (dry run)
git clean -n
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Remove ignored files too
git clean -fdxSubmodules
bash
# Add submodule
git submodule add https://github.com/user/repo.git path/to/submodule
# Clone repository with submodules
git clone --recursive https://github.com/user/repo.git
# Initialize submodules after clone
git submodule init
git submodule update
# Update all submodules
git submodule update --remote
# Remove submodule
git submodule deinit path/to/submodule
git rm path/to/submoduleConfiguration
User Settings
bash
# Set username globally
git config --global user.name "Your Name"
# Set email globally
git config --global user.email "your.email@example.com"
# Set for specific repository
git config user.name "Work Name"
git config user.email "work@example.com"Aliases
bash
# Create aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.graph 'log --graph --oneline --all'
# Use aliases
git st
git graphOther Settings
bash
# Set default editor
git config --global core.editor "code --wait"
# Set default branch name
git config --global init.defaultBranch main
# Enable color output
git config --global color.ui auto
# Line ending handling (Windows)
git config --global core.autocrlf true
# Line ending handling (Linux/Mac)
git config --global core.autocrlf inputTroubleshooting
Common Issues
Merge Conflicts:
bash
# View conflicted files
git status
# After resolving conflicts
git add resolved-file.txt
git commitDetached HEAD:
bash
# Create branch from detached state
git checkout -b new-branch-nameWrong Commit Message:
bash
# Amend last commit message
git commit --amend -m "Corrected message"Pushed Wrong Code:
bash
# Revert commit (safe, creates new commit)
git revert <commit-hash>
git push
# Force push (dangerous, use with caution)
git reset --hard <commit-hash>
git push --forceSee Also
- GitHub CLI Commands - Using
ghcommand - Cross-Platform Commands - Terminal commands
- SSH Configuration - Detailed SSH setup