1. 程式人生 > >Git 處理tag和branch的命令

Git 處理tag和branch的命令

最近想給GitHub 上的專案設定tag,可是使用GitHub Desktop,找了一圈都沒找到快速設定Tag 的地方,最後只能通過終端命令來添加了。
想要檢視Git 的命令,可以使用

git --help

可是大致看一下git的命令:

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an
empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink reset Reset current HEAD to the specified state rm Remove files
from the working tree and from the index examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the
working tree status grow, mark and tweak your common history branch List, create, or delete branches checkout Switch branches or restore working tree files commit Record changes to the repository diff Show changes between commits, commit and working tree, etc merge Join two or more development histories together rebase Forward-port local commits to the updated upstream head tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects

然後使用 git help -agit help -g 可以檢視git 的一些子命令和一些概念性的東西。

使用 git help <command>git help <concept> 可以檢視某個命令的引數 和描述等詳細資訊。

要對某個Git 工程做處理,我們首先要先進入該工程的根目錄。
可以使用 cd 工程路徑 來進入某個工程目錄。
然後可以使用 ls -a 或者 ls -al 檢視工程目錄下是否有隱藏的 .git 檔案。
例如我的操作:

bogon:HLBluetoothDemo harvey$ ls -al
total 32
drwxr-xr-x   7 harvey  staff   238 11 18 17:41 .
drwxr-xr-x  63 harvey  staff  2142 11  1 11:03 ..
-rw-r--r--@  1 harvey  staff  6148  9 28 15:53 .DS_Store
drwxr-xr-x  16 harvey  staff   544 11 18 17:41 .git
drwxr-xr-x  23 harvey  staff   782 11 18 17:41 HLBluetoothDemo
drwxr-xr-x   5 harvey  staff   170 11 18 17:41 HLBluetoothDemo.xcodeproj
-rw-r--r--   1 harvey  staff  5066 11 18 17:41 README.md

Commit 操作

//檢視所有提交記錄
git log

// 檢視簡寫的提交記錄
git log --oneline

Tag 操作

我們可以對某一個穩定的版本設定一個Tag,這樣在以後回頭來看,也更方便和直接。

檢視Tag 命令

// 檢視本地的所有Tag
git tag
// 檢視某一系列tag
git tag -l v1.*

建立Tag 命令

git tag -a v1.0 -m "對Tag的描述資訊"

我們也可以對以前的某次commit 設定tag。
具體的做法是,首先執行 git log --oneline ,找到這次提交的唯一id。
例如:

bogon:HLBluetoothDemo harvey$ git log --oneline
2b04b38 利用iOS 9後的系統API獲取特性可寫入的最大長度來分割傳送。
1468bbe Update README.md
fba0db7 斷開連線時,將connectedperpwheral 置為nil
ee76062 Update README.md
576e179 Update README.md

上面的 描述資訊前的值,就是id。

然後執行 git tag -a v1.1 2b04b38 -m "Tag的描述資訊"

我們可以在本地新增多個tag 後,執行以下 git tag 檢視一下本地的tag。

提交本地的Tag 到遠端伺服器的命令

git push origin --tags

然後,我們就可以去github 上看自己的工程裡的tags 了。

刪除本地的tag 命令

git tag -d v1.0

上面這個命令只能刪除本地的tag,如果這個tag 已經提交到遠端伺服器之後,上面的命令並不能刪除遠端伺服器上的tag。
要刪除遠端伺服器上的tag,可以使用如下的命令:

git push origin --delete tag v1.0

branch 操作

檢視branch 的命令

git branch -a

在終端裡,遠端的branch會顯示為紅色,如下圖所示:

這裡寫圖片描述

刪除遠端branch 的命令

git push origin --delete <branchName>

目前branch 就用到這麼多,更詳細的,使用git help branch 檢視吧。
以後再補充了。