Basic Commands
Essential commands for everyday version control. Copy, run, and master your workflow with .git.
Setup & Config
Set your author identity and default preferences globally or per repository.
git config --global user.email "email@example.com"
git config --list # Verify settings
Initialize a new local repository in the current directory.
Working Directory
Show working tree status: modified, staged, and untracked files.
git status -s # Short format
Stage changes for the next commit.
git add . # Stage all changes
git add -p # Interactive staging
Remove files from the working directory and stage the deletion.
git rm --cached file.txt # Keep file, remove from tracking
Branching
List, create, or delete branches.
git branch -d feature-auth # Delete
git branch -v # List with commits
Switch branches or restore working tree files.
git checkout -b new-branch # Create & switch
git checkout -- file.txt # Discard changes
Integrate changes from one branch into the current branch.
git merge --no-ff feature-auth # Keep merge history
Committing
Record staged changes to the repository history.
git commit -a -m "Update docs" # Stage & commit tracked files
Display commit history with optional formatting.
git log --oneline --graph --all # Visual history
git log -p -2 # Show last 2 diffs
Remote & Sync
Manage remote repository connections.
git remote -v # List remotes
Upload local commits to a remote repository.
git push -u origin main # Set upstream tracking
Fetch and integrate changes from a remote repository.
git pull --rebase origin main # Linear history
Create a local copy of a remote repository.
git clone --depth 1 [url] # Shallow clone
Undo & Recovery
Undo changes by moving the HEAD pointer.
git reset --mixed HEAD~1 # Uncommit, unstage changes (default)
git reset --hard HEAD~1 # Discard everything permanently
Create a new commit that undoes a previous commit (safe for shared history).
Temporarily store modified and staged changes without committing.
git stash list
git stash pop # Reapply most recent