(四)Git克隆与更新代码

虫师 创建于 over 7 years 之前

最后更新: less than a minute 之前

阅读数: 171

(四)Git克隆与更新代码

请先阅读:

GitHub注册与Git安装

通过Git提交代码到GitHub

克隆项目


我们除了可以向GitHub上提交项目外,更多的时候是我们到上面克隆(下载)优秀的开源项目来用,当然也可以将使用过程中发现的bug,通过建立分支的方式提交给项目的原作者。 我们现在的场景是在家将项目提交到了GitHub上,现在来到公司,需要将GitHub上的项目克隆到本地,那么对于公司的电脑来说,同样需要与GitHub建立连接。

首先,下载安装Git。

其次,通过Git生成本地公钥,并且将公钥添加到GitHub中。

最后,设置仓库人员的用户名和邮箱地址。

具体操作请参考前面的章节。当一切都设置完成后,就可以从GitHub上克隆项目到本地了。我们同样以Windows系统为例,打开Git Bash。

Company@MININT-IQVJFIT /d/my_test
$  git clone git@github.com:defnngj/project-name.git

Cloning into 'project-name'...
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.

Company@MININT-IQVJFIT /d/my_test
$ cd project-name/

Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ ls

test_case.py

“git clone” 命令用于克隆GitHub上的项目到本地。通过“cd”命令进入项目目录,查看项目文件。

另外,我们也可以直接通过http链接克隆项目:

$  git clone https://github.com/defnngj/project-name

更新项目


这次更新我们项目做了较大的变更,创建文件的文件与文件夹,并且删除了原有文件。

Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ git status

On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    test_case.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        126login.py
        baidu.py
        package/
        po_login.py
        test_case/

no changes added to commit (use "git add" and/or "git commit -a")

通过“git status” 命令查看当前变更。通过变更信息可以看出,删除了test_case.py文件。这个删除只是在项目目录下进行删除,Git对此文件留有记忆,所以要通过 “git rm” 命令将其删除。

Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ git rm test_case.py
rm 'test_case.py'

如果删除的是文件同样用此命令,例如,“git rm test_case/”

如果删除的文件名带空格,则需要通过双引号将文件名引起来,例如,“git rm “test case.py” ”

Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ git add .

Meizu@MININT-IQVJFIT /d/my_test/project-name (master)
$ git status

On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   126login.py
        new file:   baidu.py
        new file:   package/__init__.py
        new file:   package/location.py
        new file:   po_login.py
        deleted:    test_case.py
        new file:   test_case/126login.py
        new file:   test_case/allpage/__init__.py
        new file:   test_case/allpage/base.py
        new file:   test_case/allpage/login_page.py


Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ git commit -m "update project"

[master 0e8eece] update project
 10 files changed, 377 insertions(+), 9 deletions(-)
 create mode 100644 126login.py
 create mode 100644 baidu.py
 create mode 100644 package/__init__.py
 create mode 100644 package/location.py
 create mode 100644 po_login.py
 delete mode 100644 test_case.py
 create mode 100644 test_case/126login.py
 create mode 100644 test_case/allpage/__init__.py
 create mode 100644 test_case/allpage/base.py
 create mode 100644 test_case/allpage/login_page.py

Company@MININT-IQVJFIT /d/my_test/project-name (master)
$ git push origin master

Warning: Permanently added the RSA host key for IP address '192.30.252.128' to t
he list of known hosts.
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 3.41 KiB | 0 bytes/s, done.
Total 13 (delta 2), reused 0 (delta 0)
To git@github.com:defnngj/project-name.git
   9b4b839..0e8eece  master -> master

“git add” 命令对当前目录下的文件添加跟踪。

“git commit” 命令将添加文件提交到本地仓库。

“git push” 将本地项目提交到远程仓库GitHub。

除第一次下载项目需要通过 “git clone” 将项目克隆到本地外,后续再使用 “git pull” 命令时会直接将更新拉取到本地。

fnngj@FNNGJ-PC /d/project-name
$ git pull origin master

Warning: Permanently added the RSA host key for IP address '192.30.252.131' to t
he list of known hosts.
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 13 (delta 2), reused 13 (delta 2), pack-reused 0
Unpacking objects: 100% (13/13), done.
From github.com:defnngj/project-name
 * branch            master     -> FETCH_HEAD
   9b4b839..0e8eece  master     -> origin/master
Updating 9b4b839..0e8eece
Fast-forward
 126login.py                     |  28 +++++++++++
 baidu.py                        |  18 +++++++
 package/__init__.py             |   0
 package/location.py             |  81 +++++++++++++++++++++++++++++++
 po_login.py                     | 103 ++++++++++++++++++++++++++++++++++++++++
 test_case.py                    |   9 ----
 test_case/126login.py           |  44 +++++++++++++++++
 test_case/allpage/__init__.py   |   0
 test_case/allpage/base.py       |  47 ++++++++++++++++++
 test_case/allpage/login_page.py |  56 ++++++++++++++++++++++
 10 files changed, 377 insertions(+), 9 deletions(-)
 create mode 100644 126login.py
 create mode 100644 baidu.py
 create mode 100644 package/__init__.py
 create mode 100644 package/location.py
 create mode 100644 po_login.py
 delete mode 100644 test_case.py
 create mode 100644 test_case/126login.py
 create mode 100644 test_case/allpage/__init__.py
 create mode 100644 test_case/allpage/base.py
 create mode 100644 test_case/allpage/login_page.py


提示: 为了避免冲突我们应该形成良好的习惯,在每次 push 代码之前先把服务器上最新的代码 pull 到本地。

0

相关课程

CI/CD工具: Jenkins基础教程
图文
CI/CD工具: Jenkins基础教程

课程分类: 持续集成(CI/CD)

入门jenkins必备

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 10:04:15

免费

查看详情