Git基础教程:快速入门

需积分: 9 1 下载量 47 浏览量 更新于2024-07-31 收藏 227KB PDF 举报
"A tour of git: the basics" 本文档是《Distributed revision control with Mercurial》的修改版,最初由Bryan O’Sullivan撰写,并从http://hgbook.red-bean.com/获取。本文档的版权遵循Open Publication License 1.0版本。由于这是一个修改版,仅用Bryan O'Sullivan的名字来正确地归功于原始文本,他的名字出现并不表示或暗示他对修改后文档的背书。部分版权属于Carl Worth,他在2007年进行了修改,将内容转换为Markdown格式,删除了除第2章和附录D外的所有内容,并修改为描述Git而非Mercurial。该修改版的源代码可以通过Git在cworth.org上获取。 在“Git基础入门”中,我们将探讨Git的基本概念和安装方法: 2.1 在你的系统上安装Git Git提供了许多流行操作系统的预编译二进制包,便于快速在计算机上开始使用Git。对于不同的Linux发行版,安装方式因发行版的打包工具、政策和开发进度而异。通常,最简单的方法是在命令行下安装最新版本的Git。不过,具体步骤会根据你使用的Linux发行版有所不同。 2.1.1 Linux 对于Linux用户,虽然每个发行版的安装方法略有不同,但大多数都可以通过包管理器(如apt-get for Debian/Ubuntu, yum for CentOS/RHEL, dnf for Fedora)来安装Git。例如,在基于Debian的系统上,可以运行: ```bash sudo apt-get update sudo apt-get install git ``` 在基于RPM的系统上,可以使用: ```bash sudo yum install git ``` 或者对于Fedora用户: ```bash sudo dnf install git ``` 确保你安装的是最新版本的Git,因为包维护者的活跃程度可能会影响你得到的Git版本。 在其他操作系统上,如macOS,可以使用Homebrew: ```bash brew install git ``` 而在Windows上,可以从Git官方网站下载并安装Git for Windows。 2. Git基本概念 Git是一个分布式版本控制系统,它允许你跟踪文件和目录的变化,协同工作,管理项目的历史。主要概念包括: - **仓库(Repository)**:存储项目历史和元数据的地方。 - **提交(Commit)**:每次保存文件更改的快照。 - **分支(Branch)**:代表项目中的一个独立开发线。 - **HEAD**:指向当前活动分支的指针。 - **索引(Index)**:用于暂存即将提交的更改。 - **工作目录(Working Directory)**:包含你正在编辑的文件。 3. Git基本操作 - **初始化仓库**:`git init` 创建一个新的Git仓库。 - **克隆仓库**:`git clone URL` 复制远程仓库到本地。 - **添加文件**:`git add <filename>` 将更改添加到索引。 - **提交更改**:`git commit -m "message"` 创建一个新的提交。 - **查看状态**:`git status` 显示仓库的状态。 - **切换分支**:`git checkout <branch>` 切换到指定分支。 - **创建分支**:`git branch <branch>` 创建新分支。 - **合并分支**:`git merge <branch>` 将指定分支的更改合并到当前分支。 这些只是Git众多功能的一部分,深入学习Git将涉及到重置、变基、拉取请求、远程仓库等更高级的主题。对于初学者来说,掌握这些基本操作是开始使用Git的第一步。通过实践和进一步的学习,你将能够充分利用Git的强大功能来管理你的项目。

Traceback (most recent call last): File "C:\Users\GL\.conda\envs\pytorch\lib\site-packages\git\__init__.py", line 89, in <module> refresh() File "C:\Users\GL\.conda\envs\pytorch\lib\site-packages\git\__init__.py", line 76, in refresh if not Git.refresh(path=path): File "C:\Users\GL\.conda\envs\pytorch\lib\site-packages\git\cmd.py", line 392, in refresh raise ImportError(err) ImportError: Bad git executable. The git executable must be specified in one of the following ways: - be included in your $PATH - be set via $GIT_PYTHON_GIT_EXECUTABLE - explicitly set via git.refresh() All git commands will error until this is rectified. This initial warning can be silenced or aggravated in the future by setting the $GIT_PYTHON_REFRESH environment variable. Use one of the following values: - quiet|q|silence|s|none|n|0: for no warning or exception - warn|w|warning|1: for a printed warning - error|e|raise|r|2: for a raised exception Example: export GIT_PYTHON_REFRESH=quiet The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\eclipse workspace\yolov5-master\train.py", line 72, in <module> GIT_INFO = check_git_info() File "C:\Users\GL\.conda\envs\pytorch\lib\contextlib.py", line 79, in inner return func(*args, **kwds) File "E:\eclipse workspace\yolov5-master\utils\general.py", line 360, in check_git_info import git File "C:\Users\GL\.conda\envs\pytorch\lib\site-packages\git\__init__.py", line 91, in <module> raise ImportError("Failed to initialize: {0}".format(exc)) from exc ImportError: Failed to initialize: Bad git executable. The git executable must be specified in one of the following ways: - be included in your $PATH - be set via $GIT_PYTHON_GIT_EXECUTABLE - explicitly set via git.refresh() All git commands will error until this is rectified. This initial warning can be silenced or aggravated in the future by setting the $GIT_PYTHON_REFRESH environment variable. Use one of the following values: - quiet|q|silence|s|none|n|0: for no warning or exception - warn|w|warning|1: for a printed warning - error|e|raise|r|2: for a raised exception Example: export GIT_PYTHON_REFRESH=quiet

2023-07-10 上传