Git 命令
一份实用的 Git 日常命令参考:创建分支、撤销更改、合并与变基、与远程仓库同步。每条命令都有语法、关键选项,以及可以在终端在线编辑器中直接运行的示例。
设置与配置
创建仓库并配置 Git:init、config、remotes。
git initCreate a new Git repository in the current folder.git remoteList, add, rename, and change the remote repositories your repo points at.git submoduleEmbed one repository inside another and keep it pinned to a specific commit.git cloneCopy a remote repository, with all its history, to your machine.git clone branchClone only a specific branch of a repository, or a single branch only.git configSet your name, email, editor, and other Git settings - globally or per repo.
暂存与提交
把更改记录到历史中:add、commit 和 amend。
分支
创建、切换、重命名和删除分支,在隔离环境中修改代码。
git delete local branchDelete a local branch once its work is merged - and force-delete when it is not.git rename branchRename the branch you are on, or any other branch, locally and on the remote.git create branchCreate a new branch and switch to it, from the current commit or any starting point.git checkoutSwitch branches, restore files, or check out a specific commit.git checkout remote branchFetch and check out a branch that exists on the remote but not yet locally.git branchList, create, rename, and delete branches - the core branch command.git worktreeCheck out multiple branches at once in separate working directories.
撤销更改
从错误中恢复:安全地 reset、revert 和撤销提交。
git undo commitUndo any commit - an older one, several at once, or one that is already pushed.git resetMove HEAD and choose what happens to the index and working tree: --soft, --mixed, --hard.git revertUndo a commit safely by adding a new commit that reverses it - the right choice for pushed history.git undo last commitUndo just the most recent commit - keep the work, discard it, or reverse it if it was pushed.git reset --hardForce your branch and working tree back to a commit, discarding all changes since it.git stashShelve your uncommitted changes so you can switch context, then reapply them later.git reflogSee everywhere HEAD has been - and recover commits you thought were lost.
合并与变基
用 merge、rebase、squash 和 cherry-pick 整合两个分支的工作。
git rebase vs mergeTwo ways to combine branches: a merge commit that preserves history, or a linear replay.git squash commitsCombine several commits into one with interactive rebase for a clean history.git abort mergeCancel a merge in progress and return the branch to the state before you started.git cherry-pickApply a specific commit from one branch onto another, without merging the whole branch.git mergeCombine another branch into your current one, with or without a merge commit.git rebaseReplay your commits on top of another branch for a clean, linear history.
远程仓库
将本地仓库与 GitHub 等远程仓库同步:push、pull、fetch。
git fetch vs pullThe difference between downloading remote changes and downloading-and-merging them.git force pushOverwrite the remote branch with your local history - and do it safely with --force-with-lease.git pullDownload remote changes and merge them into your current branch in one step.git pushUpload your commits to a remote and set the upstream branch.git delete remote branchDelete a branch on the remote (GitHub) once it is merged - and locally too.git set upstreamLink a local branch to a remote branch so push and pull need no arguments.git pull remote branchPull changes from a specific remote branch into your current branch.
查看与清理
查看改动并清理工作区:status、log、diff、clean。
git remove untracked filesClean untracked files and directories out of the working tree with git clean.git tagMark a commit with a version tag, and push tags to the remote.git statusSee which files are staged, modified, or untracked in your working tree.git diffSee exactly what changed - unstaged, staged, between commits or branches.git bisectBinary-search your history to find the commit that introduced a bug.git logBrowse commit history, with a compact one-line and graph view.
撤销更改:该用哪个命令?
四个命令以不同方式撤销工作。根据你想保留什么,以及提交是否已经推送来选择:
- git reset - 将分支移回之前的位置并改写历史。最适合尚未推送的本地提交。
- git revert - 新增一个提交来撤销之前的提交。对于别人已经拉取的提交,这是安全的选择。
- git checkout - 切换分支,或将文件恢复到之前的版本。
- git stash - 不提交就把更改暂存起来,之后再重新应用。