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 code
This sets VS Code as the default editor.
git config --global --list
This shows your global Git config values.
2. Create or Clone a Repository
Create a new local repository
git init
Initializes Git tracking in your project folder.
Clone a remote repository
git clone https://github.com/user/repo.git
Downloads a GitHub repository to your local machine.
3. Basic Workflow
These are the steps for daily Git work.
Check status
git status
Shows which files are changed, staged, or untracked.
Add files to staging
git add filename
Stages 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-name
Switch to a branch
git checkout branch-name
Create and switch to a new branch
git checkout -b new-branch
List all branches
git branch
Delete a branch
git branch -d branch-name
Use -D
instead of -d
to force delete.
5. Merging and Rebase
Merge another branch into current branch
git merge branch-name
Combines changes from the target branch.
Rebase current branch onto another
git rebase branch-name
Moves your branch on top of another to make history cleaner.
6. Remote Commands
Check remotes
git remote -v
Shows which remotes are connected (like GitHub).
Add a new remote
git remote add origin https://github.com/user/repo.git
Push code to GitHub
git push origin branch-name
Sends your code to GitHub on the specified branch.
Pull latest code
git pull origin branch-name
Gets the latest code from GitHub and merges it.
7. Viewing History
Show commit history
git log
Shows a list of past commits.
git log --oneline
Shows commit history in one-line format.
8. Undoing Changes
Unstage a file
git reset filename
Undo last commit (keep changes)
git reset --soft HEAD~1
Undo last commit and remove changes
git reset --hard HEAD~1
9. Stash Changes
Save changes temporarily
git stash
See all stashes
git stash list
Apply latest stash
git stash apply
Remove applied stash
git stash drop
10. Tagging Versions
Add a tag
git tag v1.0
Push tags to GitHub
git push origin v1.0
11. .gitignore File
Tells Git to ignore certain files or folders.
Create a file named .gitignore
and add file patterns like:
*.log
node_modules/
.env
12. 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 status
Now you can use:
git co main
git ci -m "Message"
13. Working with Pull Requests (On GitHub)
- Push your branch to GitHub
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 diff
See difference of staged files
git diff --cached
Blame line by line
git blame filename
Shows who last changed each line.
Show a specific commit
git show commit-id
15. Handling Mistakes
Recover a deleted file
git checkout HEAD filename
View file history
git log filename
16. Advanced Commands (for Interviews or Complex Projects)
Rebase interactively to edit history
git rebase -i HEAD~n
Edit, reorder, squash, or remove last n commits.
Clean untracked files
git clean -fd
Deletes 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.