当前位置: 首页 > article >正文

Git的详细使用方法

Git 是一个分布式版本控制系统,用于跟踪和管理代码的变更。以下是 Git 的详细使用方法:


1. 安装 Git

  • Windows:从 Git 官网 下载安装包。

  • Linux(Ubuntu/Debian)

    sudo apt install git
  • macOS

  • 使用 Homebrew。

    brew install git

验证安装

git --version

2. 配置用户信息

首次使用

首次使用时,Git 前需配置全局用户名和邮箱:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

查看配置

git config --list

3. 创建仓库

初始化新仓库

git init

克隆现有仓库

git clone https://github.com/user/test.git

4. 基础操作

修改文件

在工作目录中修改或新增文件。

添加到暂存区

git add <file>       # 添加单个文件
git add .            # 添加所有修改

提交到本地仓库

git commit -m "说明"

查看状态

git status

查看提交历史

git log              # 完整的提交历史
git log --oneline    # 简单的提交历史
git log --graph      # 图形化分支历史

5. 分支管理

创建分支

git branch <branch-name>

切换分支

git checkout <branch-name>
# 或创建并切换到新分支
git checkout -b <branch-name>

合并分支

git checkout master
git merge <branch-name>

删除分支

git branch -d <branch-name>  # 删除本地分支
git push origin --delete <branch-name>  # 删除远程分支

解决冲突

手动编辑冲突文件后,执行 git add 和 git commit


6. 远程仓库

  • 关联远程仓库

    git remote add origin https://github.com/user/test.git
  • 查看远程仓库

    git remote -v
  • 推送代码

    git push origin <branch-name>
    # 首次推送时指定分支:
    git push -u origin <branch-name>
  • 拉取代码

    git pull origin <branch-name>  # 拉取并合并
    git fetch origin               # 仅下载远程变更

7. 高级操作

撤销更改

  • 撤销工作区修改

    git checkout -- <file>
  • 撤销暂存区修改

    git reset HEAD <file>
  • 回退到某次提交

    git reset --hard <commit-id>  # 彻底回退(请慎用)
    git revert <commit-id>        # 生成新提交以撤销旧提交

标签管理

  • 创建标签

    git tag v1.0.0
    git tag -a v1.0.0 -m "版本说明"
  • 推送标签到远程

    git push origin --tags

暂存修改

git stash        # 暂存当前修改
git stash pop    # 恢复暂存的修改

比较差异

git diff          # 工作区与暂存区的差异
git diff --staged # 暂存区与最新提交的差异

8. 协作流程(示例)

克隆仓库

git clone https://github.com/user/test.git

创建新分支开发

git checkout -b feature/new-feature

开发完成后提交

git add .
git commit -m "版本说明"

推送分支到远程

git push origin feature/new-feature

Pull Request(PR)或 Merge Request(MR)

在 GitHub/Gitee 上发起 Pull Request(PR)或 Merge Request(MR)。

合并分支

审核通过后合并到主分支(如 master)。


9. 配置与别名

忽略文件

在项目根目录创建 .gitignore,列出需忽略的文件/目录(如 node_modules/.env)。

设置别名

设置别名可以简化命令

git config --global alias.co checkout
git config --global alias.br branch

10. 常见问题

提交到错误分支

使用 git reset 回退提交,再切换到正确分支提交。

误删分支

通过 git reflog 查找提交记录恢复。



http://www.kler.cn/a/584987.html

相关文章:

  • 基于stm32的视觉物流机器人
  • 智慧城市新基建!图扑智慧路灯,点亮未来城市生活!
  • AWS云编排详解-Cloud Formation
  • 一文讲清楚CUDA与PyTorch、GPU之间的关系
  • Gemini Robotics:Google DeepMind 让 AI 机器人真正“动”起来!
  • 深度学习——Diffusion Model学习,扩散模型
  • 编程助手学Python--Deepseek对OpenAI的Python库调用GPT-4模型生成对话回复理解
  • 解决启动Vue项目时遇到的 error:0308010C:digital envelope routines::unsupported 错误
  • 深入理解pytest框架中的conftest.py:使用与作用原理
  • 爬取数据时如何处理可能出现的异常?
  • 系统开发资源
  • Qt 实现透明可移动悬浮工具条
  • c#面试题整理10
  • 纽扣电池缺陷分割数据集labelme格式28张2类别
  • 阶乘之和(信息学奥赛一本通-2033)
  • 【互联网性能指标】QPS/TPS/PV/UV/IP/GMV/DAU/MAU/RPS
  • Linux第七讲:基础IO
  • 记忆 `lower_bound` 和 `upper_bound`(或你实现的 `last_less_equal`)
  • 基于springboot vue前后端分离的网上书城
  • 单一责任原则在Java设计模式中的深度解析