Git 常用命令大全

bash 复制代码
# 丢弃所有未暂存修改
git checkout -- .

# 可选:查看状态确认是否清理成功
git status
bash 复制代码
git reset --hard    # 重置暂存区和工作区
git clean -df       # 删除未跟踪文件和目录

Git 常用命令大全

基础操作
  1. 初始化仓库
    git init

    在当前目录创建新的 Git 仓库

  2. 克隆仓库
    git clone <仓库URL>

    克隆远程仓库到本地

  3. 查看状态
    git status

    显示工作目录和暂存区的状态

  4. 添加文件到暂存区
    git add <文件名>
    git add . (添加所有修改)

  5. 提交更改
    git commit -m "提交说明"

    将暂存区内容提交到本地仓库


分支管理
  1. 创建分支
    git branch <分支名>

  2. 切换分支
    git checkout <分支名>
    git switch <分支名> (Git 2.23+)

  3. 创建并切换分支
    git checkout -b <新分支名>

  4. 合并分支
    git merge <要合并的分支名>

  5. 删除分支
    git branch -d <分支名> (安全删除)
    git branch -D <分支名> (强制删除)


远程操作
  1. 添加远程仓库
    git remote add <别名> <仓库URL>

  2. 推送到远程
    git push <远程名> <分支名>

  3. 拉取更新
    git pull <远程名> <分支名>

  4. 查看远程仓库
    git remote -v


版本控制
  1. 查看提交历史
    git log
    git log --oneline (简洁版)

  2. 撤销工作区修改
    git checkout -- <文件名>

  3. 撤销暂存区文件
    git reset HEAD <文件名>

  4. 回退提交
    git reset --hard <commit_id>

  5. 创建标签
    git tag -a v1.0 -m "版本说明"


高级操作
  1. 储藏更改
    git stash
    git stash pop (恢复储藏)

  2. 比较差异
    git diff (工作区 vs 暂存区)
    git diff --staged (暂存区 vs 最新提交)

  3. 重写提交历史
    git rebase -i <commit_id>

  4. 查看文件修改记录
    git blame <文件名>

  5. 子模块管理
    git submodule add <仓库URL

配置相关
  1. 设置用户信息

    bash 复制代码
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
  2. 查看配置
    git config --list

  3. 设置默认编辑器
    git config --global core.editor "vim"

bash 复制代码
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
相关推荐
南菠湾1 小时前
How to set up SSH, Git and Copilot Extensions in Visual Studio Code
git·ssh·copilot
Jditinpc12 小时前
Git使用
git
兔斯基灬木木15 小时前
【技术工具】源码管理 - GIT工具
git
工呈士17 小时前
Git 工作流与版本管理策略
前端·git·面试
C++ 老炮儿的技术栈18 小时前
文本文件与二进制文件的区别
大数据·c语言·开发语言·c++·git·算法·visual studio
Jooolin21 小时前
【编程史】Gitee是啥?它和GitHub关系是什么?
git·github·ai编程
貂蝉空大1 天前
Git Switch 与 Git Restore 详解
git
aini_lovee1 天前
python在容器内克隆拉取git私有仓库
git·python·elasticsearch
zhangphil1 天前
git merge合并分支push报错:Your branch is ahead of ‘xxx‘ by xx commits.
git