Git 简明教程

Git - Online Repositories

GitHub 是一个基于 Web 的软件开发项目托管服务,它使用 Git 版本控制系统。它还有自己的标准 GUI 应用程序,可直接从该服务的网站下载(Windows、Mac、GNU/Linux)。但在本会话中,我们只关注 CLI 部分。

GitHub is a web-based hosting service for software development projects that uses the Git revision control system. It also has their standard GUI application available for download (Windows, Mac, GNU/ Linux) directly from the service’s website. But in this session, we will see only CLI part.

Create GitHub Repository

访问 github.com 。如果您已经拥有 GitHub 帐户,请使用该帐户登录或创建一个新帐户。按照 github.com 网站上的步骤创建新仓库。

Go to github.com. If you already have the GitHub account, then login using that account or create a new one. Follow the steps from github.com website to create a new repository.

Push Operation

Tom 决定使用 GitHub 服务器。要启动一个新项目,他在其中创建了一个新目录和一个文件。

Tom decides to use the GitHub server. To start a new project, he creates a new directory and one file inside that.

[tom@CentOS]$ mkdir github_repo

[tom@CentOS]$ cd github_repo/

[tom@CentOS]$ vi hello.c

[tom@CentOS]$ make hello
cc hello.c -o hello

[tom@CentOS]$ ./hello

上述命令将生成以下结果:

The above command will produce the following result:

Hello, World !!!

在验证他的代码后,他使用 git init 命令初始化目录,并在本地提交其更改。

After verifying his code, he initializes the directory with the git init command and commits his changes locally.

[tom@CentOS]$ git init
Initialized empty Git repository in /home/tom/github_repo/.git/

[tom@CentOS]$ git status -s
?? hello
?? hello.c

[tom@CentOS]$ git add hello.c

[tom@CentOS]$ git status -s
A hello.c
?? hello

[tom@CentOS]$ git commit -m 'Initial commit'

之后,他添加了 GitHub 仓库 URL 作为远程源,并将他的更改推送到远程仓库。

After that, he adds the GitHub repository URL as a remote origin and pushes his changes to the remote repository.

[tom@CentOS]$ git remote add origin https://github.com/kangralkar/testing_repo.git

[tom@CentOS]$ git push -u origin master

推送操作将要求 GitHub 用户名和密码。成功身份验证后,该操作将成功。

Push operation will ask for GitHub user name and password. After successful authentication, the operation will succeed.

上述命令将生成以下结果:

The above command will produce the following result:

Username for 'https://github.com': kangralkar
Password for 'https://kangralkar@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/kangralkar/test_repo.git
 * [new branch]      master −> master
 Branch master set up to track remote branch master from origin.

从现在开始,Tom 可以将任何更改推送到 GitHub 仓库。他可以使用本教程中讨论的所有命令,以及 GitHub 仓库。

From now, Tom can push any changes to the GitHub repository. He can use all the commands discussed in this chapter with the GitHub repository.

Pull Operation

Tom 成功将所有更改推送到 GitHub 仓库。现在,其他开发人员可以通过执行克隆操作或更新其本地仓库来查看这些更改。

Tom successfully pushed all his changes to the GitHub repository. Now, other developers can view these changes by performing clone operation or updating their local repository.

Jerry 在他的主目录中创建了一个新目录,并使用 git clone 命令克隆了 GitHub 仓库。

Jerry creates a new directory in his home directory and clones the GitHub repository by using the git clone command.

[jerry@CentOS]$ pwd
/home/jerry

[jerry@CentOS]$ mkdir jerry_repo

[jerry@CentOS]$ git clone https://github.com/kangralkar/test_repo.git

以上命令会产生以下结果:

The above command produces the following result:

Cloning into 'test_repo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.

他通过执行 ls 命令验证了目录的内容。

He verifies the directory contents by executing the ls command.

[jerry@CentOS]$ ls
test_repo

[jerry@CentOS]$ ls test_repo/
hello.c