Git常用指令合集

一、Git 配置

复制代码
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

查看配置信息

复制代码
git config --list

二、基本流程(最常用)

操作 指令
创建本地仓库 git init
查看状态 git status
添加到暂存区 git add <file>
添加所有 git add .
提交 git commit -m "说明"
修改最新提交信息 git commit --amend
查看版本记录 git log

三、查看日志(简洁用法)

复制代码
git log --oneline --graph --decorate --all

四、分支相关

操作 指令
查看本地分支 git branch
创建分支 git branch <name>
切换分支 git checkout <name>
创建并切换 git checkout -b <name>
删除分支 git branch -d <name>
强制删除 git branch -D <name>
合并分支 git merge <name>

合并指:把 <name> 分支内容并入当前分支

例如:

复制代码
git checkout main
git merge dev

五、远程仓库

操作 指令
查看远程地址 git remote -v
添加远程仓库 git remote add origin <url>
推送 git push
首次推送 git push -u origin main
拉取 git pull
从远程获取但不合并 git fetch

六、回退(非常重要)

场景 指令
查看提交历史 git log --oneline
回退到某提交(保留改动) git reset --soft <commit>
回退到某提交(丢弃改动) git reset --hard <commit>
丢弃工作区更改 git checkout -- <file>
丢弃暂存区更改 git reset HEAD <file>

小心使用 --hard:会丢失本地未提交内容。

七、暂存(stash)

操作 指令
暂存当前修改 git stash
暂存并备注 git stash save "info"
查看 stash 列表 git stash list
恢复 git stash pop
删除 stash git stash drop

什么时候用?

→ 想切分支,但当前内容没保存,用 stash 暂存

八、克隆

复制代码
git clone <url>

九、标签(tag)

操作 指令
查看 tag git tag
创建 tag git tag v1.0
删除 tag git tag -d v1.0
推送 tag git push origin v1.0
推送全部 tag git push origin --tags

十、.gitignore

忽略某文件/目录

创建 .gitignore 填入:

复制代码
*.log
*.exe
build/

十一、diff(比较)

操作 指令
查看未提交差异 git diff
与暂存区对比 git diff --cached
查看两个 commit 差异 git diff <commit1> <commit2>

十二、冲突处理

合并出现冲突:

复制代码
git merge dev

编辑冲突文件 → 解决 → add → commit

十三、常用组合命令(推荐收藏)

查看分支图

复制代码
git log --oneline --graph --decorate --all

添加 + 提交

复制代码
git add .
git commit -m "msg"

初始化 + 推送

复制代码
git init
git add .
git commit -m "first"
git remote add origin <url>
git push -u origin main

十四、常见误区

错误 正确
git commit m "msg" git commit -m "msg"
git-log git log
addcommit 必须 add

十五、助记版(超简短)

复制代码
git init
git add .
git commit -m "msg"
git status
git log
git branch
git checkout -b dev
git merge dev
git push
git pull
git reset --hard <commit>
git stash
git clone
相关推荐
_OP_CHEN2 小时前
Linux网络编程:(七)Vim 编辑器完全指南:从入门到精通的全方位实战教程
linux·运维·服务器·编辑器·vim·linux生态·linux软件
Maple_land2 小时前
第1篇:Linux工具复盘上篇:yum与vim
linux·运维·服务器·c++·centos
shizhan_cloud2 小时前
Linux 硬盘分区管理
linux·运维
蓁蓁啊3 小时前
Ubuntu 虚拟机文件传输到 Windows的一种好玩的办法
linux·运维·windows·单片机·ubuntu
9ilk4 小时前
【仿RabbitMQ的发布订阅式消息队列】 ---- 功能测试联调
linux·服务器·c++·分布式·学习·rabbitmq
q***7484 小时前
在Linux系统上使用nmcli命令配置各种网络(有线、无线、vlan、vxlan、路由、网桥等)
linux·服务器·网络
2301_807583234 小时前
zabbix监控ES集群健康状态并触发钉钉告警
linux·zabbix
Elias不吃糖4 小时前
eventfd 初认识Reactor/多线程服务器的关键唤醒机制
linux·服务器·c++·学习
melt_10264 小时前
【gitee账号设置】git多个账号在多台电脑上使用
git·gitee·git账号管理