Git can be complex, but with the right reference, it becomes a powerful ally. This cheat sheet is designed to be clear, visual, and comprehensive.
🛠️ Configuration & Setup
First things first: identify yourself to Git.
You can use --global to apply settings to all repositories on your system, or omit it for project-specific settings.
git config --global user.name "Your Name"git config --global user.email "[email protected]"🚀 Repository Initialization
Start a new project or grab an existing one from the cloud.
git initgit clone <url>🔄 Daily Workflow (Staging & Committing)
This is where the magic happens. Move your changes from the working directory to the repository.
Status Check
git status before committing to see exactly what is being included.git statusgit add <file>git commit -m "Descriptive message"🌿 Branching & Merging
Branches are essential for working in parallel without affecting the main codebase (main).
Branching Concept
| Command | Action |
|---|---|
git branch | List all local branches |
git checkout -b <name> | Create and switch to a new branch |
git merge <name> | Merge branch <name> into current branch |
git branch -d <name> | Safely delete a merged branch |
🏗️ Pull Requests (PRs)
A Pull Request is the way you propose changes to a repository and ask others to review them before they are integrated.
PR Merge Types:
- Standard Merge: Creates a “merge commit”, keeping the entire commit history of the branch.
- Squash and Merge: Combines all commits from the branch into a single one. Great for keeping a clean history.
- Rebase and Merge: Applies the branch’s commits one by one on top of the current base. Does not create a merge commit.
Use Draft Pull Requests if you want to showcase your work-in-progress but aren’t ready for official review yet.
🛠️ Workflow
The most common workflow on platforms like GitHub or SourceHut is:
- Fork: Create a copy of the repository in your own account.
- Clone: Download your copy locally:
git clone <url>. - Branch: Create a branch for your change:
git checkout -b my-improvement. - Commit: Make your changes and save them:
git commit -am "Add X". - Push: Upload the branch to your remote:
git push origin my-improvement. - PR: Open a Pull Request from the platform’s web interface.
🌐 Remote Operations
Sync your local work with a remote server (like GitHub or SourceHut).
git remote -vgit push origin <branch>git pull origin <branch>⏪ Undoing Changes
Mistakes happen. Here is how to fix them.
Caution
--hard resets, as they permanently discard uncommitted changes.- Unstage a file:
git reset <file> - Discard local changes:
git checkout -- <file> - Undo last commit (keep changes):
git reset --soft HEAD~1 - Revert a commit (safe):
git revert <hash>
📦 Stashing
Need to switch branches but not ready to commit? Stash it!
git stashgit stash pop🔍 Inspection & Logs
Understand the history of your project.
git log --oneline --graph --allgit diff --staged🎓 Advanced Mastery
If you ever “lose” a commit after a bad reset, git reflog is your best friend. It tracks every move of the HEAD.
- Cherry-pick:
git cherry-pick <hash>(Apply a specific commit from elsewhere) - Reflog:
git reflog - Interactive Rebase:
git rebase -i HEAD~n

Comments
This blog uses a public mailing list for comments. Any message sent to the list will be publicly visible. You can join the discussion by viewing the archive or replying via email.