Git & GitHub Commands

Essential Git commands for version control and collaboration.

Setup & Config

git init

Initialize a new Git repository in the current directory

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

Clone a remote repository to local machine

git config --global user.name "Your Name"

Set your name for all Git commits

git config --global user.email "you@email.com"

Set your email for all Git commits

Branching

git branch

List all local branches

git branch feature/login

Create a new branch

git checkout -b feature/login

Create and switch to a new branch

git switch main

Switch to an existing branch

git merge feature/login

Merge a branch into the current branch

git branch -d feature/login

Delete a branch after merging

Staging & Commits

git status

Show working tree status - modified, staged, untracked files

git add .

Stage all changes in the current directory

git add -p

Interactively stage specific chunks of changes

git commit -m "feat: add login page"

Commit staged changes with a message

git commit --amend

Modify the last commit (message or content)

git diff

Show unstaged changes line by line

Stash

git stash

Temporarily save uncommitted changes

git stash pop

Restore the most recent stashed changes

git stash list

List all stashed change sets

git stash drop

Delete the most recent stash

Remote & Sync

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

Link local repo to a remote repository

git push -u origin main

Push branch and set upstream tracking

git pull origin main

Fetch and merge changes from remote

git fetch --all

Download all remote branches without merging

git push origin --delete feature/old

Delete a remote branch

Undo & Fix

git reset HEAD~1

Undo last commit but keep changes staged

git reset --hard HEAD~1

Undo last commit and discard all changes

git checkout -- file.txt

Discard changes in a specific file

git revert abc123

Create a new commit that undoes a specific commit

git log --oneline -10

Show last 10 commits in compact format