网站首页 > 博客文章 正文
不管是前端还是后端,Git都是源代码版本控制的不二之选。对于开发者来说,掌握 Git 常用命令以及冲突的解决方案是至关重要的。本文将汇总 Git 常用命令,介绍一些值得收藏的核心用法,让你更加游刃有余地驾驭 Git。
设置
Git 的配置文件通常存储在用户目录下名为 .gitconfig 的文件中。例:C:\Users\Username\.gitconfig
在熟悉配置文件结构的情况下,可以直接打开文件进行编辑。
常用的设置命令如下:
# 设置全局的用户名和Email
git config --global user.name "[your-name]"
git config --global user.email "[your-email]"
# 在项目目录下执行,为当前项目设置特定的用户信息
git config user.name "[your-name]"
git config --list
# 查看所有配置
git config user.name
# 查看指定配置
注意:配置项较多时,在显示完一屏的时候,会出现“:”,此时按空格进入下一页,按 Q 键(Quit)退出。以下所有出现多页数据的时候,同理。
建议给经常使用的命令设置别名!以下几个配置可节约不少时间!
git config --global alias.co checkout
# git co 相当于 git checkout
git config --global alias.ci commit
# git ci 相当于 git commit
git config --global alias.st status
# git st 相当于 git status
git config --global alias.br branch
# git br 相当于 git branch
git config --global alias.m "checkout master"
# git m 相当于 git checkout master
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# git lg 这是美化后的git log,效果非常惊艳
初始化
git init
# 初始化一个新的Git仓库
git clone <repository>
# 克隆远程仓库到本地
添加和提交文件
git add <file>
# 将文件添加到暂存区,多个文件用空格隔开
git add .
# 将所有修改过的文件添加到暂存区
git add -u
# 将所有修改过的文件添加到暂存区,不包括新文件
git commit -m "<message>"
# 提交暂存区的文件,并附上提交信息
git commit -a
# 将所有修改过的文件添加到暂存区,并提交暂存区的文件
git commit --amend -m "<new commit message>"
# 修改最后一次提交的提交信息
git show <commit>
# 查看指定提交的详细内容
删除和重命名
git rm <file>
# 删除工作区文件,并将这次删除放入暂存区
git rm --cached <file>
# 删除暂存区文件,但保留在工作区
git rm -r <dir>
# 删除工作区目录,并将这次删除放入暂存区
git rm -rf <dir>
# 删除工作区目录,不将这次删除放入暂存区
git mv <file-original> <file-renamed>
# 将文件重命名,并将这次重命名放入暂存区
拉取和推送
务必养成在修改文件前先拉取远程变更的习惯。
git pull
# 拉取远程仓库的变更到本地
git push
# 将本地变更推送到远程仓库
分支
git branch
# 列出本地分支
git checkout <branch>
# 切换到指定分支
git branch -a
# 列出所有分支(包括远程分支)
git branch <branch-name>
# 创建新分支
git checkout <branch-name>
# 切换到指定分支
git checkout -b <new-branch-name>
# 创建并切换到新分支
git branch -d <branch-name>
# 删除本地分支
git branch -D <branch-name>
# 强制删除本地分支
git merge <branch>
# 合并指定分支到当前分支
git rebase <branch-name>
# 将指定分支的变更合并到当前分支(使用rebase)
git merge --no-commit --squash <branch>
# 合并时只保留与当前分支不同的部分,并将变更合并为一个提交
状态和比较
git status
查看当前仓库的状态
git diff
# 比较工作目录和暂存区的差异
git diff --cached
# 比较暂存区和本地仓库的差异
git diff --staged
# 比较暂存区和最新提交的差异
git diff --stat
# 以统计信息的形式显示差异
git diff -w
# 忽略空白字符的差异
git diff <file>
# 比较指定文件在工作目录和暂存区的差异
git diff <commit1>..<commit2>
# 比较两个提交之间的差异
git diff --name-status <commit1>..<commit2>
# 显示变化(包括改动、新增和删除)的文件
git diff <branch1>..<branch2>
# 比较指定分支之间的差异
git diff <commit> -- <file>
# 比较某个提交中的具体文件差异
日志
git log
# 查看提交日志
git lg
# 参考文中“设置”部分绑定的美化后的log
git log --follow <file>
# 查看指定文件的提交日志
git log --oneline
# 查看提交日志,仅显示一行
git log --pretty=oneline
# 查看提交日志,仅显示一行
git log --pretty=format:"%h - %an, %ar : %s"
# 查看提交日志,并按照格式显示
git log --graph
# 查看分支合并图
git log --graph --oneline --decorate --all --date=relative
# 查看分支合并图,单号显示,并按照时间排序
git log --since=2.weeks
# 查看最近两周的提交日志
git log --author=<pattern>
# 查看指定作者的提交日志
git log <branch1>..<branch2>
# 查看branch2相对于branch1的提交日志
git log --no-merges
# 查看提交日志,不显示合并提交
git log --grep <pattern>
# 查看提交说明中包含指定关键字的提交日志
git log -p <file>
# 查看指定文件的每次详细修改内容的提交日志
git log -p -2
# 查看最近两次详细修改内容的提交日志
git log --stat
# 查看提交日志,仅显示简要的增改行数统计
git log --summary
# 查看提交日志,仅显示简要的增改行数统计
git log --pretty=short
# 查看提交日志,显示较少的提交信息
git log --pretty=full
# 查看提交日志,显示较多的提交信息
git log --pretty=format:"%h %s" --graph
# 查看提交日志,显示提交的简略信息和分支合并图
标签
git tag
# 列出所有标签
git tag <tag-name>
# 在当前commit新建一个tag
git tag <tagname> <commit>
# 在指定commit新建一个tag
git tag -a <tag-name> -m "Your tag message"
# 创建带有附注的标签
git show <tag-name>
# 查看标签信息
git tag -d <tag-name>
# 删除标签
撤销
git checkout -- <file>
# 撤销工作区指定文件的修改
git checkout .
# 撤销工作区所有文件的修改
git checkout -- <dir>
# 撤销工作区目录的修改
git reset HEAD <file>
# 将暂存区的指定文件还原成和HEAD相同的状态
git reset --hard HEAD
# 将暂存区和工作区都还原成和HEAD相同的状态
git reset --hard <commit>
# 将暂存区和工作区都还原成和指定commit相同的状态
远程操作
git remote -v
# 列出所有远程仓库,并显示URL
git remote add <remote-name> <remote-url>
# 添加远程仓库
git remote remove <remote-name>
# 删除远程仓库
git remote rename <old-remote-name> <new-remote-name>
# 重命名远程仓库
git remote show <remote-name>
# 显示远程仓库的信息
git remote set-url <remote-name> <new-remote-url>
# 修改远程仓库的URL
git fetch <remote-name>
# 获取远程仓库的变化,但不合并
git pull <remote-name> <branch-name>
# 拉取远程分支到本地
git push <remote-name> <local-branch-name>
# 推送本地分支到远程
git push <remote-name> --delete <branch-name>
# 删除远程分支(本地分支不受影响)
git fetch origin <remote-branch-name>:<local-branch-name>
# 将远程分支拉取到本地
git push origin <local-branch-name>:<remote-branch-name>
# 将本地分支推送到远程
git push origin <tag-name>
# 推送单个标签到远程
git push origin --tags
# 推送所有标签到远程
git push origin --delete <tag-name>
# 删除远程标签
更多好用的资料正在整理中,收藏、关注,不迷路。
- 上一篇: Git 从入门到放不下(github)
- 下一篇: Git分布式版本控制器常用命令和使用
猜你喜欢
- 2025-05-10 Git 主仓库下开发子模块的流程和注意事项
- 2025-05-10 32位单片机定时器入门介绍(52单片机定时器三种工作模式)
- 2025-05-10 Git分布式版本控制器常用命令和使用
- 2025-05-10 Git 从入门到放不下(github)
- 2025-05-10 轻松部署Gemma3-27B,L20服务器+最新版vLLM高效推理
- 2025-05-10 如何在github建一个自己的博客和git本地常规操作?
- 2025-05-10 Python进阶-day27: 版本控制与协作
- 2025-05-10 抖音品质建设 - iOS启动优化《原理篇》
- 2025-05-10 掌握这 20 个 Git 命令,成为团队协作高手!
- 2025-05-10 Git从入门到精通:拯救你的代码仓库的终极指南
你 发表评论:
欢迎- 367℃用AI Agent治理微服务的复杂性问题|QCon
- 358℃初次使用IntelliJ IDEA新建Maven项目
- 356℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 351℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 346℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 345℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 342℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)