Global Configuration
Set up your Git identity before making commits. This information appears in your commit history.
# Set your name and email globally
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Verify your settings
git config --global --list
Stash Commands
Git stash temporarily saves your uncommitted changes so you can switch branches or pull updates without losing work.
Basic Stash Operations
# Save current changes to stash
git stash
# Save with a descriptive message
git stash save "WIP: feature login form"
# List all stashes
git stash list
# Apply the most recent stash (keeps it in stash list)
git stash apply
# Apply and remove the most recent stash
git stash pop
Working with Multiple Stashes
# Apply a specific stash
git stash apply stash@{2}
# Show changes in a specific stash
git stash show -p stash@{0}
# Create a patch file from a stash
git stash show -p stash@{0} > my-changes.patch
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes (use with caution!)
git stash clear
Stash includes tracked files by default. Use
git stash -uto include untracked files, orgit stash -ato include ignored files too.
Reset Commands
Git reset moves the HEAD pointer and optionally modifies the staging area and working directory. Use carefully—especially --hard.
Undo the Last Commit
# Soft reset: undo commit, keep changes staged
git reset --soft HEAD~1
# Mixed reset (default): undo commit, unstage changes, keep files
git reset HEAD~1
# Hard reset: undo commit AND delete changes (DANGEROUS!)
git reset --hard HEAD~1
Reset to Match Remote
# Discard all local commits and match remote exactly
git fetch origin
git reset --hard origin/main
# Reset a single file to match remote
git checkout origin/main -- path/to/file.js
Warning:
git reset --hardpermanently deletes uncommitted changes. Always stash or commit important work first.
Branch Management
Create and Switch Branches
# Create a new branch and switch to it
git checkout -b feature/new-feature
# Modern alternative (Git 2.23+)
git switch -c feature/new-feature
# Push new branch to remote
git push -u origin feature/new-feature
List and Delete Branches
# List local branches
git branch
# List all branches (including remote)
git branch -a
# Delete a local branch
git branch -d feature/old-feature
# Force delete (if not merged)
git branch -D feature/old-feature
# Delete remote branch
git push origin --delete feature/old-feature
Merge Branches
# Merge feature branch into main
git checkout main
git merge feature/new-feature
# See conflicted files after a merge conflict
git status
# Abort a merge with conflicts
git merge --abort
Creating Patches
Patches are useful for sharing changes via email or applying fixes across repositories.
# Create a patch from the last commit
git format-patch -1 HEAD
# Create a patch from the last 3 commits
git format-patch -3 HEAD
# Create a patch with a custom output file
git format-patch -1 HEAD --stdout > fix-bug-123.patch
# Apply a patch
git apply fix-bug-123.patch
# Apply a patch as a commit (preserves author info)
git am fix-bug-123.patch
Viewing History
# View commit history
git log --oneline
# View history with graph
git log --oneline --graph --all
# See who changed each line of a file
git blame path/to/file.js
# GUI blame viewer
git gui blame path/to/file.js
# Search commit messages
git log --grep="bug fix"
# Find commits that changed a specific file
git log --follow -- path/to/file.js
Hide Files from Git Status
Sometimes you need to modify a tracked file locally without Git constantly showing it as changed (e.g., local config overrides).
# Tell Git to ignore changes to a tracked file
git update-index --assume-unchanged path/to/config.js
# Resume tracking changes
git update-index --no-assume-unchanged path/to/config.js
# List all files marked as assume-unchanged
git ls-files -v | grep '^h'
This is different from
.gitignore, which only works for untracked files. Use--assume-unchangedfor tracked files you want to modify locally.
Submodules
# Add a submodule
git submodule add https://github.com/user/repo.git path/to/submodule
# Initialize submodules after cloning
git submodule init
git submodule update
# Update all submodules to latest
git submodule update --remote --merge
# Clone a repo with submodules
git clone --recurse-submodules https://github.com/user/repo.git
Miscellaneous Commands
# Amend the last commit message
git commit --amend -m "New commit message"
# Amend the last commit author
git commit --amend --author="New Name <new@email.com>"
# Get a file from another branch
git checkout other-branch -- path/to/file.js
# Rename a file (and track the rename)
git mv old-name.js new-name.js
# Cherry-pick a specific commit
git cherry-pick abc1234
# Clean untracked files (dry run first!)
git clean -n # preview what will be deleted
git clean -f # actually delete
Quick Reference Table
| Task | Command |
|---|---|
| Save work temporarily | git stash |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Undo last commit (delete changes) | git reset --hard HEAD~1 |
| Create new branch | git checkout -b branch-name |
| Push new branch | git push -u origin branch-name |
| Delete remote branch | git push origin --delete branch-name |
| Create patch | git format-patch -1 HEAD |
| View file history | git log --follow -- file.js |
| Ignore local changes | git update-index --assume-unchanged file |
Next Steps
- Learn about Git Rebase for cleaner history
- Explore Git workflows (Gitflow, trunk-based)
- Set up Git hooks for automation
Need help with Git workflows for your team? Contact us for consulting.