git常用命令大全

2024-07-14 10:24:55 250
git常用命令大全,涵盖了从仓库操作、基本操作、分支操作、远程操作、标签操作到撤销操作的各个方面。

仓库配置

配置项命令说明
全局用户名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 --softHEAD~1撤销最后一次提交(保留更改)git reset --soft HEAD~1
git reset --hardHEAD~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时的主要操作和情景。通过掌握这些命令,可以更高效地进行版本控制和项目管理。