3.9 KiB
3.9 KiB
Essential Git Commands for Common Problems
This guide covers basic Git commands to help you handle common challenges like merge conflicts, overwriting changes, and navigating commit history.
Basic Commands
# Check status of your repository
git status
# Check commit history
git log
git log --oneline --graph --decorate # More visual representation
# View changes before committing
git diff
git diff --staged # View staged changes
Handling Merge Conflicts
When your branch falls behind and you encounter conflicts:
# Update your local repository with remote changes
git fetch origin
# Merge changes from origin/main to your current branch
git merge origin/main
# If conflicts occur, Git will tell you which files are in conflict
# Edit the files manually to resolve conflicts, then:
git add <conflicted-files>
git commit -m "Resolve merge conflicts"
# Alternative: abort the merge if needed
git merge --abort
Force Your Version as the Latest
When you want your version to override others:
# Force push your changes (use with caution!)
git push --force origin <branch-name>
# A safer alternative: force-with-lease checks if remote has new changes
git push --force-with-lease origin <branch-name>
# Overwrite local changes with remote changes
git reset --hard origin/<branch-name>
# Overwrite a single file with the version from another branch
git checkout <branch-name> -- <file-path>
Going Back to a Specific Commit
# Move back to a specific commit without changing files (detached HEAD)
git checkout <commit-hash>
# Reset your branch to a specific commit (will lose later commits)
git reset --hard <commit-hash>
# Create a new commit that undoes changes from a previous commit
git revert <commit-hash>
Branch Management
# Create and switch to a new branch
git checkout -b <new-branch-name>
# Switch to an existing branch
git checkout <branch-name>
# List all branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Delete a branch
git branch -d <branch-name> # Safe delete (prevents deleting unmerged changes)
git branch -D <branch-name> # Force delete
Stashing Changes
Useful when you need to switch branches but aren't ready to commit:
# Save changes to the stash
git stash save "Your stash message"
# List stashes
git stash list
# Apply the most recent stash without removing it
git stash apply
# Apply a specific stash
git stash apply stash@{n}
# Apply and remove the most recent stash
git stash pop
# Remove a stash
git stash drop stash@{n}
# Clear all stashes
git stash clear
Fixing Mistakes
# Amend the last commit message
git commit --amend -m "New commit message"
# Add forgotten files to the last commit
git add <forgotten-files>
git commit --amend --no-edit
# Undo staging of files
git reset <file>
# Discard changes in working directory
git checkout -- <file>
git restore <file> # In newer Git versions
Advanced Techniques
# Interactive rebase to edit, squash, or reorder commits
git rebase -i HEAD~n # Where n is the number of commits to go back
# Cherry-pick specific commits from another branch
git cherry-pick <commit-hash>
# Cleanup unnecessary files
git clean -n # Dry run - shows what would be deleted
git clean -f # Actually delete the files
# Temporarily save changes from a dirty working directory
git worktree add <path> <branch>
Safety Tips
- Always create backups before performing destructive operations
- Use
--dry-run
when available to preview command effects - Avoid force push on shared branches (especially main/master)
- Set up aliases for complex commands you use frequently
- Configure Git to require confirmation for potentially harmful actions
Remember that Git preserves history in the .git
directory, so most "destructive" commands can be undone, but it's better to be cautious.