Setup & Configuration
-
Initialize a new Git repository:
git init -
Set your global Git username:
git config --global user.name "Your Name" -
Set your global Git email:
git config --global user.email "you@example.com"
Staging & Committing
-
Stage a specific file:
git add <file> -
Stage all changes:
git add . -
Commit staged changes with a message:
git commit -m "message" -
Stage and commit tracked files in one step:
git commit -am "message"
Branching
-
List all branches:
git branch -
Create a new branch:
git branch <branch-name> -
Switch to a different branch:
git checkout <branch-name> -
Create and switch to a new branch:
git checkout -b <branch-name>
Merging & Rebasing
-
Merge another branch into the current branch:
git merge <branch-name> -
Reapply commits from your branch onto another:
git rebase <branch-name> -
Pull changes from remote with rebase:
git pull --rebase
Viewing History
-
View full commit history:
git log -
View commit history in a compact form:
git log --oneline -
Show current staging status:
git status -
Show changes in working directory vs last commit:
git diff
Remote Operations
-
Clone a remote repository:
git clone <repo-url> -
Show configured remotes:
git remote -v -
Push local commits to remote:
git push -
Fetch updates from remote without merging:
git fetch -
Fetch and merge remote changes:
git pull
Undoing Changes
-
Unstage a file:
git reset <file> -
Discard local changes to a file:
git checkout -- <file> -
Create a new commit that undoes a previous one:
git revert <commit-hash> -
Reset to a specific commit and erase all changes:
git reset --hard <commit-hash>
Clean Up
-
Temporarily save uncommitted changes:
git stash -
Reapply the last stashed changes:
git stash pop