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
相关推荐
zzzzzz3101 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode1 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒3 天前
TShark:Wireshark CLI 功能
linux
A小辣椒3 天前
TShark:基础知识
linux
AlfredZhao3 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao4 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334664 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
深海鱼在掘金4 天前
Git 完全指南 —— 第1章:Git 概览与版本控制演进
git
猪脚踏浪4 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠5 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql