Git學習1

git

1.基礎概念

  1. 工做區(Working Directory)github

    就是你在電腦裏能看到的目錄,git init所在的目錄。緩存

  2. 版本庫(Repository)安全

    工做區有一個隱藏目錄.git,這個不算工做區,而是Git的版本庫。Git的版本庫裏存了不少東西,其中最重要的就是稱爲stage(或者叫index)的暫存區,還有Git爲咱們自動建立的第一個分支master,以及指向master的一個指針叫HEAD。
    服務器

    第一步是用git add把文件添加進去,實際上就是把文件修改添加到暫存區;第二步是用git commit提交更改,實際上就是把暫存區的全部內容提交到當前分支。由於咱們建立Git版本庫時,Git自動爲咱們建立了惟一一個master分支,因此,如今,git commit就是往master分支上提交更改。你能夠簡單理解爲,須要提交的文件修改統統放到暫存區,而後,一次性提交暫存區的全部修改。app

    因此,git add命令實際上就是把要提交的全部修改放到暫存區(Stage),而後,執行git commit就能夠一次性把暫存區的全部修改提交到分支。ssh

2.git安裝

  1. 安裝homebrew,而後經過homebrew安裝Git,brew install git。
  2. 從AppStore安裝Xcode,Xcode集成了Git,不過默認沒有安裝,你須要運行Xcode,選擇菜單「Xcode」->「Preferences」,在彈出窗口中找到「Downloads」,選擇「Command Line Tools」,點「Install」就能夠完成安裝了。推薦!

3.git建立本地版本庫

3.1 建立本地庫

  什麼是版本庫呢?版本庫又名倉庫,英文名repository,你能夠簡單理解成一個目錄,這個目錄裏面的全部文件均可以被Git管理起來,每一個文件的修改、刪除,Git都能跟蹤,以便任什麼時候刻均可以追蹤歷史,或者在未來某個時刻能夠「還原」。分佈式

  1. 選擇一個合適的地方,建立一個空目錄。post

    bangdeMacBook-Pro:~ bang$ mkdir gitlocal
    bangdeMacBook-Pro:~ bang$ cd gitlocal/
    bangdeMacBook-Pro:gitlocal bang$ pwd
    /Users/bang/gitlocal
    bangdeMacBook-Pro:gitlocal bang$

      注意:若是你使用Windows系統,爲了不遇到各類莫名其妙的問題,請確保目錄名(包括父目錄)不包含中文。學習

  2. 經過git init命令把這個目錄變成Git能夠管理的倉庫。

    bangdeMacBook-Pro:gitlocal bang$ git init
    Initialized empty Git repository in /Users/bang/gitlocal/.git/
    bangdeMacBook-Pro:gitlocal bang$ ls -a
    .   ..  .git
    bangdeMacBook-Pro:gitlocal bang$

      瞬間Git就把倉庫建好了,並且告訴你是一個空的倉庫(empty Git repository),.git的目錄是Git來跟蹤管理版本庫的,沒事千萬不要手動修改這個目錄裏面的文件,否則改亂了,就把Git倉庫給破壞了。也不必定必須在空目錄下建立Git倉庫,選擇一個已經有東西的目錄也是能夠的。不過,不建議你使用本身正在開發的公司項目來學習Git,不然形成的一切後果概不負責。

3.2 添加文件到本地庫

  首先這裏再明確一下,全部的版本控制系統,其實只能跟蹤文本文件的改動,好比TXT文件,網頁,全部的程序代碼等等,Git也不例外。版本控制系統能夠告訴你每次的改動,好比在第5行加了一個單詞「Linux」,在第8行刪了一個單詞「Windows」。而圖片、視頻這些二進制文件,雖然也能由版本控制系統管理,但無法跟蹤文件的變化,只能把二進制文件每次改動串起來,也就是隻知道圖片從100KB改爲了120KB,但到底改了啥,版本控制系統不知道,也無法知道。不幸的是,Microsoft的Word格式是二進制格式,所以,版本控制系統是無法跟蹤Word文件的改動的,若是要真正使用版本控制系統,就要以純文本方式編寫文件。由於文本是有編碼的,好比中文有經常使用的GBK編碼,日文有Shift_JIS編碼,若是沒有歷史遺留問題,強烈建議使用標準的UTF-8編碼,全部語言使用同一種編碼,既沒有衝突,又被全部平臺所支持。

  1. 咱們建立一個文件。
      文件必定要放到learngit目錄下(子目錄也行),由於這是一個Git倉庫,放到其餘地方Git再厲害也找不到這個文件。

    bangdeMacBook-Pro:gitlocal bang$ vi text1.txt
    bangdeMacBook-Pro:gitlocal bang$ ls
    text1.txt
  2. 用命令git add告訴Git,把文件添加到倉庫

    bangdeMacBook-Pro:gitlocal bang$ git add text1.txt

    git add * 能夠把全部文件添加到本地倉庫。

  3. 用命令git commit告訴Git,把文件提交到倉庫

    bangdeMacBook-Pro:gitlocal bang$ git commit 
    [master (root-commit) ed7560b] text1建立
     1 file changed, 1 insertion(+)
     create mode 100644 text1.txt

    git commit * 提交全部文件到本地倉庫。
    git commit -a -m 「全部文件提交"
    簡單解釋一下git commit命令,-m後面輸入的是本次提交的說明,能夠輸入任意內容,固然最好是有意義的,這樣你就能從歷史記錄裏方便地找到改動記錄。git commit命令執行成功後會告訴你,1個文件被改動(咱們新添加的text1.txt文件),插入了1行內容。

  4. 查看倉庫狀態。

    git status查看倉庫狀態

    bangdeMacBook-Pro:gitlocal bang$ 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:   text1.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    倉庫無任何改動狀態:

    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    nothing to commit, working tree clean

    git diff 查看具體修改

    bangdeMacBook-Pro:gitlocal bang$ git diff
    diff --git a/text1.txt b/text1.txt
    index 368785c..3760d94 100644
    --- a/text1.txt
    +++ b/text1.txt
    @@ -1 +1,2 @@
     test1_1
    +test1_2

    git diff head -- 文件名 查看工做區和版本庫裏面最新版本的區別

    bangdeMacBook-Pro:gitlocal bang$ git diff head -- text4.txt 
    diff --git a/text4.txt b/text4.txt
    index 35f7725..f2383f8 100644
    --- a/text4.txt
    +++ b/text4.txt
    @@ -1 +1,2 @@
     text4_1
    +text4_2
  5. 查看提交的歷史記錄

    bangdeMacBook-Pro:gitlocal bang$ git log
    commit f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff (HEAD -> master)
    Author: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Wed Jan 3 11:30:58 2018 +0800
    
        text2_2
    
    commit 0996e4e77c5002cf45e96f1d99841acba24cdf6f
    Author: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Wed Jan 3 11:28:04 2018 +0800
    
        text1_2
    
    commit 66ce043a24be29d5093515282c4e850aaf162022
    Author: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Wed Jan 3 11:21:59 2018 +0800
    
        commit all text
    
    commit ed7560bddcbd275cdb570fc63b9e086ba1b7200b
    Author: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Wed Jan 3 11:13:27 2018 +0800
    
        text1建立

    或者

    bangdeMacBook-Pro:gitlocal bang$ git log --pretty=oneline
    f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff (HEAD -> master) text2_2
    0996e4e77c5002cf45e96f1d99841acba24cdf6f text1_2
    66ce043a24be29d5093515282c4e850aaf162022 commit all text
    ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立

    須要友情提示的是f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff是commit id(版本號),和SVN不同,Git的commit id不是1,2,3……遞增的數字,而是一個SHA1計算出來的一個很是大的數字,用十六進制表示,並且你看到的commit id和個人確定不同,以你本身的爲準。爲何commit id須要用這麼一大串數字表示呢?由於Git是分佈式的版本控制系統,後面咱們還要研究多人在同一個版本庫裏工做,若是你們都用1,2,3……做爲版本號,那確定就衝突了。

    顯示最後一次的提交
    git log -1

  6. 版本回退

    在Git中,用HEAD表示當前版本,也就是最新的提交f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff(注意個人提交ID和你的確定不同),上一個版本就是HEAD^,上上一個版本就是HEAD^^,固然往上100個版本寫100個^比較容易數不過來,因此寫成HEAD~100。咱們使用git reset進行版本回退。

    bangdeMacBook-Pro:gitlocal bang$ git log --pretty=oneline
    f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff (HEAD -> master) text2_2
    0996e4e77c5002cf45e96f1d99841acba24cdf6f text1_2
    66ce043a24be29d5093515282c4e850aaf162022 commit all text
    ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立
    bangdeMacBook-Pro:gitlocal bang$ git reset --hard head^
    HEAD is now at 0996e4e text1_2
    bangdeMacBook-Pro:gitlocal bang$ git log --pretty=oneline
    0996e4e77c5002cf45e96f1d99841acba24cdf6f (HEAD -> master) text1_2
    66ce043a24be29d5093515282c4e850aaf162022 commit all text
    ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立

    從以前版本恢復到最新版本,注意須要記住當前版本的commit id,版本號不必寫全,前幾位就能夠了。

    bangdeMacBook-Pro:gitlocal bang$ git reset --hard f0c55f
    HEAD is now at f0c55f3 text2_2
    bangdeMacBook-Pro:gitlocal bang$ git log --pretty=oneline
    f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff (HEAD -> master) text2_2
    0996e4e77c5002cf45e96f1d99841acba24cdf6f text1_2
    66ce043a24be29d5093515282c4e850aaf162022 commit all text
    ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立

    若是你忘記了commit id,也是能夠恢復的。git reflog 能夠查看以前的操做命令,經過命令能夠查找到當前版本的commit id

    bangdeMacBook-Pro:gitlocal bang$ git reflog
    f0c55f3 (HEAD -> master) HEAD@{0}: reset: moving to f0c55f
    0996e4e HEAD@{1}: reset: moving to head^
    f0c55f3 (HEAD -> master) HEAD@{2}: reset: moving to head
    f0c55f3 (HEAD -> master) HEAD@{3}: commit: text2_2
    0996e4e HEAD@{4}: commit: text1_2
    66ce043 HEAD@{5}: commit: commit all text
    ed7560b HEAD@{6}: commit (initial): text1建立
  7. 丟棄工做區的修改

    git checkout -- file能夠丟棄工做區的修改

    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   text4.txt
    
    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:   text1.txt
    
    bangdeMacBook-Pro:gitlocal bang$ git checkout -- text1.txt 
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   text4.txt
    
    bangdeMacBook-Pro:gitlocal bang$ 
    bangdeMacBook-Pro:gitlocal bang$ git checkout -- text4.txt 
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   text4.txt

      命令git checkout -- text1.txt意思就是,把text1.txt文件在工做區的修改所有撤銷,這裏有兩種狀況:一種是text1.txt自修改後尚未被放到暫存區,如今,撤銷修改就回到和版本庫如出一轍的狀態;一種是readme.txt已經添加到暫存區後,又做了修改,如今,撤銷修改就回到添加到暫存區後的狀態。總之,就是讓這個文件回到最近一次git commit或git add時的狀態。git checkout -- file命令中的--很重要,沒有--,就變成了「切換到另外一個分支」的命令。

    若是想把上面已經存入暫存區的文件刪除可使用git reset

    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   text4.txt
    
    bangdeMacBook-Pro:gitlocal bang$ git reset Head -- text4.txt 
    Unstaged changes after reset:
    M   text4.txt
    bangdeMacBook-Pro:gitlocal bang$ 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:   text4.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    bangdeMacBook-Pro:gitlocal bang$ git checkout -- text4.txt 
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    nothing to commit, working tree clean
    bangdeMacBook-Pro:gitlocal bang$
  8. 刪除文件git rm

    注意刪除的文件直接git add也能夠

    bangdeMacBook-Pro:gitlocal bang$ rm text3.txt 
    bangdeMacBook-Pro:gitlocal bang$ git rm text3.txt
    rm 'text3.txt'
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        deleted:    text3.txt

4. git建立遠程庫

4.1 搭建遠程庫

能夠本身搭建一臺運行Git的服務器,不過現階段,爲了學Git先搭個服務器絕對是小題大做。好在這個世界上有個叫GitHub的神奇的網站,從名字就能夠看出,這個網站就是提供Git倉庫託管服務的,因此,只要註冊一個GitHub帳號,就能夠免費得到Git遠程倉庫。
因爲你的本地Git倉庫和GitHub倉庫之間的傳輸是經過SSH加密的,因此,須要一點設置:

  1. 建立SSH Key。在用戶主目錄下,看看有沒有.ssh目錄,若是有,再看看這個目錄下有沒有id_rsa和id_rsa.pub這兩個文件,若是已經有了,可直接跳到下一步。若是沒有,建立SSH Key:

    bangdeMacBook-Pro:.ssh bang$ ssh-keygen -t rsa -C "youremail@qq.com"

    你須要把郵件地址換成你本身的郵件地址,而後一路回車,使用默認值便可,因爲這個Key也不是用於軍事目的,因此也無需設置密碼。若是一切順利的話,能夠在用戶主目錄裏找到.ssh目錄,裏面有id_rsa和id_rsa.pub兩個文件,這兩個就是SSH Key的祕鑰對,id_rsa是私鑰,不能泄露出去,id_rsa.pub是公鑰,能夠放心地告訴任何人。

  2. 登錄GitHub,打開「Account settings」,「SSH Keys」頁面添加:

    點「Add SSH Key」,填上任意Title,在Key文本框裏粘貼id_rsa.pub文件的內容

    爲何GitHub須要SSH Key呢?由於GitHub須要識別出你推送的提交確實是你推送的,而不是別人冒充的,而Git支持SSH協議,因此,GitHub只要知道了你的公鑰,就能夠確認只有你本身才能推送。固然,GitHub容許你添加多個Key。假定你有若干電腦,你一下子在公司提交,一下子在家裏提交,只要把每臺電腦的Key都添加到GitHub,就能夠在每臺電腦上往GitHub推送了。
    最後友情提示,在GitHub上免費託管的Git倉庫,任何人均可以看到喔(但只有你本身才能改)。因此,不要把敏感信息放進去。
    若是你不想讓別人看到Git庫,有兩個辦法,一個是交點保護費,讓GitHub把公開的倉庫變成私有的,這樣別人就看不見了(不可讀更不可寫)。另外一個辦法是本身動手,搭一個Git服務器,由於是你本身的Git服務器,因此別人也是看不見的。這個方法咱們後面會講到的,至關簡單,公司內部開發必備。

  3. 建立遠程倉庫

    注意:

    • public是公有倉庫,其餘人能夠看到;private是私有倉庫,可是建立須要收費。
    • 咱們勾選Initialize this repository with a README,這樣GitHub會自動爲咱們建立一個README.md文件。建立完畢後,能夠看到README.md文件
    • Add .gitignore能夠選擇Objective-C
    • Add a license 添加一個證書,證書選擇標準參考:

      • 我想要一個簡單寬鬆的許可證 建議: MIT許可證。這是一個寬鬆的、簡明扼要的許可證,只要用戶在項目副本中包含了版權聲明和許可聲明,他們就能夠拿你的代碼作任何想作的事情,你也無需承擔任何責任。使用該許可證的項目:jQuery、Rails
      • 我比較關心專利。建議: Apache許可證。這相似於MIT許可證,但它同時還包含了貢獻者向用戶提供專利受權相關的條款。使用該許可證的項目:Apache、SVN和NuGet
      • 我關心項目的共享改進。建議:GNU GPL( V2或 V3)許可證。這是一種copyleft許可證,要求修改項目代碼的用戶再次分發源碼或二進制代碼時,必須公佈他的相關修改。V3版本與V2相似,但其進一步約束了在某些限制軟件更改的硬件上的使用範圍。使用該許可證的項目:Linux、Git
    • .gitignore失效的狀況

      • Git忽略規則

        在git中若是想忽略掉某個文件,不讓這個文件提交到版本庫中,可使用修改根目錄中 .gitignore 文件的方法(若是沒有這個文件,則需本身手工創建此文件)。這個文件每一行保存了一個匹配的規則例如:

        # 此爲註釋–將被Git忽略
        *.sample # 忽略全部.sample 結尾的文件
        !lib.sample #但lib.sample除外
        /TODO # 僅僅忽略項目根目錄下的 TODO 文件,不包括subdir/TODO
        build/ # 忽略build/目錄下的全部文件
        doc/*.txt # 會忽略doc/notes.txt 但不包括 doc/server/arch.txt
      • gitignore規則不生效的解決辦法

        把某些目錄或文件加入忽略規則,按照上述方法定義後發現並未生效,緣由是.gitignore只能忽略那些原來沒有被追蹤的文件,若是某些文件已經被歸入了版本管理中,則修改.gitignore是無效的。那麼解決方法就是先把本地緩存刪除(改變成未被追蹤狀態),而後再提交,進入.git所在目錄,執行如下命令:

        git rm -r --cached .
        git add .
        git commit -m 'update .gitignore'

        .gitignore未生效


        .gitignore已生效

4.2 連接遠程庫

  1. 本地庫關聯遠程庫

    bangdeMacBook-Pro:gitlocal bang$ git remote add origin git@github.com:luoleiwuhen/gitlocal.git

    請千萬注意,把上面的git@github.com:luoleiwuhen/gitlocal.git替換成你本身的遠程庫地址,不然,你在本地關聯的就是個人遠程庫,關聯沒有問題,可是你之後推送是推不上去的,由於你的SSH Key公鑰不在個人帳戶列表中。會報下面的錯誤

    bangdeMacBook-Pro:gitlocal bang$ git push -u origin master
    git@github.com: Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.

    添加後,遠程庫的名字就是origin,這是Git默認的叫法,也能夠改爲別的,可是origin這個名字一看就知道是遠程庫。

  2. 推送修改到遠程庫

    因爲遠程庫是空的,咱們第一次推送master分支時,加上了-u參數,Git不但會把本地的master分支內容推送的遠程新的master分支,還會把本地的master分支和遠程的master分支關聯起來,在之後的推送或者拉取時就能夠簡化命令。

    bangdeMacBook-Pro:gitlocal bang$ git push -u origin master
    To github.com:luoleiwuhen/gitlocal.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'git@github.com:luoleiwuhen/gitlocal.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    上面報錯緣由分析:
    問題(Non-fast-forward)的出現緣由在於:git倉庫中已經有一部分代碼,因此它不容許你直接把你的代碼覆蓋上去。因而你有2個選擇方式:
    一、強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內的內容
    git push -f
    二、先把git的東西fetch到你本地而後merge後再push
    git fetch
    git merge
    這2句命令等價於
    git pull

    bangdeMacBook-Pro:gitlocal bang$ git pull
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
        git pull <remote> <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> master

    上面代碼說明2件事:
    一、當你處於master branch, 默認的remote就是origin。
    二、當你在master branch上使用git pull時,沒有指定remote和branch,若是要讓git採用默認的remote(也就是origin)來merge在master branch上全部的改變,配置以下

    bangdeMacBook-Pro:gitlocal bang$ git branch --set-upstream-to=origin/master

    繼續git pull

    bangdeMacBook-Pro:gitlocal bang$ git pull
    fatal: refusing to merge unrelated histories

    若是報上面錯誤,可使用:

    bangdeMacBook-Pro:gitlocal bang$ git pull --allow-unrelated-histories origin master
    From github.com:luoleiwuhen/gitlocal
     * branch            master     -> FETCH_HEAD
    Merge made by the 'recursive' strategy.
     .gitignore | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     README.md  |  1 +
     2 files changed, 64 insertions(+)
     create mode 100644 .gitignore
     create mode 100644 README.md
    bangdeMacBook-Pro:gitlocal bang$ git pull
    Already up-to-date.

    推送本地代碼到遠程成功:

    bangdeMacBook-Pro:gitlocal bang$ git push -u origin master
    Counting objects: 26, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (17/17), done.
    Writing objects: 100% (26/26), 2.20 KiB | 1.10 MiB/s, done.
    Total 26 (delta 4), reused 0 (delta 0)
    remote: Resolving deltas: 100% (4/4), done.
    To github.com:luoleiwuhen/gitlocal.git
       b60bdaf..d83ba97  master -> master
    Branch master set up to track remote branch master from origin.


    查看遠程庫已經能夠看到推送的內容。

4.3 clone遠程庫

git clone + 倉庫地址

bangdeMacBook-Pro:~ bang$ git clone https://github.com/luoleiwuhen/gitlocal.git
Cloning into 'gitlocal'...
remote: Counting objects: 30, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 30 (delta 5), reused 26 (delta 4), pack-reused 0
Unpacking objects: 100% (30/30), done.

GitHub給出的地址不止一個,Git支持多種協議,ssh協議,但也可使用https等其餘協議。
ssh:git@github.com:luoleiwuhen/gitlocal.git
https:https://github.com/luoleiwuhen/gitlocal.git
使用https除了速度慢之外,還有個最大的麻煩是每次推送都必須輸入口令,可是在某些只開放http端口的公司內部就沒法使用ssh協議而只能用https。

5 分支管理

在Git裏,主分支,即master分支。HEAD嚴格來講不是指向提交,而是指向master,master纔是指向提交的,因此,HEAD指向的就是當前分支。
一開始的時候,master分支是一條線,Git用master指向最新的提交,再用HEAD指向master,就能肯定當前分支,以及當前分支的提交點:

每次提交,master分支都會向前移動一步,這樣,隨着你不斷提交,master分支的線也愈來愈長。
當咱們建立新的分支,例如dev時,Git新建了一個指針叫dev,指向master相同的提交,再把HEAD指向dev,就表示當前分支在dev上。

Git建立一個分支很快,由於除了增長一個dev指針,改改HEAD的指向,工做區的文件都沒有任何變化!
不過,從如今開始,對工做區的修改和提交就是針對dev分支了,好比新提交一次後,dev指針往前移動一步,而master指針不變:


假如咱們在dev上的工做完成了,就能夠把dev合併到master上。Git怎麼合併呢?最簡單的方法,就是直接把master指向dev的當前提交,就完成了合併。

因此Git合併分支也很快!就改改指針,工做區內容也不變!
合併完分支後,甚至能夠刪除dev分支。刪除dev分支就是把dev指針給刪掉,刪掉後,咱們就剩下了一條master分支。

  1. git branch 查看當前分支

    bangdeMacBook-Pro:gitlocal bang$ git branch
    * master
  2. git branch xiaoming 建立xiaoming分支

    bangdeMacBook-Pro:gitlocal bang$ git branch xiaoming 
    bangdeMacBook-Pro:gitlocal bang$ git branch
    * master
      xiaoming
  3. git checkout xiaoming 切換到xiaoming分支

    bangdeMacBook-Pro:gitlocal bang$ git checkout xiaoming 
    Switched to branch 'xiaoming'
    bangdeMacBook-Pro:gitlocal bang$ git branch
      master
    * xiaoming
  4. 切換並建立分支
    git checkout -b xiaogang 建立xiaogang分支並切換到該分支。
    git checkout命令加上-b參數表示建立並切換,至關於
    git branch xiaogang
    git checkout xiaogang

    bangdeMacBook-Pro:gitlocal bang$ git checkout -b xiaogang 
    Switched to a new branch 'xiaogang'
    bangdeMacBook-Pro:gitlocal bang$ git branch
      master
    * xiaogang
      xiaoming
  5. 合併分支git merge
    若是咱們在xiaogang分支上修改text1.txt提交,而後切換到master分支上查看text1.txt內容是沒有修改的。由於修改提交到了xiaogang分支上

    合併分支:
    首先咱們要切換到master分支上,而後合併

    bangdeMacBook-Pro:gitlocal bang$ git branch
    * master
      xiaogang
      xiaoming
    bangdeMacBook-Pro:gitlocal bang$ git merge xiaogang
    Updating d83ba97..7f2a267
    Fast-forward
     text1.txt | 1 +
     1 file changed, 1 insertion(+)

    此時查看text1.txt文件能夠發現內容已經和xiaogang分支同樣了。

    git merge命令用於合併指定分支到當前分支。注意到上面的Fast-forward信息,Git告訴咱們,此次合併是「快進模式」,也就是直接把master指向dev的當前提交,因此合併速度很是快。固然,也不是每次合併都能Fast-forward,咱們後面會講其餘方式的合併。

  6. 刪除分支

    bangdeMacBook-Pro:gitlocal bang$ git branch -d xiaogang
    Deleted branch xiaogang (was 7f2a267).
    bangdeMacBook-Pro:gitlocal bang$ git branch
    * master
      xiaoming
    bangdeMacBook-Pro
  7. 合併衝突

    若是咱們同時在master分支和xiaogang分支上都修改了text1.txt文件,合併分支會報錯。

    bangdeMacBook-Pro:gitlocal bang$ git merge xiaogang 
    Auto-merging text1.txt
    CONFLICT (content): Merge conflict in text1.txt
    Automatic merge failed; fix conflicts and then commit the result.


    狀態以下:

    衝突如上,test1_4_branch_master是master分支上的修改,test1_4_branch_xiaogang是xiaogang分支的修改,更具需求進行處理,這裏刪除xiaogang分支的修改。Git用<<<<<<<,=======,>>>>>>>標記出不一樣分支的內容,咱們修改以下後保存.

    合併完成以後狀態以下:

    查看提交狀態:

    bangdeMacBook-Pro:gitlocal bang$ git log --graph --pretty=oneline --abbrev-commit
    *   76a3e34 (HEAD -> master) 合併master和xiaogang分支衝突
    |\  
    | * cc71d22 (xiaogang) 分支xiaogang修改text1.txt
    * | 7e54f09 分支master修改text1.txt
    |/  
    * 7f2a267 xiaogang分支修改text1
    *   d83ba97 (origin/master, origin/HEAD, xiaoming) 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal
    |\  
    | * b60bdaf Initial commit
    * 4aa2fd6 刪除text3
    * 62bd4ea 刪除text4
    * 2444e9f 修改text2
    * 47dc252 提交text4.txt
    * f0c55f3 text2_2
    * 0996e4e text1_2
    * 66ce043 commit all text
    * ed7560b text1建立

    一般,合併分支時,若是可能,Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支信息。

    bangdeMacBook-Pro:gitlocal bang$ git log --graph --pretty=oneline
    *   76a3e3429ff12fdf75181867ab599efc39faba59 (HEAD -> master) 合併master和xiaogang分支衝突
    |\  
    | * cc71d221719013d4c130ebac05185d6dc19a6855 分支xiaogang修改text1.txt
    * | 7e54f09d75daa45269f72c2b4eb0aa9d23bf6c83 分支master修改text1.txt
    |/  
    * 7f2a26765450d2f295c6134993c470ed4e3796d4 xiaogang分支修改text1
    *   d83ba972a45be71d82dde386aa569693e5b830ca (origin/master, origin/HEAD, xiaoming) 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal
    |\  
    | * b60bdaf38af68593b91236d36c814d14285f46dc Initial commit
    * 4aa2fd6a460da40cb9d99e9e984dc47f0146932a 刪除text3
    * 62bd4ea485e2380095eefde95cfda0ba216af3c7 刪除text4
    * 2444e9f5665dbba5148b7bf289eb2990680a577c 修改text2
    * 47dc25236a6a2c36c7d5cfceb3b17f68a7825898 提交text4.txt
    * f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff text2_2
    * 0996e4e77c5002cf45e96f1d99841acba24cdf6f text1_2
    * 66ce043a24be29d5093515282c4e850aaf162022 commit all text
    * ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立

    cc71d221719013d4c130ebac05185d6dc19a6855 分支xiaogang修改text1.txt再也不是cc71d221719013d4c130ebac05185d6dc19a6855 (xiaogang) 分支xiaogang修改text1.txt

    若是要強制禁用Fast forward模式,Git就會在merge時生成一個新的commit,這樣,從分支歷史上就能夠看出分支信息。
    下面咱們實戰一下--no-ff方式的git merge:

    bangdeMacBook-Pro:gitlocal bang$ git merge --no-ff -m "merge with no-ff" xiaogang
    Merge made by the 'recursive' strategy.
     text1.txt | 1 +
     1 file changed, 1 insertion(+)

    本次合併要建立一個新的commit,因此加上-m參數,把commit描述寫進去

  8. 分支策略

    在實際開發中,咱們應該按照幾個基本原則進行分支管理:首先,master分支應該是很是穩定的,也就是僅用來發布新版本,平時不能在上面幹活;那在哪幹活呢?幹活都在dev分支上,也就是說,dev分支是不穩定的,到某個時候,好比1.0版本發佈時,再把dev分支合併到master上,在master分支發佈1.0版本;你和你的小夥伴們每一個人都在dev分支上幹活,每一個人都有本身的分支,時不時地往dev分支上合併就能夠了。
    因此,團隊合做的分支看起來就像這樣:

    軟件開發中,bug就像屢見不鮮同樣。有了bug就須要修復,在Git中,因爲分支是如此的強大,因此,每一個bug均可以經過一個新的臨時分支來修復,修復後,合併分支,而後將臨時分支刪除。

  9. 工做區存儲

    當你接到一個修復緊急bug的任務時,很天然地,你想建立一個分支來修復它,可是,當前正在dev上進行的工做尚未提交,並且暫時尚未開發完不能提交:

    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch xiaoming
    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:   text2.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")

    這時能夠git stash功能,能夠把當前工做現場「儲藏」起來,等之後恢復現場後繼續工做。

    bangdeMacBook-Pro:gitlocal bang$ git stash
    Saved working directory and index state WIP on xiaoming: d83ba97 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch xiaoming
    nothing to commit, working tree clean
    bangdeMacBook-Pro:gitlocal bang$

    用git status查看工做區,就是乾淨的(除非有沒有被Git管理的文件),所以能夠放心地建立分支來修復bug。切換到master分支建立bugfix分支修復bug,等修復完成再合併到master分支上並刪除bugfix分支。

    修改完成後切換到工做分支繼續幹活。首先恢復工做區現場。

    git stash list查看工做區存放位置

    bangdeMacBook-Pro:gitlocal bang$ git stash list
    stash@{0}: WIP on xiaoming: d83ba97 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal

    恢復工做區:

    • 一是用git stash apply恢復,可是恢復後,stash內容並不刪除,你須要用git stash drop來刪除;
    • 另外一種方式是用git stash pop,恢復的同時把stash內容也刪了:
    bangdeMacBook-Pro:gitlocal bang$ git stash apply 
    On branch xiaoming
    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:   text2.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    bangdeMacBook-Pro:gitlocal bang$ git stash list
    stash@{0}: WIP on xiaoming: d83ba97 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal
    bangdeMacBook-Pro:gitlocal bang$ git stash drop
    Dropped refs/stash@{0} (c718ed368b38350aae172bf2b1d4049ef83df91a)
    bangdeMacBook-Pro:gitlocal bang$ git stash list
    bangdeMacBook-Pro:gitlocal bang$
    bangdeMacBook-Pro:gitlocal bang$ git stash pop
    On branch xiaoming
    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:   text2.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (b302d48cd814921cc263a5a577cfa95746adf8bf)

    你能夠屢次stash,恢復的時候,先用git stash list查看,而後恢復指定的stash,用命令:
    git stash apply stash@{0}

  10. 強行刪除分支

    若是建立xiaogang分支修改代碼而且提交,切換到master分支沒有進行merge,此時若是刪除xiaogang分支會失敗

    bangdeMacBook-Pro:gitlocal bang$ git branch -d xiaogang
    error: The branch 'xiaogang' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D xiaogang'.
    bangdeMacBook-Pro:gitlocal bang$ git branch -D xiaogang
    Deleted branch xiaogang (was fd22ddb).
  11. 查看遠程分支信息

    bangdeMacBook-Pro:gitlocal bang$ git remote
    origin
    bangdeMacBook-Pro:gitlocal bang$ git remote -v
    origin  https://github.com/luoleiwuhen/gitlocal.git (fetch)
    origin  https://github.com/luoleiwuhen/gitlocal.git (push)
  12. 推送分支

    bangdeMacBook-Pro:gitlocal bang$ git push origin master
    Counting objects: 18, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (16/16), done.
    Writing objects: 100% (18/18), 1.70 KiB | 1.70 MiB/s, done.
    Total 18 (delta 9), reused 0 (delta 0)
    remote: Resolving deltas: 100% (9/9), completed with 1 local object.
    To https://github.com/luoleiwuhen/gitlocal.git
       d83ba97..d907513  master -> master
  13. 多人合做場景

    1. 如今,模擬一個你的小夥伴,能夠在另外一臺電腦(注意要把SSH Key添加到GitHub)或者同一臺電腦的另外一個目錄下克隆。這裏選擇一個新目錄。

      bangdeMacBook-Pro:gitlocal_zhenbang bang$ git clone git@github.com:luoleiwuhen/gitlocal.git
      Cloning into 'gitlocal'...
      remote: Counting objects: 51, done.
      remote: Compressing objects: 100% (25/25), done.
      remote: Total 51 (delta 15), reused 46 (delta 13), pack-reused 0
      Receiving objects: 100% (51/51), 5.28 KiB | 1.76 MiB/s, done.
      Resolving deltas: 100% (15/15), done.

      上面會克隆遠程分支master到本地,查看本地分支也確實是master。

      bangdeMacBook-Pro:gitlocal bang$ git branch
      * master
      bangdeMacBook-Pro:gitlocal bang$ git branch -a
      * master
        remotes/origin/HEAD -> origin/master
        remotes/origin/dev
        remotes/origin/master
    2. 如今,你的小夥伴要在dev分支上開發,就必須建立遠程origin的dev分支到本地,因而他用這個命令建立本地dev分支:

      bangdeMacBook-Pro:gitlocal bang$ git checkout -b dev origin/dev 
      Branch dev set up to track remote branch dev from origin.
      Switched to a new branch 'dev'
      bangdeMacBook-Pro:gitlocal bang$ git branch -a
      * dev
        master
        remotes/origin/HEAD -> origin/master
        remotes/origin/dev
        remotes/origin/master

      git checkout -b dev origin/dev 建立
      修改代碼並提交到遠程dev分支上

      bangdeMacBook-Pro:gitlocal bang$ git commit text1.txt -m "zhenbang分支"
      [dev 9b135df] zhenbang分支
       1 file changed, 1 insertion(+)
       bangdeMacBook-Pro:gitlocal bang$ git push origin dev
      Counting objects: 3, done.
      Delta compression using up to 8 threads.
      Compressing objects: 100% (3/3), done.
      Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
      Total 3 (delta 1), reused 0 (delta 0)
      remote: Resolving deltas: 100% (1/1), completed with 1 local object.
      To github.com:luoleiwuhen/gitlocal.git
         23a4112..9b135df  dev -> dev

      git push origin dev這裏origin表明遠程庫

      bangdeMacBook-Pro:gitlocal bang$ git remote -v
      origin  git@github.com:luoleiwuhen/gitlocal.git (fetch)
      origin  git@github.com:luoleiwuhen/gitlocal.git (push)
    3. 你的小夥伴已經向origin/dev分支推送了他的提交,而碰巧你也對一樣的文件做了修改,並試圖推送:

      bangdeMacBook-Pro:gitlocal bang$ git push origin dev
      To https://github.com/luoleiwuhen/gitlocal.git
       ! [rejected]        dev -> dev (fetch first)
      error: failed to push some refs to 'https://github.com/luoleiwuhen/gitlocal.git'
      hint: Updates were rejected because the remote contains work that you do
      hint: not have locally. This is usually caused by another repository pushing
      hint: to the same ref. You may want to first integrate the remote changes
      hint: (e.g., 'git pull ...') before pushing again.
      hint: See the 'Note about fast-forwards' in 'git push --help' for details.

      推送失敗,由於你的小夥伴的最新提交和你試圖推送的提交有衝突,解決辦法也很簡單,Git已經提示咱們,先用git pull把最新的提交從origin/dev抓下來,而後,在本地合併,解決衝突,再推送

      bangdeMacBook-Pro:gitlocal bang$ git pull
      There is no tracking information for the current branch.
      Please specify which branch you want to merge with.
      See git-pull(1) for details.
      
          git pull <remote> <branch>
      
      If you wish to set tracking information for this branch you can do so with:
      
          git branch --set-upstream-to=origin/<branch> dev

      git pull也失敗了,緣由是沒有指定本地dev分支與遠程origin/dev分支的連接,根據提示,設置dev和origin/dev的連接:

      bangdeMacBook-Pro:gitlocal bang$ git branch --set-upstream-to=origin/dev dev
      Branch dev set up to track remote branch dev from origin.
      bangdeMacBook-Pro:gitlocal bang$ git pull
      remote: Counting objects: 3, done.
      remote: Compressing objects: 100% (2/2), done.
      remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
      Unpacking objects: 100% (3/3), done.
      From github.com:luoleiwuhen/gitlocal
      9b135df..92847e4  dev        -> origin/dev
      Auto-merging text1.txt
      CONFLICT (content): Merge conflict in text1.txt
      Automatic merge failed; fix conflicts and then commit the result.

      這回git pull成功,可是合併有衝突,須要手動解決,解決的方法和分支管理中的解決衝突徹底同樣。解決後,提交,再push

6 標籤管理

  發佈一個版本時,咱們一般先在版本庫中打一個標籤(tag),這樣,就惟一肯定了打標籤時刻的版本。未來不管何時,取某個標籤的版本,就是把那個打標籤的時刻的歷史版本取出來。因此,標籤也是版本庫的一個快照。
  Git的標籤雖然是版本庫的快照,但其實它就是指向某個commit的指針(跟分支很像對不對?可是分支能夠移動,標籤不能移動),因此,建立和刪除標籤都是瞬間完成的。
  Git有commit,爲何還要引入tag?
  「請把上週一的那個版本打包發佈,commit號是6a5819e...」
  「一串亂七八糟的數字很差找!」
  若是換一個辦法:
  「請把上週一的那個版本打包發佈,版本號是v1.2」
  「好的,按照tag v1.2查找commit就行!」
  因此,tag就是一個讓人容易記住的有意義的名字,它跟某個commit綁在一塊兒。

  1. 打標籤

    首先切換到須要打標籤的分支上

    bangdeMacBook-Pro:gitlocal bang$ git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.

    而後,敲命令git tag name就能夠打一個新標籤了

    bangdeMacBook-Pro:gitlocal bang$ git tag v1.0

    默認標籤是打在最新提交的commit上的。有時候,若是忘了打標籤,好比,如今已是週五了,但應該在週一打的標籤沒有打,怎麼辦?
    方法是找到歷史提交的commit id,而後打上就能夠了:

    bangdeMacBook-Pro:gitlocal bang$ git log --graph --pretty=oneline
    *   d907513c086b73970acd81eefe3422642ba5ea0a (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) merge bugfix
    |\  
    | * 84d9daf8c69a11e0e8970260b060c7f324b77bcd fixbug
    * |   4c811ab83e78ea66e6e2eea475fc7760452746ff merge with no-ff
    |\ \  
    | * | 776eee31e3ce6b053d1b263fed0b5bd6f916fda4 xiaogang分支修改text1.txt
    |/ /  
    * |   76a3e3429ff12fdf75181867ab599efc39faba59 合併master和xiaogang分支衝突
    |\ \  
    | * | cc71d221719013d4c130ebac05185d6dc19a6855 分支xiaogang修改text1.txt
    * | | 7e54f09d75daa45269f72c2b4eb0aa9d23bf6c83 分支master修改text1.txt
    |/ /  
    * | 7f2a26765450d2f295c6134993c470ed4e3796d4 xiaogang分支修改text1
    |/  
    *   d83ba972a45be71d82dde386aa569693e5b830ca 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal
    |\  
    | * b60bdaf38af68593b91236d36c814d14285f46dc Initial commit
    * 4aa2fd6a460da40cb9d99e9e984dc47f0146932a 刪除text3
    * 62bd4ea485e2380095eefde95cfda0ba216af3c7 刪除text4
    * 2444e9f5665dbba5148b7bf289eb2990680a577c 修改text2
    * 47dc25236a6a2c36c7d5cfceb3b17f68a7825898 提交text4.txt
    * f0c55f361fdd3d61f2a7a1f3ca40375f470c78ff text2_2
    * 0996e4e77c5002cf45e96f1d99841acba24cdf6f text1_2
    * 66ce043a24be29d5093515282c4e850aaf162022 commit all text
    * ed7560bddcbd275cdb570fc63b9e086ba1b7200b text1建立
    bangdeMacBook-Pro:gitlocal bang$ git tag v0.1 4aa2f
    bangdeMacBook-Pro:gitlocal bang$ git tag
    v0.1
    v1.0

    還能夠建立帶有說明的標籤,用-a指定標籤名,-m指定說明文字:

    bangdeMacBook-Pro:gitlocal bang$ git tag -a v0.2 -m"分支合併" 76a3e3
    bangdeMacBook-Pro:gitlocal bang$ git tag
    v0.1
    v0.2
    v1.0
  2. 查看打過的tag

    bangdeMacBook-Pro:gitlocal bang$ git tag
        v0.1
        v1.0
  3. 查看分支詳情

    bangdeMacBook-Pro:gitlocal bang$ git show v0.2
    tag v0.2
    Tagger: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Thu Jan 4 17:22:48 2018 +0800
    
    分支合併
    
    commit 76a3e3429ff12fdf75181867ab599efc39faba59 (tag: v0.2, tag: show)
    Merge: 7e54f09 cc71d22
    Author: wangzhenbang <wangzhenbang@100tal.com>
    Date:   Wed Jan 3 18:10:22 2018 +0800
    
        合併master和xiaogang分支衝突
  4. 刪除標籤

    bangdeMacBook-Pro:gitlocal bang$ git tag
    show
    v0.1
    v0.2
    v1.0
    bangdeMacBook-Pro:gitlocal bang$ git tag -d v0.1
    Deleted tag 'v0.1' (was 4aa2fd6)
    bangdeMacBook-Pro:gitlocal bang$ git tag
    show
    v0.2
    v1.0
  5. 推送標籤到遠程

    由於建立的標籤都只存儲在本地,不會自動推送到遠程。因此,打錯的標籤能夠在本地安全刪除。
    若是要推送某個標籤到遠程,使用命令git push origin tagname:

    bangdeMacBook-Pro:gitlocal bang$ git push origin v1.0
    Total 0 (delta 0), reused 0 (delta 0)
    To github.com:luoleiwuhen/gitlocal.git
     * [new tag]         v1.0 -> v1.0

    或者,一次性推送所有還沒有推送到遠程的本地標籤:

    bangdeMacBook-Pro:gitlocal bang$ git push origin --tags
    Counting objects: 1, done.
    Writing objects: 100% (1/1), 174 bytes | 174.00 KiB/s, done.
    Total 1 (delta 0), reused 0 (delta 0)
    To github.com:luoleiwuhen/gitlocal.git
     * [new tag]         show -> show
     * [new tag]         v0.2 -> v0.2

  6. 刪除遠程標籤

    若是標籤已經推送到遠程,要刪除遠程標籤就麻煩一點,先從本地刪除:

    bangdeMacBook-Pro:gitlocal bang$ git tag -d v0.2
    Deleted tag 'v0.2' (was 9f1a177)
    bangdeMacBook-Pro:gitlocal bang$ git tag
    show
    v1.0

    而後,從遠程刪除。刪除命令也是push,可是格式以下:

    bangdeMacBook-Pro:gitlocal bang$ git push origin :refs/tags/v0.2
    To github.com:luoleiwuhen/gitlocal.git
     - [deleted]         v0.2

7 Git配置

  1. 顏色配置

    bangdeMacBook-Pro:gitlocal bang$ git config --global color.ui true
    bangdeMacBook-Pro:gitlocal bang$ git config --global color.ui false
  2. 指令替換

    若是敲git st就表示git status那就簡單多了,固然這種偷懶的辦法咱們是極力同意的。
    咱們只須要敲一行命令,告訴Git,之後st就表示status:

    bangdeMacBook-Pro:gitlocal bang$ git config --global alias.st status
    bangdeMacBook-Pro:gitlocal bang$ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    nothing to commit, working tree clean
    bangdeMacBook-Pro:gitlocal bang$ git st
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    nothing to commit, working tree clean

    打印記錄

    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    bangdeMacBook-Pro:gitlocal bang$ git lg
    *   d907513 - (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) merge bugfix (23 hours ago) <wangzhenbang>
    |\  
    | * 84d9daf - fixbug (24 hours ago) <wangzhenbang>
    * |   4c811ab - merge with no-ff (24 hours ago) <wangzhenbang>
    |\ \  
    | * | 776eee3 - xiaogang分支修改text1.txt (24 hours ago) <wangzhenbang>
    |/ /  
    * |   76a3e34 - (tag: show) 合併master和xiaogang分支衝突 (25 hours ago) <wangzhenbang>
    |\ \  
    | * | cc71d22 - 分支xiaogang修改text1.txt (26 hours ago) <wangzhenbang>
    * | | 7e54f09 - 分支master修改text1.txt (26 hours ago) <wangzhenbang>
    |/ /  
    * | 7f2a267 - xiaogang分支修改text1 (26 hours ago) <wangzhenbang>
    |/  
    *   d83ba97 - 合併代碼 Merge branch 'master' of github.com:luoleiwuhen/gitlocal (27 hours ago) <wangzhenbang>
    |\  
    | * b60bdaf - Initial commit (28 hours ago) <彡天涯灬無忌彡>
    * 4aa2fd6 - 刪除text3 (29 hours ago) <wangzhenbang>
    * 62bd4ea - 刪除text4 (29 hours ago) <wangzhenbang>
    * 2444e9f - 修改text2 (29 hours ago) <wangzhenbang>
    * 47dc252 - 提交text4.txt (29 hours ago) <wangzhenbang>
    * f0c55f3 - text2_2 (32 hours ago) <wangzhenbang>
    * 0996e4e - text1_2 (32 hours ago) <wangzhenbang>
    * 66ce043 - commit all text (32 hours ago) <wangzhenbang>
    * ed7560b - text1建立 (32 hours ago) <wangzhenbang>
  3. 配置文件

    配置Git的時候,加上--global是針對當前用戶起做用的,若是不加,那隻針對當前的倉庫起做用。
    配置文件放哪了?每一個倉庫的Git配置文件都放在.git/config文件中:

    bangdeMacBook-Pro:.git bang$ cat config 
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
    [remote "origin"]
        url = git@github.com:luoleiwuhen/gitlocal.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    [branch "dev"]
        remote = origin
        merge = refs/heads/dev

    而當前用戶的Git配置文件放在用戶主目錄(cd ~)下的一個隱藏文件.gitconfig中:

    bangdeMacBook-Pro:gitlocal bang$ cd ~
    bangdeMacBook-Pro:~ bang$ cat .gitconfig 
    [user]
        name = wangzhenbang
        email = wangzhenbang@100tal.com
    [http]
        postBuffer = 524288000
    [filter "lfs"]
        clean = git-lfs clean %f
        smudge = git-lfs smudge %f
        required = true
    [color]
        ui = true
    [alias]
        lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
        st = status
    [core]
        excludesfile = /Users/bang/.gitignore_global
    [difftool "sourcetree"]
        cmd = opendiff \"$LOCAL\" \"$REMOTE\"
        path = 
    [mergetool "sourcetree"]
        cmd = /Applications/SourceTree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
        trustExitCode = true

    別名就在[alias]後面,要刪除別名,直接把對應的行刪掉便可。的行刪掉便可。

相關文章
相關標籤/搜索