Git 版本控制介绍

重要

有关 Git 的更深入的指南可以在 Git 网站 上找到。

git 是 Linus Torvalds 创建的分布式版本控制系统 (VCS),也因创建和维护 Linux 内核而闻名。版本控制是一个为开发人员跟踪代码更改的系统。 Git 版本控制的优点是:

  • Separation of testing environments into branches

  • 能够导航到特定的 提交 而不删除历史记录

  • 能够以多种方式管理 提交,包括将它们组合在一起

  • 各种其他功能,请参见 这里

先决条件

重要

本教程使用 Windows 操作系统

你必须从以下链接下载并安装 Git :

备注

你可能需要将 Git 添加到 路径 <https://www.google.com/search?q=adding+git+to+path> __

Git 词汇

Git revolves around several core data structures and commands:

  • Repository 代码的数据结构,包括根目录中的 .git 文件夹

  • Commit: a particular saved state of the repository, which includes all files and additions

  • Branch: a means of grouping a set of commits. Each branch has a unique history. This is primarily used for separating development and stable branches.

  • Push: 使用本地更改更新远程存储库

  • Pull: 使用远程更改更新本地存储库

  • Clone: retrieve a local copy of a repository to modify

  • Fork: duplicate a pre-existing repository to modify, and to compare against the original

  • Merge: combine various changes from different branches/commits/forks into a single history

资料库

Git 存储库是一种数据结构,其中包含项目的结构,历史记录和文件。

Git 存储库通常包括:

  • 一个 .git 文件夹。此文件夹包含有关存储库的各种信息。

  • 一个 .gitignore 文件。该文件包含您你在提交时不希望包含的文件或目录。

  • 文件和文件夹。这是存储库的主要内容。

创建存储库

You can store the repository locally, or through a remote – a remote being the cloud, or possibly another storage medium or server that hosts your repository. GitHub is a popular free hosting service. Numerous developers use it, and that’s what this tutorial will use.

备注

有许多提供托管存储库服务的提供者。 “Gitlab <https://about.gitlab.com>”_和 “Bitbucket <https://bitbucket.org/>”_是Github 的一些替代方案。

创建一个 GitHub 帐户

Go ahead and create a GitHub account by visiting the website and following the on-screen prompts.

如何创建一个新的GitHub帐户。

本地创作

创建并验证您的帐户后,你会要访问主页。它看起来类似于展示的图像。

显示新创建的帐户主页。

单击右上角的加号图标。

加号按钮的位置。

然后点击*“New Repository” *

点击加号按钮后创建一个新菜单。

填写适当的信息,然后单击 *“Create repository” *

显示“创建存储库”按钮

你应该会在屏幕上看到与此类似的内容

创建存储库后的快速设置屏幕。

备注

The keyboard shortcut Ctrl+~ can be used to open a terminal in Visual Studio Code for Windows.

现在,你将要打开一个 PowerShell 窗口并导航到您的项目目录。可以在此处找到<https://programminghistorian.org/en/lessons/intro-to-powershell> __ 上有关 PowerShell 的优秀教程。请使用你的搜索引擎,以了解如何在其他操作系统上打开终端。

一个空的powershell窗口。

If a directory is empty, a file needs to be created in order for git to have something to track. In the below Empty Directory example, we created a file called README.md with the contents of # Example Repo. For FRC® Robot projects, the below Existing Project commands should be run in the root of a project created by the VS Code WPILib Project Creator. More details on the various commands can be found in the subsequent sections.

备注

将文件路径 “C:Users ExampleUser9007 Documents Example Folder” 替换为你要在其中创建存储库的路径,然后替换远程 URL ‘https://github.com/ExampleUser9007/ExampleRepo.git’. 以及你在先前步骤中创建的仓库的 URL。

> cd "C:\Users\ExampleUser9007\Documents\Example Folder"
> git init
Initialized empty Git repository in C:/Users/ExampleUser9007/Documents/Example Folder/.git/
> echo "# ExampleRepo" >> README.md
> git add README.md
> git commit -m "First commit"
[main (root-commit) fafafa] First commit
 1 file changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README.md
> git remote add origin https://github.com/ExampleUser9007/ExampleRepo.git
> git push -u origin main
> cd "C:\Users\ExampleUser9007\Documents\Example Folder"
> git init
Initialized empty Git repository in C:/Users/ExampleUser9007/Documents/Example Folder/.git/
> git add .
> git commit -m "First commit"
[main (root-commit) fafafa] First commit
 1 file changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README.md
> git remote add origin https://github.com/ExampleUser9007/ExampleRepo.git
> git push -u origin main

提交

存储库主要由提交组成。提交是保存状态或代码的 “版本”。

在前面的示例中,我们创建了一个名为 README.md 的文件。在你喜欢的文本编辑器中打开该文件,然后编辑几行。修补文件后,只需保存并关闭即可。导航到 PowerShell,然后键入以下命令。

> git add README.md
> git commit -m "Adds a description to the repository"
[main bcbcbc] Adds a description to the repository
 1 file changed, 2 insertions(+), 0 deletions(-)
> git push

备注

Writing good commit messages is a key part of a maintainable project. A guide on writing commit messages can be found here.

Git Pull

备注

当用户不希望自动合并到当前工作分支时,可以使用 git fetch

此命令从远程存储库检索历史记录或提交。当遥控器包含你没有的工作时,它将尝试自动合并。请参阅:docs/software/basic-programming/git-getting-started:Merging。

运行: git pull

Git Add

This command “stages” the specified file(s) so that they will be included in the next commit.

For a single file, run git add FILENAME.txt where FILENAME.txt is the name and extension of the file to add. To add every file/folder that isn’t excluded via gitignore, run git add .. When run in the root of the repository this command will stage every untracked, unexcluded file.

Git Commit

This command creates the commit and stores it locally. This saves the state and adds it to the repository’s history. The commit will consist of whatever changes (“diffs”) were made to the staged files since the last commit. It is required to specify a “commit message” explaining why you changed this set of files or what the change accomplishes.

运行:git commit -m "在此处输入消息"

Git Push

将本地更改上传 (推送) 到远程 (云)

运行: git push

分支

Branches in Git are similar to parallel worlds. They start off the same, and then they can “branch” out into different varying paths. Consider the Git control flow to look similar to this.

分支工作流状态图。

在上面的示例中,main 被分支 (或复制) 到分支 Feature 1 中,并且有人查看了分支,从而创建了本地副本。然后,某人提交 (或上传) 了他们的更改,将其合并到分支功能 1 中。你正在将更改从一个分支 “合并” 到另一个分支。

创建一个分支

运行: git branch branch-name,其中 branch-name 是要创建的分支的名称。新的分支历史将从当前的活动分支中创建。

输入一个分支

创建分支后,你必须进入分支。

运行: git checkout branch-name 其中 branch-name 是先前创建的分支。

合并

In scenarios where you want to copy one branches history into another, you can merge them. A merge is done by calling git merge branch-name with branch-name being the name of the branch to merge from. It is automatically merged into the current active branch.

It’s common for a remote repository to contain work (history) that you do not have. Whenever you run git pull, it will attempt to automatically merge those commits into your local copy. That merge may look like the below.

合并工作流状态图。

However, in the above example, what if File A was modified by both branch Feature1 and Feature2? This is called a merge conflict. A merge conflict can be resolved by editing the conflicting file. In the example, we would need to edit File A to keep the history or changes that we want. After that has been done, simply re-add, re-commit, and then push your changes.

重置

有时历史记录需要被重置,或者提交需要被撤消。这可以通过多种方式完成。

还原提交

备注

你无法还原合并,因为 git 不知道应选择哪个分支或源。

要还原导致提交的历史记录,运行 git revert commit-id。可以使用 git log 命令显示提交ID。

重设 Head 指针

警告

强制磁头复位是一个危险的指令。此指令将永久删除目标之外的所有历史记录。请三思后行!

运行: git reset --hard commit-id

Forks

可以像处理分支一样处理 Fork。你可以将上游库 (原始存储库) 合并到本地库 (克隆存储库) 中。

克隆已存在的 “Repo”

在已经创建存储库并将其存储在远程服务器上的情况下,可以使用以下命令克隆该存储库:

git clone https://github.com/myrepo.git

where myrepo.git is replaced with your git repo. If you follow this, you can skip to commits.

更新 Fork

  1. 添加上游: git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

  2. 确认通过以下方式添加了它:git remote -v

  3. 从上游进行 Pull 更改: git fetch upstream

  4. 将更改整合进 head: git merge upstream/upstream-branch-name

Gitignore

重要

队伍 请勿 修改其机器人项目中随附的 .gitignore 文件,这一点非常重要。这可能导致下线部署无法正常工作。

一个 .gitignore 文件通常用作不使用 git add 自动提交的文件列表。此文件中列出的任何文件或目录都将 不提交。它们也不会显示为 git status.

Additional Information can be found here.

隐藏一个文件夹

只需添加包含要隐藏的文件夹的新行,并在末尾加正斜杠

例如: directory-to-exclude/

隐藏一个文件

用要隐藏的文件名添加新行,包括相对于存储库根目录的任何前缀目录。

例如:`` directory / file-to-hide.txt ``

例如:`` file-to-hide2.txt ``

附加的信息

在 git <https://git-scm.com/docs/gittutorial> 官方网站上可以找到更深入的教程。

可以在 git`flight 规则 <https://github.com/k88hudson/git-flight-rules/blob/master/README.md>`_ 存储库中找到纠正常见错误的指南。