Git
Git Commands
Essential git commands organized by category. From basics to advanced workflows.
Basics
| Command / Property | Description |
|---|---|
| git init | Initialize a new git repository |
| git clone <url> | Clone a remote repository |
| git status | Show working tree status |
| git add <file> | Stage file for commit |
| git add . | Stage all changes |
| git commit -m "msg" | Commit staged changes with message |
| git log --oneline | Show compact commit history |
| git diff | Show unstaged changes |
| git diff --staged | Show staged changes |
Branching
| Command / Property | Description |
|---|---|
| git branch | List local branches |
| git branch <name> | Create a new branch |
| git checkout <branch> | Switch to a branch |
| git checkout -b <name> | Create and switch to a new branch |
| git merge <branch> | Merge branch into current branch |
| git branch -d <name> | Delete a merged branch |
| git branch -D <name> | Force delete a branch |
| git rebase <branch> | Rebase current branch onto another |
Remote
| Command / Property | Description |
|---|---|
| git remote -v | List remote repositories |
| git remote add origin <url> | Add a remote repository |
| git fetch | Download remote changes without merging |
| git pull | Fetch and merge remote changes |
| git push | Push local commits to remote |
| git push -u origin <branch> | Push and set upstream branch |
| git push --force-with-lease | Force push safely (checks remote) |
Undoing
| Command / Property | Description |
|---|---|
| git restore <file> | Discard working directory changes |
| git restore --staged <file> | Unstage a file |
| git reset HEAD~1 | Undo last commit, keep changes |
| git reset --hard HEAD~1 | Undo last commit, discard changes |
| git revert <commit> | Create a new commit that undoes a commit |
| git commit --amend | Modify the last commit |
Stash
| Command / Property | Description |
|---|---|
| git stash | Stash working directory changes |
| git stash pop | Apply and remove latest stash |
| git stash list | List all stashes |
| git stash apply stash@{n} | Apply a specific stash without removing |
| git stash drop stash@{n} | Delete a specific stash |
| git stash clear | Delete all stashes |