【收藏】Git 常用命令备忘录
2023-03-14 22:37:35 时间
不管是对于 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
相关文章
- 混合云市场之争将成为现实
- React Native 0.71正式版发布,Ts成为默认开发语言
- 图扑智慧充电桩可视化运营平台|新能源数字化
- .live域名暴增30%的背后
- 【蓝桥杯备赛系列 | 真题 | 简单题】2014年第五届真题-分糖果
- SAP BTP 的 ABAP Environment 和 CloudFoundry 环境的关系
- 外观设计专利的保护内容有哪些?外观设计专利申请时要注意哪些问题?
- 160. 如何根据 SAP Fiori UI 界面上的错误消息,找到后台 ABAP 对应的消息 ID
- EVM Gas 分析
- gRPC: gRPC 接口与 Restful API 混合使用
- 如何做好R语言笔记
- 自建 Kubernetes 集群
- 飞天加速计划续领申请
- 【学习笔记】Docker学习笔记
- 5 年迭代 5 次,抖音基于 Flink 的推荐系统演进历程
- 如何根据 SAP Fiori UI 界面上的错误消息,找到后台 ABAP 对应的消息 ID 试读版
- 常见安全组端口分享
- 关于对ESC的使用体验
- 云虚拟主机ip怎么看
- 基本的SELECT语句与显示表结构