Loading...
Loading...
免费的在线 Git 命令参考——按分类浏览和复制常用 Git 命令。
Configure user name
git config --global user.name "Your Name"Configure user email
git config --global user.email "you@example.com"Check configuration
git config --listInitialize new repo
git initClone repository
git clone <url>Clone with branch
git clone -b <branch> <url>Check status
git statusStage all changes
git add .Stage specific file
git add <file>Commit changes
git commit -m "message"Commit with all staged
git commit -am "message"View commit log
git log --oneline --graphList branches
git branchCreate branch
git branch <name>Switch branch
git checkout <name>Create and switch
git checkout -b <name>Merge branch
git merge <branch>Delete branch
git branch -d <name>Add remote
git remote add origin <url>Push to remote
git push -u origin mainPull from remote
git pullFetch all remotes
git fetch --allUnstage file
git restore --staged <file>Discard changes
git restore <file>Amend last commit
git commit --amendReset to commit
git reset --hard <commit>Revert commit
git revert <commit>Show unstaged diff
git diffShow staged diff
git diff --stagedDiff two commits
git diff <commit1> <commit2>Stash changes
git stashList stashes
git stash listApply stash
git stash popCreate tag
git tag <name>Push tags
git push --tagsGit 是由 Linus Torvalds 于 2005 年为 Linux 内核开发而创建的分布式版本控制系统。与集中式系统(SVN、CVS)不同,每个 Git 克隆都是存储完整历史的整个仓库的完整备份。这使得 Git 极其可靠、大多数操作快速,并且既适合独立开发者,也适合大型分布式团队。
核心 Git 工作流围绕三个区域:工作目录(当前文件)、暂存区(索引——准备提交的更改)和仓库(包含完整历史的 .git 目录)。更改通过 git add 和 git commit 命令从工作目录流向暂存区再流向仓库。
分支是 Git 的杀手级功能。在 Git 中创建分支几乎不花成本——只是一个 41 字节的文件。这启用了轻量级功能分支开发模式,开发者可在隔离分支上工作并通过拉取请求合并回来。Git 的合并算法(三路合并与递归策略)能处理令旧系统困惑的复杂合并场景。
分布式工作流是 Git 的另一重大创新。开发者可以本地提交、创建分支和实验,而不影响他人。推送和拉取操作与远程仓库(GitHub、GitLab)同步。这启用了 GitFlow、GitHub Flow、主干开发和复刻拉取协作模型等工作流。
Git 的灵活性伴随着学习曲线。常用命令覆盖 90% 的日常需求:git clone、add、commit、push、pull、branch、checkout、merge、log、status 和 diff。更高级的功能如 rebase、bisect、stash、submodules 和 reflog 处理复杂场景。掌握这些基本和中级命令足以满足大多数开发工作。
Git 钩子是在 Git 工作流的关键节点自动运行的脚本——提交前(pre-commit)、提交后(post-commit)、推送前(pre-push)等。钩子可以通过运行 linter 和格式化工具来强制执行代码质量,防止提交密钥或大文件,或触发部署流水线。虽然钩子位于 .git/hooks 目录且不受版本控制,但团队可以通过 Husky 或 Lefthook 等工具从配置文件管理并共享钩子安装。
Git 是版本控制系统——跟踪更改的软件。GitHub 是托管 Git 仓库的平台,增加了协作功能:拉取请求、问题跟踪、代码审查、CI/CD 和项目管理。可以在没有 GitHub 的情况下使用 Git,但 GitHub 在 Git 之上增加了价值。
当两个分支对同一文件的同一行进行不同修改时,就会发生合并冲突。Git 无法自动决定保留哪个版本。解决方法是编辑冲突文件(标记为 <<<<<<<、=======、>>>>>>>),选择正确版本,删除标记,暂存文件,完成合并。
两者都用于集成更改。Merge 创建合并提交,保留并行开发的完整历史——适合公共/共享分支。Rebase 通过在最新基础上重新应用提交来重写历史——创建线性历史。共享分支使用 merge(不重写历史),推送前清理本地提交使用 rebase。
Git 钩子是在工作流关键节点自动运行的脚本——pre-commit、pre-push、post-commit 等。它们可以在提交前运行 linter 和格式化工具,防止提交密钥或大文件,或触发部署。钩子位于 .git/hooks(不受版本控制),但 Husky 或 Lefthook 等工具帮助团队通过配置文件共享和管理钩子。
如果提交仅在本地(未推送),使用 git reset --soft HEAD~1 撤销提交但保持更改暂存,或 git reset --hard HEAD~1 丢弃所有内容。如果已推送,使用 git revert <提交哈希> 创建一个反转更改的新提交——切勿强制推送以撤销共享历史。