Python第十二周學習總結

Git的使用

一、Git

1、git的由來

圖片1

2、安裝

  • 下載對應操作系統的Git客戶端版本
  • Linux上安裝,yum install git
  • Windows 上面安裝 git –version ?查看版本號
  • 3、概念圖片2

    遠程倉庫 ??????????????????????????本地倉庫 ?????????????????????????工作區

    Repository倉庫、版本號

    git 初始化后,會在當前目錄生成一個.git目錄,這就版本庫。

    Workspace 工作空間,工作區。

    .git 所在的目錄就是工作區,一般是項目的根目錄。

     

    Index 索引

    介于工作區和版本庫之間,暫存修改的

     

    Remote遠程版本庫

    網絡上的另一個版本庫,可以和本地庫交互。

  • 4、使用

    1)初始化一個版本庫

    #### git init 初始化。

    在目錄中增加一個.git目錄,不能自行修改這個目錄里面的文件,

    這個目錄一般是根目錄。

    2)添加文件

    利用vim創建文件并增加文件內容。

    單個添加文件:git add index.htm

    這一步是把文件的當前變化增加到索引中,也就是以后這個文件需要版本庫來跟蹤管理,不是提交。此時文件還可以繼續修改,還可以添加新的被跟蹤文件,一定要add才把這個改變加入索引中。

    批量增加文件:git ?add .

    .號,代表當前目錄,這條命令就地柜添加到當前目錄及其子目錄的所有文件。

    只要是目錄,就會遞歸添加到該目錄下的文件和子目錄。

    3)查看狀態: git status

  • 圖片3
  • 4)git文件的分類

    追蹤的Tracked,已經加入到版本庫的文件。

    為追蹤的UNtracked,未加入到版本庫的文件。

    忽略的ignored,git不在關注的文件,例如一些臨時文件。

    .gitignore文件中,目錄以/結尾,行起始的!是取反。

    .gitignore 內容如下:*.ipynb ??__pycache__/ ??.*

    忽略的文件不需要自己寫,Python的已經有了。

    5)提交代碼 ??git commit -m first commit

    Commit 提交更改到版本庫。 -m填寫本次日志消息,必須寫,工作中,需要注明每一次提交都做了什么修改。

    更改文件后再次提交:顯示的是modified: ??index.htm

    git commit -m “2 No2 commit”

     

    6)git 的提交

    Git的提交,分為兩個步驟。

    暫存變更:add的作用是把新文件或者新的文件的改動添加到一個暫存區stage,也就是加入到index中。

    提交變更:commit提交的是暫存區中的改動,而不是物理文件目前的改動,提交到當前分支,默認是master分支。

     

    兩步合為一步:git commint -a -m

    改動一批文件,一個個名詞很麻煩,使用git commit -a

    -a ,–all 會把所有跟蹤的文件改為自動暫存,然后commit。

     

    7)增補

    增加一個文件,

    touch about.htm

    git commit –amend

     

    -amend 修改

    git log 查看版本庫里面提交的歷史記錄。

     

    8)diff比較

    git diff 工作區和暫存區

    git diff HEAD 工作區和本地倉庫,比較工作區和上一次commit的差異。HEAD指代最后一的commit。

    git diff –cached 跟蹤文件的暫存修改,比較暫存區和上一次commit的差異。

     

    9)HEAD

    HEAD,指代最后一次commit

    HEAD可以看做是一個游標,指向當前分支最后一次提交。

    HEAD^上一次。

    上n次,表示為HEAD~n

     

    10)檢出和重置

    命令 說明
    Git checkout 列出暫存區
    Git checkout file 從暫存區檢出文件到工作區,就是覆蓋工作區文件,可指定檢出的文件,但是不清楚stage
    Git checkout commit file 檢出某個commit的指定文件到暫存區和工作區
    Git checkout 檢出暫存區的所有文件到工作區

     

    Checkout用于切換分支,或恢復工作區文件。

    checkout會重寫工作區,這個命令還是比較危險的。

     

    [root@localhost ~]# echo >about.htm ??清空文件

    [root@localhost ~]# git checkout about.htm ?從暫存區檢出到工作區

    [root@localhost ~]# git checkout HEAD about.htm ?從最后一次commit檢出覆蓋暫存區和工作區

    [root@localhost ~]# git diff

    [root@localhost ~]# git diff –cached

    [root@localhost ~]# git diff HEAD

    [root@localhost ~]# ??????????????三條命令顯示結果一致了

     

    命令 說明
    Git reset 列出被reset的文件
    Git reset file 重置文件的暫存區,和上一次commit一致,工作區不影響
    Git ?reset-hard 重置暫存區與工作區,與上一次commit保持一致

    [root@localhost ~]# echo “welcome about<html>” > about.htm ??文件更改內容

    [root@localhost ~]# git add about.htm ??添加到暫存區

    [root@localhost ~]# git reset about.htm ?使用最后一次提交覆蓋暫存區

    Unstaged changes after reset:

    M about.htm

    [root@localhost ~]# cat about.htm 內容不變

    welcome about<html>

    [root@localhost ~]# git add about.htm

    [root@localhost ~]# git reset –hard 重置暫存區與工作區為上一次commit

    HEAD is now at 1ed76cf No3 commit

    [root@localhost ~]# cat about.htm 工作區內容恢復未改變之前。

    welcome about

    命令 說明
    Git reflog 顯示commit信息,只要是HEAD發生變化,就可以看到
    Git reset commit 重置當前分支的HEAD為指定commit,同時重置暫存區,但是工作區不變
    Git reset -hard [commit] 重置當前分支的HEAD為指定commit,同時重置暫存區和工作區,與指定commit
    Git reset –keep [commit] 重置當前HEAD為指定commit,但保持暫存區和工作區不變

    重置的時候,使用hash值只要能唯一確定一個commit就可以了。

    reset操作 ?要慎重。

     

     

     

     

    11)移動和刪除

    Git mv src dest 改名,直接把改名的改動放入暫存區。

    Git rm file 會同時在版本庫和工作目錄中刪除文件。

    Git rm -cached file 將文件從暫存區轉成未暫存,從版本庫中刪除,但不能刪除工作目錄的該文件,即文件恢復不追蹤狀態。 ?都是算是改動,commit才算是真改動。

    ###創建一個新的文件

    [root@localhost ~]# echo “python” > python.htm

    [root@localhost ~]# git add python.htm

    [root@localhost ~]# git commit -m “add python”

    [master 50e12c9] add python

    Committer: root <root@localhost.localdomain>

    Your name and email address were configured automatically based

    on your username and hostname. Please check that they are accurate.

    You can suppress this message by setting them explicitly:

     

    git config –global user.name “Your Name”

    git config –global user.email you@example.com

     

    If the identity used for this commit is wrong, you can fix it with:

     

    git commit –amend –author=’Your Name <you@example.com>’

     

    1 files changed, 1 insertions(+), 0 deletions(-)

    create mode 100644 python.htm

    [root@localhost ~]# git log

    commit 50e12c9e4dd5928dbaf14b647bccd954293ff5e6

    Author: root <root@localhost.localdomain>

    Date: ??Tue May 22 09:21:17 2018 +0800

     

    add python

     

    commit 1ed76cf5c048bfeef64916ffa4d66abdc81fd55f

    Author: root <root@localhost.localdomain>

    Date: ??Tue May 22 08:58:00 2018 +0800

     

    No3 commit

     

    commit ab76fd902da7168e69b2ca1152373c9c571e7045

    Author: root <root@localhost.localdomain>

    Date: ??Mon May 21 20:11:18 2018 +0800

     

    2 No2 commit

     

    commit 584479710780dc2069751636c1d27f2233abfc4b

    Author: root <root@localhost.localdomain>

    Date: ??Mon May 21 20:05:51 2018 +0800

     

    1 No1 commit

     

    ###2mv

    [root@localhost ~]# git mv python.htm python.py

    [root@localhost ~]# git commit -m “my python”

    [master 30cd6ee] my python

    Committer: root <root@localhost.localdomain>

    Your name and email address were configured automatically based

    on your username and hostname. Please check that they are accurate.

    You can suppress this message by setting them explicitly:

     

    git config –global user.name “Your Name”

    git config –global user.email you@example.com

     

    If the identity used for this commit is wrong, you can fix it with:

     

    git commit –amend –author=’Your Name <you@example.com>’

     

    1 files changed, 0 insertions(+), 0 deletions(-)

    rename python.htm => python.py (100%)

     

    ###3rm

    rename python.htm => python.py (100%)

    [root@localhost ~]# echo “print(“hello python”)” > python.py

    [root@localhost ~]# git add python.py

    [root@localhost ~]# git diff -cached

    error: invalid option: -cached

    [root@localhost ~]# git diff –cached

    diff –git a/python.py b/python.py

    index fdc793e..a280d44 100644

    — a/python.py

    +++ b/python.py

    @@ -1 +1 @@

    -python

    +print(hello python)

    [root@localhost ~]# git rm –cached python.py

    rm ‘python.py’

    [root@localhost ~]# git diff –cached

    diff –git a/python.py b/python.py

    deleted file mode 100644

    index fdc793e..0000000

    — a/python.py

    +++ /dev/null

    @@ -1 +0,0 @@

    -python

    [root@localhost ~]# git commit -m “delete python”

    [master 501fec3] delete python

    Committer: root <root@localhost.localdomain>

    Your name and email address were configured automatically based

    on your username and hostname. Please check that they are accurate.

    You can suppress this message by setting them explicitly:

     

    git config –global user.name “Your Name”

    git config –global user.email you@example.com

     

    If the identity used for this commit is wrong, you can fix it with:

     

    git commit –amend –author=’Your Name <you@example.com>’

     

    1 files changed, 0 insertions(+), 1 deletions(-)

    delete mode 100644 python.py

    [root@localhost ~]# ls

    about.htm

    anaconda-ks.cfg

    Desktop

    Documents

    Downloads

    git-2.16.3-intel-universal-mavericks.dmg

    gogs.script

    index.htm

    install.log

    install.log.syslog

    Music

    Percona-Server-5.5.45-37.4-r042e02b-el6-x86_64-bundle.tar

    Percona-Server-55-debuginfo-5.5.45-rel37.4.el6.x86_64.rpm

    Percona-Server-client-55-5.5.45-rel37.4.el6.x86_64.rpm

    Percona-Server-devel-55-5.5.45-rel37.4.el6.x86_64.rpm

    Percona-Server-server-55-5.5.45-rel37.4.el6.x86_64.rpm

    Percona-Server-shared-55-5.5.45-rel37.4.el6.x86_64.rpm

    Percona-Server-test-55-5.5.45-rel37.4.el6.x86_64.rpm

    Pictures

    Public

    python.py

    Templates

    Videos

    [root@localhost ~]# git status

    # On branch master

    # Untracked files:

    # ??(use “git add <file>…” to include in what will be committed)

    #

    # .ICEauthority

    # .Xauthority

    # .abrt/

    # .bash_history

    # .bash_logout

    # .bash_profile

    # .bashrc

    # .cache/

    # .config/

    # .cshrc

    # .dbus/

    # .esd_auth

    # .gconf/

    # .gnome2/

    # .gnote/

    # .gnupg/

    # .gtk-bookmarks

    # .imsettings.log

    # .local/

    # .mysql_history

    # .pulse-cookie

    # .pulse/

    # .tcshrc

    # .viminfo

    # Percona-Server-5.5.45-37.4-r042e02b-el6-x86_64-bundle.tar

    # Percona-Server-55-debuginfo-5.5.45-rel37.4.el6.x86_64.rpm

    # Percona-Server-client-55-5.5.45-rel37.4.el6.x86_64.rpm

    # Percona-Server-devel-55-5.5.45-rel37.4.el6.x86_64.rpm

    # Percona-Server-server-55-5.5.45-rel37.4.el6.x86_64.rpm

    # Percona-Server-shared-55-5.5.45-rel37.4.el6.x86_64.rpm

    # Percona-Server-test-55-5.5.45-rel37.4.el6.x86_64.rpm

    # anaconda-ks.cfg

    # git-2.16.3-intel-universal-mavericks.dmg

    # gogs.script

    # install.log

    # install.log.syslog

    # python.py

    nothing added to commit but untracked files present (use “git add” to track)

    [root@localhost ~]#

     

    12)push到服務器

    本地搭建私服,模仿GitHub

    配置本地用戶名和郵箱:

    [root@localhost ~]# git config –global user.name “myself”

    [root@localhost ~]# git config –global user.email “myself@qq.com”

    [root@localhost ~]#

    [root@localhost ~]# cat ~/.gitconfig

    [user]

    name = myself

    email = myself@qq.com

     

     

    關聯遠程版本庫:

    [root@localhost ~]# git remote add origin http://192.168.142.128:3000/myself/test.git

    [root@localhost ~]# cat ~/.gitconfig

    [user]

    name = myself

    email = myself@qq.com

    [root@localhost ~]# cat .git/config

    [core]

    repositoryformatversion = 0

    filemode = true

    bare = false

    logallrefupdates = true

    [remote “origin”]

    url = http://192.168.142.128:3000/myself/test.git

    fetch = +refs/heads/*:refs/remotes/origin/*

    遠程版本庫名origin,這是一個習慣用法,將建立origin和后面的URL映射,這些信息都保存在.git/config 文件中的[remote “origin”]中。

     

    推送數據:

    [root@localhost ~]# git push -u origin master

    error: The requested URL returned error: 401 Unauthorized while accessing http://192.168.142.128:3000/myself/test.git/info/refs

     

    fatal: HTTP request failed

    通訊失敗解決辦法:vi .git/config

    里面url里面加入一個用戶名。

    [core]

    repositoryformatversion = 0

    filemode = true

    bare = false

    logallrefupdates = true

    [remote “origin”]

    url = http://myself@192.168.142.128:3000/myself/test.git

    fetch = +refs/heads/*:refs/remotes/origin/*

    [branch “master”]

    remote = origin

    merge = refs/heads/master

     

     

    [root@localhost ~]# git push -u origin master

    Xlib: ?extension “RANDR” missing on display “localhost:10.0”.

    error: The requested URL returned error: 401 while accessing http://myself@192.168.142.128:3000/myself/test.git/info/refs

     

    fatal: HTTP request failed

    [root@localhost ~]# git push -u origin master

    Xlib: ?extension “RANDR” missing on display “localhost:10.0”.

    Counting objects: 16, done.

    Delta compression using up to 2 threads.

    Compressing objects: 100% (10/10), done.

    Writing objects: 100% (16/16), 1.21 KiB, done.

    Total 16 (delta 3), reused 0 (delta 0)

    To http://myself@192.168.142.128:3000/myself/test.git

    * [new branch] ?????master -> master

    Branch master set up to track remote branch master from origin.

     

    輸入密碼就能連接到遠程倉庫了。私有的倉庫必須登錄,只能用戶自己看。

    -u 第一次推送的時候加上,以后就不需要-u參數,可以使用git origin master或者git push就可以了。

     

     

     

    從命令行創建一個新的倉庫

    touch README.md

    git init

    git add README.md

    git commit -m “first commit”

    git remote add origin http://192.168.142.128:3000/myself/test.git

    git push -u origin master

    從命令行推送已經創建的倉庫

    git remote add origin http://192.168.142.128:3000/myself/test.git

    git push -u origin master

     

     

    13)從遠程庫克隆

     

    創建公鑰和私鑰

    git config –global user.name “myself”

    git config –global user.email “myself@qq.com”

    $ cat ~/.gitconfig?查看添加的信息。

     

    [root@localhost ~]# ssh-keygen -t rsa -C “myself”

    Generating public/private rsa key pair.

    Enter file in which to save the key (/root/.ssh/id_rsa):

    Enter passphrase (empty for no passphrase):

    Enter same passphrase again:

    Your identification has been saved in /root/.ssh/id_rsa.

    Your public key has been saved in /root/.ssh/id_rsa.pub.

    The key fingerprint is:

    56:2a:7f:69:70:38:cf:32:75:cd:c7:ad:d3:4d:22:d3 myself

    The key’s randomart image is:

    +–[ RSA 2048]—-+

    | ????????????????|

    | ????????????????|

    | ?????????. ?????|

    | ????????+ ??+ ..|

    | ?????. S o + E =|

    | ??????+ B o o *.|

    | ???????+ * ??o o|

    | ????????= ????. |

    | ????????????????|

    +—————–+

    [root@localhost ~]# ls .ssh

    id_rsa ?id_rsa.pub

     

    Windows 下:

    git config –global user.name “myself”

    git config –global user.email “myself@qq.com”

    $ cat ~/.gitconfig?查看添加的信息。

    WCL@Lenovo-PC MINGW64 /c?(master)

    $ ssh-keygen -t rsa -C “myself@qq.com”

    Generating public/private rsa key pair.

    Enter file in which to save the key (/c/Users/WCL/.ssh/id_rsa):

    Created directory ‘/c/Users/WCL/.ssh’.

    Enter passphrase (empty for no passphrase):

    Enter same passphrase again:

    Your identification has been saved in /c/Users/WCL/.ssh/id_rsa.

    Your public key has been saved in /c/Users/WCL/.ssh/id_rsa.pub.

    The key fingerprint is:

    SHA256:95Fv6r5DOyTl8h5Bh7luOhYOZnrmxUf0RmSxRNuiNSQ myself@qq.com

    The key’s randomart image is:

    +—[RSA 2048]—-+

    | ???????????E.B. |

    | ????????????O + |

    | ???????????= O .|

    | ??????????o.O o |

    | ???????S .oB o ?|

    | ???????+o+++= ??|

    | ??????+ ooB*.o ?|

    | ?????. o.++=+ ??|

    | ??????+…=*+ ??|

    +—-[SHA256]—–+

     

     

    WCL@Lenovo-PC MINGW64 /c?(master)

    $ cd ~/.ssh

     

    WCL@Lenovo-PC MINGW64 ~/.ssh?(master)

    $ ls

    id_rsa ?id_rsa.pub

     

    $ cat id_rsa.pub

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz2m7NkCWNxnFhsqxM2yJBnyKmzJAF1tY8T2Wwnqg+5aXZfH1JC7uYncXQY+yFomCDWlSGUeC6A9Ca5bufFa/zNTwKSj/VfM7IdFQm5/l9PO+cYsT+jyDnzrnWRPE0w1E+No4YCeASlMh19ywAcjCmlAKZNBQHiyGagcb3Xdi4whoM4fMLyuK/7oCZwJyBlAKySCXO946wnNR1U60yDgn7bIAnLWasE1at39u8B4D6A7Vq7CXOAeW9h4rHLwPO1Z0Lq2F8QM2qAfbKL7Jyj3SgLnyVhi2nassPXXypVfG43voW3l/rXQcoAl9D2UIqtmVmRAHOj2rRE16nENHn9N0d myself@qq.com

     

     

    在用戶設置里面的ssh秘鑰里面增加秘鑰:

    圖片4

  • ssh遠程連接庫:

    git@192.168.142.128:myself/test.git

    $ git clone git@192.168.142.128:myself/test.git

    Cloning into ‘test’…

    The authenticity of host ‘192.168.142.128 (192.168.142.128)’ can’t be established.

    RSA key fingerprint is SHA256:4baHBvpJvWzXDTKFpbpih5hmcsUhExyKXJroD9PCVAU.

    Are you sure you want to continue connecting (yes/no)? yes

    Warning: Permanently added ‘192.168.142.128’ (RSA) to the list of known hosts.

    remote: Counting objects: 16, done.

    remote: Compressing objects: 100% (10/10), done.

    remote: Total 16 (delta 3), reused 0 (delta 0)

    Receiving objects: 100% (16/16), done.

    Resolving deltas: 100% (3/3), done.

    克隆成功

     

    5、pycharm中使用git

    Gib私服創建cmdb項目版本庫。

    創建一個Python的倉庫:

    或者ssh的地址:git@192.168.142.128:myself/cmdb.git

    增加秘鑰:

     

    從版本控制工具中獲取項目,選擇git。

    選擇項目目錄,填寫遠程版本庫地址,test測試一下:

    成功,并直接用pycharm打開項目。圖片5

    6、項目開發

    圖片6

  • 存儲:Stash:應用場景。
    命令說明
    git stash暫存最后一次提交后的變化,放入棧中
    git stash pop從棧中取出剛才保存的變化,并合并。

    利用stash暫存。

    圖片7

  • 恢復以前的代碼。圖片8圖片9

    應用場景:

    需要緊急中斷當前的工作完成其他的工作。

    開發中,當前手中的工作未完成,需要中斷當前工作來完成其他請求,例如修復bug等。

    已完成的工作內容提交不合適,需要大的調整,緊急請求也得做,就需要stash。存儲未完成的工作。

    7、分支

    多人協作完成,項目中不同的獨立的功能,這些功能能需要好幾天才能完成,定制版本,往往需要一個不同的定制需求。

    代碼中,至少有一個分支,就是主干分支。

    圖片10

     

    圖中節點表示每一次提交。

    項目往往是并行多人開發的,都在主分支上克隆,然后修改叫,那么主分支就存在大量沖突,甚至不完善的代碼提交。

     

    引入分支

    分支名:

    分支名在版本庫中必須唯一。

    不能以 – 號開頭。

    可以使用/,但是不能以其結尾,被他分隔的名稱不能以.開頭。

    不能使用兩個連續的..

    不能包含任何空白字符,git的特殊符號。

    圖片11

  • 創建分支

    需要知名從什么分支上創建什么名字的分支。

    圖片12圖片13

  • 分支合并:
  • 圖片14圖片15圖片16圖片17
  • 目前的合并,只是本地庫內的,需要push到遠程庫里面去。

    可以繼續檢索到dev分支,開發完成后繼續合并。

    Fast Forward

  • 圖片18

8、GitFlow工作流:

不同公司,不同的項目規模,不同的管理水平都有著不同的git工作流方式。

使用git一般至少兩個分支:master和develop

 

Master 主分支 ?穩定的代碼。

Develop開發分支

輔助分支

Feature 分支 ?具體功能開發分支

Release 分支

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/99565

(0)
604603701@qq.com604603701@qq.com
上一篇 2018-05-27
下一篇 2018-05-27

相關推薦

  • Python面向對象基礎

    語言分類 面向機器 抽象成機器指令,讓機器容易理解 代表:匯編語言 面向過程 按照步驟一步一步走,若出現情況A做相應的處理,若出現情況B做相應的處理 問題規模小,可以步驟化,按部就班處理 代表:C 面向對象OOP 計算機需要處理的問題的規模越來越大,需要多人、多部門協作 代表:C++、Java、Python 面向對象 一種認識世界、分析世界的方法論。將萬事萬…

    2018-05-06
  • 高階函數和裝飾器

    高階函數和裝飾器 高階函數 : 滿足以下條件之一的稱為高階函數 接受一個或多個函數作為參數 輸出一個函數 高階函數舉例: def counter(base): def inc(step=1): nonlocal base base += step return base return inc 1)自定義sort函數 def sort(itertable): …

    Python筆記 2018-04-23
  • Python 部分知識點總結(一)

    此篇博客只是記錄第三周未掌握或不熟悉的知識點,用來加深印象。

    Python筆記 2018-03-26
  • 文件操作

    文件操作 馮諾依曼體系架構 CPU由運算器和控制器組成 運算器,完成各種算數的運算,邏輯運算,數據傳輸等數據加工處理 控制器,控制程序的執行 存儲器,用于記憶程序的數據,列如內存 輸入設備,將數據或者程序輸入到計算機中列如鍵盤 鼠標 輸出設備,將數據或者程序的處理結果展示給用戶,列如顯示器,打印機等等 ? 一般說的IO操作,指的是文件的IO,如果是指網絡的I…

    Python筆記 2018-05-02
  • Python第十四周網絡知識和數據庫知識總結

    高性能集群Linux virtual server、Nginx、高可用性集群keepalived

    Python筆記 2018-06-10
欧美性久久久久