Set the global commit user name
git config --global user.name "Zhang San"Frequently used commands for config, commits, branches, remotes, undo and history forensics, with warnings on rewriting shared history.
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 mainDefine an lg alias for a frequently used log command
git config --global alias.lg "log --oneline --graph --decorate"Show the current branch and changes in short format
git status -sbInteractively stage selected hunks
git add -pCommit 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-editAmending pushed commits requires a force push; avoid on shared branches
Create and switch to a new branch
git switch -c feat/loginSwitch to an existing branch
git switch mainList local branches with their upstream tracking
git branch -vvDelete a merged local branch; -D forces deletion
git branch -d feat/loginShallow clone, fetching only the latest commit
git clone --depth 1 https://github.com/user/repo.gitFetch all remotes and prune deleted branches
git fetch --all --prunePull with rebase to keep a linear history
git pull --rebase origin mainPush a new branch and set its upstream
git push -u origin <BRANCH>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
Discard uncommitted changes in a file; cannot be undone
git restore src/App.vueUnstage a file while keeping working tree changes
git restore --staged src/App.vueUndo the last commit but keep its changes staged
git reset --soft HEAD~1Reset to the previous commit, discarding staged and working changes
git reset --hard HEAD~1Use 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
View commit history across all branches as a graph
git log --oneline --graph --all --decorateShow the last author and commit for a line range
git blame -L 20,40 src/App.vueFind commits that added or removed a string
git log -S "TODO" --onelineBinary search for the commit that introduced a bug
git bisect start
git bisect bad
git bisect good v1.0.0Run git bisect reset after locating the culprit
Show recent HEAD moves to recover after a bad reset
git reflog -n 20List 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.0Delete a remote tag; pipelines depending on it will fail
git push origin --delete v1.2.0Delete the local tag with git tag -d v1.2.0
This website uses cookies to ensure you get the best experience on our website. Privacy Policy.