A practical cheat‑sheet for mastering Git and GitHub from basics to advanced workflows.
Table of contents
- Command Line Basics
- Git Concepts
- Initialize Git
- Staging Changes
- Creating Commits
- Conventional Commits
- Specify the type of commit
- Configure Git
- Reset & Checkout
- Viewing Previous Commits
- Restore Files from Previous Commit
- Git Aliases
- .gitignore
- Delete Git
- Connect to GitHub
- Push Code
- Clone / Pull
- Branching
- Merging
- Merge Conflicts
- Feature Branch Workflow
- Sync After Merge
- Delete Branch
- Git Rebase
- Git Reflog
- Git Revert
- Git: Revert vs Reflog vs Rebase
- Pro Tip
- Reference
ls # List files and folders
cd ~/Desktop/folder # Change directory
⚠️ Run git commands inside the project folder.
- Version = Commit
- Version History = Commit history
- Working Area → Staging Area → Commit History
git init # Git will start tracking all changes in the current folder
git status # Show all changes since the previous commit- Working Area = contains changes start in the working area
- Staging Area = contains changes that will go into the next commit
git add file # Pick individual file
git add folder/ # Pick all files inside a folder (and subfolders)
git add . # Pick all files (in folder command line is running in)git commit -m "message" # Creates a commit with a message attached
git commit -m "message" --amend # Update previous commit instead of creating new one
git log # View the commit history
git log --all # Show all commits (not just current branch)
git log --all --graph # Show branching visually in the command line
git log --oneline- Structure:
<type>: <description>
[optional body]
[optional footer]- Example:
git commit -m "feat: advanced login" `
-m "Added JWT token verification middleware" `
-m "Fixed bug in session timeout logic" `
-m "Closes #42"- feat: The new feature you're adding to a particular application
- fix: A bug fix
- style: Feature and updates related to styling
- refactor: Refactoring a specific section of the codebase
- test: Everything related to testing
- docs: Everything related to documentation
- chore: Regular code maintenance.[ You can also use emojis to represent commit types]
git config --global user.name "Your Name"
git config --global user.email "email@example.com"git reset file
git reset folder/
git reset .
git reset --hard head~1 #to completely go back to previous commit | Deletes the latest commit from history.
git push -f # to remove the future commit from present commit | Forces remote to forget that commit toogit checkout <commit> <file/folder> #syntax
git checkout -- file
git checkout -- folder/
git checkout -- .git checkout <commit_hash>
git checkout <branch_name>HEAD→ current commitmaster/main→ latest commit on branch
git checkout <hash> file
git checkout <hash> folder/
git checkout <hash> .git config --global alias.s "status"
git s
git config --global --unset alias.s # to remove an aliasTell Git which files/folders NOT to track. .gitignore
rm -rf .git # to remove git completely from the project - it deletes the .git folderWindows PowerShell:
Remove-Item -Recurse -Force -LiteralPath ".\.git"- Delete the .git from hidden folders in the project
- Local Repo = On your computer
- Remote Repo = On GitHub
git remote add <remote_name> <url> # Link a local repository to a remote repository andgive a name for this link
git remote # List all remote repositories that are linked
git remote -v # List all remote repositories (but with more detail)
git remote remove origin # Removes the link to the remote repository named "origin"
git config --global credential.username <username> # Configure your GitHub username so you can get access to your Github repositorygit push origin main # Upload a branch of your git version history to your remote repository
git push origin main --set-upstream # Next time you are on the main branch and you run git push, it will automatically push the main branch to origin
git push origin main -f # Force-push the branch to the remote repository (it will overwrite what's on the remote repository)git clone <url> <folder_name>
git fetch
# The git fetch command is primarily used to safely download the latest commits, branches, and tags from a remote repository to your local machine without modifying your local working files
git pull origin main
git pull origin main --set-upstreamgit branch feature1
git checkout feature1
git branch -D feature1
git push origin --delete project/meme-pageHEAD -> feature1→ current branch
git checkout main
git merge feature1 -m "message"
git merge --no-ff feature1 -m "message" # no fast forward commit, feature 1 is shown as separate and mergedWhen conflicts occur, Git marks the file like this:
<<<<<<< HEAD
code from current branch
=======
code from merging branch
>>>>>>> branch
- Remove conflict markers
- Keep the final code you want
- Commit the changes
git add .
git commit -m "resolved conflict"git branch new-feature
git checkout new-feature
git add .
git commit -m "new feature"
git push origin new-featureThen create a Pull Request on GitHub and merge into main.
git checkout main
git pull origin maingit branch -D branch-nameRebasing is the process of moving or combining a sequence of commits to a new base commit. It is primarily used to maintain a clean, linear project history.
Use this to edit, squash, or reorder commits before they are shared.
git rebase -i HEAD~n- Common Commands in Interactive Mode:
- reword: Use the commit, but edit the commit message.
- squash: Meld the commit into the previous one.
- pick: Use the commit as is.
- drop: Remove the commit entirely.
Think of Reflog as your local "undo" history. While git log shows the history of the branch, git reflog records every time your HEAD pointer moves—including resets, checkouts, and rebases.
When to Use:
- You accidentally deleted a branch.
- You performed a git reset --hard and lost work.
- A rebase went wrong and you want to go back.
How to Undo a Rebase
- Find the state before the rebase:
git reflog-
Locate the entry that says rebase (start) and look at the line directly below it (e.g., HEAD@{5}).
-
Reset back to that moment:
git reset --hard HEAD@{n}If the merge commit has been pushed to a shared or remote repository (like GitHub, GitLab, etc.), you should use git revert. This approach is safer for collaboration because it creates a new commit that undoes the changes of the merge, rather than rewriting the shared history.
- Identify the merge commit; Use git log --oneline --graph to find the hash (ID) of the specific merge commit you want to undo.
- Revert the merge commit: Run the git revert command with the -m (mainline parent number) option:
git revert -m 1 <merge-commit-hash>The -m 1 flag tells Git to keep the parent side of the merge that was the branch you merged into (e.g., main or master branch).
-
Resolve conflicts (if any): Git may pause and ask you to resolve conflicts if subsequent changes conflict with the revert. After resolving, use git add and then git commit to finalize the revert.
-
Push the new revert commit: Push the new commit to the remote repository:
git push origin <your-branch-name>| Feature | git revert | git reflog | git rebase |
|---|---|---|---|
| Purpose | Undo a commit safely | View reference history | Rewrite commit history |
| Rewrites history? | ❌ No | ❌ No (just logs) | ✅ Yes |
| Safe for shared branches? | ✅ Yes | ❌ No (avoid on shared branches) | |
| Creates new commit? | ✅ Yes (undo commit) | ❌ No | ❌ No (rewrites existing commits) |
| Used for recovery? | ❌ Not mainly | ✅ Yes | ❌ Not mainly |
| Used for cleaning history? | ❌ No | ❌ No | ✅ Yes |
| Works on pushed commits? | ✅ Yes | ✅ Yes | |
| Typical Use Case | Undo a pushed commit | Recover lost commit | Clean up local commit history |
| Synonym | Undo Safely | Time Machine | Rewrite History |
- You pushed a bad commit
- You want to safely undo changes
- You're working in a team
- You don’t want to rewrite history
Example:
git revert <commit-hash>- You accidentally reset or deleted a branch
- You lost a commit
- You need to find old HEAD positions
- You want to recover something
Example:
git reflog- You want clean commit history
- You want to squash commits
- You want linear history
- You're working locally (before pushing)
Example:
git rebase mainUse branches for features and fixes. Keep main clean and production-ready.
Reference PDF by: supersimple.dev Tutorial Source: https://www.youtube.com/watch?v=hrTQipWp6co