Introduction
GitHub is a platform where you store your code online using a tool called Git. Git helps you track changes, work in teams, and manage code history. This cheatsheet explains important Git commands in simple terms.
1. Git Setup Commands
These commands help you set Git for the first time.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"These set your identity for all Git commits on your machine.
git config --global core.editor codeThis sets VS Code as the default editor.
git config --global --listThis shows your global Git config values.
2. Create or Clone a Repository
Create a new local repository
git initInitializes Git tracking in your project folder.
Clone a remote repository
git clone https://github.com/user/repo.gitDownloads a GitHub repository to your local machine.
3. Basic Workflow
These are the steps for daily Git work.
Check status
git statusShows which files are changed, staged, or untracked.
Add files to staging
git add filenameStages a file to be committed.
git add .Stages all changed files in the folder.
Commit changes
git commit -m "your message"Saves a snapshot of the changes with a message.
4. Branching
Create a new branch
git branch branch-nameSwitch to a branch
git checkout branch-nameCreate and switch to a new branch
git checkout -b new-branchList all branches
git branchDelete a branch
git branch -d branch-nameUse -D instead of -d to force delete.
5. Merging and Rebase
Merge another branch into current branch
git merge branch-nameCombines changes from the target branch.
Rebase current branch onto another
git rebase branch-nameMoves your branch on top of another to make history cleaner.
6. Remote Commands
Check remotes
git remote -vShows which remotes are connected (like GitHub).
Add a new remote
git remote add origin https://github.com/user/repo.gitPush code to GitHub
git push origin branch-nameSends your code to GitHub on the specified branch.
Pull latest code
git pull origin branch-nameGets the latest code from GitHub and merges it.
7. Viewing History
Show commit history
git logShows a list of past commits.
git log --onelineShows commit history in one-line format.
8. Undoing Changes
Unstage a file
git reset filenameUndo last commit (keep changes)
git reset --soft HEAD~1Undo last commit and remove changes
git reset --hard HEAD~19. Stash Changes
Save changes temporarily
git stashSee all stashes
git stash listApply latest stash
git stash applyRemove applied stash
git stash drop10. Tagging Versions
Add a tag
git tag v1.0Push tags to GitHub
git push origin v1.011. .gitignore File
Tells Git to ignore certain files or folders.
Create a file named .gitignore and add file patterns like:
*.log
node_modules/
.env12. Git Aliases (Optional but Useful)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st statusNow you can use:
git co main
git ci -m "Message"13. Working with Pull Requests (On GitHub)
- Push your branch to GitHub
Bash
git push origin feature-branch - Go to GitHub and click Compare & pull request.
- Review your changes and click Create pull request.
14. Other Useful Commands
See difference before committing
git diffSee difference of staged files
git diff --cachedBlame line by line
git blame filenameShows who last changed each line.
Show a specific commit
git show commit-id15. Handling Mistakes
Recover a deleted file
git checkout HEAD filenameView file history
git log filename16. Advanced Commands (for Interviews or Complex Projects)
Rebase interactively to edit history
git rebase -i HEAD~nEdit, reorder, squash, or remove last n commits.
Clean untracked files
git clean -fdDeletes all untracked files and folders.
Conclusion
This GitHub Cheatsheet is your quick guide for all important Git tasks. Once you use these commands a few times, they will become part of your daily workflow.
For teams or real projects, always remember:
- Commit often with clear messages.
- Use branches for new features.
- Pull before you push.
- Never commit sensitive files.

Join the conversation! Your thoughts help the community grow.