常用命令:

Git 是目前最流行的分布式版本控制系统。为了帮助你高效掌握 Git,以下将常用命令按照‌工作流程‌和‌功能场景进行了分类整理‌,涵盖了从初始化配置到高级撤销操作的核心指令。

1. 初始配置与仓库建立

  1. 配置用户信息:

    • git config --global user.name "Your Name" : 配置用户名
    • git config --global user.email "your.email@example.com" : 配置邮箱
    • git config --list : 列出当前配置信息
  2. 创建或获取仓库:

    • git init:初始化一个空仓库
    • git clone <repository_url>:克隆一个远程仓库
  3. 远程管理:
    • git remote -v:查看远程仓库地址
    • git remote add <remote_name> <repository_url>:添加远程仓库
    • git remote remove <remote_name>:删除远程仓库
    • git remote set-url <remote_name> <repository_url>:修改远程仓库地址
    • git remote rename <old_name> <new_name>:修改远程仓库名称
    • git remote update:更新远程仓库信息

2. 基本操作

  1. 添加文件到暂存区:
    • git add <file_name>:添加指定文件
    • git add .:添加所有文件
  2. 提交更改:
    • git commit -m "commit message":提交暂存区文件到本地仓库
  3. 查看状态和日志:
    • git status:查看当前仓库状态
    • git log:查看提交日志

3. 分支管理

  1. 创建和切换分支:
    • git branch <branch_name>:创建新分支
    • git checkout <branch_name>:切换到指定分支
    • git checkout -b <branch_name>:创建并切换到新分支
  2. 合并分支:
    • git merge <branch_name>:合并指定分支到当前分支
  3. 删除分支:
    • git branch -d <branch_name>:删除指定分支
    • git branch -D <branch_name>:强制删除指定分支
    • git push origin --delete <branch_name>:删除远程分支
  4. 查看分支:
    • git branch:查看本地分支
    • git branch -a:查看所有分支
    • git branch -r:查看远程分支

4. 撤销操作

  1. 撤销工作区修改:
    • git checkout -- <file_name>:撤销工作区文件修改
    • git restore <file_name>:撤销工作区文件修改
  2. 撤销暂存区修改:
    • git reset HEAD <file_name>:撤销暂存区文件修改
  3. 撤销提交:
    • git reset --hard <commit_hash>:撤销指定提交

5. 远程仓库操作

  1. 添加远程仓库:
    • git remote add <remote_name> <repository_url>:添加远程仓库
  2. 获取远程仓库数据:
    • git fetch <remote_name>:获取远程仓库数据
  3. 推送本地仓库数据到远程仓库:
    • git push <remote_name> <branch_name>:推送指定分支数据
  4. 拉取远程仓库数据并合并到当前分支:
    • git pull <remote_name> <branch_name>:拉取并合并指定分支数据

6. 标签管理

  1. 创建标签:
    • git tag <tag_name>:创建标签
  2. 查看标签:
    • git tag:查看所有标签
  3. 删除标签:
    • git tag -d <tag_name>:删除指定标签
  4. 推送标签到远程仓库:
    • git push <remote_name> <tag_name>:推送指定标签
  5. 拉取远程仓库所有标签:
    • git fetch --tags:获取所有标签

7. 暂存区

  1. 保存暂存区修改:
    • git stash:保存暂存区修改
    • git stash pop:恢复暂存区修改
    • git stash apply:应用暂存区修改
    • git stash list:查看暂存区修改列表
    • git stash show:查看暂存区修改详情
    • git stash drop:删除暂存区修改
    • git stash clear:清空暂存区修改

8. 其他操作

  1. 查看帮助:
    • git --help:查看 Git 帮助
  2. 查看版本:
    • git --version:查看 Git 版本

results matching ""

    No results matching ""