1. 程式人生 > 其它 ># Git 基礎命令

# Git 基礎命令

第一章

第二章

2.1Git安裝

2.1.1 Git原始碼安裝

2.1.1 Git 原始碼包下載地址

https://mirrors.edge.kernel.org/pub/software/scm/git/

2.1.2 Git 安裝步驟

##原始碼編譯git需要先安裝支援git的依賴包
[root@Git02 ~]# yum -y install curl-devel expat-devel gettext-devel zlib-devel perl-ExtUtils-Embed
[root@Git02 ~]# tar xf git-2.9.5.tar.gz -C /usr/src/
[root@Git02 src]# cd /usr/src/git-2.9.5/
[root@Git02 git-2.9.5]# ./configure --prefix=/usr/local/git &>/dev/null && make &>/dev/null && make install &>/dev/null
##做軟連結
[root@Git02 git-2.9.5]# ln -s /usr/local/git/bin/* /usr/bin
[root@Git02 git-2.9.5]# ln -s /usr/libexec/git-core/* /usr/bin
##這裡需要用git2.9.5版本的git,這樣才有svn到git的資料遷移的一些命令
[root@Git02 git-2.9.5]# find / -type f -name 'git-svn'
/usr/local/git/libexec/git-core/git-svn
/usr/src/git-2.9.5/git-svn
[root@Git02 git-2.9.5]# ln -s /usr/local/git/libexec/git-core/git-svn /usr/bin/git-svn
[root@Git02 git-2.9.5]# which git-svn
/usr/bin/git-svn

2.2 Git 命令基礎

[root@Git01 git]# LANG=zh_CN.UTF8
[root@Git01 git]# git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

這些是各種場合常見的 Git 命令:

開始一個工作區(參見:git help tutorial)
   clone      克隆一個倉庫到一個新目錄
   init       建立一個空的 Git 倉庫或重新初始化一個已存在的倉庫

在當前變更上工作(參見:git help everyday)
   add        新增檔案內容至索引
   mv         移動或重新命名一個檔案、目錄或符號連結
   reset      重置當前 HEAD 到指定狀態
   rm         從工作區和索引中刪除檔案

檢查歷史和狀態(參見:git help revisions)
   bisect     通過二分查詢定位引入 bug 的提交
   grep       輸出和模式匹配的行
   log        顯示提交日誌
   show       顯示各種型別的物件
   status     顯示工作區狀態

擴充套件、標記和調校您的歷史記錄
   branch     列出、建立或刪除分支
   checkout   切換分支或恢復工作區檔案
   commit     記錄變更到倉庫
   diff       顯示提交之間、提交和工作區之間等的差異
   merge      合併兩個或更多開發歷史
   rebase     在另一個分支上重新應用提交
   tag        建立、列出、刪除或校驗一個 GPG 簽名的標籤物件

協同(參見:git help workflows)
   fetch      從另外一個倉庫下載物件和引用
   pull       獲取並整合另外的倉庫或一個本地分支
   push       更新遠端引用和相關的物件

命令 'git help -a' 和 'git help -g' 顯示可用的子命令和一些概念幫助。
檢視 'git help <命令>' 或 'git help <概念>' 以獲取給定子命令或概念的
幫助。

2.2.1 Git init 初始化GIT工作目錄

[root@Git01 ~]# mkdir -p /mycode
[root@Git01 ~]# cd /mycode
[root@Git01 mycode]# git init
初始化空的 Git 倉庫於 /mycode/.git/
[root@Git01 mycode]# ls -la
總用量 0
drwxr-xr-x   3 root root  18 6月  11 15:46 .
dr-xr-xr-x. 18 root root 238 6月  11 15:46 ..
drwxr-xr-x   7 root root 119 6月  11 15:46 .git

2.2.2 Git add 將檔案提交到暫存區

[root@Git01 mycode]# touch test.txt
[root@Git01 mycode]# git add test.txt 
[root@Git01 mycode]# git status
位於分支 master

初始提交

要提交的變更:
  (使用 "git rm --cached <檔案>..." 以取消暫存)

	新檔案:   test.txt

2.2.3 Git commit 將暫存區的檔案提交到本地倉庫

[root@Git01 mycode]# git commit -m "test.txt"

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@Git01.(none)')

這裡報錯是因為之前沒有做全域性變數

[root@Git01 mycode]# git config --global user.email "[email protected]"
[root@Git01 mycode]# git config --global user.name "Rock"
[root@Git01 mycode]# git commit -m "test.txt"
[master(根提交) 4889630] test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git01 mycode]# git status
位於分支 master
nothing to commit, working tree clean

2.2.4 Git remote 用於管理遠端倉庫

# 檢視遠端倉庫
[root@Git01 mycode]# git remote -v
[root@Git01 mycode]# git remote add --help
用法:git remote add [<選項>] <名稱> <地址>

    -f, --fetch           抓取遠端的分支
    --tags                抓取時匯入所有的標籤和關聯物件
                          或不抓取任何標籤(--no-tags)
    -t, --track <分支>    跟蹤的分支
    -m, --master <分支>   主線分支
    --mirror[=<push|fetch>]
                          把遠端設定為用以推送或抓取的映象
  • 建立遠端倉庫,如下圖所示:

[root@Git01 mycode]# git remote add origin
# origin 為代號,如果不設定預設的代號為origin
[root@Git01 mycode]# git remote add test https://github.com/Rock940120/rock.git
# 這裡 test 為自己設定的代號
[root@Git01 mycode]# git push -u test master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com': 
物件計數中: 3, 完成.
寫入物件中: 100% (3/3), 202 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
 * [new branch]      master -> master
分支 master 設定為跟蹤來自 test 的遠端分支 master。
  • 上傳成功後,可以在遠端倉庫重新整理後看到傳上來的test.txt,如圖:

2.2.5 Git clone 克隆一個現有倉庫到本地

新建立一個Git02,在Git02上來模擬克隆主分支上的master版本

[root@Git02 ~]# mkdir /mycode
[root@Git02 ~]# cd /mycode
[root@Git02 mycode]# git clone https://github.com/Rock940120/rock.git master
Cloning into 'master'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@Git02 mycode]# ll -ld *
drwxr-xr-x 3 root root 34 Jun 11 16:52 master
[root@Git02 mycode]# cd master/
[root@Git02 master]# ls
test.txt
[root@Git02 master]# git status
# On branch master
nothing to commit, working directory clean
[root@Git02 master]# echo "welcome to BeiJing" > test.txt 
[root@Git02 master]# LANG=zh_CN.UTF8
[root@Git02 master]# git add test.txt 
[root@Git02 master]# git status
# 位於分支 master
# 要提交的變更:
#   (使用 "git reset HEAD <file>..." 撤出暫存區)
#
#	修改:      test.txt

  • 設定全域性變數
[root@Git02 master]# git config --global user.email "[email protected]"
[root@Git02 master]# git config --global user.name "Rock"
## 提交到本地倉庫
[root@Git02 master]# git commit -m "user02"
[master 64c448b] user02
 1 file changed, 1 insertion(+)
[root@Git02 master]# git status
# 位於分支 master
# 您的分支領先 'origin/master' 共 1 個提交。
#   (使用 "git push" 來發布您的本地提交)
#
無檔案要提交,乾淨的工作區
  • 檢視遠端倉庫

[root@Git02 master]# git remote -v
origin	https://github.com/Rock940120/rock.git (fetch)
origin	https://github.com/Rock940120/rock.git (push)
## 這裡沒有設定遠端倉庫,克隆下來代號預設為origin,

## 推送到遠端倉庫
[root@Git02 master]# git push -u origin master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 249 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   4889630..64c448b  master -> master
分支 master 設定為跟蹤來自 origin 的遠端分支 master。

  • 檢視遠端倉庫,看代號為users,版本已經更新,如下圖所示:

2.2.6 Git fetch 將遠端倉庫的某個版本同步到本地倉庫

當修改git02的test後,將test提交到遠端倉庫,然後在git01拉取到git01的本地倉庫。
[root@Git01 mycode]# cat test.txt 
[root@Git01 mycode]# git remote -v
test	https://github.com/Rock940120/rock.git (fetch)
test	https://github.com/Rock940120/rock.git (push)
[root@Git01 mycode]# git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
展開物件中: 100% (3/3), 完成.
來自 https://github.com/Rock940120/rock
   4889630..64c448b  master     -> test/master
[root@Git01 mycode]# cat test.txt
###這裡只是同步到本地倉庫,工作目錄檔案沒有改變
###如果將某個使用者的某個分支同步,下面命令
[root@Git01 mycode]# git fetch -u test master

2.2.7 Git merge 將本地倉庫同步到工作目錄

#將git01本地倉庫同步到本地目錄
[root@Git01 mycode]# git status
位於分支 master
您的分支落後 'test/master' 共 1 個提交,並且可以快進。
  (使用 "git pull" 來更新您的本地分支)
nothing to commit, working tree clean
[root@Git01 mycode]# git merge test/master
更新 4889630..64c448b
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)

2.2.7 Git pull 將遠端倉庫的變更拉取到本地倉庫,並更新本地工作目錄

git pull 相當於 git fetch + git merge
開發一般不會用這種方法拉取程式碼,因為一旦有版本衝突,這樣就沒辦法修改程式的。

[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
[root@Git01 mycode]# echo "git01" >> test.txt 
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)

	修改:     test.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@Git01 mycode]# git add test.txt 
[root@Git01 mycode]# git commit -m "git01"
[master f28df52] git01
 1 file changed, 1 insertion(+)
[root@Git01 mycode]# git push -u test master
Username for 'https://github.com': Rock940120
Password for 'https://[email protected]': 
物件計數中: 3, 完成.
寫入物件中: 100% (3/3), 254 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   64c448b..f28df52  master -> master
分支 master 設定為跟蹤來自 test 的遠端分支 master。

###
賬號:Rock940120
密碼:wl545355.

在Git02直接拉取程式碼
[root@Git02 master]# git status
# 位於分支 master
無檔案要提交,乾淨的工作區
[root@Git02 master]# git remote -v
origin	https://github.com/Rock940120/rock.git (fetch)
origin	https://github.com/Rock940120/rock.git (push)
[root@Git02 master]# git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
來自 https://github.com/Rock940120/rock
 * branch            master     -> FETCH_HEAD
更新 64c448b..f28df52
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
[root@Git02 master]# cat test.txt 
welcome to BeiJing
git01

2.2.8 Git mv && Git reset 暫存區檔案的修改和撤銷

[root@Git01 mycode]# touch change
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	change

提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
[root@Git01 mycode]# git add change 
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   change

[root@Git01 mycode]# mv change changed
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   change

尚未暫存以備提交的變更:
  (使用 "git add/rm <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)

	刪除:     change

未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	changed

[root@Git01 mycode]# git add changed 
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   change
	新檔案:   changed

尚未暫存以備提交的變更:
  (使用 "git add/rm <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)

	刪除:     change

[root@Git01 mycode]# git rm change
rm 'change'
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   changed

[root@Git01 mycode]# git mv changed change
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   change

[root@Git01 mycode]# git reset change 
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	change

提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
[root@Git01 mycode]# git add *
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   change


2.2.9 Git diff 檔案對比

將本地工作目錄的檔案與本地倉庫中的檔案進行對比

[root@Git01 mycode]# git reset change
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	change

提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
[root@Git01 mycode]# rm -rf change 
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
nothing to commit, working tree clean
[root@Git01 mycode]# echo "wendy" >> test.txt 
[root@Git01 mycode]# git diff test.txt 
diff --git a/test.txt b/test.txt
index 24c8146..a205cdb 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 welcome to BeiJing
 git01
+wendy
[root@Git01 mycode]# vim test.txt 
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# git diff test.txt 
diff --git a/test.txt b/test.txt
index 24c8146..ecb0ac7 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 welcome to BeiJing
-git01
+wendy

2.2.10 Git log 檢視git提交歷史紀錄

  • Git log 檢視提交歷史紀錄
  • git log -2:檢視最近幾條記錄
  • git log -p -1 : 顯示每次提交內容差異
  • git log --stat -2:stat簡要顯示資料增改行數,這樣就能看到提交中修改的內容
  • git log -- pretty=oneline:一行顯示提交的歷史紀錄
[root@Git01 mycode]# git log -2
commit f28df52dfa37c6c90573ec677a1b8d104e36641f
Author: Rock <[email protected]>
Date:   Wed Jun 17 10:47:42 2020 +0800

    git01

commit 64c448b325a650c2402a6e4d6f85a5eef99e9600
Author: Rock <[email protected]>
Date:   Thu Jun 11 17:02:56 2020 +0800

    user02
[root@Git01 mycode]# git log -p -1
commit f28df52dfa37c6c90573ec677a1b8d104e36641f
Author: Rock <[email protected]>
Date:   Wed Jun 17 10:47:42 2020 +0800

    git01

diff --git a/test.txt b/test.txt
index 14908ba..24c8146 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 welcome to BeiJing
+git01
[root@Git01 mycode]# git log --pretty=oneline
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git add *
[root@Git01 mycode]# git commit -m "ceshi"
[master ae81646] ceshi
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt

2.3 Git 追根述源-git log

2.3.1 Git還原歷史資料

Git服務程式中有個叫HEAD的版本指標,當用戶申請還原資料時,其實就是將HEAD指標指向到某個特定的提交版本,但是因為Git是分散式版本控制系統,為了避免歷史記錄衝突,故使用了SHA-1計算出十六進位制的雜湊字串來區分每個提交版本,另外預設的HEAD版本指標會指向到最近的一次提交版本記錄,而上一次提交版本會叫HEAD,上上個個版本則會叫做HEAD^,當然一般會用HEAD-5來表示往上數第五個提交版本。

  • git reset --hard HEAD^ #-->還原歷史提交版本上一次
  • git reset --hard 4889630383afc854273be11f9c0e320041f0a98f #--> 找到歷史還原點的SHA-1值後,就可以還原
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git reset --hard HEAD^
HEAD 現在位於 f28df52 git01
[root@Git01 mycode]# git log --pretty=oneline
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
git01
[root@Git01 mycode]# git reset --hard 4889630383afc854273be11f9c0e320041f0a98f
HEAD 現在位於 4889630 test.txt
[root@Git01 mycode]# cat test.txt

2.3.2 Git reflog 還原未來資料

當git回滾到某個版本提交後,解決辦法如下:

  • GIt reflog: 檢視未來歷史更新點
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git reflog
ae81646 HEAD@{0}: reset: moving to ae81646
4889630 HEAD@{1}: reset: moving to 4889630383afc854273be11f9c0e320041f0a98f
f28df52 HEAD@{2}: reset: moving to HEAD^
ae81646 HEAD@{3}: commit: ceshi
f28df52 HEAD@{4}: commit: git01
64c448b HEAD@{5}: merge test/master: Fast-forward
4889630 HEAD@{6}: commit (initial): test.txt
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy

2.3.3 Git tag 標籤使用

前面回滾使用的是一串字串,又長又難記
git tag <標籤> -m "描述"
每次提交可以打一個標籤
|命令|說明|
|:-|:-|
|git tag|檢視當前所有標籤|
|git show V1.0|檢視當前v1.0版本的詳細資訊|
|git tag v1.0 -m "描述"|建立標籤|
|git tag -d v1.0|刪除v1.0標籤|

[root@Git01 mycode]# git tag
[root@Git01 mycode]# git tag v1.0
[root@Git01 mycode]# git tag
v1.0
[root@Git01 mycode]# git show v1.0
commit ae81646948b78fea961fe2fd3c252b8e8d0dcc0e
Author: Rock <[email protected]>
Date:   Wed Jun 17 11:37:05 2020 +0800

    ceshi

diff --git a/test.txt b/test.txt
index 24c8146..ecb0ac7 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 welcome to BeiJing
-git01
+wendy
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# echo "i miss you" >> test.txt 
[root@Git01 mycode]# git add * 
[root@Git01 mycode]# git commit -m "v1.0"
[master 52fe609] v1.0
 1 file changed, 1 insertion(+)
[root@Git01 mycode]# git status
位於分支 master
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
nothing to commit, working tree clean
[root@Git01 mycode]# git tag v2.0
[root@Git01 mycode]# git show v2.0
commit 52fe609625a9ccea56ef8a3d8932df8474d2579f
Author: Rock <[email protected]>
Date:   Wed Jun 17 14:27:25 2020 +0800

    v1.0

diff --git a/test.txt b/test.txt
index ecb0ac7..a2a418d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 welcome to BeiJing
 wendy
+i miss you
[root@Git01 mycode]# git tag
v1.0
v2.0
[root@Git01 mycode]# git reset --hard v1.0
HEAD 現在位於 ae81646 ceshi
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# git reset --hard v2.0
HEAD 現在位於 52fe609 v1.0
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
i miss you

2.3.4 Gitignore 檔案

[root@Git01 mycode]# vim .gitignore
[root@Git01 mycode]# cat .gitignore 
target
*.log
?.idea
[root@Git01 mycode]# git status
位於分支 master
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	.gitignore

提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
[root@Git01 mycode]# git add .gitignore 
[root@Git01 mycode]# git status
位於分支 master
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	新檔案:   .gitignore

[root@Git01 mycode]# git commit -m "提交gitignore"
[master 800a025] 提交gitignore
 1 file changed, 3 insertions(+)
 create mode 100644 .gitignore
[root@Git01 mycode]# git push test master
Username for 'https://github.com': Rock940120
Password for 'https://[email protected]': 
物件計數中: 9, 完成.
壓縮物件中: 100% (4/4), 完成.
寫入物件中: 100% (9/9), 759 bytes | 0 bytes/s, 完成.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   f28df52..800a025  master -> master

[root@Git01 mycode]# touch 111.log
[root@Git01 mycode]# touch 222.idea
[root@Git01 mycode]# touch 333
[root@Git01 mycode]# git status
位於分支 master
您的分支與上游分支 'test/master' 一致。
未跟蹤的檔案:
  (使用 "git add <檔案>..." 以包含要提交的內容)

	222.idea
	333

提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
  • 在Git02克隆程式碼
[root@Git02 master]# rm -rf /mycode/
[root@Git02 master]# mkdir /mycode
[root@Git02 master]# cd /mycode
[root@Git02 mycode]# git clone https://github.com/Rock940120/rock.git
正克隆到 'rock'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 18 (delta 0), reused 18 (delta 0), pack-reused 0
Unpacking objects: 100% (18/18), done.
[root@Git02 mycode]# ls
rock
[root@Git02 mycode]# cd rock/
[root@Git02 rock]# ls -la
總用量 8
drwxr-xr-x 3 root root  52 6月  17 14:48 .
drwxr-xr-x 3 root root  18 6月  17 14:48 ..
drwxr-xr-x 8 root root 163 6月  17 14:48 .git
-rw-r--r-- 1 root root  20 6月  17 14:48 .gitignore
-rw-r--r-- 1 root root  36 6月  17 14:48 test.txt

第三章

3.1 Git的分支管理

在實際的專案開發中,進來保證master分支穩定,僅用於釋出新版本,平時不要隨便直接修改裡面的資料檔案。幹活都會在dev分支上,每個人從dev分支上建立自己個人分支,開發完合併到dev分支,最後dev分支合併到master分支。

3.2 Git 本地分支管理

3.2.1 本地分支的建立與切換

命令 說明
git branch 檢視當前分支情況
git branch linux 建立分支
git checkout 檢查本地分支與遠端分支的變更差異
git checkout linux 切換分支
[root@Git01 mycode]# git branch
* master
[root@Git01 mycode]# git branch dev
[root@Git01 mycode]# git branch
  dev
* master
[root@Git01 mycode]# git checkout dev
切換到分支 'dev'
[root@Git01 mycode]# git branch
* dev
  master
[root@Git01 mycode]# ls
111.log  222.idea  333  test.txt
[root@Git01 mycode]# rm -rf 111.log 222.idea 
[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# git status
位於分支 dev
尚未暫存以備提交的變更:
  (使用 "git add/rm <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)

	刪除:     222.idea

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@Git01 mycode]# git rm 222.idea
rm '222.idea'
[root@Git01 mycode]# git status 
位於分支 dev
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)

	刪除:     222.idea

[root@Git01 mycode]# git commit -m "dev1.0"
[dev 7d0b5bf] dev1.0
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 222.idea
[root@Git01 mycode]# git status
位於分支 dev
nothing to commit, working tree clean
[root@Git01 mycode]# git remote -v
test	https://github.com/Rock940120/rock.git (fetch)
test	https://github.com/Rock940120/rock.git (push)
[root@Git01 mycode]# git push -u test dev
Username for 'https://github.com': Rock940120
Password for 'https://[email protected]': 
物件計數中: 7, 完成.
壓縮物件中: 100% (6/6), 完成.
寫入物件中: 100% (7/7), 622 bytes | 0 bytes/s, 完成.
Total 7 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/Rock940120/rock/pull/new/dev
remote: 
To https://github.com/Rock940120/rock.git
 * [new branch]      dev -> dev
分支 dev 設定為跟蹤來自 test 的遠端分支 dev。
[root@Git01 mycode]# git branch
* dev
  master
[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# git checkout master
切換到分支 'master'
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  test.txt
[root@Git01 mycode]# git checkout dev
切換到分支 'dev'
您的分支與上游分支 'test/dev' 一致。
[root@Git01 mycode]# ls
333  test.txt

切換分支的時候,要清空暫存區,暫存區是公用的

[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# touch QQ
[root@Git01 mycode]# git add QQ
[root@Git01 mycode]# git checkout master
切換到分支 'master'
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  QQ  test.txt
[root@Git01 mycode]# git checkout dev
A	QQ
切換到分支 'dev'
您的分支與上游分支 'test/dev' 一致。
[root@Git01 mycode]# git commit -m "提交"
[dev ee6a4bc] 提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 QQ
[root@Git01 mycode]# git checkout master
切換到分支 'master'
您的分支領先 'test/master' 共 2 個提交。
  (使用 "git push" 來發布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  test.txt

3.3 本地分支的合併與刪除

想把linux的工作成果合併到master分支上
先切換到master分支
git merge dev分支到master
git branch -d dev 確定合併後,可以放心的刪掉dev分支

3.3.1 自動合併本地fen分支

[root@Git01 mycode]# git branch
  dev
 * master
[root@Git01 mycode]# git merge dev
更新 a1c2e44..ee6a4bc
Fast-forward
 222.idea => QQ | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 222.idea => QQ (100%)
[root@Git01 mycode]# ls
333  QQ  test.txt
[root@Git01 mycode]# git branch -d dev
warning: 並未刪除分支 'dev', 雖然它已經合併到 HEAD,
         然而卻尚未被合併到分支 'refs/remotes/test/dev' 。
error: 分支 'dev' 沒有完全合併。
如果您確認要刪除它,執行 'git branch -D dev'。
[root@Git01 mycode]# git checkout dev
切換到分支 'dev'
您的分支領先 'test/dev' 共 1 個提交。
  (使用 "git push" 來發布您的本地提交)
[root@Git01 mycode]# git branch 
 * dev
  master
[root@Git01 mycode]# git status
位於分支 dev
您的分支領先 'test/dev' 共 1 個提交。
  (使用 "git push" 來發布您的本地提交)
nothing to commit, working tree clean
[root@Git01 mycode]# git push -u test dev 
Username for 'https://github.com': Rock940120
Password for 'https://[email protected]': 
物件計數中: 2, 完成.
壓縮物件中: 100% (2/2), 完成.
寫入物件中: 100% (2/2), 223 bytes | 0 bytes/s, 完成.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Rock940120/rock.git
   7d0b5bf..ee6a4bc  dev -> dev
分支 dev 設定為跟蹤來自 test 的遠端分支 dev。
[root@Git01 mycode]# git checkout master
切換到分支 'master'
您的分支領先 'test/master' 共 4 個提交。
  (使用 "git push" 來發布您的本地提交)
[root@Git01 mycode]# git merge dev
Already up-to-date.
[root@Git01 mycode]# git branch -d dev
已刪除分支 dev(曾為 ee6a4bc)。

3.4 Git遠端分支管理

3.4.1 將linux本地分支程式碼和標籤推送到github遠端倉庫的linux分支

[root@Git01 mycode]# touch tag.txt
[root@Git01 mycode]# ls
333  QQ  tag.txt  test.txt
[root@Git01 mycode]# 
[root@Git01 mycode]# git add tag.txt 
[root@Git01 mycode]# git commit -m "tag.txt"
[master 032659f] tag.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tag.txt
[root@Git01 mycode]# git tag v3.0 -m "推送標籤測試"
[root@Git01 mycode]# git push -u test v3.0
Username for 'https://github.com': Rock940120
Password for 'https://[email protected]': 
物件計數中: 3, 完成.
壓縮物件中: 100% (3/3), 完成.
寫入物件中: 100% (3/3), 369 bytes | 0 bytes/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Rock940120/rock.git
 * [new tag]         v3.0 -> v3.0

3.4 普通使用者建立倉庫

企業一般用普通使用者建立git倉庫,建立只用於作為倉庫,不作為工作目錄的倉庫可以用git --bare init 初始化倉庫

  • 在Git01初始化普通使用者倉庫
[root@Git01 ~]# useradd git
[root@Git01 ~]# echo "666666" | passwd --stdin git
更改使用者 git 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[root@Git01 ~]# useradd git
[root@Git01 ~]# echo "666666" | passwd --stdin git
更改使用者 git 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[git@Git01 ~]# mkdir repos
[git@Git01 ~]# cd repos
[git@Git01 repos]# mkdir dev webapp benet
[git@Git01 repos]# ls
benet  dev  webapp
[git@Git01 repos]# cd dev
[git@Git01 dev]# git --bare init
初始化空的 Git 版本庫於 /home/git/repos/dev/
[git@Git01 repos]# cd dev/
[git@Git01 dev]# ls -la
總用量 12
drwxrwxr-x 7 git git 119 6月  20 13:16 .
drwxrwxr-x 5 git git  44 6月  20 13:15 ..
drwxrwxr-x 2 git git   6 6月  20 13:16 branches
-rw-rw-r-- 1 git git  66 6月  20 13:16 config
-rw-rw-r-- 1 git git  73 6月  20 13:16 description
-rw-rw-r-- 1 git git  23 6月  20 13:16 HEAD
drwxrwxr-x 2 git git 242 6月  20 13:16 hooks
drwxrwxr-x 2 git git  21 6月  20 13:16 info
drwxrwxr-x 4 git git  30 6月  20 13:16 objects
drwxrwxr-x 4 git git  31 6月  20 13:16 refs
  • Git02克隆Git01的普通使用者倉庫
[root@Git02 ~]# mkdir /mycode
[root@Git02 ~]# cd /mycode
[root@Git02 mycode]# git init
初始化空的 Git 版本庫於 /mycode/.git/
[root@Git02 mycode]# git clone [email protected]:/home/git/repos/dev
正克隆到 'dev'...
The authenticity of host '192.168.43.4 (192.168.43.4)' can't be established.
ECDSA key fingerprint is SHA256:4c6EfrB36b2OGYchGBiXZdK7nboDcpfQ2h8LIsGEGKk.
ECDSA key fingerprint is MD5:db:d1:0d:a6:80:f8:c8:03:e8:e0:27:66:cd:a3:0e:a1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.43.4' (ECDSA) to the list of known hosts.
[email protected]'s password: 
warning: 您似乎克隆了一個空版本庫。
[root@Git02 mycode]# ls
dev
[root@Git02 mycode]# ls dev/
[root@Git02 mycode]# ls -la dev/
總用量 0
drwxr-xr-x 3 root root  18 6月  20 14:57 .
drwxr-xr-x 4 root root  29 6月  20 14:57 ..
drwxr-xr-x 7 root root 119 6月  20 14:57 .git
[root@Git02 mycode]# cd dev/
[root@Git02 dev]# touch test.txt
[root@Git02 dev]# git add test.txt 
[root@Git02 dev]# git status
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
#   (使用 "git rm --cached <file>..." 撤出暫存區)
#
#	新檔案:    test.txt
#
[root@Git02 dev]# git commit -m "V_01"
[master(根提交) 3107d23] V_01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git02 dev]# git status
# 位於分支 master
無檔案要提交,乾淨的工作區
[root@Git02 dev]# git remote -v
origin	[email protected]:/home/git/repos/dev (fetch)
origin	[email protected]:/home/git/repos/dev (push)
[root@Git02 dev]# git commit -m "V_01"
[master(根提交) 3107d23] V_01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git02 dev]# git status
# 位於分支 master
無檔案要提交,乾淨的工作區
[root@Git02 dev]# git remote -v
origin	[email protected]:/home/git/repos/dev (fetch)
origin	[email protected]:/home/git/repos/dev (push)