Git結構

Git和代碼托管中心
代碼托管中心的任務:維護遠端庫
區域網路環境下:GitLab伺服器
外網環境下:碼雲
本地庫和遠端庫的互動方式
團隊内部協作
$ git init // 初始化本地庫
Initialized empty Git repository in /Users/xxx/sangyu/sangyuStudy/.git/ // .git目錄中存放的是本地庫相關的子目錄和檔案,不要删除和随意修改
複制
設定簽名
作用:區分不同開發人員的身份
設定的簽名和登入遠端庫(代碼托管中心)的賬号、密碼沒有任何關系
級别優先級:就近原則,項目優先級優先于系統使用者級别,二者都沒有不允許
// 項目級别/倉庫級别:僅在目前本地庫範圍内有效,資訊儲存位置:./git/config 檔案
$ git config user.name sangyu // 設定user
$ git config user.email [email protected] // 設定email
$ cat .git/config // 檢視設定的user和email
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = sangyu
email = [email protected]
複制
// 系統使用者級别:登入目前作業系統的使用者範圍,資訊儲存位置:~/.gitconfig 檔案
git config --global user.name xxx
git config --global user.email [email protected]
複制
添加送出以及檢視狀态操作
$ git status
On branch master // 在master分支,預設的,也可以稱為主幹
No commits yet // 本地庫沒有送出過任何東西
Untracked files: // 可以送出到暫存區的内容
(use "git add <file>..." to include in what will be committed) // 可以使用git add送出
sangyuStudy.iml
study/
nothing added to commit but untracked files present (use "git add" to track)
複制
$ git add * // 添加到暫存區所有可添加的
$ git status
On branch master
No commits yet
Changes to be committed: // 更改要送出的
(use "git rm --cached <file>..." to unstage)
new file: sangyuStudy.iml
new file: study/Git.md
複制
$ git rm --cached study/Git.md // 從暫存區移除study/Git.md
rm 'study/Git.md'
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sangyuStudy.iml
Untracked files:
(use "git add <file>..." to include in what will be committed)
study/
$ git add study/Git.md // 重新添加study/Git.md到暫存區
$ git status // 檢視狀态
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sangyuStudy.iml
new file: study/Git.md
複制
$ git commit // 送出到工作區
# Please enter the commit message for your changes. Lines starting // 加入本次送出的注釋
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: sangyuStudy.iml
# new file: study/Git.md
-- 可視 --
# vim編輯模式,底部指令i進入輸入模式
# 輸入後,esc退出輸入模式
# 進入底部指令模式,:wq退出并儲存
[master (root-commit) 4b5caec] My second commits // (root-commit) 4b5caec 根送出隻能有一個
2 files changed, 12 insertions(+)
create mode 100644 sangyuStudy.iml
create mode 100644 study/Git.md
複制
// 修改檔案後檢視狀态
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed) // 添加到暫存區
(use "git checkout -- <file>..." to discard changes in working directory) // 用這個指令撤銷修改
modified: study/Git.md
no changes added to commit (use "git add" and/or "git commit -a") // and/or 可以add後commit,或者直接commit
複制
$ git commit -m "my second commit" . // 不進入vim,直接在指令中增加當次送出的注釋 使用-m
[master c9f56c0] my second commit // [master c9f56c0] ,在根送出的基礎上
1 file changed, 3 insertions(+), 1 deletion(-)
複制
版本穿梭
$ git log // 檢視版本曆史記錄
commit c9f56c0dac410a229da58dbe14824d6ba5650733 (HEAD -> master) // commit c9f56c0dac410a229da58dbe14824d6ba5650733 本次送出的索引;HEAD -> master :head是指針指向目前的版本
Author: sangyu <[email protected]>
Date: Wed Feb 5 11:14:23 2020 +0800
my second commit
commit 62149d060c283232d01a2c059e033cf67705e82b
Author: sangyu <[email protected]>
Date: Wed Feb 5 11:12:30 2020 +0800
my third commits
:// 表示的日志太多,enter繼續檢視後面的日志
// b 向上翻頁
// q 退出
複制
$ git log --pretty=oneline // 日志按行展示
c9f56c0dac410a229da58dbe14824d6ba5650733 (HEAD -> master) my second commit
62149d060c283232d01a2c059e033cf67705e82b my third commits
4b5caec431700bda84f5c42c69547054e85de96a My second commits
複制
$ git log --oneline // 日志展示的第二種方式
c9f56c0 (HEAD -> master) my second commit // c9f56c0 索引值的一部分
62149d0 my third commits
4b5caec My second commits
複制
$ git reflog // 日志展示的第三種方式
c9f56c0 (HEAD -> master) HEAD@{0}: commit: my second commit //{x} 到某個版本需要移動的步數
62149d0 HEAD@{1}: commit: my third commits
4b5caec HEAD@{2}: commit (initial): My second commits
複制
基于索引值前進後退版本-推薦
$ git reset --hard 62149d0
HEAD is now at 62149d0 my third commits // head指向了62149d0所在版本
$ git reflog
62149d0 (HEAD -> master) HEAD@{0}: reset: moving to 62149d0
c9f56c0 HEAD@{1}: commit: my second commit
62149d0 (HEAD -> master) HEAD@{2}: commit: my third commits
4b5caec HEAD@{3}: commit (initial): My second commits
複制
使用^ 後退版本
$ git reset --hard HEAD^ // ^表示以目前版本往後退一步
HEAD is now at 9994181 4
複制
使用~ 後退版本
$ git reset --hard HEAD~2 // 數字是幾表示後退幾個版本
HEAD is now at 7e610f6 3
複制
reset指令的三個參數對比
--soft
僅僅在本地庫移動HEAD指針
--mixed
在本地庫移動HEAD指針、重置暫存區
--hard
在本地庫移動HEAD指針、重置暫存、重置工作區
永久删除檔案後找回
$ rm study/aa.txt // 删除檔案
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: study/aa.txt
$ git commit -m 'delete' . // 送出到本地庫,記錄這個檔案的删除,git隻會增加版本,不會删除
[master a281a9a] delete
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 study/aa.txt
$ git reset --hard fc81d02 // 執行後删除的檔案會恢複
HEAD is now at fc81d02 1
複制
添加到暫存區的删除檔案找回
$ rm study/apple.txt // 删除檔案後沒有添加到本地庫
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: study/apple.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git reset --hard HEAD // 找回檔案
HEAD is now at 3b9743c apple
複制
比較檔案
$ git diff study/apple.txt // 修改檔案,diff工作區和暫存區的比較
diff --git a/study/apple.txt b/study/apple.txt
index 04a9f41..297bd50 100644
--- a/study/apple.txt
+++ b/study/apple.txt
@@ -1,3 +1,3 @@
1111
-2222 // - 表示删除
+2222 aaa // + 表示新增加
3333
\ No newline at end of file
複制
n$ git diff HEAD study/apple.txt // 和暫存區的某個版本進行比較
diff --git a/study/apple.txt b/study/apple.txt
index 04a9f41..297bd50 100644
--- a/study/apple.txt
+++ b/study/apple.txt
@@ -1,3 +1,3 @@
1111
-2222
+2222 aaa
3333
\ No newline at end of file
複制
$ git diff HEAD^ study/ee.txt // 和某個曆史版本比較
diff --git a/study/ee.txt b/study/ee.txt
new file mode 100644
index 0000000..d5644ad
--- /dev/null
+++ b/study/ee.txt
@@ -0,0 +1,11 @@
+1
+
+2
+
+3 111
複制
$ git diff // 不帶檔案名,比較多個檔案
diff --git a/study/Git.md b/study/Git.md
index c0da29a..3f0d704 100644
--- a/study/Git.md
+++ b/study/Git.md
@@ -4,4 +4,8 @@
2. 修改
-3.
\ No newline at end of file
+3.
複制
分支概述
分支的好處:
同時并行推進多個功能開發,提高開發效率
各個分支在開發過程中,如果某一個分支開發失敗,不會對其他分支有任何影響。失敗的分支删除重新開始即可
分支操作
$ git branch -v // 檢視目前所有的分支
* master 801553e apple. // 隻有1個分支master,并且目前所在分支也是master
複制
$ git branch hot-fix // 建立新的分支 hot-fix
$ git branch -v
hot-fix 801553e apple.
* master 801553e apple.
複制
$ git checkout hot-fix // 切換分支
Switched to branch 'hot-fix'
$ git branch -v
* hot-fix 801553e apple. // 目前所在分支hot-fix
master 801553e apple.
複制
// 在hot-fix 分支上修改
$ git add .
$ git commit -m 'hot-fix' .
[hot-fix e50785a] hot-fix
4 files changed, 27 insertions(+), 2 deletions(-)
create mode 100644 study/cc.txt
create mode 100644 study/ee.txt
$ git branch -v
* hot-fix e50785a hot-fix
master 801553e apple.
// 合并分支
$ git checkout master // 第一步:合并到接受修改的分支上(被合并,增加新内容)
$ git branch -v
hot-fix e50785a hot-fix
* master 801553e apple.
$ git merge hot-fix //第二步:執行merge指令
Updating 801553e..e50785a
Fast-forward
study/Git.md | 6 +++++-
study/apple.txt | 2 +-
study/cc.txt | 10 ++++++++++
study/ee.txt | 11 +++++++++++
4 files changed, 27 insertions(+), 2 deletions(-)
create mode 100644 study/cc.txt
create mode 100644 study/ee.txt
複制
解決合并分支後産生的沖突
// 兩個分支上分别對同一個檔案同一個地方做不同的修改
// 合并分支
$ git merge master
Auto-merging study/good.txt // 産生分支沖突,必須手動修改
CONFLICT (content): Merge conflict in study/good.txt
Automatic merge failed; fix conflicts and then commit the result.
複制
分支沖突的表現
// 沖突的解決
$ git status // 修改檔案後檢視status
On branch hot-fix
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: study/good.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add . // 使用add 标記沖突已解決
$ git status // 此時在檢視status,沖突已修複
On branch hot-fix
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: study/good.txt
$ git commit -m 'conclude merge' // 使用git commit結束合并 不可帶檔案名稱,可帶 注釋
複制
本地建立遠端倉庫别名
$ git remote add origin https://github.com/xingyuexingyue/sangyu.git // origin: 别名;url是遠端倉庫位址
$ git remote -v // 檢視遠端倉庫位址
origin https://github.com/xingyuexingyue/sangyu.git (fetch) // 取回
origin https://github.com/xingyuexingyue/sangyu.git (push) // 推送
複制
遠端倉庫推送操作
$ git push origin master // 推送到遠端倉庫必須指定分支
複制
克隆操作
$ git clone https://github.com/xingyuexingyue/sangyu.git
Cloning into 'sangyu'...
remote: Enumerating objects: 50, done.
remote: Counting objects: 100% (50/50), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 50 (delta 5), reused 50 (delta 5), pack-reused 0
Unpacking objects: 100% (50/50), done.
複制
遠端庫修改的拉取
// pull 相當于fetch 和merge
$ git fetch origin master
From https://github.com/xingyuexingyue/sangyu
* branch master -> FETCH_HEAD
$ git merge origin/master
複制
協同開發沖突的解決
如果不是基于GitHub遠端庫的最新版所做的修改,不能推送,必須先拉取
拉取下來後如果進入沖突狀态,則按照“分支沖突解決”操作解決即可
// 兩個賬号分别對遠端倉庫相同位置做不同的修改
// 先pull下來,再送出到遠端倉庫
複制
協同開發沖突表現
協同開發沖突解決