提交、分支、合并、变基、远程等基本 Git 命令。
git config --global user.name "Name"设置与配置Set global author name
例如 git config --global user.name "Jane Doe"
git config --global user.email "email"设置与配置Set global author email
例如 git config --global user.email "[email protected]"
git config --list设置与配置List all configuration settings
git config --global core.editor "code"设置与配置Set VS Code as default editor
git config --global alias.<alias> <cmd>设置与配置Create a command alias
例如 git config --global alias.st status
git init设置与配置Initialize a new local repository
git init <dir>设置与配置Initialize a repo in a specific directory
例如 git init my-project
git clone <url>设置与配置Clone a remote repository
例如 git clone https://github.com/user/repo.git
git clone <url> <dir>设置与配置Clone into a specific folder
例如 git clone https://github.com/user/repo.git my-dir
git status暂存与基础Show working tree status
git status -s暂存与基础Short/compact status output
git add <file>暂存与基础Stage a specific file
例如 git add README.md
git add .暂存与基础Stage all changes in current directory
git add -p暂存与基础Interactively stage hunks of changes
git commit -m "msg"暂存与基础Commit staged changes with a message
例如 git commit -m "feat: add login page"
git commit -am "msg"暂存与基础Stage tracked files and commit in one step
git commit --amend破坏性暂存与基础Modify the most recent commit (message or content)
git diff暂存与基础Show unstaged changes
git diff --staged暂存与基础Show staged changes (vs last commit)
git diff <branch1>..<branch2>暂存与基础Compare two branches
例如 git diff main..feature
git rm <file>暂存与基础Remove a file from working tree and index
例如 git rm old-file.txt
git rm --cached <file>暂存与基础Untrack a file without deleting it
例如 git rm --cached secret.txt
git mv <old> <new>暂存与基础Move or rename a tracked file
例如 git mv old.js new.js
git branch分支与合并List local branches
git branch -a分支与合并List all branches (local + remote)
git branch <name>分支与合并Create a new branch
例如 git branch feature/auth
git checkout <branch>分支与合并Switch to an existing branch
例如 git checkout main
git checkout -b <branch>分支与合并Create and switch to a new branch
例如 git checkout -b feature/auth
git switch <branch>分支与合并Switch branches (modern syntax)
例如 git switch main
git switch -c <branch>分支与合并Create and switch (modern syntax)
例如 git switch -c feature/auth
git branch -d <branch>分支与合并Delete a merged branch
例如 git branch -d feature/auth
git branch -D <branch>破坏性分支与合并Force delete a branch (unmerged too)
例如 git branch -D feature/old
git branch -m <old> <new>分支与合并Rename a branch
例如 git branch -m old-name new-name
git merge <branch>分支与合并Merge a branch into current branch
例如 git merge feature/auth
git merge --no-ff <branch>分支与合并Merge with a merge commit (no fast-forward)
git merge --squash <branch>分支与合并Squash all commits from branch into one staged change
git merge --abort分支与合并Abort an in-progress merge
git remote -v远程操作List remote connections with URLs
git remote add <name> <url>远程操作Add a new remote
例如 git remote add origin https://github.com/user/repo.git
git remote remove <name>远程操作Remove a remote connection
例如 git remote remove origin
git remote rename <old> <new>远程操作Rename a remote
例如 git remote rename origin upstream
git fetch远程操作Download all changes from remote (no merge)
git fetch <remote>远程操作Fetch from a specific remote
例如 git fetch origin
git pull远程操作Fetch and merge changes from remote
git pull --rebase远程操作Fetch and rebase onto remote branch
git push <remote> <branch>远程操作Push branch to remote
例如 git push origin main
git push -u origin <branch>远程操作Push and set upstream tracking
例如 git push -u origin feature/auth
git push --force-with-lease破坏性远程操作Safe force push (fails if others pushed)
git push --tags远程操作Push all tags to remote
git push origin --delete <branch>破坏性远程操作Delete a remote branch
例如 git push origin --delete feature/old
git restore <file>破坏性撤销更改Discard changes in working directory
例如 git restore index.html
git restore --staged <file>撤销更改Unstage a file (keep changes)
例如 git restore --staged index.html
git reset HEAD <file>撤销更改Unstage a file (older syntax)
例如 git reset HEAD index.html
git reset --soft HEAD~1撤销更改Undo last commit, keep changes staged
git reset --mixed HEAD~1撤销更改Undo last commit, unstage changes (default)
git reset --hard HEAD~1破坏性撤销更改Undo last commit, discard all changes
git reset --hard <commit>破坏性撤销更改Reset to a specific commit, discard everything after
git revert <commit>撤销更改Create a new commit that undoes a specific commit
例如 git revert a1b2c3d
git revert HEAD撤销更改Revert the most recent commit
git clean -fd破坏性撤销更改Remove untracked files and directories
git clean -n撤销更改Dry run: show what would be cleaned
git stash存储Stash current working directory changes
git stash push -m "msg"存储Stash with a descriptive message
例如 git stash push -m "WIP: auth feature"
git stash list存储List all stashes
git stash pop存储Apply most recent stash and remove it
git stash apply stash@{n}存储Apply a specific stash without removing
例如 git stash apply stash@{0}
git stash drop stash@{n}存储Delete a specific stash
例如 git stash drop stash@{0}
git stash clear破坏性存储Remove all stashes
git stash branch <branch>存储Create a branch from a stash
例如 git stash branch feature/from-stash
git rebase <branch>变基与高级Rebase current branch onto another
例如 git rebase main
git rebase -i HEAD~n变基与高级Interactive rebase for last n commits
例如 git rebase -i HEAD~3
git rebase --continue变基与高级Continue rebase after resolving conflicts
git rebase --abort变基与高级Abort an in-progress rebase
git cherry-pick <commit>变基与高级Apply a specific commit to current branch
例如 git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>变基与高级Apply a range of commits
例如 git cherry-pick a1b2c3d..f6e5d4c
git bisect start变基与高级Start binary search for a bug
git bisect good <commit>变基与高级Mark a commit as good in bisect
git bisect bad <commit>变基与高级Mark a commit as bad in bisect
git bisect reset变基与高级End bisect session
git worktree add <path> <branch>变基与高级Check out a branch into a separate directory
git log检查与日志Show commit history
git log --oneline检查与日志Compact one-line log
git log --oneline --graph --all检查与日志Visual branch graph in terminal
git log -n <count>检查与日志Show last N commits
例如 git log -n 5
git log --author="name"检查与日志Filter commits by author
例如 git log --author="Jane Doe"
git log --since="2 weeks ago"检查与日志Show commits since a date
git log -p <file>检查与日志Show changes to a specific file over time
例如 git log -p README.md
git log --follow <file>检查与日志Follow renames of a file in history
git show <commit>检查与日志Show details of a specific commit
例如 git show a1b2c3d
git blame <file>检查与日志Show who last modified each line
例如 git blame src/app.tsx
git shortlog -sn检查与日志Summarize commits by author
git reflog检查与日志Show history of HEAD movements (recovery tool)
git grep "pattern"检查与日志Search working directory for a pattern
例如 git grep "TODO"
git tag标签List all tags
git tag <name>标签Create a lightweight tag
例如 git tag v1.0.0
git tag -a <name> -m "msg"标签Create an annotated tag
例如 git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>标签Tag a past commit
例如 git tag -a v0.9.0 a1b2c3d
git show <tag>标签Show tag metadata and commit
例如 git show v1.0.0
git push origin <tag>标签Push a specific tag to remote
例如 git push origin v1.0.0
git push origin --tags标签Push all tags to remote
git tag -d <tag>标签Delete a local tag
例如 git tag -d v1.0.0
git push origin --delete <tag>标签Delete a remote tag
例如 git push origin --delete v1.0.0
git merge通过创建合并提交来合并两个分支,保留两个分支的完整历史。git rebase将提交从一个分支移动到另一个分支,重写历史以创建线性、更清晰的提交历史。对公共分支和共享工作使用merge;对合并前清理本地功能分支使用rebase。
使用git reset --soft HEAD~1可以撤销最后一次提交,同时保持更改处于暂存状态。使用git reset HEAD~1(或--mixed)取消暂存更改但将其保留在工作目录中。使用git reset --hard HEAD~1永久丢弃提交和所有更改(破坏性操作)。
git fetch从远程下载更改,但不会将它们合并到当前分支。它会更新远程跟踪分支(origin/main)。git pull是git fetch + git merge(或带--rebase标志的git rebase)。当您想在集成之前检查更改时,使用fetch。
在冲突的合并/变基之后,Git用'<<<<<<'、=======、>>>>>>标记来标记冲突文件。打开每个冲突文件,手动选择保留哪些更改,删除标记,然后用git add <文件>暂存已解决的文件,并用git merge --continue或git commit完成。