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
没 add 就 commit 必须 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
相关推荐
GeW4 小时前
新质生产力路线图解读:数据库性能优化与Linux调优专场
linux
এ慕ོ冬℘゜4 小时前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn
mounter6255 小时前
深入理解 gccrs:GCC 如何实现 Rust 编译器,以及编译 Linux 内核面临的挑战
linux·驱动开发·rust
垚垚学技术_聚焦云原生5 小时前
Ansible Playbook 进阶:when 条件、loop 循环、handlers 触发器落地
linux·ansible·自动化运维·devops
无足鸟ICT5 小时前
【RHCA+】type命令(判断命令类型)
linux·ssh·bash
咸鱼梦想家π5 小时前
Linux开发工具(下)
linux·运维·服务器
LHICOD6 小时前
Linux-1
linux·ip·vmware·虚拟机·cmd·ping
DevLoom_7 小时前
macOS 命令行 curl/git 报 SSL 证书错误?eMudhra 事件完整修复指南
git·macos·ssl
丰锋ff7 小时前
用户管理命令
linux·运维·服务器
刚入门的大一新生8 小时前
Linux-线程概念
linux·jvm