Console9

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

CommandDescriptionExample
git initInitialize a new Git repository in the current directory.git init
git clone URLClone a remote repository to the local machine.git clone git@github.com:user/repo.git
git remote add NAME URLAdd a remote repository reference.git remote add origin git@github.com:user/repo.git

Git Staging and Committing

CommandDescriptionExample
git add FILEStage a file for the next commit.git add nginx.conf
git add -AStage all changes (new, modified, deleted).git add -A
git commit -m MSGCommit staged changes with a message.git commit -m "Fix typo in config"
git commit --amendModify the last commit (message or files).git commit --amend -m "New message"

Git Branching and Merging

CommandDescriptionExample
git branchList local branches.git branch
git branch NAMECreate a new branch.git branch feature/ssl
git checkout BRANCHSwitch to a branch.git checkout main
git checkout -b NAMECreate and switch to a new branch.git checkout -b feature/ssl
git switch -c NAMECreate and switch (newer syntax).git switch -c feature/ssl
git merge BRANCHMerge a branch into the current branch.git merge feature/ssl
git rebase BRANCHRebase the current branch onto another branch.git rebase main

Git History and Inspection

CommandDescriptionExample
git logShow commit history.git log --oneline --graph --all
git diffShow unstaged changes.git diff
git diff --stagedShow staged changes.git diff --staged
git blame FILEShow who last modified each line.git blame nginx.conf
git show COMMITShow details of a specific commit.git show abc1234

Git Undo and Recovery

CommandDescriptionExample
git revert COMMITCreate a new commit undoing the specified commit (safe).git revert abc1234
git reset --soft HEAD~1Undo last commit, keep changes staged.git reset --soft HEAD~1
git reset --hard HEAD~1Undo last commit, discard all changes.git reset --hard HEAD~1
git stashSave uncommitted changes and clean the working tree.git stash
git stash popRestore stashed changes.git stash pop
git reflogShow history of HEAD movements (recover lost commits).git reflog