Linus Torvalds在1991年建立了開源系統Linux,在2002年之前,世界各地的志願者把源代碼文件經過diff的方式發給Linus,而後由Linus本人經過手工方式合併代碼!而後,Linux系統已經發展了十年了,代碼庫之大讓Linus很難繼續經過手工方式管理了。php
因而,Linus Torvalds花了兩週時間本身用C寫了一個分佈式版本控制系統,這就是Git!一個月以內,Linux系統的源碼已經由Git管理了!css
Git是一個開源的分佈式版本控制系統,用以有效、高速地處理從很小到很是大的項目版本管理。相比CVS、SVN等版本控制工具,Git無疑更加優秀,功能更增強大,在項目版本管理中被愈來愈多的人普遍的使用。但 Git 相對來講比較難學。html
使用Git來管理項目有兩種方式:python
一種是本地部署Git版本管理系統。git
另外一種是經過在線代碼託管。github
本地部署Git版本管理系統,須要本身來搭建環境,但項目的提交與更新速度快,更適合較爲封閉的項目;使用在線託管最大的好處是在有網絡的狀況下能夠隨時隨地提交本身的代碼,但項目是公開的,固然也能夠建立私有項目,大多屬於付費服務。shell
GitHub就是基於Git的在線代碼託管平臺。編程
GitHub爲當前最流行的開源項目託管平臺,數以萬計優秀的開源項目被託管在GitHub上面。隨着愈來愈多的應用程序轉移到雲上,Github已經成爲了管理軟件開發以及發現已有代碼的首選方法。對於普通開發人員來說,其在GitHub上託管的項目已經成爲了他的一張我的名片。許多優秀的互聯網公司在招聘開發人員時都但願對方能提供我的的GitHub地址。固然,GitHub做爲一個開源軟件的大寶庫,學習和使用它,也會爲咱們的平常開發帶來許多便利。瀏覽器
咱們工做中須要對項目代碼進行管理,個人工做中就前後使用了SVN和Git對項目的版本進行管理。因此,這是測試人員必備技能。安全
你工做中所使用的編程語言的第三方庫,大量都在GitHub上面託管,要想安裝這些庫就會用到Git;另一些主流的Selenium、Appium開源項目也都是在GitHub上進行維護。
將你本身工做中的一些項目和代碼放到GitHub上面進行管理。這無疑是對我的能力最好的展現。
GitHub官方地址:https://github.com。
在瀏覽器中打開GitHub網址,經過首頁進行註冊,以下圖所示。
Git官方下載地址:http://git-scm.com/download/。
Git支持多平臺(Mac OS X/Windows/Linux/Solaris),讀者可根據本身的平臺選擇相應的版本下載。
Linux 各版本下安裝Git:
Debian/Ubuntu $ apt-get install git-core Fedora $ yum install git Gentoo $ emerge --ask --verbose dev-vcs/git Arch Linux $ pacman -S git
下載並安裝完成後,咱們一般在Mac OSX及Linux平臺下用終端工具(Terminal)來使用Git,而在Windows平臺下用Git Bash工具,以下圖所示。
本地Git與GitHub服務器之間保持通訊時,咱們使用SSH key認證方式來保證通訊安全,因此在使用GitHub前你必須先建創自已的SSH key。 咱們後續操做以Windows爲例,打開Git Bash,以下圖所示。
ssh建立方法:https://jingyan.baidu.com/article/09ea3ede7026ddc0afde3942.html
(1)進入 SSH 目錄。
fnngj@FNNGJ-PC ~ $ cd ~/.ssh fnngj@FNNGJ-PC ~/.ssh $ pwd /c/Users/fnngj/.ssh
(2)生成新的 SSH 祕鑰。
若是你已經有了一個祕鑰(默認祕鑰位置~/.ssh/id_rsa文件存在。)
fnngj@FNNGJ-PC ~/.ssh $ ssh-keygen -t rsa -C "fnngj@126.com" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/fnngj/.ssh/id_rsa): --回車 Enter passphrase (empty for no passphrase): --回車 Enter same passphrase again: --回車 Your identification has been saved in /c/Users/fnngj/.ssh/id_rsa. Your public key has been saved in /c/Users/fnngj/.ssh/id_rsa.pub. The key fingerprint is: 78:51:9b:2c:6c:fb:74:0b:6b:b9:c4:23:8f:5e:10:6b fnngj@126.com The key's randomart image is: +--[ RSA 2048]----+ | . | | . o o | | * + | | o * | | . E o . | | o = = . | | . X . | | B o | | .o o | +-----------------+ fnngj@FNNGJ-PC ~/.ssh $ ls id_rsa id_rsa.pub
查看目錄下會生成兩個問題,id_rsa 是私鑰,id_rsa.pub 是公鑰。記住千萬不要把私鑰文件id_rsa 透露給任何人。
(3)添加 SSH 公鑰到 GitHub。
用文本工具打開公鑰文件 ~/.ssh/id_rsa.pub ,複製裏面的全部內容到剪貼板,以下圖所示。
登陸GitHub,單擊右上角我的頭像→Settings→SSH Keys→Add SSH Keys ,在 Title 文本框中輸入任意字符,在 Key文本框粘貼剛纔複製的公鑰字符串,單擊「Add key」按鈕完成操做,以下圖所示。
(4)測試鏈接。
以上步驟完成後,你就能夠經過如下命令來測試是否能夠鏈接 GitHub服務器了。
fnngj@FNNGJ-PC ~/.ssh $ ssh -T git@github.com The authenticity of host 'github.com (192.30.252.129)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of know n hosts. Hi defnngj! You've successfully authenticated, but GitHub does not provide shell access.
在GitHub選擇並建立一個項目。首先,登陸 GitHub,單擊頁面右上角加號「+」 ,選擇「New repository」 選項。
填寫項目名稱及描述,默認項目爲「Public」,若是想建立「Private」項目,GitHub須要收費。最後單擊「Create repository」完成項目的建立,以下圖所示。
項目名稱爲:project-name
建立完一個項目以後,GitHub會提示咱們如何提交項目到它上面。
Quick setup — if you’ve done this kind of thing before HTTPS https://github.com/defnngj/project-name.git SSH git@github.com:defnngj/project-name.git …or create a new repository on the command line echo # project-name >> README.md git init git add README.md git commit -m "first commit" git remote add origin git@github.com:defnngj/project-name.git git push -u origin master …or push an existing repository from the command line git remote add origin git@github.com:defnngj/project-name.git git push -u origin master …or import code from another repository You can initialize this repository with code from a Subversion, Mercurial, or TFS project.
首先進行初始化配置:設置倉庫人員的用戶名和郵箱地址,這一步必不可少。
fnngj@FNNGJ-PC /d/project-name (master) $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) test_case.py nothing added to commit but untracked files present (use "git add" to track)
在本地建立一個 「project-name」 項目,與GitHub上建立的項目名 保持一致。
爲了練習,咱們在項目下建立一個test_case.py文件。
fnngj@FNNGJ-PC /d/project-name $ ls test_case.py
「ls」 爲Linux命令,用於查看當前目錄下的文件及文件夾。
fnngj@FNNGJ-PC /d/project-name (master) $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) test_case.py nothing added to commit but untracked files present (use "git add" to track)
「git init」 命令用於對當前目錄進行初始化,使當前的project-name目錄交由Git進行管理。
fnngj@FNNGJ-PC /d/project-name (master) $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) test_case.py nothing added to commit but untracked files present (use "git add" to track)
「git status」 命令用於查看當前項目下全部文件的狀態。
status 是指本地的狀態,顯示的是本地工做空間與本地倉庫之間的差別 你新建的文件、修改但沒提交(commit)的文件,都顯示在這裏,提交以後就是空的了 pull/push 是本地倉庫與遠端倉庫的同步操做,up-to-date 說明本地倉庫與遠端倉庫是同步的
咱們能夠看到當前處於master(主)分支,羅列了當前目錄下的文件(test_case.py),而且提示未對當前目錄下的文件進行跟蹤(跟蹤文件增、刪、改的狀態);並告訴咱們能夠經過 「git add」 命令對文件進行跟蹤。
fnngj@FNNGJ-PC /d/project-name (master) $ git add . fnngj@FNNGJ-PC /d/project-name (master) $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test_case.py
「git add」 命令能夠對指定文件添加跟蹤。若是後面跟空格加點號「.」,則表示對當前目錄下的全部文件進行跟蹤。
再次經過 「git status」 命令查看當前Git倉庫的信息。
fnngj@FNNGJ-PC /d/project-name (master) $ git commit -m "first commit file" [master (root-commit) 9b4b839] first commit file 1 file changed, 9 insertions(+) create mode 100644 test_case.py
「git commit」 命令將文件(git add 進行管理的文件)提交到本地倉庫。-m
參數對本次的提交加以描述。通常提交的描述必不可少,從而方便追溯每次提交都作了哪些修改。
準備工做已經完成,下面提交代碼到GitHub。這裏GitHub提供了兩種連接方式:HTTPS和SSH,提交的地址有所不一樣,請查看前面GitHub提示信息。
fnngj@FNNGJ-PC /d/project-name (master) $ git remote add origin git@github.com:defnngj/project-name.git fnngj@FNNGJ-PC /d/project-name (master) $ git push -u 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: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 354 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:defnngj/project-name.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
若是是第一次提交項目,這一句很是重要,它會將本地的項目與遠程的倉庫之間創建鏈接。這裏選擇SSH協議方式進行鏈接。
將本地的項目提交到遠程倉庫中。
如今訪問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
Git官方文檔:https://git-scm.com/book/zh/v2
Git廖雪峯文檔:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
git
實際上是是一個不用網絡的倉庫(本地倉庫),你也能夠把數據push
到github
上(遠程倉庫)。
你如今的pull
和push
都是本地版本庫和遠程倉庫之間的數據交互。
在你的本地倉庫,實際上是由兩部分組成:
(Working Directory)
//看得見的(Repository)
//看不見的
(Stage)
(branch)
版本庫包含暫存區和分支
流程:
初次提交:
- 經過git add
將文件 工做區 ---》暫存區 (本地)
- 經過git commit
將文件 暫存區 ---》分支 (本地)
- 經過git push
將文件 分支 ---》遠程庫 (github
)
提交改動:
- 經過git commit
將文件 暫存區 ---》分支 (本地)
- 經過git push
將文件 分支 ---》遠程庫 (github
)
pull
&push
- 經過git pull
將文件 遠程庫 ---》分支 (本地)
- 經過git push
將文件 分支 ---》遠程庫 (github
)
而上面的兩個操做是須要有改動,有差別才能執行。
因此會提示暫存區和遠程庫的內容一致。