Git commands and flags reference
Complete reference for Git commands, flags, and configuration options for version control.
Git commands and flags reference
Complete reference for Git commands, flags, and configuration options for version control.
Git Repository Commands
| Command | Description | Example |
|---|---|---|
git init | Initialize a new Git repository in the current directory. | git init |
git clone URL | Clone a remote repository to the local machine. | git clone git@github.com:user/repo.git |
git remote add NAME URL | Add a remote repository reference. | git remote add origin git@github.com:user/repo.git |
Git Staging and Committing
| Command | Description | Example |
|---|---|---|
git add FILE | Stage a file for the next commit. | git add nginx.conf |
git add -A | Stage all changes (new, modified, deleted). | git add -A |
git commit -m MSG | Commit staged changes with a message. | git commit -m "Fix typo in config" |
git commit --amend | Modify the last commit (message or files). | git commit --amend -m "New message" |
Git Branching and Merging
| Command | Description | Example |
|---|---|---|
git branch | List local branches. | git branch |
git branch NAME | Create a new branch. | git branch feature/ssl |
git checkout BRANCH | Switch to a branch. | git checkout main |
git checkout -b NAME | Create and switch to a new branch. | git checkout -b feature/ssl |
git switch -c NAME | Create and switch (newer syntax). | git switch -c feature/ssl |
git merge BRANCH | Merge a branch into the current branch. | git merge feature/ssl |
git rebase BRANCH | Rebase the current branch onto another branch. | git rebase main |
Git History and Inspection
| Command | Description | Example |
|---|---|---|
git log | Show commit history. | git log --oneline --graph --all |
git diff | Show unstaged changes. | git diff |
git diff --staged | Show staged changes. | git diff --staged |
git blame FILE | Show who last modified each line. | git blame nginx.conf |
git show COMMIT | Show details of a specific commit. | git show abc1234 |
Git Undo and Recovery
| Command | Description | Example |
|---|---|---|
git revert COMMIT | Create a new commit undoing the specified commit (safe). | git revert abc1234 |
git reset --soft HEAD~1 | Undo last commit, keep changes staged. | git reset --soft HEAD~1 |
git reset --hard HEAD~1 | Undo last commit, discard all changes. | git reset --hard HEAD~1 |
git stash | Save uncommitted changes and clean the working tree. | git stash |
git stash pop | Restore stashed changes. | git stash pop |
git reflog | Show history of HEAD movements (recover lost commits). | git reflog |