GIT

git簡介html

  • linus用c語言編寫
  • 2005年誕生
  • 分佈式版本管理系統
  • 速度快,適合大規模,跨地區多人協同開發

git生態python

  • git 分佈式版本管理系統
  • gitlab git私庫解決方案
  • github git共有庫解決方案

git安裝git

  • 不建議用yum install git,安裝的是1.8版本,生產環境2.7以上
  • yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker epel-release
  • wget https://github.com/git/git/archive/v2.7.4.zip
  • unzip v2.7.4.zip
  • make prefix=/usr/local/git all
  • make prefix=/usr/local/git install
  • rm -fr /usr/bin/git
  • ln -s /usr/local/git/bin/git /usr/bin/git
  • git --version

git配置github

  • 設置與配置 git config
  • 幫助命令 git help
  • 初始化
  • [root@localhost test]# git init 該目錄受版本庫控制
  • [root@localhost test]# git config --global user.name "joker" 誰操做
  • [root@localhost test]# git config --global user.email "joker@126.com" 操做者郵箱
  • [root@localhost test]# git config --list
  • user.name=joker
  • user.email=joker@126.com
  • core.repositoryformatversion=0
  • core.filemode=true
  • core.bare=false
  • core.logallrefupdates=true
  • 獲取
  • git clone http://xxx.git

四個區域緩存

  • 給咱們的直覺,本地倉庫,暫存區域,工做目錄就是咱們初始化的目錄

四種狀態curl

  • 工做目錄新放入的文件叫作爲追蹤的文件untracked(這個新文件只能說是放到文件裏面了,和git庫沒有任何關聯)
  • git add 直接推送暫存區域staged
  • commit 存成一個版本,變成未被修改的狀態unmodified,放到了本地倉庫
  • 經過修改這個文件 就會從未被修改的狀態變成修改的狀態Modifed,那修改的文件會從本地倉庫從新拉回到工做目錄裏面來
  • git add 放到了暫存區域staged
  • commit 存成一個版本,放到本地倉庫裏面

經常使用命令分佈式

  • git add               加入暫存(索引區)
  • git status                查看狀態
  • git status -s            狀態概覽
  • git diff                    還沒有暫存的文件
  • git diff --staged      暫存區文件
  • git commit -m ""      提交更新
  • git reset                  回滾
  • git rm                     從版本庫中移除
  • git rm --cached README 從暫存區中移除
  • git mv                     至關於 git rm git add三個命令
  • [root@localhost test]# echo "pay" > pay.html
    [root@localhost test]# echo "center" > news.html
    [root@localhost test]# git add pay.html news.html 
    [root@localhost test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            new file:   news.html
            new file:   pay.html
    
    [root@localhost test]# git rm --cached pay.html 
    rm 'pay.html'
    [root@localhost test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            new file:   news.html
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            pay.html
     

分支管理gitlab

  • 每次切換分支,也就是指針變更的時候

分支命令fetch

  • git branch
  • git branch –v
  • git branch –merged
  • git branch --no-merged
  • git branch -b testing
  • git checkout
  • git merge              合併,注意指針,在master主分支合併分支
  • git log
  • git stash
  • git tag

git高級this

git checkout命令

 

  • git checkout 用於切換分支
  • git checkout -- file.ext 撤銷對文件的修改,從本地庫裏面拉出來覆蓋到工做目錄
  • [root@localhost test]# echo "222" >> index.html 
    [root@localhost test]# git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@localhost test]# git checkout -- index.html 
    [root@localhost test]# git status
    On branch master
    nothing to commit, working directory clean
    [root@localhost test]# cat index.html 
    joker  

git reset命令

意義在於,區域用不用保留

  • --soft 緩存區和工做目錄都不會被改變
  • --mixed 默認選項。緩存區和你指定的提交同步,但工做目錄不受影響
  • --hard 緩存區和工做目錄都同步到你指定的提交
  • 工做目錄的回滾
    git checkout -- file.ext 撤銷對文件的修改
    暫存區的回滾
    git rm --cached 撤回緩存區的文件

  • [root@localhost test]# echo 333 >> index.html 
    [root@localhost test]# echo 333 >> news.html 
    [root@localhost test]# git add news.html 
    [root@localhost test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   news.html
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   index.html
    
    [root@localhost test]# git log
    commit c203cce4db08d7787c48cf6fe7f5edda50b7c906
    Merge: fe7194c 885b5d3
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 22:24:56 2018 -0500
    
        Merge branch 'about'
    
    commit 885b5d3dedfc59346535588685f4f41464f85594
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 22:13:14 2018 -0500
    
        about
    
    commit fe7194ca1d692cf55316481b2d34b3de2c354b9e
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 22:05:06 2018 -0500
    
        pay
    
    commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:06:55 2018 -0500
    
        news
    
    commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:01:32 2018 -0500
    
        first commit
    [root@localhost test]# git reset --hard c415b7b(回滾到哪一次提交)
    HEAD is now at c415b7b news
    [root@localhost test]# git status
    On branch master
    nothing to commit, working directory clean
    [root@localhost test]# git log
    commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:06:55 2018 -0500
    
        news
    
    commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:01:32 2018 -0500
    
        first commit

文件層操做

 

  • 運行git reset HEAD foo.py 會將當前的foo.py從緩存區中移除出去,而不會影響工做目錄中對foo.py的更改。
  • [root@localhost test]# echo "222" > pay.html
  • [root@localhost test]# git add pay.html
  • [root@localhost test]# git status
  • On branch master
  • Changes to be committed:
  • (use "git reset HEAD <file>..." to unstage)
  • new file: pay.html
  • [root@localhost test]# git reset pay.html

git reflog

  • 分析你全部分支的頭指針的日誌來查找出你在重寫歷史上可能丟失的提交
  • [root@localhost test]# git reflog
    c415b7b HEAD@{0}: reset: moving to c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    885b5d3 HEAD@{1}: merge res: Fast-forward
    c415b7b HEAD@{2}: checkout: moving from res to master
    885b5d3 HEAD@{3}: checkout: moving from 885b5d3dedfc59346535588685f4f41464f85594 to res
    885b5d3 HEAD@{4}: checkout: moving from master to 885b5d3
    c415b7b HEAD@{5}: checkout: moving from reset to master
    c415b7b HEAD@{6}: checkout: moving from master to reset
    c415b7b HEAD@{7}: checkout: moving from 885b5d3dedfc59346535588685f4f41464f85594 to master
    885b5d3 HEAD@{8}: checkout: moving from master to 885b5d3
    c415b7b HEAD@{9}: reset: moving to c415b7b
    c203cce HEAD@{10}: merge about: Merge made by the 'recursive' strategy.
    fe7194c HEAD@{11}: checkout: moving from about to master
    885b5d3 HEAD@{12}: checkout: moving from master to about
    fe7194c HEAD@{13}: checkout: moving from about to master
    885b5d3 HEAD@{14}: commit: about
    c415b7b HEAD@{15}: checkout: moving from master to about
    fe7194c HEAD@{16}: commit: pay
    c415b7b HEAD@{17}: checkout: moving from about to master
    c415b7b HEAD@{18}: checkout: moving from master to about
    c415b7b HEAD@{19}: commit: news
    8c8152d HEAD@{20}: commit (initial): first commit
    [root@localhost test]# git checkout 885b5d3
    Note: checking out '885b5d3'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
      git checkout -b <new-branch-name>
    
    HEAD is now at 885b5d3... about
    [root@localhost test]# git status
    HEAD detached at 885b5d3
    nothing to commit, working directory clean
    [root@localhost test]# git branch rese
    [root@localhost test]# git checkout rese
    Switched to branch 'rese'
    [root@localhost test]# git log
    commit 885b5d3dedfc59346535588685f4f41464f85594
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 22:13:14 2018 -0500
    
        about
    
    commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:06:55 2018 -0500
    
        news
    
    commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:01:32 2018 -0500
    
        first commit
    [root@localhost test]# git checkout master
    Switched to branch 'master'
    [root@localhost test]# git log
    commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:06:55 2018 -0500
    
        news
    
    commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:01:32 2018 -0500
    
        first commit
    [root@localhost test]# git merge rese
    Updating c415b7b..885b5d3
    Fast-forward
     about.html | 1 +
     1 file changed, 1 insertion(+)
     create mode 100644 about.html
    [root@localhost test]# git log
    commit 885b5d3dedfc59346535588685f4f41464f85594
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 22:13:14 2018 -0500
    
        about
    
    commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:06:55 2018 -0500
    
        news
    
    commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
    Author: joker <joker@126.com>
    Date:   Mon Dec 17 10:01:32 2018 -0500
    
        first commit  

遠程倉庫

遠程命令

  • git clone https://github.com/jokerbj/cmdb.git
  • git pull                         拉下來,合併
  • git fetch                       拉下來,手動合併
  • git push origin master    上傳
  • git remote                     查看遠程分支
  • git remote –v                查看遠程地址
  • git remote add origin http://xxx  添加遠程地址
  • git remote show origin
  • git remote rename pb paul 變動遠程地址名字
  • git tag -a v1.0 -m ‘abc’    

標籤管理

  • git tag -a v1.4 -m 'my version 1.4‘
  • git show v1.4 git tag -a v1.2 9fceb02 對歷史打標籤
  • git push origin v1.5 將標籤推向遠程
  • git push origin --tags 推送多個標籤
  • git checkout -b version2 v2.0.0 檢出標籤
相關文章
相關標籤/搜索