网站首页 > 技术文章 正文
不管是对于 Git 的初学者还是经常使用 Git 的码农们,在日常工作中难免会有遇到有的命令一时想不起来。不妨将下面总结的一些 Git 常用命令及技巧收藏或打印出来,以备需要的时候可以很快找到。
git config
# 检查 git 配置
git config -l
# 设置你的 git 提交 username 和 email
# 例如:对于公司里项目
git config --global user.name "Your Name"
git config --global user.email "your_email@organization.com"
# 例如:对于个人的 GitHub 项目
git config user.name "Your Name"
git config user.email "your_emailn@gmail.com"
# 使用 HTTP/HTTPS 避免每次输入密码
git config --global credential.helper cache
git init
# 初始化一个仓库
git init
git add
# 将文件添加到暂存区
git add file_name
# 将所有文件添加到暂存区
git add .
# 仅将某些文件添加到暂存区, 例如:仅添加所有以 'test*' 开头的文件
git add test*
git status
# 检查仓库状态
git status
git commit
# 提交更改
git commit
# 提交带有消息的更改
git commit -m "This is a commit message"
git log
# 查看提交历史
git log
# 查看提交历史和显示相应的修改
git log -p
# 显示提交历史统计
git log --stat
# 显示特定的提交
git show commit_id
# 以图形方式显示当前分支的提交信息
git log --graph --oneline
# 以图形方式显示所有分支的提交信息
git log --graph --oneline --all
# 获取远程仓库的当前提交日志
git log origin/master
git diff
# 在使用 diff 提交之前所做的更改
git diff
git diff some_file.js
git diff --staged
git rm
# 删除跟踪文件
git rm file_name
git mv
# 重命名文件
git mv old_file_name new_file_name
git checkout
# 切换分支
git checkout branch_name
# 还原未暂存的更改
git checkout file_name
git reset
# 还原暂存区的更改
git reset HEAD file_name
git reset HEAD -p
git commit
# 修改最近的提交信息
git commit --amend
# 修改最近的提交信息为:New commit message
git commit --amend -m "New commit message"
git revert
# 回滚最后一次提交
git revert HEAD
# 回滚指定一次提交
git revert commit_id
git branch
# 创建分支
git branch branch_name
# 创建分支并切到该分支
git checkout -b branch_name
# 显示当前分支
git branch
# 显示所有分支
git branch -a
# 检查当前正在跟踪的远程分支
git branch -r
# 删除分支
git branch -d branch_name
git merge
# 将 branch_name 合并到当分支
git merge branch_name
# 中止合并
git merge --abort
git pull
# 从远程仓库拉取更改
git pull
git fetch
# 获取远程仓库更改
git fetch
git push
# 推送更改到远程仓库
git push
# 推送一个新分支到远程仓库
git push -u origin branch_name
# 删除远程仓库分支
git push --delete origin branch_name
git remote
# 添加远程仓库
git add remote https://repository_name.com
# 查看远程仓库
git remote -v
# 查看远程仓库的更多信息
git remote show origin
Git技巧和窍门
清理已合并分支
清理已经合并的本地分支
git branch --merged master | grep -v "master" | xargs -n 1 git branch -d
.gitignore
指明 Git 应该忽略的故意不跟踪的文件的文件,比如 .gitignore 如下
# 忽略 .vscode 目录
.vscode/
# 忽略 build 目录
build/
# 忽略文件
output.log
.gitattributes
关于 .gitattributes 请参考
- https://www.git-scm.com/docs/gitattributes
- https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings
猜你喜欢
- 2025-06-28 Windows 下 Git 拉 Gitlab 代码(gitlab拉取代码到本地)
- 2025-06-28 【超详细】Git 所有常用命令 + 提交规范全指南(建议收藏!)
- 2025-06-28 掌握这 20 个 Git 命令,成为团队协作高手!
- 2025-06-28 探索eBPF:Linux内核的黑科技(bpf之巅:洞悉linux系统和应用性能)
- 2025-06-28 阅读Asp.Net Core源码(asp.net core 源码)
- 2025-06-28 每日github项目分享:PocketBase(pocket server)
- 2025-06-28 前端铜九铁十面试必备八股文——工程化
- 2025-06-28 在大型项目中如何使用Git子模块开发,看完涨姿势了
- 2025-06-28 WebRTC实现的视频会议流程设计和工程架构分享
- 2025-06-28 git的基本操作(git基础操作)
- 1510℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 546℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 498℃MySQL service启动脚本浅析(r12笔记第59天)
- 478℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 476℃启用MySQL查询缓存(mysql8.0查询缓存)
- 455℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 436℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 433℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)