# GitHub Commands Cheat Sheet
GitHub offers a variety of commands to manage repositories, branches, commits, and more. Here's a cheat sheet to help you navigate GitHub efficiently.
### Create a New Repository
```bash
git init
```
Clone an Existing Repository
git clone <repository-url>
Add Remote Repository
git remote add origin <repository-url>
Branch Operations
Create a New Branch
git checkout -b <branch-name>
Switch Branch
git checkout <branch-name>
List Branches
git branch
Delete Branch
git branch -d <branch-name>
Commit Changes
Stage Changes
git add .
Commit Changes
git commit -m "Commit message"
Push Changes
git push origin <branch-name>
Pull Request
Create Pull Request
gh pr create --base <base-branch> --head <head-branch>
List Pull Requests
gh pr list
Merge Pull Request
gh pr merge <pull-request-number>
Repository Collaboration
Add Collaborator
gh repo collaborator add <username>
Fork Repository
gh repo fork
Miscellaneous
Check Repository Status
git status
View Commit History
git log
Undo Changes
git checkout -- <file>
Undo Last Commit
If you’ve committed changes in Git but haven’t pushed them yet, you have a few ways to "uncommit" depending on whether you want to keep the changes or discard them.
1. Undo the last commit but keep the changes (soft reset)
git reset --soft HEAD~1
- This will remove the last commit but keep your changes staged (ready to be committed again).
2. Undo the last commit and unstage the changes (mixed reset)
git reset --mixed HEAD~1
- This removes the last commit and unstages the changes, but your modified files remain in your working directory.
3. Undo the last commit and discard all changes (hard reset)
git reset --hard HEAD~1
- ⚠ Warning: This will delete the commit and all changes in the working directory. Only use this if you're sure you don't need the changes.
4. Undo multiple commits
If you want to undo the last 2 commits, replace HEAD~1
with HEAD~2
:
git reset --soft HEAD~2
(Replace 2
with however many commits you want to undo.)
5. If you already pushed the commit (Force Push)
If you've already pushed the commit and want to undo it:
git reset --hard HEAD~1
git push --force
git commit --date="" -m ""
Ignore Files
touch .gitignore