# 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 ```bash # 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: ```bash # 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 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: ```bash # Force push your changes (use with caution!) git push --force origin # A safer alternative: force-with-lease checks if remote has new changes git push --force-with-lease origin # Overwrite local changes with remote changes git reset --hard origin/ # Overwrite a single file with the version from another branch git checkout -- ``` ## Going Back to a Specific Commit ```bash # Move back to a specific commit without changing files (detached HEAD) git checkout # Reset your branch to a specific commit (will lose later commits) git reset --hard # Create a new commit that undoes changes from a previous commit git revert ``` ## Branch Management ```bash # Create and switch to a new branch git checkout -b # Switch to an existing branch git checkout # List all branches git branch # Local branches git branch -r # Remote branches git branch -a # All branches # Delete a branch git branch -d # Safe delete (prevents deleting unmerged changes) git branch -D # Force delete ``` ## Stashing Changes Useful when you need to switch branches but aren't ready to commit: ```bash # 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 ```bash # Amend the last commit message git commit --amend -m "New commit message" # Add forgotten files to the last commit git add git commit --amend --no-edit # Undo staging of files git reset # Discard changes in working directory git checkout -- git restore # In newer Git versions ``` ## Advanced Techniques ```bash # 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 # 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 ``` ## Safety Tips 1. **Always create backups** before performing destructive operations 2. **Use `--dry-run`** when available to preview command effects 3. **Avoid force push** on shared branches (especially main/master) 4. Set up **aliases** for complex commands you use frequently 5. 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.