Git & GitHub Commands
Essential Git commands for version control and collaboration.
Setup & Config
git initInitialize a new Git repository in the current directory
git clone https://github.com/user/repo.gitClone 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 branchList all local branches
git branch feature/loginCreate a new branch
git checkout -b feature/loginCreate and switch to a new branch
git switch mainSwitch to an existing branch
git merge feature/loginMerge a branch into the current branch
git branch -d feature/loginDelete a branch after merging
Staging & Commits
git statusShow working tree status - modified, staged, untracked files
git add .Stage all changes in the current directory
git add -pInteractively stage specific chunks of changes
git commit -m "feat: add login page"Commit staged changes with a message
git commit --amendModify the last commit (message or content)
git diffShow unstaged changes line by line
Stash
git stashTemporarily save uncommitted changes
git stash popRestore the most recent stashed changes
git stash listList all stashed change sets
git stash dropDelete the most recent stash
Remote & Sync
git remote add origin https://github.com/user/repo.gitLink local repo to a remote repository
git push -u origin mainPush branch and set upstream tracking
git pull origin mainFetch and merge changes from remote
git fetch --allDownload all remote branches without merging
git push origin --delete feature/oldDelete a remote branch
Undo & Fix
git reset HEAD~1Undo last commit but keep changes staged
git reset --hard HEAD~1Undo last commit and discard all changes
git checkout -- file.txtDiscard changes in a specific file
git revert abc123Create a new commit that undoes a specific commit
git log --oneline -10Show last 10 commits in compact format