天天看點

Git指令速查AliasGit ConfigBasic UsageRepositoryCheckoutLogUndo thingsResetRevertDiffMergeRebaseCherry PickBranch workflowTagSubmoduleStashoh-my-zsh 常用指令

Alias

下面的隻是例子,想改成什麼跟随自己的意願即可。

git config --global alias.st status //status 縮寫成 st
git config --global alias.co checkout //checkout 縮寫成 co
git config --global alias.br branch //branch 縮寫成 br
git config --global alias.ci commit //commit 縮寫成 ci
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"           

如果不想使用了,删除掉的話,直接删除 conf 配置檔案中的行即可,global 的在目前使用者下

vim ~/.gitconfig

删除

alias

下你配置的内容接口;若是目前倉庫則在

.git/config

中。

Git Config

Git 配置檔案分為三級,系統級(--system)、使用者級(--global)和目錄級(--local),三者的使用優先級以離目錄 (repository)最近為原則,如果三者的配置不一樣,則生效優先級 目錄級>使用者級>系統級,可以通過

git config --help

檢視更多内容。

  • 系統級配置存儲在

    /etc/gitconfig

    檔案中,可以使用

    git config --system user.name "jim"

    ,

    git config --sytem user.email "[email protected]"

    來進行配置,該配置對系統上所有使用者及他們所擁有的倉庫都生效的配置值。
  • 使用者級存儲在每個使用者的

    ~/.gitconfig

    中,可以使用

    git config --global user.name "jim"

    git config --global user.email "[email protected]"

    來進行配置,該配置對目前使用者上所有的倉庫有效。
  • 目錄級存儲在每個倉庫下的

    .git/config

    git config --local user.name "jim"

    git config --local user.email "[email protected]"

    來進行配置,隻對目前倉庫生效。

Basic Usage

  • 添加檔案到暫存區(staged):

    git add filename

    /

    git stage filename

  • 将所有修改檔案添加到暫存區(staged):

    git add --all

    git add -A

  • 送出修改到暫存區(staged):

    git commit -m 'commit message'

    git commit -a -m 'commit message'

    注意了解 -a 參數的意義
  • 從Git倉庫中删除檔案:

    git rm filename

  • 從Git倉庫中删除檔案,但本地檔案保留:

    git rm --cached filename

  • 重命名某個檔案:

    git mv filename newfilename

    或者直接修改完畢檔案名 ,進行

    git add -A && git commit -m 'commit message'

    Git會自動識别是重命名了檔案
  • 擷取遠端最新代碼到本地:

    git pull (origin branchname)

    可以指定分支名,也可以忽略。pull 指令自動 fetch 遠端代碼并且 merge,如果有沖突,會顯示在狀态欄,需要手動處理。更推薦使用:

    git fetch

    之後

    git merge --no-ff origin branchname

    拉取最新的代碼到本地倉庫,并手動 merge 。

Repository

  • 檢出(clone)倉庫代碼:

    git clone repository-url

    git clone repository-url local-directoryname

    • 例如,clone jquery 倉庫到本地:

      git clone git://github.com/jquery/jquery.git

    • clone jquery 倉庫到本地,并且重命名為 my-jquery :

      git clone git://github.com/jquery/jquery.git my-jquery

  • 檢視遠端倉庫:

    git remote -v

  • 添加遠端倉庫:

    git remote add [name] [repository-url]

  • 删除遠端倉庫:

    git remote rm [name]

  • 修改遠端倉庫位址:

    git remote set-url origin new-repository-url

  • 拉取遠端倉庫:

    git pull [remoteName] [localBranchName]

  • 推送遠端倉庫:

    git push [remoteName] [localBranchName]

    例:

    git push -u orgin master

    将目前分支推送到遠端master分支
  • 将本地 test 分支送出到遠端 master 分支:

    git push origin test:master

    (把本地的某個分支 test 送出到遠端倉庫,并作為遠端倉庫的 master 分支) 送出本地 test 分支作為遠端的 test 分支 :

    git push origin test:test

Checkout

checkout指令用于從曆史送出(或者暫存區域)中拷貝檔案到工作目錄,也可用于切換分支。

匿名分支:如果既沒有指定檔案名,也沒有指定分支名,而是一個标簽、遠端分支、SHA-1值或者是像 master~3 類似的東西,就得到一個匿名分支,稱作 detached HEAD(被分離的 HEAD 辨別)。

當HEAD處于分離狀态(不依附于任一分支)時,送出操作可以正常進行,但是不會更新任何已命名的分支。(你可以認為這是在更新一個匿名分支。)一旦此後你切換到别的分支,比如說 master,那麼這個送出節點(可能)再也不會被引用到,然後就會被丢棄掉了。注意這個指令之後就不會有東西引用 2eecb。詳細檢視:

visual-git-guide#detached

但是,如果你想儲存這個狀态,可以用指令

git checkout -b name

來建立一個新的分支。

Log

Description : Shows the commit logs.

The command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown.

git log [options] [revision range] [path]

常用指令整理如下:

  • 檢視日志:

    git log

  • 檢視日志,并檢視每次的修改内容:

    git log -p

  • 檢視日志,并檢視每次檔案的簡單修改狀态:

    git log --stat

  • 一行顯示日志:

    git log --pretty=oneline

    git log --pretty='format:"%h - %an, %ar : %s'

  • 檢視日志範圍:
    • 檢視最近10條日志:

      git log -10

    • 檢視2周前:

      git log --until=2week

      或者指定2周的明确日期,比如:

      git log --until=2015-08-12

    • 檢視最近2周内:

      git log --since=2week

      或者指定2周明确日志,比如:

      git log --since=2015-08-12

    • 隻檢視某個使用者的送出:

      git log --committer=user.name

      git log --author=user.name

    • 隻檢視送出msg中包含某個資訊的曆史,比如包含'測試'兩個字的:

      git log --grep '測試'

    • 試試這個 :

      git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

      感覺好用就加成 alias ,友善日後用,方法:

      git config --global alias.aliasname 'alias-content'

    • 更多用法: Viewing the History -- 《Pro Git2》

log

的目的就是為了檢視改動點來排查問題,除了

git log

還可以使用

git show

git blame

來檢視檔案的改動。

  • Who changed what and when in a file :

    git blame $file

  • 檢視一次 commit 中修改了哪些檔案:

    git show --pretty="" --name-only <sha1-of-commit>

    或者

    git diff-tree --no-commit-id --name-only -r <sha1-of-commit>

Undo things

  • 上次送出 msg 錯誤/有未送出的檔案應該同上一次一起送出,需要重新送出備注:

    git commit --amend -m 'new msg'

  • 修改上次送出的 author、email :

    git commit --amend --author="newName <newEmail>"

  • 修改整個曆史記錄中的某些錯誤的 author、email:

    git rebase 或者 git filter-branch

    # git rebase  模式
    git rebase -i -p 76892625a7b126f4772f8d7e331ada3552c11ce1 
    # 彈出編輯器,在需要修改的 commit 處 由 picked 改變為 edit ,然後 wq 退出 vim;
    git commit --amend --author 'newName <newEmail>'
    # 執行後即變更了相應的 author 和 email 
    git rebase --continue 
    ################################################################
    # git filter-branch 模式 https://help.github.com/articles/changing-author-info/
    git filter-branch --env-filter '
    OLD_EMAIL="[email protected]"
    CORRECT_NAME="Your Correct Name"
    CORRECT_EMAIL="[email protected]"
    if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags           
  • 一次

    git add -A

    後,需要将某個檔案撤回到工作區,即:某個檔案不應該在本次commit中:

    git reset HEAD filename

  • 撤銷某些檔案的修改内容:

    git checkout -- filename

    注意:一旦執行,所有的改動都沒有了,謹慎!謹慎!謹慎!
  • 将工作區内容回退到遠端的某個版本:

    git reset --hard <sha1-of-commit>

Reset

reset指令把目前分支指向另一個位置,并且有選擇的變動工作目錄和索引,也用來在從曆史倉庫中複制檔案到索引,而不動工作目錄。

git reset --hard <sha1-of-commit>

  • git reset --hard HEAD^

    reset index and working directory ,
  • git reset --soft HEAD^

    nothing changed to index and working directory ,僅僅将 HEAD 指向
  • git reset --mixed HEAD^

    default,reset index ,nothing to working directory 預設選項,工作區代碼不改動,添加變更到index區

Revert

git revert

will create a new commit that's the opposite (or inverse) of the given SHA. If the old commit is "matter", the new commit is "anti-matter"—anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit.

This is Git's safest, most basic "undo" scenario, because it doesn't alter history—so you can now git push the new "inverse" commit to undo your mistaken commit.

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…​
git revert --continue
git revert --quit
git revert --abort           

Revert VS Reset

Diff

  • 檢視工作區(working directory)和暫存區(staged)之間差異:

    git diff

  • 檢視工作區(working directory)與目前倉庫版本(repository)HEAD版本差異:

    git diff HEAD

  • 檢視暫存區(staged)與目前倉庫版本(repository)差異:

    git diff --cached

    git diff --staged

  • 不檢視具體改動,隻檢視改動了哪些類:

    git diff --stat

Merge

  • 解決沖突後/擷取遠端最新代碼後合并代碼:

    git merge branchname

    ,将 branchname 分支上面的代碼合并到目前分支
  • 保留該存在版本合并log:

    git merge --no-ff branchname

    參數

    --no-ff

    防止 fast-forward 的送出,詳情參考: the difference ,fast-forward:分支内容一緻,指針直接移動,并未能看出分支資訊

    Rebase

    Rebase 同 Merge 的結果是一樣的,就是合并本地、遠端的改動,但過程中還有差別。
    git checkout mywork
    git rebase origin           

    這些指令會把你的"mywork"分支裡的每個送出(commit)取消掉,并且把它們臨時 儲存為更新檔(patch)(這些更新檔 放到".git/rebase"目錄中),然後把"mywork"分支更新 到最新的"origin"分支,最後把儲存的這些更新檔應用 到"mywork"分支上。

    一張圖厘清 rebase 和 merge 的差別

在rebase的過程中,也許會出現沖突(conflict). 在這種情況,Git會停止rebase并會讓你去解決沖突;在解決完沖突後,用

git-add

指令去更新這些内容的索引(index), 然後,你無需執行 git-commit,隻要執行:

git rebase --continue

這樣git會繼續應用(apply)餘下的更新檔。在任何時候,你可以用 --abort 參數來終止rebase的行動,并且"mywork" 分支會回到rebase開始前的狀态。

git rebase --abort

Cherry Pick

cherry-pick 指令"複制"一個送出節點并在目前分支做一次完全一樣的新送出。

Branch workflow

Aone2 Git 分支開發部署模型詳細解讀

http://docs.alibaba-inc.com:8090/pages/viewpage.action?pageId=194872297

分支情況 origin

  • master
  • develop
  • release
    • 20161129163217010_r_release_yingyongming
    • 20161029163217010_r_release_yingyongming
  • feature
    • 20161129_163448_newfeature_1
    • 20161129_163448_newfeature_2
  • hotfix
    • 20161129_163448_hotfix_1
  • tags
    • 20161129163217010_r_release_newfeature_yingyongming

      建立分支的時候直接操作:

      git checkout -b feature/20161129_163448_newfeature_1

  • master:master永遠是線上代碼,最穩定的分支,存放的是随時可供在生産環境中部署的代碼,當開發活動告一段落,産生了一份新的可供部署的代碼時,釋出成功之後,代碼才會由 aone2 送出到 master,master 分支上的代碼會被更新。應用上 aone2 後禁掉所有人的 master的寫權限
  • develop:儲存目前最新開發成果的分支。通常這個分支上的代碼也是可進行每日夜間釋出的代碼,隻對開發負責人開放develop權限。
  • feature: 功能特性分支,每個功能特性一個 feature/ 分支,開發完成自測通過後合并入 develop 分支。可以從 master 或者develop 中拉出來。
  • hotfix: 緊急bug分支修複分支。修複上線後,可以直接合并入master。

Git-Develop 分支模式是基于 Git 代碼庫設計的一種需要嚴格控制釋出品質和釋出節奏的開發模式。develop 作為固定的持續內建和釋出分支,并且分支上的代碼必須經過 CodeReview 後才可以送出到 Develop 分支。它的基本流程如下:

  • 每一個需求/變更都單獨從Master上建立一條Branch分支;
  • 使用者在這個Branch分支上進行Codeing活動;
  • 代碼達到釋出準入條件後aone上送出Codereview,Codereview通過後代碼自動合并到Develop分支;
  • 待所有計劃釋出的變更分支代碼都合并到Develop後,系統再 rebase master 代碼到Develop 分支,然後自行建構,打包,部署等動作。
  • 應用釋出成功後Aone會基于Develop分支的釋出版本打一個“目前線上版本Tag”基線;
  • 應用釋出成功後Aone會自動把Develop分支的釋出版本合并回master;

Branch 指令

  • 檢視分支:

    git branch

    git branch -v

    git branch -vv

    (檢視目前分支 tracking 哪個遠端分支)、

    git branch --merged

    git branch --no-merged

  • 建立分支:

    git branch branchname

    • 例: 基于 master 分支建立 dev 分支 :

      git branch dev

  • 基于之前的某個 Commit 新開分支:

    git branch branchname <sha1-of-commit>

    • 例: 基于上線的的送出 a207a38d634cc10441636bc4359cd8a18c502dea 建立 hotfix 分支 :

      git branch hotfix a207a38

    • 例: 基于 remoteBranch、localBranch、commitId、tag 建立分支均可以

      git checkout -b newbranch localBranch/remoteBranch/commitId/tag

    • 例: 建立一個空的分支
      git checkout --orphan gh-pages # 建立一個orphan的分支,這個分支是獨立的
      Switched to a new branch \'gh-pages\'
      git rm -rf . # 删除原來代碼樹下的所有檔案
      rm \'.gitignore\'
      #注意這個時候你用git branch指令是看不見目前分支的名字的,除非你進行了第一次commit。添加新的檔案,并且 commit 掉,就能看到分支了。           
  • 切換分支:

    git checkout branchname

    • 例: 由分支 master 切換到 dev 分支:

      git checkout dev

  • 建立新分支并切換到下面:

    git checkout -b branchname

    git branch branchname && git checkout branchname

    • 例:基于 master 分支建立 dev 分支,并切換到 dev 分支上:

      git checkout -b dev

      git branch dev && git checkout dev

  • 檢視分支代碼不同:

    git diff branchname

    比較 branchname 分支與目前分支的差異點,若隻看檔案差異,不看差異内容:

    git diff branchName --stat

  • 合并分支:

    git merge branchname

    将 branchname 分支代碼合并到目前分支
  • 删除分支:

    git branch -d branchname

    強制删除未合并過的分支:

    git branch -D branchname

  • 重命名分支:

    git branch -m dev development

    将分支 dev 重命名為 development
  • 檢視遠端分支:

    git branch -r

    git branch -r -v

  • 擷取遠端分支到本地:

    git checkout -b local-branchname origin/remote-branchname

  • 推送本地分支到遠端:

    git push origin remote-branchname

    git push origin local-branchname:remote-branchname

    • 将本地 dev 代碼推送到遠端 dev 分支:

      git push (-u) origin dev

      git push origin dev:dev

    • (技巧)将本地 dev 分支代碼推送到遠端 master 分支:

      git push origin dev:master

  • 删除遠端分支:

    git push origin :remote-branchname

    git push origin --delete remote-branchname

  • 手動跟蹤分支,master分支追蹤origin/next分支:

    git branch --track master origin/next

    git branch --set-upstream-to=origin/master

    看 git 的版本是否支援。
  • TrackingBranch,可以通過

    git branch -vv

    來檢視目前 track 的分支情況。建立立分支時會自動 track 相應遠端分支,

    git checkout -b sf origin/serverfix

    (Branch sf set up to track remote branch serverfix from origin. Switched to a new branch 'sf'). 也可以手動 track:

    git branch -u origin/serverfix

    (Branch serverfix set up to track remote branch serverfix from origin). 等同于指令

    git checkout --track origin/serverfix

“Checking out a local branch from a remote branch automatically creates what is called a “tracking branch” (or sometimes an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. However, you can set up other tracking branches if you wish – ones that track branches on other remotes, or don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. This is a common enough operation that git provides the --track shorthand:”

Tag

  • 檢視 tag:

    git tag

  • 查找指定 tag,比如查找 V1.0.* :

    git tag -l 'V1.0.*'

    會列出比對到的,比如 V1.0.1,V1.0.1.1,V1.0.2 等
  • 建立輕量級 tag(lightweight tags):

    git tag tag-name

    ,例如:

    git tag v1.0

  • 建立 tag(annotated tags):

    git tag -a tag-name -m 'msg'

    ,例如:

    git tag -a v1.0.0 -m '1.0.0版本上線完畢打tag'

    • annotated tags VS lightweight tags 可以通過指令真實檢視下:

      git show v1.0

      git show v1.0.0

    • “A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit.

      Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). ”

  • 檢視指定 tag 資訊:

    git show tag-name

  • 基于曆史某次送出(commit)建立 tag :

    git tag -a tagname <sha1-of-commit>

    • 例:基于上線時的送出 a207a38d634cc10441636bc4359cd8a18c502dea 建立tag:

      git tag -a v1.0.0 a207a38

  • 删除 tag :

    git tag -d tagname

  • 拉取遠端 tag 到本地:

    git pull remotename --tags

    例如:

    git pull origin --tags

  • 推送 tag 到遠端伺服器:

    git push remotename tagname

    git push origin v1.0.0

  • 将本地所有 tag 推送到遠端:

    git push remotename --tags

    git push origin --tags

  • 删除遠端 tag :

    git push origin :tagname

    git push origin --delete tagname

    git push origin :refs/tags/v0.9

Submodule

添加子子產品:$ git submodule add [url] [path]

如:$ git submodule add

git://github.com/soberh/ui-libs.git

src/main/webapp/ui-libs

初始化子子產品:$ git submodule init ----隻在首次檢出倉庫時運作一次就行

更新子子產品:$ git submodule update ----每次更新或切換分支後都需要運作一下

删除子子產品:(分4步走哦)

1) $ git rm --cached [path]

2) 編輯“.gitmodules”檔案,将子子產品的相關配置節點删除掉

3) 編輯“ .git/config”檔案,将子子產品的相關配置節點删除掉

4) 手動删除子子產品殘留的目錄

Stash

經常有這樣的事情發生,當你正在進行項目中某一部分的工作,裡面的東西處于一個比較雜亂的狀态,而你想轉到其他分支上進行一些工作。問題是,你不想送出進行了一半的工作,否則以後你無法回到這個工作點。解決這個問題的辦法就是

git stash

指令。

stash

可以擷取你工作目錄的中間狀态,也就是你修改過的被追蹤的檔案和暫存的變更,并将它儲存到一個未完結變更的堆棧中,随時可以重新應用。

usage: git stash list [<options>] 檢視目前 stash 的清單
   or: git stash show [<stash>] 檢視某一個版本的詳細内容
   or: git stash drop [-q|--quiet] [<stash>] 删除 stash 中内容
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>] 将 stash 中的代碼應用到工作區中
   or: git stash branch <branchname> [<stash>]
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
               [-u|--include-untracked] [-a|--all] [<message>]]
   or: git stash clear  清空 stash 中所有内容           

oh-my-zsh 常用指令

alias g='git'
alias ga='git add'
alias gco='git checkout'
alias gcb='git checkout -b'
alias gcm='git checkout master'
alias gcd='git checkout develop'
alias gd='git diff'
alias gf='git fetch'
alias gfo='git fetch origin'
alias gl='git pull'
alias gp='git push'           

繼續閱讀