Git 教程

浏览10401
更新时间2025-08-21

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


创建 GitHub 存储库

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


Push 推送操作

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

[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

上面的命令会产生如下结果。

Hello, World !!!

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

[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 添加为远程源并将他的更改推送到远程存储库。

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

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

推送操作将要求输入 GitHub 用户名和密码。 认证成功后,操作就会成功。

上面的命令会产生如下结果。

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 存储库一起使用。


Pull 拉动操作

Tom 成功地将他的所有更改推送到 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

上述命令产生以下结果:

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 命令验证目录内容。

[jerry@CentOS]$ ls
test_repo

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

相关视频

更多

免费

phpStudy极速入门视频教程
初级phpStudy极速入门视频教程

535885次学习

收藏

免费

Midjourney基础课程
初级Midjourney基础课程

13367次学习

收藏

免费

极客学院Git使用视频教程
初级极客学院Git使用视频教程

48352次学习

收藏

免费

尚观shell视频教程
高级尚观shell视频教程

16864次学习

收藏

免费

尚观Linux入门视频教程
初级尚观Linux入门视频教程

46713次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时 | 62.6万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时 | 39.8万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时 | 73.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号