git实战(7)git常用命令速查表

一、基础操作

1. 初始化仓库
bash 复制代码
git init                  # 当前目录初始化为Git仓库
git clone <repo_url>      # 克隆远程仓库到本地
2. 查看状态与日志
bash 复制代码
git status                # 查看工作区/暂存区状态
git log                   # 查看提交历史
git log --oneline         # 简洁版提交历史
git log -p                # 显示历史提交的代码差异
3. 添加与提交
bash 复制代码
git add <file>            # 添加文件到暂存区
git add .                 # 添加所有修改到暂存区
git commit -m "message"   # 提交暂存区内容
git commit -am "message"  # 添加所有修改并直接提交(跳过`git add`)

二、分支管理

1. 分支操作
bash 复制代码
git branch                # 查看本地分支
git branch <name>         # 创建新分支
git checkout <branch>     # 切换到分支
git switch <branch>       # (Git 2.23+) 更安全的切换命令
git merge <branch>        # 合并指定分支到当前分支
git branch -d <branch>    # 删除分支(已合并)
git branch -D <branch>    # 强制删除分支(未合并)
2. 远程分支
bash 复制代码
git fetch                 # 拉取远程最新元数据(不自动合并)
git pull                  # 拉取远程代码并合并(= fetch + merge)
git pull --rebase         # 拉取远程代码并用变基合并
git push                  # 推送本地提交到远程
git push -u origin <branch> # 首次推送并设置上游跟踪分支

三、撤销与回退

1. 撤销工作区修改
bash 复制代码
git restore <file>        # 撤销工作区的修改(未add)
git checkout -- <file>    # 同上(旧版写法)
2. 撤销暂存区修改
bash 复制代码
git restore --staged <file> # 将文件移出暂存区(保留工作区修改)
git reset HEAD <file>       # 同上(旧版写法)
3. 回退提交
bash 复制代码
git reset --soft HEAD^     # 回退到上一提交(保留修改到暂存区)
git reset --hard HEAD^     # 彻底回退到上一提交(丢弃所有修改)
git revert <commit_id>     # 创建新提交来撤销指定提交(安全!)

注意reset --hard 会永久丢弃修改,慎用!


四、远程仓库管理

bash 复制代码
git remote -v              # 查看远程仓库地址
git remote add <name> <url> # 添加远程仓库
git remote remove <name>   # 删除远程仓库
git push origin --delete <branch> # 删除远程分支

五、高级操作

1. 储藏临时修改
bash 复制代码
git stash                  # 储藏当前工作区修改
git stash pop              # 恢复最近储藏的修改
git stash list             # 查看所有储藏记录
2. 标签管理
bash 复制代码
git tag                    # 查看所有标签
git tag v1.0.0             # 创建轻量标签
git tag -a v1.0.0 -m "msg" # 创建附注标签
git push origin --tags     # 推送所有标签到远程
3. 变基(Rebase)
bash 复制代码
git rebase <branch>        # 将当前分支变基到目标分支
git rebase --abort         # 终止变基
git rebase --continue      # 解决冲突后继续变基

六、配置相关

bash 复制代码
git config --global user.name "Your Name"   # 设置全局用户名
git config --global user.email "email@example.com" # 设置邮箱
git config --list          # 查看所有配置

七、文件忽略规则

  • 创建 .gitignore 文件,添加需忽略的文件/目录:

    plaintext 复制代码
    # 示例:
    *.log
    /node_modules
    .DS_Store

八、图形化工具推荐

  • 命令行增强

    bash 复制代码
    git lg  # 使用以下别名获得美观日志:

    配置别名:

    bash 复制代码
    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  • 可视化工具

    • VS Code(内置Git GUI)
    • Sourcetree
    • GitKraken

九、工作流示意图

复制代码
工作区 (Working Directory)
     │
     ↓
git add → 暂存区 (Staging Area)
     │
     ↓
git commit → 本地仓库 (Local Repository)
     │
     ↓
git push → 远程仓库 (Remote Repository)

十、常用命令速查表

场景 命令
克隆仓库 git clone <url>
提交修改 git add . + git commit -m "msg"
推送代码 git push
拉取更新 git pull --rebase
创建分支 git branch <name>
合并分支 git merge <branch>
撤销工作区修改 git restore <file>
查看差异 git diff

掌握这些命令后,可应对 95% 的日常开发需求。建议结合实践加深理解!

相关推荐
果然_30 分钟前
为什么你的 PR 总是多出一堆奇怪的 commit?90% 的人都踩过这个 Git 坑
前端·git
yyuuuzz1 小时前
独立站搭建:从入门到避坑实战
前端·git·github
splage2 小时前
Nginx 反向代理之upstream模块以及完整配置反向代理示例
git·nginx·github
福老板的生意经3 小时前
从成本失控到ROI翻倍:企业数字化营销投放的落地路径与工具选型指南
大数据·运维·人工智能
@insist1233 小时前
网络工程师-实战配置篇(二):精通 ACL 与策略路由,实现智能流量管控
大数据·网络·网络工程师·软考·软件水平考试
互联网科技看点3 小时前
以青春种黄芪 用科技兴乡村
大数据·人工智能·科技
阿崽meitoufa3 小时前
hermes-agent安装到本地 Git方法
git·hermes·hermes-agent
2501_933670794 小时前
2026大学生必看!互联网行业含金量最高
大数据
Ulyanov4 小时前
像素迷宫:路径规划算法的可视化与实战
大数据·开发语言·python·算法
pride.li5 小时前
Git 笔记:将一段旧历史压缩成一个提交
大数据·elasticsearch·搜索引擎