Git commands cheat sheet
Setup git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
Create a New Repository
git init
Clone an Existing Repository
git clone <repository-url>
#Working with Changes
Check Status
git status
Add Changes
git add <file>
git add . # Add all changes
Commit Changes
git commit -m "Commit message"
Remove Files
git rm <file>
Move/Rename Files
git mv <old-name> <new-name>
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 <branch-name>
Merge a Branch
git checkout <target-branch>
git merge <source-branch>
Delete a Branch
git branch -d <branch-name>
Add a Remote Repository
git remote add <remote-name> <remote-url>
Fetch Changes from Remote
git fetch <remote-name>
Push Changes to Remote
git push <remote-name> <branch-name>
Pull Changes from Remote
git pull <remote-name> <branch-name>
Show Commit History
git log
Show Changes in Files
git diff
Show a Specific Commit
git show <commit-hash>
Stash Changes
git stash
Apply Stashed Changes
git stash apply
List Stashes
git stash list
Undo Changes in Working Directory
git checkout -- <file>
Reset to Last Commit
git reset --hard HEAD
Revert a Commit
git revert <commit-hash>
Create a Tag
git tag <tag-name>
Push Tags to Remote
git push <remote-name> --tags
Update Local Repository
git pull origin main
Push Your Branch
git push origin <branch-name>
- Push your branch to the remote repository.
- Go to the repository on GitHub.
- Click "Compare & pull request".
- Add a description and click "Create pull request".
Interactive Rebase
git rebase -i <commit-hash>
Squash Commits
git rebase -i HEAD~<number-of-commits>
Cherry Pick a Commit
git cherry-pick <commit-hash>