仓库配置
配置项 | 命令 | 说明 |
---|
全局用户名 | git config --global user.name "Your Name" | 设置全局 Git 用户名,这个用户名会在所有的仓库中使用 |
全局用户邮箱 | git config --global user.email "your_email@example.com" | 设置全局 Git 用户邮箱,这个邮箱会在所有的仓库中使用 |
仓库用户名 | git config user.name "Your Name" | 设置当前仓库的 Git 用户名,只在当前仓库中生效 |
仓库用户邮箱 | git config user.email "your_email@example.com" | 设置当前仓库的 Git 用户邮箱,只在当前仓库中生效 |
查看配置 | git config --list | 查看当前的 Git 配置信息,包括全局和仓库级别的配置 |
编辑全局配置 | git config --global --edit | 编辑全局配置文件 .gitconfig |
编辑仓库配置 | git config --edit | 编辑当前仓库的配置文件 .git/config |
设置默认编辑器 | git config --global core.editor "editor" | 设置全局默认的编辑器,例如 vim , nano 或其他编辑器 |
设置 diff 工具 | git config --global diff.tool "tool" | 设置全局默认的差异比较工具,例如 vimdiff , meld 等 |
设置 merge 工具 | git config --global merge.tool "tool" | 设置全局默认的合并工具,例如 vimdiff , meld 等 |
设置缓存凭证 | git config --global credential.helper cache | 设置全局凭证缓存,默认缓存 15 分钟 |
设置缓存凭证时间 | git config --global credential.helper "cache --timeout=3600" | 设置全局凭证缓存时间为 1 小时 |
设置代理 | git config --global http.proxy "proxy_url" | 设置全局 HTTP 代理,例如 http://proxyuser:proxypwd@proxy.server.com:port |
取消代理 | git config --global --unset http.proxy | 取消全局 HTTP 代理配置 |
设置忽略文件 | git config --global core.excludesfile ~/.gitignore_global | 设置全局忽略文件配置文件 |
设置推送默认行为 | git config --global push.default "matching" | 设置全局推送默认行为为匹配所有分支 |
设置拉取默认行为 | git config --global pull.rebase "true" | 设置全局拉取时使用 rebase 而不是 merge |
仓库基本操作
命令 | 参数 | 说明 | 示例 |
---|
git init | | 初始化一个新的Git仓库 | git init |
git clone | <repository_url> | 克隆远程仓库到本地 | git clone https://github.com/user/repo.git |
git status | | 查看当前仓库状态 | git status |
分支操作
命令 | 参数 | 说明 | 示例 |
---|
git branch | | 列出本地分支 | git branch |
git branch | <branch_name> | 创建新分支 | git branch feature-branch |
git checkout | <branch_name> | 切换到指定分支 | git checkout feature-branch |
git checkout -b | <branch_name> | 创建并切换到新分支 | git checkout -b feature-branch |
git merge | <branch_name> | 合并指定分支到当前分支 | git merge feature-branch |
git branch -d | <branch_name> | 删除本地分支 | git branch -d feature-branch |
远程仓库操作
命令 | 参数 | 说明 | 示例 |
---|
git remote -v | | 查看远程仓库 | git remote -v |
git remote add | <name> <url> | 添加远程仓库 | git remote add origin https://github.com/user/repo.git |
git remote remove | <name> | 移除远程仓库 | git remote remove origin |
git remote rename | <old_name> <new_name> | 重命名远程仓库 | git remote rename origin upstream |
同步操作
命令 | 参数 | 说明 | 示例 |
---|
git fetch | <remote> | 从指定远程仓库拉取更新 | git fetch origin |
git fetch --all | | 从所有配置的远程仓库拉取更新 | git fetch --all |
git pull | <remote> <branch> | 拉取并合并远程仓库的指定分支 | git pull origin main |
git pull --rebase | <remote> <branch> | 拉取代码并将本地提交变基到远程分支之上 | git pull --rebase origin main |
git push | <remote> <branch> | 推送本地分支到远程仓库 | git push origin main |
git push --force | <remote> <branch> | 强制推送 | git push origin main --force |
git push --all | <remote> | 推送所有分支到远程仓库 | git push --all origin |
暂存和提交
命令 | 参数 | 说明 | 示例 |
---|
git add | <file> | 添加文件到暂存区 | git add README.md |
git add . | | 添加所有更改的文件到暂存区 | git add . |
git commit | -m "<message>" | 提交暂存区的更改并附加提交信息 | git commit -m "Add new feature" |
git commit -a | -m "<message>" | 提交所有更改(包括未暂存的)并附加提交信息 | git commit -a -m "Update README" |
查看历史
命令 | 参数 | 说明 | 示例 |
---|
git log | | 查看提交历史 | git log |
git log --oneline | | 查看简洁的提交历史 | git log --oneline |
git log --graph | | 查看带图形的提交历史 | git log --graph |
git diff | <commit1> <commit2> | 比较两个提交之间的差异 | git diff HEAD~1 HEAD |
标签操作
命令 | 参数 | 说明 | 示例 |
---|
git tag | | 列出所有标签 | git tag |
git tag | <tagname> | 创建轻量标签 | git tag v1.0 |
git tag -a | <tagname> -m "<message>" | 创建带注释的标签 | git tag -a v1.0 -m "Release version 1.0" |
git push origin | <tagname> | 推送标签到远程仓库 | git push origin v1.0 |
git tag -d | <tagname> | 删除本地标签 | git tag -d v1.0 |
git push origin --delete | <tagname> | 删除远程标签 | git push origin --delete v1.0 |
撤销和重置
命令 | 参数 | 说明 | 示例 |
---|
git checkout -- | <file> | 撤销工作区的更改 | git checkout -- README.md |
git reset | <file> | 撤销暂存区的更改 | git reset README.md |
git reset --soft | HEAD~1 | 撤销最后一次提交(保留更改) | git reset --soft HEAD~1 |
git reset --hard | HEAD~1 | 撤销最后一次提交(删除更改) | git reset --hard HEAD~1 |
子模块操作
命令 | 参数 | 说明 | 示例 |
---|
git submodule add | <repository_url> <path> | 添加子模块 | git submodule add https://github.com/user/submodule.git submodule |
git submodule init | | 初始化子模块 | git submodule init |
git submodule update | | 更新子模块 | git submodule update |
git submodule update --remote | | 更新子模块到最新提交 | git submodule update --remote |
这些命令和参数涵盖了日常使用Git时的主要操作和情景。通过掌握这些命令,可以更高效地进行版本控制和项目管理。