Useful GIT Commands

1. Repository Setup

Initialize new repo

git init

Clone existing repo

git clone https://github.com/username/repo.git

Check remote repository

git remote -v

Add remote

git remote add origin https://github.com/username/repo.git

2. Daily Workflow Commands

Check status

git status

Pull latest changes

git pull origin main

Stage all files

git add .

Stage specific file

git add filename.js

Commit changes

git commit -m “Your message”

Push to GitHub

git push origin main

3. Viewing History

View commit log

git log

Compact log

git log –oneline

See file changes

git diff

See staged changes

git diff –staged

4. Branching (Very Useful Even If Working Alone)

Check branches

git branch

Create branch

git branch new-branch

Switch branch

git checkout new-branch

OR modern way:

git switch new-branch

Create + switch

git checkout -b new-branch

Merge branch

git merge branch-name

5. Undo / Fix Mistakes (Very Important)

Unstage file

git restore –staged filename

Discard local changes

git restore filename

Reset last commit (keep changes)

git reset –soft HEAD~1

Reset last commit (remove changes)

git reset –hard HEAD~1

--hard deletes changes permanently.

6. Working on Multiple Computers

Fetch without merging

git fetch

Check if branch is behind

git status

Force sync with remote (use carefully)

git reset –hard origin/main

7. Stashing (Very Useful)

When you want to temporarily save work without committing:

Save work

git stash

See stash list

git stash list

Apply stash

git stash apply

Remove stash

git stash drop

8. Tracking & Information

Show remote branch info

git branch -r

Show config

git config –list

Set global username

git config –global user.name “Your Name”

Set global email

git config –global user.email “[email protected]

9. Tagging (For Versioning)

Create tag

git tag v1.0

Push tag

git push origin v1.0