1.基础操作
- - git --version ## 显示 Git 版本。
- - git init ## 初始化一个新的 Git 仓库。
- - git init --bare ## 初始化一个裸仓库(适用于服务器端)。
- - git clone [url] ## 克隆远程仓库到本地。
- - git help <command> ## 显示指定命令的帮助信息。
- - git clone --mirror [url] ## 克隆仓库时包括所有引用。
- - git clone --bare [url] ## 克隆仓库时创建一个裸仓库。
- - git clone --recursive [url] ## 克隆仓库时同时拉取子模块。
2.配置
- - git config --global user.name "<name>" ## 设置全局用户名。
- - git config --global user.email "<email>" ## 设置全局用户邮箱。
- - git config --replace-all <key> <value> ## 替换所有匹配的配置项。
- - git config --global core.editor "<editor>" ## 设置默认的文本编辑器。
- - git config --global credential.helper store ## 配置 Git 使用凭据存储,避免重复输入用户名和密码。
- - git config --list ## 列出所有配置项。
- - git config <key> ## 显示指定配置项的值。
- - git config --list --show-origin ## 显示所有配置及其来源文件。
3.工作区与暂存区
- - git add [file] ## 添加单个文件到暂存区。
- - git add . ## 添加当前目录下所有更改(包括新文件和修改的文件)。
- - git add -A ## 添加所有更改(包括新文件、修改的文件和删除的文件)。
- - git add -u ## 仅添加已跟踪文件的更改(不包括新文件)。
- - git add -p ## 交互式添加部分更改。
- - git status ## 显示工作目录和暂存区的状态。
- - git diff ## 显示工作区和暂存区之间的差异。
- - git diff --cached ## 显示暂存区和上一次提交之间的差异。
- - git diff HEAD ## 显示工作区和上一次提交之间的所有差异。
- - git diff --stat ## 显示差异的摘要信息。
- - git diff [file] ## 显示指定文件在工作区和暂存区之间的差异。
- - git restore [file] ## 恢复工作区中指定文件的内容(类似 git checkout -- [file])。
- - git restore --staged [file] ## 取消暂存区中指定文件的更改。
4.提交
- - git commit -m "Commit message" ## 将暂存区的内容提交到本地仓库。
- - git commit -a -m "Commit message" ## 自动将所有已修改的文件添加到暂存区并提交。
- - git commit --amend -m "Commit message" ## 修改上一次提交。
- - git commit --allow-empty ## 允许提交一个空的更改。
- - git commit --no-edit ## 提交时跳过编辑提交信息的步骤。
5.分支操作
- - git branch ## 列出所有本地分支。
- - git branch -r ## 列出所有远程分支。
- - git branch -a ## 列出所有本地和远程分支。
- - git branch [branch-name] ## 创建一个新的分支。
- - git checkout [branch-name] ## 切换到指定分支。
- - git checkout -b [branch-name] ## 创建并切换到一个新的分支。
- - git merge [branch-name] ## 将指定分支合并到当前分支。
- - git rebase [branch-name] ## 将当前分支的更改重新应用到指定分支的顶部。
- - git branch -d [branch-name] ## 删除指定分支。
- - git branch -D [branch-name] ## 强制删除指定分支。
- - git branch --unset-upstream [branch-name] ## 取消本地分支的远程跟踪设置。
- - git branch --set-upstream-to=[remote-name]/[branch-name] [local-branch-name] ## 设置本地分支跟踪远程分支。
6.标签操作
- - git tag ## 列出所有标签。
- - git tag [tag-name] ## 创建一个新的标签。
- - git tag -a [tag-name] -m "Tag message" ## 创建一个新的标签,并添加标签信息。
- - git tag -d [tag-name] ## 删除本地标签。
- - git tag -l "pattern" ## 列出匹配特定模式的标签。
- - git tag -f [tag-name] ## 强制覆盖已存在的标签。
- - git push --tags ## 将所有标签推送到远程仓库。
- - git push [remote-name] [tag-name] ## 将标签推送到远程仓库。
- - git push [remote-name] :[tag-name] ## 删除远程标签。
7.远程仓库操作
- - git remote ## 显示所有远程仓库的列表。
- - git remote -v ## 显示所有远程仓库的详细信息。
- - git remote add [remote-name] [url] ## 添加一个新的远程仓库。
- - git remote remove [remote-name] ## 删除一个远程仓库。
- - git remote rename [old-name] [new-name] ## 重命名一个远程仓库。
- - git fetch [remote-name] ## 从远程仓库获取最新的分支和标签,但不自动合并。
- - git pull [remote-name] [branch-name] ## 从远程仓库拉取最新的分支并自动合并。
- - git push [remote-name] [branch-name] ## 将本地分支推送到远程仓库。
- - git push --set-upstream [remote-name] [branch-name] ## 将本地分支推送到远程仓库,并设置上游分支。
- - git push origin :[branch-name] ## 删除远程分支。
- - git remote prune [remote-name] ## 清理远程仓库中已不存在的分支。
- - git remote show [remote-name] ## 显示远程仓库的详细信息,包括跟踪的分支。
8.撤销与回滚
- - git reset ## 重置暂存区,但不改变工作区。
- - git reset --hard ## 重置暂存区和工作区,丢弃所有未提交的更改。
- - git reset --soft ## 重置暂存区,但不改变工作区,保留暂存区的更改。
- - git reset [commit] ## 重置暂存区到指定提交,但不改变工作区。
- - git reset --hard [commit] ## 重置暂存区和工作区到指定提交。
- - git revert [commit] ## 创建一个新的提交,撤销指定提交的更改。
- - git checkout -- [file] ## 撤销工作区中指定文件的更改。
- - git reset HEAD [file] ## 撤销暂存区中指定文件的更改,但不改变工作目录。
- - git clean -fd ## 删除未跟踪的文件和目录。
- - git clean -xdf ## 强制删除未跟踪的文件和目录,包括忽略的文件。
- - git reset --merge ## 在合并冲突时,撤销合并操作。
- - git revert --no-commit [commit] ## 撤销指定提交的更改,但不自动提交。
9.日志与差异
- - git log ## 显示提交日志。
- - git log -p ## 显示每次提交的详细差异。
- - git log --stat ## 显示每次提交的统计信息。
- - git log --oneline ## 以一行显示每次提交。
- - git log --graph ## 以图形方式显示提交历史。
- - git log --since="2 weeks ago" ## 显示最近两周的提交日志。
- - git log --author="Your Name" ## 显示指定作者的提交日志。
- - git log --all -S"search_string" ## 在所有分支的提交历史中搜索包含指定字符串的内容。
- - git log --graph --oneline --all ## 以图形方式显示所有分支的提交历史。
- - git log --author-date-order ## 按作者提交时间排序显示提交日志。
- - git log --grep="Commit message" ## 显示包含指定消息的提交日志。
- - git log --patch ## 显示每次提交的完整差异。
- - git log --follow [file] ## 显示指定文件的历史,即使文件被重命名。
- - git log --stop-before=[commit] ## 显示指定提交之前的日志。
10.搜索与查找
- - git grep "Search term" ## 在当前仓库中搜索指定的字符串。
- - git grep -i "Search term" ## 不区分大小写地搜索指定的字符串。
- - git show [commit] ## 显示指定提交的详细信息。
- - git blame [file] ## 显示指定文件的每一行的提交信息。
- - git difftool ## 使用默认的差异工具查看工作区和最近提交之间的差异。
- - git difftool --staged ## 使用默认的差异工具查看暂存区和最近提交之间的差异。
- - git blame --show-email [file] ## 显示指定文件的每一行的提交信息,并显示作者邮箱。
11.其他
- - git stash ## 保存当前工作目录中未提交的修改,以便切换分支或执行其他操作。
- - git stash pop ## 恢复保存的修改。
- - git reflog ## 查看版本控制系统的操作日志,可用于恢复意外删除的分支或回退到正确的提交。
- - git bisect ## 用于二分查找问题提交,帮助快速定位引入问题的提交。
- - git rm [file] ## 删除文件。
- - git mv [file] [new-file] ## 重命名文件。
- - git cherry-pick [commit] ## 将指定提交应用到当前分支。
- - git submodule add [url] [path] ## 添加子模块。
- - git submodule update --init --recursive ## 初始化并更新子模块。
- - git worktree add [path] [branch-name] ## 创建一个新的工作树(工作目录)。
- - git worktree prune ## 清理无效的工作树。