Useful GIT commands

Sep 12, 2023

List of all the useful GIT commands which I use for my daily web development work.

Repository Setup / Initialize new repo

git init

Set global email

git config –global user.email “your@email.com”

Clone existing repo

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

Check remote repository

git remote -v

Add remote repository

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

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

View commit log

git log

Compact log

git log –oneline

See file changes

git diff

See staged changes

git diff –staged

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

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.

Fetch without merging

git fetch

Check if branch is behind

git status

Force sync with remote (use carefully)

git reset –hard origin/main

Stashing (Very Useful) When you want to temporarily save work without committing

git stash

See stash list

git stash list

Apply stash

git stash apply

Remove stash

git stash drop

Show remote branch info

git branch -r

Show config

git config –list

Set global username

git config –global user.name “Your Name”

Create tag

git tag v1.0

Push tag

git push origin v1.0