Git克隆與更新代碼


克隆項目

我們除了可以向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

$  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 到本地。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM