SyntaxHighlighter

About Me

2015年12月28日 星期一

Git 常用指令

git

整理一下 git 比較常用的一些指令

git

如何使用 git 縮寫

> vim ~/.gitconfig

# 加上下面內容
[alias]
      st = status
      co = checkout
      br = branch
      up = rebase
      ci = commit
      di = diff

git 指令與縮寫後的指令比較

git status   = git st
git checkout = git co
git branch   = git br
git rebase   = git up
git commit   = git ci
git diff     = git di

git 指令(以下都用縮寫)

  1. Status

     git st
    
  2. Branch

     # 顯示目前 local git 的 branch
     git br
    
     # 顯示所有 branch (包含 remote branch))
     git br -a
    
  3. Add

     # add single file
     git add <file_path>
    
     # add all unstage file
     git add .
    
     # 強制新增 ignore file in git
     git add -f [file_path]
    
  4. Commit

     git ci -m "content"
     git ci --amend -c {commit hash}
    
  5. Checkout

     # 切換到任何一個 branch 或是任何一點 commit hash 上
     git co <branch_name>
     git co <commit_hash_number>
    
     # 在現在的 branch 上面新開一個 branch 
     git co -b <new_branch_name>
    
     # checkout 一個 remote branch 
     git co -b <local_branch_name> --track origin/<remote_branch_name>
     EX: git co -b feature/123 --track origin/feature/123
    
     # 清除所有 unstage 檔案到上一次的 commit
     git co -- .
    
  6. Diff

     # 查看目前所有 git 的更動內容
     git diff
    
     # 查看某個檔案的變動
     git diff <file_path>
    
     # 查看 git 更動內容並且忽略空白
     git diff --ignore-all-space
    
  7. Merge

     # 一般直接 merge (ex: feature/123 merge to develop)
     # 建議先 pull 在 merge
     git co develop
     git merge feature/123
    
     # 保留 feature branch 上的線
     # 建議先 pull 在 merge
     git co develop
     git merge --no-ff feature/123
    
  8. Pull

     git pull or git pull origin <branch_name>
    
  9. Push

     # 一般的 push 方法
     git push or git push origin <branch_name>
    
     # 與 tag 一起 push
     git push --follow-tags or git push --follow-tags origin <branch_name>
    
     # Delete remote git branch
     git push origin --delete [branch_name]
    
  10. Fetch

     # 同步目前遠端 git 的狀態
     git fetch origin
    
  11. Tag

     # 顯示目前的 tag 狀態
     git tag
    
     # 在某個 commit hash 上面加上 tag
     git tag -a <tag_name> -m "<tag_content>" <commit_hash>
     EX: git tag -a v1.1.0 -m "test1" a9bf65b
    
     # 查詢 tag 的詳細資料
     git show <tag_name>
     EX: git show v1.1.0
    
  12. Reset

     # reset all include unstage
     git reset -q --hard HEAD --
    
     # hard reset 到某次 commit
     ps. 小心使用,中間的所有 commit 都會消失,必須透過 git reflog 去還原之前的 commit
     git reset --hard <commit_hash>
    
  13. Git Flow

    • 認識 git flow: ihower - Git flow 開發流程
    • 安裝 git flow on mac: git-flow cheatsheet
    • 簡單介紹 git flow 種類
      • feature
        • 從 develop 分支出去,常用於開發新的功能,開發完成後就會 merge 回 develop 上
      • hotfix
        • 從 master 分支出去,常用於 stable 版的緊急修復,修復完成後會 merge 回 master 跟 develop 上
      • release
        • 從 develop 分支出去,用於開發版本要釋出成 stable 版,釋出完畢會 merge 到 master 跟 develop 上

    初始化 git flow

    git flow init

    使用 git flow feature 流程

    git flow feature start 123 // 從 develop 建立一個 feature branch 123 出來 git flow feature publish 123 // 將 feature/123 這個 branch push 到 remote git repo 上 git flow feature finish 123 // 將 feature/123 這個 feature branch 結束後 merge 回 develop 上,並且會將這個 feature branch 移除

    使用 git flow release 流程

    git flow release start v1.1.0 // 從 develop 建立一個 release branch v1.1.0 git flow feature publish v1.1.0 // 將 release/v1.1.0 這個 branch push 到 remote git repo 上 git flow feature finish v1.1.0 // 將 release/v1.1.0 這個 release branch 結束後 merge 進 master 與 develop 上並且壓上版本號,然後將這個 release branch 移除

    使用 git flow hotfix 流程

    git flow hotfix start v1.1.0 // 從 master 建立一個 hotfix branch v1.1.0 git flow hotfix finish v1.1.0 // 將 hotfix/v1.1.0 這個 branch 結束後 merge 回 master 與 develop 上並且壓上版本號,然後將這個 hotfix branch 移除

沒有留言:

張貼留言