Initialize a repository Getting started
git initCreate a new Git repository in the current directory.
Clone a repository Getting started
git clone <url>Copy a remote repository and check out its default branch locally.
Set the author name Getting started
git config --global user.name "Your Name"Set the global commit author name for the current user.
Set the author email Getting started
git config --global user.email "you@example.com"Set the global commit author email for the current user.
Show status Everyday work
git statusInspect the working tree, staging area, and current branch.
Stage a file Everyday work
git add <file>Add the current changes in the specified file to the staging area.
Stage the current directory Everyday work
git add .Stage changes under the current directory; check the scope with status first.
Create a commit Everyday work
git commit -m "message"Record the staged changes as a new commit.
Amend the latest commit Everyday work
git commit --amendUpdate the latest commit to add files or edit its message.
Show unstaged changes Everyday work
git diffCompare the working tree with the staging area.
Show staged changes Everyday work
git diff --stagedInspect the changes that the next commit will include.
List local branches Branches
git branchList local branches; an asterisk marks the current branch.
Create a branch Branches
git branch <name>Create a new local branch from the current commit.
Switch branches Branches
git switch <name>Switch to an existing local branch.
Create and switch branches Branches
git switch -c <name>Create a new branch and switch to it immediately.
Merge a branch Branches
git merge <branch>Merge the specified branch into the current branch.
Rebase a branch Branches
git rebase <base>Replay the current branch commits on the specified base; check team conventions before rewriting shared history.
Delete a merged branch Branches
git branch -d <name>Delete a local branch that has already been merged.
Show remote URLs Remotes
git remote -vList fetch and push URLs for remote repositories.
Add a remote repository Remotes
git remote add origin <url>Register the remote URL as origin for the current repository.
Fetch remote updates Remotes
git fetch originDownload remote objects and references without modifying the current working branch.
Pull and rebase Remotes
git pull --rebaseFetch upstream updates and rebase local commits on top of them.
Push and set upstream Remotes
git push -u origin <branch>Push the specified branch and set its upstream tracking branch.
Push the current branch Remotes
git pushPush new commits on the current branch to its configured upstream.
Show a history graph History
git log --oneline --decorate --graphView a compact graph of commit history, branches, and tags.
Show file history patches History
git log -p -- <file>Inspect the patch for the specified file in each commit.
Show commit details History
git show <commit>Display a commit’s metadata and changes.
Trace file lines History
git blame <file>Show the commit and author that last changed each line.
Show the reference log History
git reflogInspect local reference movements to find commits from before a mistake.
Discard working-tree changes Undo
git restore <file>Overwrite the specified file with its staged version; confirm its unstaged changes can be discarded first.
Unstage a file Undo
git restore --staged <file>Remove changes from the staging area while keeping them in the working tree.
Revert a commit Undo
git revert <commit>Create a new commit that reverses the specified commit; suitable for shared history.
Undo the latest commit Undo
git reset --soft HEAD~1Move HEAD back one commit while keeping changes staged.
Stash uncommitted changes Undo
git stash push -m "message"Save current uncommitted changes in a stash to switch tasks temporarily.
Restore the latest stash Undo
git stash popApply the latest stash and remove it from the list if it applies successfully.
Create an annotated tag History
git tag -a v1.0.0 -m "release"Create an annotated version tag for the current commit.
This is a local command reference; it does not execute commands or connect to remote repositories. Confirm the target before using commands that overwrite, delete, or rewrite history.