Git & Version Control53 entries
Git Commands
Complete Git reference: init, branching, merging, rebasing, stashing, and remote operations
1Setup & Config
git init | Initialize a new Git repository |
git clone <url> | Clone a remote repository |
git config --global user.name "Name" | Set your name for all repos |
git config --global user.email "email" | Set your email for all repos |
git config --list | Show all configuration settings |
2Staging & Committing
git status | Show working tree status |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git add -p | Interactively stage hunks |
git commit -m "message" | Commit staged changes with message |
git commit --amend | Modify the last commit |
git reset HEAD <file> | Unstage a file |
git checkout -- <file> | Discard changes in working directory |
3Branching & Merging
git branch | List all local branches |
git branch <name> | Create a new branch |
git checkout <branch> | Switch to a branch |
git checkout -b <branch> | Create and switch to new branch |
git switch <branch> | Switch to a branch (modern) |
git switch -c <branch> | Create and switch to new branch (modern) |
git merge <branch> | Merge branch into current branch |
git branch -d <branch> | Delete a merged branch |
git branch -D <branch> | Force delete a branch |
git branch -m <old> <new> | Rename a branch |
4Remote Operations
git remote -v | List remote repositories |
git remote add origin <url> | Add a remote repository |
git fetch | Download objects from remote |
git pull | Fetch and merge from remote |
git pull --rebase | Fetch and rebase onto remote |
git push | Push commits to remote |
git push -u origin <branch> | Push and set upstream tracking |
git push --force-with-lease | Force push safely (checks remote) |
5Inspection & Comparison
git log | Show commit history |
git log --oneline --graph | Compact log with branch graph |
git log -p <file> | Show changes over time for a file |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff <branch1>..<branch2> | Compare two branches |
git show <commit> | Show details of a commit |
git blame <file> | Show who changed each line |
6Stashing
git stash | Stash current changes |
git stash save "message" | Stash with a description |
git stash list | List all stashes |
git stash pop | Apply and remove latest stash |
git stash apply | Apply latest stash without removing |
git stash drop | Delete latest stash |
git stash clear | Delete all stashes |
7Undoing Changes
git revert <commit> | Create new commit that undoes a commit |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo last commit and discard changes |
git cherry-pick <commit> | Apply a specific commit to current branch |
git rebase <branch> | Reapply commits on top of another branch |
git rebase -i HEAD~3 | Interactive rebase for last 3 commits |