Git簡記

0. 前言

最近有個項目比較趕,因而決定8我的一塊兒並行完成,單獨把項目拿出來用gitea管理,每人分一個模塊開發,對應每人開一個我的分支,一個develop分支和一個master分支,效果不錯,從中也幫助本身從新溫習了一下git的使用,小記一下git相關知識點。html

1. 安裝和使用

  • 在MAC上,安裝homebrew,而後經過homebrew 安裝Git。git

    在MAC上另外一種安裝方法,從AppStore安裝Xcode,Xcode集成了Git,不過默認沒有安裝,你須要運行Xcode,選擇菜單「Xcode」->「Preferences」,在彈出窗口中找到「Downloads」,選擇「Command Line Tools」,點「Install」就能夠完成安裝了。
  • 在Windows上,下載安裝包,默認下一步,安裝完成便可。
  • 安裝完後自報家門github

    $ git config --global user.name "wuwhs"
    $ git config --global user.email "email@example.com"
  • 建立版本庫
    初始化一個Git倉庫,使用git init命令。添加文件到Git倉庫,分兩步:緩存

    1. 第一步,使用命令git add <file>,注意,可反覆屢次使用,添加多個文件;
    2. 第二步,使用命令git commit,完成。

2. 時光穿梭

  • 若是git status告訴你有文件被修改過,用git diff能夠查看修改內容。
  • HEAD指向的版本就是當前版本,所以,Git容許咱們在版本的歷史之間穿梭,使用命令git reset --hard commit_id
  • 穿梭前,用git log能夠查看提交歷史,以便肯定要回退到哪一個版本。
  • 要重返將來,用git reflog查看命令歷史,以便肯定要回到將來的哪一個版本,git log --pretty=oneline --abbrev-commit在一行顯示縮寫提交號。
  • 場景1:當你改亂了工做區某個文件的內容,想直接丟棄工做區的修改時,用命令git checkout -- file
  • 當你不但改亂了工做區某個文件的內容,還添加到了暫存區時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了場景1,第二步按場景1操做。
  • 命令git rm用於刪除一個文件。若是一個文件已經被提交到版本庫,那麼你永遠不用擔憂誤刪,可是要當心,你只能恢復文件到最新版本,你會丟失最近一次提交後你修改的內容。

3. 遠程倉庫

  • 建立SSH Key。$ ssh-keygen -t rsa -C "youremail@example.com"
  • 登錄GitHub,打開「Account settings」,「SSH Keys」頁面。而後,點「Add SSH Key」,填上任意Title,在Key文本框裏粘貼id_rsa.pub文件的內容。
  • 要關聯一個遠程庫,使用命令git remote add origin git@server-name:path/repo-name.git
  • 關聯後,使用命令git push -u origin master第一次推送master分支的全部內容。
  • 此後,每次本地提交後,只要有必要,就可使用命令git push origin master推送最新修改。
  • 要克隆一個倉庫,首先必須知道倉庫的地址,而後使用git clone命令克隆。

4. 分支管理

  • Git鼓勵大量使用分支。
  • 查看分支:git branch
  • 建立分支:git branch <name>
  • 切換分支:git checkout <name>
  • 建立+切換到當前分支:git checkout -b <name>
  • 合併某分支到當前分支:git merge <name>
  • 刪除分支:git branch -d <name>
  • 當Git沒法自動合併分支時,就必須首先剞劂衝突,解決衝突後,再提交,合併完成用git log --graph命令能夠看到分支合併圖。
  • 合併分支時,加上--no-ff參數就能夠用普通模式合併,合併後的歷史有分支,能看出來曾經作過合併,而fash-forward合併就看不出來曾經作過合併。
  • 當手頭工做沒有完成時,先把工做現場git stash一下,而後去修復bug,修復後,再git stash list查看歷史stash,一是用git stash apply恢復,但恢復後,stash內容並不刪除,你須要用git stash drop來刪除;另外一種方式是用git stash pop,恢復的同時把stash內容也刪了。
  • 查看遠程庫信息,使用git remote -v
  • 從本地推送分支,使用git push origin branch-name,若是失敗,先用git pull抓取遠程的新提交。
  • 再本地建立和遠程分支對應的分支,使用git checkout -b branch-name origin/branch-name,本地和遠程分支的名稱最好一致。
  • 從遠程抓取分支,使用git pull,若是有衝突,要先處理衝突。

5. 標籤

  • 命令git tag <name>用於新建一個標籤,默認爲HEAD,也能夠指定一個commit id。
  • git tag -a <tagname> -m "balabala..."能夠指定標籤信息。
  • git tag -s <tagname> -m "balabala..."能夠用PGP簽名標籤。
  • 命令git tag能夠查看全部標籤。
  • 命令git push origin <tagname>能夠推送一個本地標籤。
  • 命令git push origin --tags能夠推送所有未推送過的本地標籤。
  • 命令git tag -d <tagname>能夠刪除一個本地標籤。
  • 命令git push origin :refs/tags/<tagname>能夠刪除一個遠程標籤。

6. 舉個應用栗子

  1. 最初在遠程建立項目倉庫有masterdevelp分支,參與開發人員先在本身一個文件夾下,調出git Bash,而後輸入命令git clone URL把倉庫clone下來app

    MINGW32 /d/appSoft/wampserver/wamp64/www (master)
    $ git clone git@github.com:wuwhs/demo.git
    Cloning into 'demo'...
    Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
    warning: You appear to have cloned an empty repository.
    Checking connectivity... done.
  2. cd demo進入clone下載的目錄裏,用git branch develop在本地建立一個對應的develop分支dom

    $ cd demo
    
    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (master)
    $ git branch
    *master
    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (master)
    $ git branch develop
    
    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (master)
    $ git branch
    develop   
    *master

    再次用git branch查看已經新建了一個develop分支ssh

  3. git checkout develop切換到當前develop分支編輯器

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (master)
    $ git checkout develop
    Switched to branch 'develop'
  4. git pull origin develop:develop,即:git pull origin <遠程主機名> <遠程分支名>:<本地分支名>,當本地和遠程分支名相同時,能夠簡寫成一個,也就是git pull origin develop,拉取遠程develop分支完成,而後開發人員就能夠在這個分支上工做了ide

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git pull origin develop:develop
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From github.com:wuwhs/demo
       7ff2cb0..7ab2842  develop    -> develop
       7ff2cb0..7ab2842  develop    -> origin/develop
    warning: fetch updated the current branch head.
    fast-forwarding your working tree from
    commit 7ff2cb0627be357fa15db4e38e1bfe8fc820b8ec.
    Already up-to-date.
  5. 當一天了工做完成,要提交到遠程分支,首先要拉取一下別人提交的代碼,防止版本衝突測試

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git pull
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From github.com:wuwhs/demo
       f848dc7..d696375  develop    -> origin/develop
    Updating f848dc7..d696375
    Fast-forward
     demo.txt | 2 ++
     1 file changed, 2 insertions(+)

    PS:直接偷懶pull可能會出現沒有找到tracking的分支

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ 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> develop

    這時候要手動添加一下對應分支依賴git branch --set-upstream-to=origin/<branch> develop

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git branch --set-upstream-to=origin/develop develop
    Branch develop set up to track remote branch develop from origin.
    
    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git pull
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From github.com:wuwhs/demo
       f848dc7..d696375  develop    -> origin/develop
    Updating f848dc7..d696375
    Fast-forward
     demo.txt | 2 ++
     1 file changed, 2 insertions(+)
  6. 將本地分支提交到對應遠程分支上,git push origin develop:develop,即:git push origin <遠程主機><本地分支名>:<遠程分支名>,若是名稱同樣能夠簡寫,也就是git push origin develop

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git push origin develop:develop
    Counting objects: 9, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (5/5), done.
    Writing objects: 100% (9/9), 759 bytes | 0 bytes/s, done.
    Total 9 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), done.
    To git@github.com:wuwhs/demo.git
       d696375..3c00c0c  develop -> develop
  7. 項目測試OK了,本地分支合併到master分支上,要用到git merge <branch>

    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (develop)
    $ git checkout master
    Switched to branch 'master'
    
    MINGW32 /d/appSoft/wampserver/wamp64/www/demo (master)
    $ git merge develop
    Updating c4d0377..3c00c0c
    Fast-forward
     demo.txt | 9 +++++++++
     1 file changed, 9 insertions(+)

經常使用的操做就以上七步了,固然會有不一樣情形的應用。

7. 附錄:git-cheat-sheet

通常而言,經常使用的就是以上那些命令,有人專門的整理了一份比較全一點的文檔git-cheat-sheet,方便查閱。

配置

列出當前配置:

$ git config --list

列出repository配置:

$ git config --local --list

列出全局配置:

$ git config --global --list

列出系統配置:

$ git config --system --list

設置用戶名:

$ git config --global user.name "[firstname lastname]"

設置用戶郵箱:

$ git config --global user.email "[valid-email]"

設置git命令輸出爲彩色:

$ git config --global color.ui auto

設置git使用的文本編輯器設:

$ git config --global core.editor vi

配置文件

Repository配置對應的配置文件路徑[--local]:

<repo>/.git/config

用戶全局配置對應的配置文件路徑[--global]:

~/.gitconfig

系統配置對應的配置文件路徑[--local]:

/etc/gitconfig

建立

複製一個已建立的倉庫:

# 經過 SSH
$ git clone ssh://user@domain.com/repo.git
#經過 HTTP
$ git clone http://domain.com/user/repo.git

建立一個新的本地倉庫:

$ git init

本地修改

顯示工做路徑下已修改的文件:

$ git status

顯示與上次提交版本文件的不一樣:

$ git diff

把當前全部修改添加到下次提交中:

$ git add .

把對某個文件的修改添加到下次提交中:

$ git add -p <file>

提交本地的全部修改:

$ git commit -a

提交以前已標記的變化:

$ git commit

附加消息提交:

$ git commit -m 'message here'

提交,並將提交時間設置爲以前的某個日期:

git commit --date="`date --date='n day ago'`" -am "Commit Message"

修改上次提交
(請勿修改已發佈的提交記錄!)

$ git commit --amend

修改上次提交的committer date:

GIT_COMMITTER_DATE="date" git commit --amend

修改上次提交的author date:

git commit --amend --date="date"

把當前分支中未提交的修改移動到其餘分支:

git stash
git checkout branch2
git stash pop

將 stashed changes 應用到當前分支:

git stash apply

刪除最新一次的 stashed changes:

git stash drop

搜索

從當前目錄的全部文件中查找文本內容:

$ git grep "Hello"

在某一版本中搜索文本:

$ git grep "Hello" v2.5

提交歷史

從最新提交開始,顯示全部的提交記錄(顯示hash, 做者信息,提交的標題和時間):

$ git log

顯示全部提交(僅顯示提交的hash和message):

$ git log --oneline

顯示某個用戶的全部提交:

$ git log --author="username"

顯示某個文件的全部修改:

$ git log -p <file>

僅顯示遠端分支與遠端分支提交記錄的差集:

$ git log --oneline <origin/master>..<remote/master> --left-right

誰,在什麼時間,修改了文件的什麼內容:

$ git blame <file>

顯示reflog:

$ git reflog show

刪除reflog:

$ git reflog delete

分支與標籤

列出全部的分支:

$ git branch

列出全部的遠端分支:

$ git branch -r

切換分支:

$ git checkout <branch>

建立並切換到新分支:

$ git checkout -b <branch>

基於當前分支建立新分支:

$ git branch <new-branch>

基於遠程分支建立新的可追溯的分支:

$ git branch --track <new-branch> <remote-branch>

刪除本地分支:

$ git branch -d <branch>

強制刪除一個本地分支:
將會丟失未合併的修改!

$ git branch -D <branch>

給當前版本打標籤:

$ git tag <tag-name>

給當前版本打標籤並附加消息:

$ git tag -a <tag-name>

更新與發佈

列出當前配置的遠程端:

$ git remote -v

顯示遠程端的信息:

$ git remote show <remote>

添加新的遠程端:

$ git remote add <remote> <url>

下載遠程端版本,但不合併到HEAD中:

$ git fetch <remote>

下載遠程端版本,並自動與HEAD版本合併:

$ git remote pull <remote> <url>

將遠程端版本合併到本地版本中:

$ git pull origin master

以rebase方式將遠端分支與本地合併:

git pull --rebase <remote> <branch>

將本地版本發佈到遠程端:

$ git push remote <remote> <branch>

刪除遠程端分支:

$ git push <remote> :<branch> (since Git v1.5.0)
# or
git push <remote> --delete <branch> (since Git v1.7.0)

發佈標籤:

$ git push --tags

合併與重置(Rebase)
將分支合併到當前HEAD中:

$ git merge <branch>

將當前HEAD版本重置到分支中:
請勿重置已發佈的提交!

$ git rebase <branch>

退出重置:

$ git rebase --abort

解決衝突後繼續重置:

$ git rebase --continue

使用配置好的merge tool 解決衝突:

$ git mergetool

在編輯器中手動解決衝突後,標記文件爲已解決衝突:

$ git add <resolved-file>
$ git rm <resolved-file>

合併提交:

$ git rebase -i <commit-just-before-first>

把上面的內容替換爲下面的內容:

原內容:

pick <commit_id>
pick <commit_id2>
pick <commit_id3>

替換爲:

pick <commit_id>
squash <commit_id2>
squash <commit_id3>

撤銷

放棄工做目錄下的全部修改:

$ git reset --hard HEAD

移除緩存區的全部文件(i.e. 撤銷上次git add):

$ git reset HEAD

放棄某個文件的全部本地修改:

$ git checkout HEAD <file>

重置一個提交(經過建立一個大相徑庭的新提交)

$ git revert <commit>

將HEAD重置到指定的版本,並拋棄該版本以後的全部修改:

$ git reset --hard <commit>

用遠端分支強制覆蓋本地分支:

git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature

將HEAD重置到上一次提交的版本,並將以後的修改標記爲未添加到緩存區的修改:

$ git reset <commit>

將HEAD重置到上一次提交的版本,並保留未提交的本地修改:

$ git reset --keep <commit>

刪除添加.gitignore文件前錯誤提交的文件:

$ git rm -r --cached .
$ git add .
$ git commit -m "remove xyz file"

完~

可參考文章:

  1. git-guide
  2. 廖雪峯git教程
  3. git-scm
  4. Git Cheat Sheet 中文版
相關文章
相關標籤/搜索