Git Common Commands - CodeSwordsman

Frequently used commands for config, commits, branches, remotes, undo and history forensics, with warnings on rewriting shared history.

Git 2.28+Updated 2026-10-2933 commands

Setup & Init

Set the global commit user name

git config --global user.name "Zhang San"

Set the global commit email used by hosting platforms

git config --global user.email "dev@example.com"

Initialize a repository with main as the default branch

git init -b main

Define an lg alias for a frequently used log command

git config --global alias.lg "log --oneline --graph --decorate"

Everyday Commits

Show the current branch and changes in short format

git status -sb

Interactively stage selected hunks

git add -p

Commit staged changes with a conventional message

git commit -m "feat: add login page"

Fold staged changes into the last commit without editing its message

git commit --amend --no-edit

Amending pushed commits requires a force push; avoid on shared branches

Branches

Create and switch to a new branch

git switch -c feat/login

Switch to an existing branch

git switch main

List local branches with their upstream tracking

git branch -vv

Delete a merged local branch; -D forces deletion

git branch -d feat/login

Remote Collaboration

Shallow clone, fetching only the latest commit

git clone --depth 1 https://github.com/user/repo.git

Fetch all remotes and prune deleted branches

git fetch --all --prune

Pull with rebase to keep a linear history

git pull --rebase origin main

Push a new branch and set its upstream

git push -u origin <BRANCH>
Risky

Force-push and overwrite the remote branch, possibly losing others work

git push --force-with-lease origin <BRANCH>

--force-with-lease aborts when the remote has new commits; safer than --force

Undo & Rollback

Risky

Discard uncommitted changes in a file; cannot be undone

git restore src/App.vue

Unstage a file while keeping working tree changes

git restore --staged src/App.vue

Undo the last commit but keep its changes staged

git reset --soft HEAD~1
Risky

Reset to the previous commit, discarding staged and working changes

git reset --hard HEAD~1

Use git revert for pushed commits to avoid rewriting shared history

Create an inverse commit to undo pushed changes safely

git revert <COMMIT>

Apply a specific commit onto the current branch

git cherry-pick <COMMIT>

Stash current changes with a message before switching branches

git stash push -m "wip: login"

Restore with git stash pop; list entries with git stash list

History & Forensics

View commit history across all branches as a graph

git log --oneline --graph --all --decorate

Show the last author and commit for a line range

git blame -L 20,40 src/App.vue

Find commits that added or removed a string

git log -S "TODO" --oneline

Binary search for the commit that introduced a bug

git bisect start
git bisect bad
git bisect good v1.0.0

Run git bisect reset after locating the culprit

Show recent HEAD moves to recover after a bad reset

git reflog -n 20

Tags & Releases

List tags matching a pattern

git tag -l "v1.*"

Create an annotated tag with a message

git tag -a v1.2.0 -m "release 1.2.0"

Push one tag; a plain push does not include tags

git push origin v1.2.0
Risky

Delete a remote tag; pipelines depending on it will fail

git push origin --delete v1.2.0

Delete the local tag with git tag -d v1.2.0

Related tools

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.