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
相关推荐
小杜今天学AI了吗8 分钟前
如何配置 linux 系统的conda 环境
linux·运维·conda
oMcLin11 分钟前
如何在Ubuntu 22.04 LTS上通过配置ZFS存储池,提升高吞吐量数据库的读写性能与可靠性?
linux·数据库·ubuntu
这就是佬们吗11 分钟前
告别 Node.js 版本冲突:NVM 安装与使用全攻略
java·linux·前端·windows·node.js·mac·web
christine-rr12 分钟前
linux常用命令(9)——查看系统与硬件信息
linux·运维·服务器·网络·后端
南_山无梅落17 分钟前
Git 结合 Gitee 使用教程:从入门到实战
git·gitee
这就是佬们吗17 分钟前
Windows 的 CMD 网络环境:解决终端无法联网与更新的终极指南
java·windows·git·python·spring·maven
阿豪学编程21 分钟前
【Linux】线程同步和线程互斥
linux·开发语言
oMcLin26 分钟前
如何在CentOS Stream 9上通过配置Hyper‑V虚拟化实现高效的资源隔离与虚拟机管理?
linux·运维·centos
liuyunshengsir27 分钟前
Elasticsearch 高级查询must 多个条件同时满足
linux·服务器·elasticsearch
草莓熊Lotso27 分钟前
Linux系统进程调度优化:优先级策略与切换机制深度实践
linux·运维·服务器·c++·人工智能·经验分享·其他