Git 使用操作手册

170 阅读2分钟

Git 是一个开源的分布式版本控制系统,广泛用于协作开发和版本管理。以下是一份简要的 Git 使用操作手册:

1. 安装 Git

  • 在 Linux 上安装:使用包管理工具(如 apt、yum)进行安装。
  • 在 macOS 上安装:可以使用 Homebrew 进行安装。
  • 在 Windows 上安装:下载 Git for Windows 安装程序并运行。

2. 配置 Git

  • 设置用户名:git config --global user.name "Your Name"
  • 设置邮箱:git config --global user.email "youremail@example.com"

3. 初始化仓库

  • 在现有项目中初始化仓库:git init
  • 克隆现有仓库:git clone <repository-url>

4. 添加文件到暂存区

  • 将文件添加到暂存区:git add <file>
  • 添加所有修改过的文件:git add .

5. 提交到本地仓库

  • 提交暂存区的文件到本地仓库:git commit -m "Commit message"

6. 查看状态和历史

  • 查看当前文件状态:git status
  • 查看提交历史:git log

7. 分支管理

  • 创建新分支:git branch <branch-name>
  • 切换分支:git checkout <branch-name>
  • 创建并切换到新分支:git checkout -b <branch-name>
  • 合并分支:git merge <branch-name>

8. 远程操作

  • 添加远程仓库:git remote add origin <repository-url>
  • 推送到远程仓库:git push -u origin <branch-name>
  • 拉取远程仓库:git pull origin <branch-name>

9. 撤销操作

  • 撤销工作目录中的修改:git checkout -- <file>
  • 撤销暂存区的文件:git reset HEAD <file>
  • 撤销最近的提交:git reset --soft HEAD~1

10. 其他常用命令

  • 查看帮助文档:git --help
  • 创建标签:git tag <tag-name>
  • 查看标签:git tag
  • 查看远程分支:git branch -r
  • 查看所有分支(包括远程分支):git branch -a

这些是 Git 的基本操作,你可以根据需要进一步学习和掌握更多高级功能。 Git 的学习曲线可能会有些陡峭,但随着实践和经验的积累,你会变得越来越熟练。