Useful git commands

Git Commands Cheat Sheet

Git Commands Cheat Sheet

Pro Tip: HEAD is a pointer to your current branch. If you check out a feature branch, HEAD moves to point there.

1. Remote Repositories & Setup

Action Command
Clone a Repository git clone <URL>
Connect Local to Remote git remote add origin <URL>
View Remote URLs git remote -v
Remove Remote Connection git remote rm <name>

2. Branching & Basic Workflow

Action Command
Check Status git status
Create Branch git branch <name>
Switch to Branch git checkout -b <name>
Stage All Changes git add . or git add -A
Commit Changes git commit -m "message"
Stage & Commit (shortcut) git commit -am "message"

3. Syncing with Remote

Action Command
Pull Latest Changes git pull origin <branch>
Push Changes git push origin <branch>
Fetch Updates (No Merge) git fetch

4. History & Inspection

Action Command
View Commit Log git log
Log (One Line) git log --oneline
Show Last 5 Commits git log -n 5
View Specific Commit git show <hash>

5. Stashing (Temporary Storage)

Action Command
Stash Changes git stash
Stash Including New Files git stash -u -m "name"
List All Stashes git stash list
Apply & Delete Stash git stash pop
Delete All Stashes git stash clear

6. Corrections & Cleanup

Action Command
Amend Last Commit git commit --amend --no-edit
Reset to Commit ID git reset <commit_id>
Discard Local Changes git reset --hard
Delete Untracked Files git clean -fd
Dry Run (Clean) git clean -fdn
Reset vs. Revert: Use Reset for local changes you want to erase. Use Revert for changes already pushed to a shared repo to keep the history clean for others.

Comments