史上最經典的git教程

原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本聲明。不然將追究法律責任。http://wsyht90.blog.51cto.com/9014030/1832284html

 

文檔的下載地址:python

連接:https://pan.baidu.com/s/1FIph8gHIuwwwQuLi-g8pcA 密碼:ykp5git

至關的經典github

 

【原理】shell

1)Git介紹數據庫

2)Git和SVN的區別、SVN工做流程、架構圖講解vim

3)Git優勢、架構圖、文件三種狀態、三個工做區域、工做流程講解windows

 

【實戰】安全

1)準備工做bash

2)建立版本庫

3)版本回退

4)工做區與暫存區的區別

5)刪除文件和恢復

6)遠程倉庫

7)遠程克隆到本地

8)GitHub刪除項目

9)分支的建立與合併

10)分支合併衝突解決

11)查看歷史合併功能

12)分支的注意事項

13)雙分支操做

14)強制刪除分支

15)多人協做

16)建立工做目錄的兩個方法

17)忽略須要push的文件

18)更新本地工做區

19)CentOS下搭建Git服務器

20)鉤子腳本

 

【拓展知識】

1)別名配置

2)標籤操做

 

【Git經常使用命令總結

 

前言:本人技術有限,若有說得不對或作的不對的地方,請你們提出來,謝謝你們

 

1、

Git介紹:

Git:是能夠提供版本控制的一個工具

Github:提供了你用git這個工具的一個平臺,公共代碼倉庫

git服務器:自建倉庫,私有倉庫(企業用)

 

2、

Git和SVN的區別

Git是分佈式版本控制系統,SVN是集中式版本控制系統

 

SVN的工做流程

SVN有一臺中央服務器,下面有一臺至多臺的客戶端,徹底依賴於中央服務器工做

SVN工做流程:

客戶端經過把內容Check out到本地,而後提交到中央服務器,後面再更新,而後再提交

 

弊端:一、若是中央服務器斷網了,則會致使沒法更新

    二、若是網絡很差,則會致使更新緩慢

    三、若是中央服務器單點故障,又沒及時更新的話,則會致使數據丟失

 

架構圖:

 

 

 

3、

Git優勢:

一、Git沒有中央管理器,每一個人的電腦就是一臺完整的版本庫

二、工做不須要聯網,由於版本都在自已的電腦上

三、若是多我的共同協做,只須要push到github倉庫上便可進行多人協做

四、項目提交到GitHub,每一次提到到本地操做,都是一次對代碼倉庫的完整備份

 

架構圖:

 

上圖Git服務器是企業內部自已搭的私有的,若是是自已玩的,咱們自已的電腦就是自已的倉庫,咱們不須要聯網,而後有一個GitHub是公共倉庫,咱們還能夠把自已的項目提交到公共倉庫,至關於對自已代碼又作了一個備份,而企業內部,則須要自建一臺私有的Git服務器,你們經過先把代碼提交到自已電腦的倉庫,而後再提交到Git服務器這樣的一個工做流程

 

 

Git文件三種狀態

已提交(committed)

表示該文件已經被安全地保存在本地數據庫中了

 

已修改(modified)

表示修改了某文件,但尚未提交保存

 

已暫存(staged)

表示把已修改的文件放在下次提交時要保存的清單中

 

 

Git文件流轉時的三個工做區域:

Git的工做目錄:

保存着特定的版本文件,屬於提交狀態

 

暫存區域:

作了修改並已放入暫存區域

 

本地倉庫:

顧名思義,本的版本倉庫

 

Git工做流程:

一、在工做目錄中修改某些文件

二、對修改後的文件進行快照,而後保存到暫存區域

三、提交更新,將保存在暫存區域的文件快照永久轉儲到Git目錄中

 

【實戰】

1、Git準備工做

1)cygwin和git bash的對比

因爲咱們在Windows上執行git的命令須要一個仿Unix終端的軟件,在這裏,我給你們介紹兩款軟件,一款是cygwin,一款是git bash,git bash是git提供給你用的軟件,那麼這兩款軟件有啥不同呢,請看圖:

 

一、左邊那款是git bash,他可以把你當前用戶家目錄全部文件包括windows的都列出來,而右邊的cygwin只列出他用戶家目錄的文件,並無列出windows的文件

總結第一點:cygwin簡潔性強,而bit bash把windows家目錄的東西都列出來,讓你也能夠進行操做,至於你以爲哪一個好,這個看你我的喜愛

 

再來看第二張圖

 

二、左邊那款仍是git bash,右邊那款則是cygwin,二者都在f盤下執行了ls,看到二者不什麼不同了嗎,git bash能把文件夾以高這度藍色顯示,而cygwin無論你是什麼文件都是白色,這個卻是讓咱們很差區分

總結第二點:以爲這個高這度顯示各類不同的文件這個優越性git bash是作的比較好的

 

個人選擇:git bash

 

爲何 ?

 

由於git bash不緊能夠高亮度顯示各類不同的文件,讓你能夠快速操做,並且在家目錄下他也能夠列出你的全部windows下的文件,提供給你操做,我以爲選擇git bash仍是比選擇cygwin好的,不過這個最終仍是看你自已我的選擇,我在這裏只是說出我自已的想法而已

 

2)下載安裝

git下載連接:https://git-scm.com/download/win

個人安裝是默認一路向北

 

cygwin下載連接:https://cygwin.com/install.html

安裝教程:http://jingyan.baidu.com/article/6b97984d83dfe51ca2b0bf0e.html

 

3)桌面右健鼠標點擊Git Bash Here打開Git Bash,界面以下:

 

它是在Windows下模仿Shell命令行的一個終端

 

4)查看幫助

1

2

3

4

5

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop

$ git help git         #git help <verb>

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop

$ git config --help    #git <verb> --help

 

 

5)設置身份

由於git是分佈式版本控制系統,因此須要填寫用戶名和郵箱作爲一個標識

1

2

3

4

5

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git config --global user.name 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git config --global user.email t89191958201@139.com

 

注意:git config 下有三個參數,

--global: 表示對這臺機器上的全部Git倉庫都會使用這個配置

--system:用戶級別配置,針對某個用戶生效

--local:針對本地有效

 

6)設置比較工具(可選)

1

2

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop

$  git config --global merge.tool vimdiff

 

 

7)檢查配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git config --list

core.symlinks=false

core.autocrlf=true

core.fscache=true

color.diff=auto

color.status=auto

color.branch=auto

color.interactive=true

help.format=html

http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

diff.astextplain.textconv=astextplain

rebase.autosquash=true

merge.tool=vimdiff

user.name=wsyht

user.email=t89191958201@139.com

core.repositoryformatversion=0

core.filemode=false

core.bare=false

core.logallrefupdates=true

core.symlinks=false

core.ignorecase=true

 

 

2、建立版本庫

1)(關健字:git init)

版本庫:又名倉庫,你能夠簡單的理解成一個目錄,這個目錄裏面的全部文件均可以被Git管理起來,包括修改、刪除、提交、回滾

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop

$ cd f:

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ mkdir demo1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ cd demo1/

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1

$ git init

Initialized empty Git repository in F:/demo1/.git/

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

這個時候會多了一個.git的隱藏目錄,他是用來管理跟蹤管理版本的,裏面文件不能夠修改

 

2)提交任務(關健字:git add,git commit)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ pwd

/f/demo1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "test1" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add demo1.txt   #必須先add到暫存區,才能commit

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m "demo1提交"   #-m後面是寫註釋,至關於SVN在提交前在窗口裏寫註釋

 

[master (root-commit) a7e593c] demo1提交

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 files changed, 1 insertions(+)

 create mode 100644 demo1.txt

 

 

3)查看狀態(關健字:git status)

1

2

3

4

5

6

7

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

4)修改裝態查看 (關健字:git status,modify)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "1111" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ 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:   demo1.txt    #修改裝態

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

 

 

5)對比查看 (關健字:git diff)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git diff demo1.txt

diff --git a/demo1.txt b/demo1.txt

index a5bce3f..979b2de 100644

--- a/demo1.txt

+++ b/demo1.txt

@@ -1 +1,2 @@

 test1

+1111       #可看出多了這一行

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

注意:提交前須查看一下文件修改的內容,如不問題,再git add,而後git commit

 

6)查看提交歷史記錄(關健字:git log)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git log

commit 8445579cbd259610f94a6c665fc841313a131516   #每次提交的版本號

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 23:15:40 2016 +0800

    文件增長2222一行                               #增長內容顯示的註釋,最近的一次提交,從上到下排序

commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 23:15:08 2016 +0800

    增長1111一行

commit a7e593c34a10f0b2e732153c09bce74381e7de35

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 22:56:25 2016 +0800

    demo1提交

    

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

注意:剛纔是我自已作實驗一共提交了三次,並未貼出來到博客而已,因此出現了三次提交的歷史記錄

 

7)省略部分信息 (關健字:--pretty=oneline)

1

2

3

4

5

6

7

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git log --pretty=oneline

8445579cbd259610f94a6c665fc841313a131516 文件增長2222一行

25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 增長1111一行

a7e593c34a10f0b2e732153c09bce74381e7de35 demo1提交

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

3、版本回退

1)回退命令

   一、回退到上一個版本

      git reset --hard HEAD^

   二、回退到上上一個版本

      git reset --hard HEAD^^

   三、回退到前100個版本

      git reset --hard HEAD~100

演示:

回退到上一個版本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

test1

1111

2222

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git reset --hard HEAD^

HEAD is now at 25e4895 增長1111一行

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

test1

1111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git log

commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 23:15:08 2016 +0800

    增長1111一行

commit a7e593c34a10f0b2e732153c09bce74381e7de35

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 22:56:25 2016 +0800

    demo1提交

    

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

2)回退到剛纔的最新版本

(1)首先獲取版本號 (關健字:git reflog)

1

2

3

4

5

6

7

$ git reflog

25e4895 HEAD@{0}: reset: moving to HEAD^

8445579 HEAD@{1}: commit: 文件增長2222一行    #這個是版本號8445579

25e4895 HEAD@{2}: commit: 增長1111一行

a7e593c HEAD@{3}: commit (initial): demo1提交

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(2)恢復版本(關健字:git reset --hard 版本號)

1

2

3

4

5

6

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git reset --hard 8445579

HEAD is now at 8445579 文件增長2222一行

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(3)查看恢復

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

$ cat demo1.txt

test1

1111

2222

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git log

commit 8445579cbd259610f94a6c665fc841313a131516

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 23:15:40 2016 +0800

    文件增長2222一行

commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 23:15:08 2016 +0800

    增長1111一行

commit a7e593c34a10f0b2e732153c09bce74381e7de35

Author: wsyht <t89191958201@139.com>

Date:   Thu Jul 28 22:56:25 2016 +0800

    demo1提交

    

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

4、工做區與暫存區的區別

工做區:就是在我建立的demo1的版本庫目錄裏的文件,.git目錄除外

版本庫:.git目錄就是版本庫,版本庫存着不少東西,其中最重要的就是stage(暫存區),還有Git爲咱們自動建立了第一個分支Master,以及指向Master的第一個指針HEAD,

 

提交Git文件到版本庫有兩步;

第一步:是使有tgit add把文件添加進去,實際上就是把文件添加到暫存區

第二步:使用git commit提交更改,實際上就是把暫存區的全部內容提交到當前分支上

 

演示:

(1)修改文件和建立新文件查看

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "3333" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "1111" >> demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ 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:   demo1.txt

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        demo2.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(2)添加到暫存區查看

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add demo1.txt demo2.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

On branch master

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

        modified:   demo1.txt

        new file:   demo2.txt

        

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(3)提交再查看

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m"4444" demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

85ce2b6] 4444

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m"1111" demo2.txt

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

[master fc33a37] 1111

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 create mode 100644 demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(4)一次性提交全部文件方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "5555" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "5555" >> demo2.txtc

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

 

$ git add demo1.txt demo2.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m "一次性提交全部文件"

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

7ecdd08] 一次性提交全部文件

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

warning: LF will be replaced by CRLF in demo2.txt.

The file will have its original line endings in your working directory.

 2 files changed, 2 insertions(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

4、撤銷修改

若是在一行裏面添加了新內容,如今要撤消

總結一下一共有三種方法:

第一種:把要刪除的內容添刪掉,而後add,commit

第二種:恢復上一個版本,git reset --hard HEAD^

第三種:以下 (關健字:git checkout -- 文件名)-- 很重要,請注意,這裏說的是兩個'-',若是沒有 -- 的話;那麼命令變成切換分支了 或者用 git checkout . 推薦使用這個,能夠批量還原修改的文件,前提是未add和commit,可是不包括新增的文件,只是針對已經存在文件且修改了內容的文件,進行撤消

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

$ cat demo1.txt

test1

1111

2222

3333

5555

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "6666" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ 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:   demo1.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -- demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

test1

1111

2222

3333

5555

 

 

若是,已經add添加到暫存區的,那麼撤銷方法以下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ echo "666" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ cat demo1.txt

test1

1111

2222

3333

5555

666

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

接着再以下圖操做,

 

 

若是新添加的文件,可是尚未add,如今咱們不想用這個新文件了,那麼咱們直接刪除就好了,以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "111" >> wsyht.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        wsyht.txt

nothing added to commit but untracked files present (use "git add" to track)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  oo.txt  wsyht.txt  ye.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ rm -rf wsyht.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

 

5、刪除文件和恢復

(1)刪除演示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt  test1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ rm -rf test1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ 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:    test1.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add test1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m 'delete test1.txt'

[master 43185ac] delete test1.txt

 1 file changed, 1 deletion(-)

 delete mode 100644 test1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

(2)恢復演示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ rm -rf demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ 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:    demo2.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -- demo2.txt

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git status

On branch master

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

 

總結:

三種狀況

一、未add想轍消(包括增刪改文件內容,也包括刪文件)

   git checkout -- 文件名  (轍消工做區的修改)    或

   git checkout .

二、add到暫存區後,需分兩步操做

   第一步:git reset HEAD 文件名  (轍消暫存區的修改)

三、commit以後

   git reset --hard HEAD^      #版本回退1個版本

   git reset --hard HEAD^^     #版本回退2個版本  

   git reset --hard HEAD~100    #回退到前100個版本

      git reflog                                      #獲取版本號

      git reset --hard 版本號                #恢復版本號

    

6、遠程倉庫(GitHub公共倉庫)

1)先註冊github帳號

省略...

 

2)生成ssh-keygen密鑰對

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$ cd

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$ pwd

/c/Users/Administrator

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$ ssh-keygen -C 'wsyht'   #生成密鑰對,-C 就是把引號內容給id_rsa.pub公鑰作註釋

Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):

Created directory '/c/Users/Administrator/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.

Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:oDD45Fhl3zdLv6EwJivEAHTGRyiyxifnLfYv0mDE7m0 wsyht

The key's randomart image is:

+---[RSA 2048]----+

|o..o=.           |

|.+o= o .         |

|+.O . o . +      |

|.X X . . o +     |

|o X = . S . o    |

|   O . + o . o   |

|  + B .   . .    |

|   o E           |

|    o o.         |

+----[SHA256]-----+

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$

 

 

三、在GitHub上建立項目

 

四、建立展現

 

 

五、完成展現

 

在如上截圖顯示內容我要說一下了

經過本地倉庫和GitHub倉庫的鏈接方式有兩種:

第一種:經過HTTPS方式鏈接,就是如上紅框顯示,這種方式在本地遠程GitHub倉庫,須要用輸入你的GitHub帳號和密碼,並且每次都要輸入  缺點:不安全,麻煩

第二種:經過ssh方式鏈接,在本地建立密鑰對, 再把公鑰上傳到GitHub服務器,而後實現無密碼鏈接以下截圖這是經過ssh的鏈接方式:     優勢:安全,方便

 

 

先說第一種鏈接方式:

 

這種方式是經過第一種https鏈接方式,先進入到個人工做目錄下,也就是建立的版本庫目錄下,

而後輸入遠程命令,用戶名和密碼便可

 

第二種方式展現

上面已經生成了密鑰對了,而後咱們把id_rsa.pub的公鑰複製到GitHub上去

 

 

 

 

 

 

本地測試

1

2

3

4

5

6

7

8

9

10

11

12

修改配置文件,進到版本庫目錄.git下,修改config文件

Administrator@YYBFVJDMAPF13NB MINGW64 ~/.ssh

$ cd f:

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ cd demo1/.git/

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

$ ls

COMMIT_EDITMSG  description  hooks/  info/  objects/   refs/

config          HEAD         index   logs/  ORIG_HEAD

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

 

$vim config

 

如上圖所示,本來默認是https的鏈接方式,如今改爲經過ssh方式去鏈接,只須要把giuhub剛建立項目時他顯示的鏈接命令考到config文件url後面便可

測試鏈接:

1

2

3

4

5

6

7

8

9

10

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ssh -T git@github.com

The authenticity of host 'github.com (192.30.253.113)' can't be established.

RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.

Hi wsyht! You've successfully authenticated, but GitHub does not provide shell access.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

如圖所示,看到successfully表示成功了

如今咱們再更改一個文件,而後再Push到github

先提交到本地

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "888" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m '888'

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

d693375] 888

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

 

如今咱們push到github了

1

2

3

4

5

6

7

8

9

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git remote add origin git@github.com:wsyht/demo_test.git

fatal: remote origin already exists.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git push -u origin master

fatal: remote error:

  wsyht/demo/demo_test is not a valid repository name

  Email support@github.com for help

 

 

若是報如上錯,解決辦法以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git remote rm origin  #刪除遠程默認倉庫名"origin"

#刪除遠程庫名後,則會把.git/config文件的url也刪除,因此到下面又要從新添加

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git remote add origin git@github.com:wsyht/demo_test.git #config文件從新添加url

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git push -u origin master   #第一次push須要加-u參數

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

   43185ac..07e6d4c  master -> master

Branch master set up to track remote branch master from origin.

 

成功了,哈哈哈。。。

 

.config文件展現

剛剛上面執行git remote rm origin就是把url那一行刪了

接着又執行了git remote add origin git@github.com:wsyht/demo_test.git

至關於從新加載了那一行,因此又能push上了,push便是推送文件上去github公共倉庫裏

wsyht是我建註冊的github的用戶名,demo_test是我在GitHub上建的項目名,git是用戶名,github.com是github網站的域名,origin是遠程倉庫默認的庫名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

$ cat config

[core]

        repositoryformatversion = 0

        filemode = false

        bare = false

        logallrefupdates = true

        symlinks = false

        ignorecase = true

[branch "master"]

[branch "master"]

[branch "wsyht"]

#[alias]

        #br = branch

[remote "origin"]

        url = git@github.com:wsyht/demo_test.git

        fetch = +refs/heads/*:refs/remotes/origin/*

 

 

再查看github

 

 

 

OK,已經提交上去了

 

如今咱們再來看一下config這個配置文件

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

$ cat config

[core]

        repositoryformatversion = 0

        filemode = false

        bare = false

        logallrefupdates = true

        symlinks = false

        ignorecase = true

[branch "master"]

[remote "origin"]

        url = git@github.com:wsyht/demo_test.git   #這一行是講你所用的鏈接方式

        fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]

        remote = origin             

        merge = refs/heads/master     #合併分支是master,到後面我再講什麼是分支

 

注意:在第一次咱們推送master分支時,加上了-u參數,git會本地的Master分支推送到github的master分支,而且會關聯起來,因此的就不須要再加入-u參數了

 

7、遠程庫克隆到本地

點擊進去

 

 

 

 

刪除提交後,如今咱們在把GitHub克隆到本地,你會發現少了一個文件

 

本地操做:關健字: git clone

 

能夠看到多了一個目錄出來

 

8、GitHub刪除項目

 

 

 

 

 

 

9、分支的建立與合併

首先咱們來說一下什麼是分支,分支就至關於我如今在作一個項目A,而後項目裏面有一個功能的BUG我須要去作修改,而後爲了安全,不影響個人Master分支,我就在個人Master分支上再建立一個分支出來,而後我就在我這個分支上修改個人代碼,修改完成後,再與個人Master主分支合併,這樣就不會影響到個人原文件,分支其實就是基於某一時刻的代碼進行修改,這個分支實際上只存儲這些修改,也能夠說是項目某個時期的快照,但他不是真實賦值文件,修改完成後,合併到Master分支,這樣,Master分支的內容從而也會跟着發生改變,而建立的臨時文件,則怎麼修改也不會影響個人源文件,即Master分支的內容,至關於你在虛擬機的一個測試環境,你愛昨玩昨玩,都不會影響到個人真實系統

 

1)建立分支wsyht 關健字:git checkout -b 分支名字 ,git branch

1

2

3

4

5

6

7

8

9

10

11

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git checkout -b wsyht         #建立並切換分支

Switched to a new branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ git branch    #查看當前所在分支

  master

* wsyht  

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$

 

 

git branch wsyht   #建立分支

git checkout wsyht #切換分支

 

在wsyht分支上進行修改內容提交操做

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ echo "999" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

 

$ git commit -m'wsyht999'

[wsyht warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

c4ff9ea] wsyht999

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ git checkout master

Switched to branch 'master'

Your branch is up-to-date with 'origin/master'.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ cat demo1.txt

test1

1111

2222

3333

5555

777

888

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$

 

發如今wsyht分支上修改提交,再切換回master分支,並不影響master分支文件的內容

 

如今合併分支

把Master分支和wsyht分支內容合併在一塊兒,只留Master分支,再看Master分支的內容,關健字:git merge wsyht

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git branch

* master

  wsyht

  

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git merge wsyht

Updating 3020d4c..c4ff9ea

Fast-forward

 demo1.txt | 1 +

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ cat demo1.txt

test1

1111

2222

3333

5555

777

888

999

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$  git branch -d wsyht

Deleted branch hb (was 5939679).

 

可查看到,在wsyht分支上修改的內容,合併後,在Master分支上已顯示出來

注意:在建立的臨時分支上修改了文件內容後,此時主分區不能進行合併,必定要add,commit了而後再在主分區上進行合併操做,合併後,要刪除臨時分支,不然分支了多會凌亂,合併後能夠經過git log -1查看最新合併的日誌,合併後無需再作任何操做,直接push到git服務器或GitHub便可

 

命令總結

查看分支:git branch

建立分支:git branch 分支名字

切換分支: git checkout 分支名字

建立切換分支: git checkout -b 分支名字

合併某分支到當前分支: git merge 分支名字

刪除分支: git branch -d 分支名字

 

10、分支合併衝突解決

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

$ git branch

* master

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git checkout -b hb

Switched to a new branch 'hb'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (hb)

$ cat demo1.txt

111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (hb)

$ echo "333" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (hb)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (hb)

$ git commit -m"hb3333"

[hb warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

e224bca] hb3333

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (hb)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 4 commits.

  (use "git push" to publish your local commits)

  

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ cat demo1.txt

111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ echo "222" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git commit -m 'master commit'

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

f60973f] master commit

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git merge hb

Auto-merging demo1.txt

CONFLICT (content): Merge conflict in demo1.txt

Automatic merge failed; fix conflicts and then commit the result.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master|MERGING)

$ cat demo1.txt

111

<<<<<<< HEAD

222   #master分支的內容

=======

333   #hb分支的內容

>>>>>>> hb  

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master|MERGING)

$

 

 

 

#修改爲主分支修改的內容,刪了hb分支的內容再提交就正常了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master|MERGING)

$ cat demo1.txt

111

222

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master|MERGING)

$ git add demo1.txt

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master|MERGING)

 

$ git commit -m'new'

[master b2a4ccb] new

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

 

$ cat demo1.txt

111

222

 

 

11、查看歷史合併功能

在合併分支時,默認會用fast forware模式,這樣的話在咱們刪除分支後,會刪掉咱們曾經合併過的信息,此時,你再用git log就查看不了咱們跟哪些分支作過合併了,因此在合併的時候咱們要加上一個參數,--no-ff

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git checkout -b wsyht

Switched to a new branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ cat demo1.txt

111

222

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ echo "333" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

g

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

$ git commit -m "add 333"

[wsyht warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

34d979f] add 333

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (wsyht)

 

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 7 commits.

  (use "git push" to publish your local commits)

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

 

$ git merge --no-ff -m '合併wsyht測試' wsyht

Merge made by the 'recursive' strategy.

 demo1.txt | 1 +

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$ git log --graph --pretty=oneline --abbrev-commit    #--graph以圖形化顯示以下圖最左上角,--abbrev-commit則是縮短版本號簡短顯示

*   9e70f26 合併wsyht測試    #能夠查看到wsyht的合併信息

|\

| * 34d979f add 333

|/

*   b2a4ccb new

|\

| * e224bca hb3333

* | f60973f master commit

|/

* ce302ae delete oo.txt

* 5939679 111

* c831711 qk

* c4ff9ea wsyht999

* 3020d4c oo

* d497da7 Delete demo2.txt

* d693375 888

* 07e6d4c 777

* 43185ac delete test1.txt

* 7ecdd08 一次性提交全部文件

* fc33a37 1111

* 85ce2b6 4444

* 8445579 文件增長2222一行

* 25e4895 增長1111一行

* a7e593c demo1提交

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/demo_test (master)

$

 

 

11、分支的注意事項

第一個要注意的地方:以下能夠看到git切換到wsyht分支,可是wsyht分支沒有add和commit,因此再切回master,他就至關於直接改了Master的文件,因此修改了必定要,add再commit後再切回Master

第二個要注意的地方:當你在其餘分支上修改了文件提交後,合併到Master後,必定要刪了開出來的分支,而後再開新的分支用,畢免二次污染

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -b wsyht

Switched to a new branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ sed -i '1s/1111/wsyht/g' demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status

On branch wsyht

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:   demo1.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git checkout master

M       demo1.txt

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

  

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt

c

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git commit -m 'wsyht commint'

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

3a59897] wsyht commint

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt

c

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout wsyht

Switched to branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

 

12、雙分支操做

我在這裏講一句:實踐出真知,有時候想一些東西,還不如親自動手操做明白來得快,有些地方你想來想去不明白,但你動手操做了,你就明白他是怎麼一回事了,因此不要光看不練,相信我,下面對於屬於開發比較複雜的項目的開發來講很重 ,請你們認真看

 

#這裏是指使用第一個分支開發,可是還沒開發完新功能,因此並未能add提交,這個時候上頭又說要馬止改一個很重要的bug,要快速完成,因此咱們在把這個分支儲藏起來,下次回來再拿出來繼續開發

 

第一步:

#第一個wsyht分支操做  

wsyht分支開發->儲藏-暫停

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch

* master

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -b wsyht

Switched to a new branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ sed -i '1s/1111/wsyht/g' demo1.txt

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status

On branch wsyht

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:   demo1.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git stash    #儲藏當前修改狀態

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

Saved working directory and index state WIP on wsyht: 0631e15 wsyht commit

HEAD is now at 0631e15 wsyht commit

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status     #再次查看狀態發現什麼都沒有

On branch wsyht

nothing to commit, working tree clean

 

 

第二步:

第二個bug分支操做

bug分支修改->提交-合併-刪除bug分支

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 3 commits.

  (use "git push" to publish your local commits)

  

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -b bug

Switched to a new branch 'bug'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ git status

On branch bug

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ sed -i 's/888/bbb/g' demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ git status

On branch bug

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:   demo1.txt

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ git commit -m'commit bug'

[bug warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

ed1eded] commit bug

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (bug)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 3 commits.

  (use "git push" to publish your local commits)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git merge --no-ff -m 'merged bug' bug

Merge made by the 'recursive' strategy.

 demo1.txt | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch -d bug

Deleted branch bug (was ed1eded).

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt

c

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

bbb

 

 

第三步:

#回到第一個分支wsyht操做

合併master分支->調回wsyht修改狀態->add提交

合併master分支主要就是先把剛剛修改的bug先合併了,而後再調回剛纔修改的狀態,繼續開發提交

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout wsyht

Switched to branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

c

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

888

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status

On branch wsyht

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git merge master

Updating 0631e15..cd961d2

Fast-forward

 demo1.txt | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git stash pop        #恢復原來修改的狀態

Auto-merging demo1.txt

On branch wsyht

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:   demo1.txt

no changes added to commit (use "git add" and/or "git commit -a")

Dropped refs/stash@{0} (d1ba7adfb82639ae0ecf1f946349dc064fb30617)

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git add demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status

On branch wsyht

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

        modified:   demo1.txt

        

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git commit -m'commit wsyht'

[wsyht 9d505e6] commit wsyht

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git status

On branch wsyht

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git log --graph --pretty=oneline --abbrev-commit

* 9d505e6 commit wsyht

*   cd961d2 merged bug

|\

| * ed1eded commit bug

|/

* 0631e15 wsyht commit

* 3a59897 wsyht commint

* c1e71b1 test

* d693375 888

* 07e6d4c 777

* 43185ac delete test1.txt

* 7ecdd08 一次性提交全部文件

* fc33a37 1111

* 85ce2b6 4444

* 8445579 文件增長2222一行

* 25e4895 增長1111一行

* a7e593c demo1提交

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

bbb

 

 

第四步

#主分支Master操做

切回主分支->合併wsyht分支->add提交->刪除wsyht分支

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 5 commits.

  (use "git push" to publish your local commits)

 

  

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

1111

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git merge wsyht

Updating cd961d2..9d505e6

Fast-forward

 demo1.txt | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch -d wsyht

Deleted branch wsyht (was 9d505e6).

 

雙分支操做總結:主分支、第一分支、第二分支

第一步:第一分支開發修改、而後儲藏狀態

第二步:從第一分支切回Master分支,再建立並切去第二分支,開發修改,提交,併合併到Master分支,刪除第一分支

第三步:從Master分支切到第二分支,並在第二分支合併Master分支,恢復第二分支修改狀態,繼續修第四步:從第二分支再切回Master分支,併合並第二分支修改的內容,刪除第二分支

分支命令總結

查看分支合併狀況

git log --graph --pretty=oneline --abbrev-commit   #--graph圖形化顯示,--pretty=oneline簡短顯示,--abbrev-commit驗證碼省略顯示

git stash    #儲藏內容

git stash list   #查看儲藏內容

git stash pop/git stash apply    #恢復上一個工做內容

兩者不一樣的是git stash apply恢復後,stash內容並未刪除,你須要用git stash drop來刪除,而用git stash pop,恢復的同時把stash內容也刪了

 

十4、強制刪除分支

 

當咱們在某第二個分支開發某一個功能,提交分支以後,回頭策劃卻說仍是不要改了,這個時候咱們不能合併分支,只能刪除第二個分支

以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch

* master

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git checkout -b wsyht

Switched to a new branch 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ rm -rf demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo2.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git add demo1.txt

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git commit -m'delete demo1'

[wsyht c83d5a1] delete demo1

 1 file changed, 7 deletions(-)

 delete mode 100644 demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 6 commits.

  (use "git push" to publish your local commits)

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

 

$ git branch -d wsyht  

error: The branch 'wsyht' is not fully merged.

If you are sure you want to delete it, run 'git branch -D wsyht'.   #這裏說尚未合不能用-d刪除,只能用大D強制刪除

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch -D wsyht   #用大D強制刪除

Deleted branch wsyht (was c83d5a1).

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch

* master

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

十5、多人協做

當在你提交或幫隆的時候會自動幫你把本地和遠程的master分支對應起來,遠程庫的默認名稱是origin

 

#查看遠程庫

1

2

3

4

5

6

7

8

9

10

11

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git remote   #查看遠程庫的信息

origin

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git remote -v    #查看遠程庫的詳信息

origin  git@github.com:wsyht/demo_test.git (fetch)

origin  git@github.com:wsyht/demo_test.git (push)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$

 

上面顯示了能夠抓取和推送的origin的地址。若是沒有推送權限,就看不到push的地址

 

#推送分支

推送分支就是把本地的分支提交到對應的遠程庫的分支中,若是是Master分支就提交到遠程庫的Master分支,若是是其它分支如wsyht,就提交到遠程庫的wsyht分支中與之相對應,若是一些bug分支不須要提交的,就把他先主分支先合併了,而後再提交到遠程庫中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

wsyht

1111

2222

3333

5555

777

bbb

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ echo "test" > demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ cat demo1.txt

test

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

 

$ git commit -m 'test commit' demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

[master warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

5e368ff] test commit

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+), 7 deletions(-)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git pull

remote: Counting objects: 5, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 5 (delta 0), reused 3 (delta 0), pack-reused 0

Unpacking objects: 100% (5/5), done.

From github.com:wsyht/demo_test

   d693375..3020d4c  master     -> origin/master

Removing demo2.txt

Merge made by the 'recursive' strategy.

 demo2.txt | 2 --

 oo.txt    | 1 +

 2 files changed, 1 insertion(+), 2 deletions(-)

 delete mode 100644 demo2.txt

 create mode 100644 oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ ls

demo1.txt  oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git push -u origin master     #成功了

Counting objects: 19, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (14/14), done.

Writing objects: 100% (19/19), 1.70 KiB | 0 bytes/s, done.

Total 19 (delta 2), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

   3020d4c..6bbbcdf  master -> master

Branch master set up to track remote branch master from origin.

 

#這裏再強調一次,是當第一次push你才須要-u,到後面就不須要再加-u參數了

 

查看GitHub成功同步上去了

 

2)多人同推衝突

如今假若有兩我的兩臺電腦共同推一個分支的文件,假如我先推了,張三也又接着推了,張三就會報錯推不上去,這個時候,咱們來看看怎麼操做

 

角色:「我」 和 「張三」

 

首先個人在個人電腦上建立一個wsyht分支用來作開發,由於master分支張三是沒法拉取下來作開發的,因此只能搞一個wsyht分支,在wsyht分支上作開發,開發完成功能後,提交wsyht分支,而後push到git服務器,再合併master分支,而後把master分支提交,push到git服務器

 

個人電腦操做,以下:

流程:建立分支->切換分支->推送   #此分支通常工做中事先存在,而我這裏爲了作事驗,因此臨時建立

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch

* master

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch wsyht

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git branch

* master

  wsyht

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)

$ git push origin wsyht

Everything up-to-date

 

 

張三電腦操做

操做過程:取遠程庫的分支到本地->創遠程分支到本地->修改提交->push

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ mkdir demo2

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ cd demo2

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2

$ git init

Initialized empty Git repository in F:/demo2/.git/

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ git remote add origin git@github.com:wsyht/demo_test.git

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ git fetch origin   #取遠程庫的分支到本地,(若是隻想取某個分支,git fetch origin 分支名)

remote: Counting objects: 58, done.

remote: Compressing objects: 100% (36/36), done.

remote: Total 58 (delta 3), reused 56 (delta 3), pack-reused 0

Unpacking objects: 100% (58/58), done.

From github.com:wsyht/demo_test

 * [new branch]      master     -> origin/master

 * [new branch]      wsyht      -> origin/wsyht

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ ls

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ git branch wsyht origin/wsyht    #建立遠程庫的分支到本地

Branch wsyht set up to track remote branch wsyht from origin.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ ls

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (master)

$ git checkout wsyht  #切換到建立的分支

Switched to branch 'wsyht'

Your branch is up-to-date with 'origin/wsyht'.

l

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ ls   #可查看到該分支的文件了

demo1.txt  oo.txt

 

$ cat demo1.txt

test

222

333

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ echo "111" >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ cat demo1.txt

test

222

333

111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git commit -m 'zhangsan add 111 demo1'

[wsyht warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

e4ace0b] zhangsan add 111 demo1

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git push origin wsyht

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

   924fcc5..e4ace0b  wsyht -> wsyht

 

 

個人電腦操做

操做過程:切換分支->修改文件->提交->push->解決衝突->再push

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ pwd

/f/demo1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git checkout wsyht

Already on 'wsyht'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

test

222

333

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ echo '111' >> demo1.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git add demo1.txt

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git commit -m'wo add 111 demo'

[wsyht warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

805dcba] wo add 111 demo

warning: LF will be replaced by CRLF in demo1.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git push origin wsyht   #這裏push失敗是由於衝突了,提示叫你git pull

To git@github.com:wsyht/demo_test.git

 ! [rejected]        wsyht -> wsyht (fetch first)

error: failed to push some refs to 'git@github.com:wsyht/demo_test.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git pull   #這裏git pull又提示進行下面的一步操做

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), done.

From github.com:wsyht/demo_test

   924fcc5..e4ace0b  wsyht      -> origin/wsyht

There is no tracking information for the current branch.

Please specify which branch you want to merge with.

See git-pull(1) for details.

 

    git pull <remote> <branch>

 

If you wish to set tracking information for this branch you can do so with:

 

    git branch --set-upstream-to=origin/<branch> wsyht

 

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git branch --set-upstream-to=origin/wsyht

Branch wsyht set up to track remote branch wsyht from origin.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git pull   #執行完上面那一步,終於能夠pull了,輸出的提示合併也沒有問題,若是合併有問題,就修改剛剛修改的文件,而後再push

Merge made by the 'recursive' strategy.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git pull 

Already up-to-date.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat demo1.txt

test

222

333

111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git push origin wsyht   #好push成功

Counting objects: 2, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (2/2), 338 bytes | 0 bytes/s, done.

Total 2 (delta 1), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

   e4ace0b..ca2b12a  wsyht -> wsyht

 

 

如今咱們看看GitHub上有沒有push成功了

 

 

 

 

 

 

多人協做衝突解決總結:一、若是我push報錯,則是別人先push了

                      二、這個時候我就提示git pull,或進行下一步操做

                      三、若是git pull的時候合併有問題,就要修改文件,解決衝突

                      四、再次push就成功了

 

 

十6、建立工做目錄的兩個方法

第一種、就是上面張三用的方:,git init後,而後使用取得遠程庫的分支,建立遠程庫的分支

如今咱們來演示第二種方法:git clone

當來了一位新員工,咱們就給克隆git的項目下來給他開發推送

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3

$ git clone git@github.com:wsyht/demo_test.git

Cloning into 'demo_test'...

remote: Counting objects: 73, done.

remote: Compressing objects: 100% (45/45), done.

remote: Total 73 (delta 5), reused 71 (delta 5), pack-reused 0

Receiving objects: 100% (73/73), 6.22 KiB | 0 bytes/s, done.

Resolving deltas: 100% (5/5), done.

Checking connectivity... done.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3

$ ls

demo_test/

cd

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3

$ cd demo_test/

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ ls

demo1.txt  oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ cat oo.txt

111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ echo "222" >>oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ git status

On branch master

Your branch is up-to-date with 'origin/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:   oo.txt

 

no changes added to commit (use "git add" and/or "git commit -a")

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ git add oo.txt

warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ git status

warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

On branch master

Your branch is up-to-date with 'origin/master'.

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

 

        modified:   oo.txt

 

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ git commit -m'yes'

[master warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

3538a0c] yes

warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ ls

demo1.txt  oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ cat oo.txt

111

222

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo3/demo_test (master)

$ git push origin master   #沒有問題,推送成功

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 1 (delta 0)

To git@github.com:wsyht/demo_test.git

   55c5d89..3538a0c  master -> master

 

 

十7、忽略須要push的文件

忽略push的文件很簡單,只須要在你的工做區目錄下建立一個特珠的.gitignore文件,而後把忽略的文件名或文件尾輟填進去便可,但在windows下你是建立不了.xxx的文件,他會提示你輸入文件名,這個時候你能夠用編輯器另存爲就能夠建立出這個文件

如:

1

2

3

#python

*.so

*.egg

 

 

若是你添加的某個文件被忽略了,但你又想添加,

有兩種方法解決:第一:修改.gitignore文件

                第二:就是強制執行他

 

第一種方法:查看要添加的那個文件在.gitignore的哪一行設置了忽略,而後去修改.gitignore文件

1

git check-ignore -v xx.xx  #xx.xx是被忽略的那個文件

 

 

第二種方法:加上-f 強制添加執行

1

git add -f xx.xx

 

 

GitHub也爲咱們準備了各類配置文件,你們能夠參考一下

https://github.com/github/gitignore

 

 

十8、更新本地工做區

張三操做:  #demo2是張三的工做區目錄

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ echo "333" >> oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git add oo.txt

warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git commit -m'333'

[wsyht warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

af8dc1c] 333

warning: LF will be replaced by CRLF in oo.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

 Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git push origin wsyht

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

   c3f4ac0..af8dc1c  wsyht -> wsyht

 

 

個人操做: 關健字: git pull #更新本地工做區

記得修改文件以前先git pull一下,再修改和push

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ cd ../demo1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ ls

demo1.txt  oo.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git pull

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), done.

From github.com:wsyht/demo_test

   c3f4ac0..af8dc1c  wsyht      -> origin/wsyht

Updating c3f4ac0..af8dc1c

Fast-forward

 oo.txt | 1 +

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cat oo.txt  #查看已經看到333了

111

222

333

 

 

十9、CentOS下搭建Git服務器

#服務器端部署

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[root@localhost local]# ifconfig |grep  "inet addr:" | grep Bcast | awk -F":" '{print $2}' | awk -F' ' '{print $1}'

192.168.1.102

[root@localhost ~]# yum -y install git

[root@localhost ~]# useradd  git  #添加的git必定要能登錄,不然你clone和push都會報錯,因此不能加-s參數

[root@localhost ~]# git init --bare /usr/local/wsyht.git

[root@localhost ~]# ls /usr/local/wsyht.git/

branches  description  hooks  objects

config    HEAD         info   refs

[root@localhost ~]# chown -R git:git /usr/local/wsyht.git #這裏必定要加-R,不然本地push會報沒有權限

[root@localhost ~]# cd /home/git/

[root@localhost git]# mkdir -m 700 .ssh  #必定要給700權限,不然密鑰對不上

[root@localhost git]# chown git: .ssh    #記住全部者和所屬組必定要給git用戶,不然會連不上,提示你輸入密碼,因此權限方面要注意兩個方面,第一個就是全部者所屬組,第二個就是他的讀寫執行權限

[root@localhost git]# cd .ssh

[root@localhost .ssh]# echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6y9vxDIo9rqCAW7xebYpFFIL5M0wvwfe/qbcehE5VRvcVvKnlH/TsEygxGJEMCgjfLW7sKo5Me3W5KJNfNFeWQcn6AMSm08ZQk9qCbP1DpyW+AWvnz2KORYzM0+yLSbjd8py6XpWaMO1zX4TFm6VH3IRfaIpe0G/DhIzMxfNnNdCcXw6ou1Bb6tI41Q78ovGZGyPum4Nt03vLQpe0d6p5bqk21h1VeDbnH2mKa5GbS+OLgK+1qMvUy5+q8deASn18gepxhNtk+7LqNWlhTN8hQBFHOsqMuLvb5IGqBTRy/HfEFPiEkmgy9YrSWHiaPqJ/rCIYufaIKEgyAlISIqPj wsyht" >> authorized_keys

 

解釋:上述的--bare意思是隻會建立一個裸倉庫,而沒有工做區,由於服務器上的Git倉庫純碎是爲了共享,因此不讓用戶直接到服務器上去改工做區,而且服務器上的倉庫一般以.git結尾,因此我上面起了一個wsyht.git的倉庫名字,再把倉庫的權限改好,把公鑰的Key配上去,生成key的使用方式前面已講了,這裏就再也不闡述,到下面咱們就能夠在客戶端上進行clone,push等之類的操做

 

#本地操做

鏈接方式是 git clone git@server ip:/倉庫路徑/倉庫目錄名字

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ mkdir demo2

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f

$ cd demo2

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2

$ git clone git@192.168.1.102:/usr/local/wsyht.git

Cloning into 'wsyht'...

remote: Counting objects: 11, done.

remote: Compressing objects: 100% (4/4), done.

remote: Total 11 (delta 0), reused 0 (delta 0)

Receiving objects: 100% (11/11), done.

Checking connectivity... done.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ git remote -v

origin  git@192.168.1.102:/usr/local/wsyht.git (fetch)

origin  git@192.168.1.102:/usr/local/wsyht.git (push)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2

$ ls

wsyht/

c

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2

$ cd wsyht

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ ls

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ echo "111" > test.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ git add test.txt

warning: LF will be replaced by CRLF in test.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ git commit -m'com test.txt'

[master 7108256] com test.txt

warning: LF will be replaced by CRLF in test.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 create mode 100644 test.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2/wsyht (master)

$ git push origin master

Counting objects: 3, done.

Writing objects: 100% (3/3), 238 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 2 (delta 0)

To git@192.168.1.102:/usr/local/wsyht.git

   65c1f60..7108256  master -> master

 

OK,能夠看到clone和push都沒有問題一切正常,其餘命令操做方式跟遠程倉庫同樣,惟一不同的就是鏈接的clone後面的那一串域名不一樣

 

注意:我在本地push文件到Git服務器後,由於建立的這個是裸倉庫是沒有工做區的,因此他的存文件的方式是不同的,貌似是以什麼進制方式存着,因此你在Git服務器裏是找不到他的文件的,這個很正常,他是佔用服務器的磁盤空間的

 

這個是Git服務器存取分支目錄:

/usr/local/wsyht.git/refs/heads/

當我在本地建立了新分支的時候,則全部分支文件會在上面目錄下顯示出來

以下我又建立了一個wsyht分支,則在服務器上又顯示一個wsyht的普通文件

1

2

3

4

5

6

[root@localhost heads]# pwd

/usr/local/wsyht.git/refs/heads

[root@localhost heads]# ll

總用量 8

-rw-rw-r-- 1 git git 41 7月  31 21:12 master

-rw-rw-r-- 1 git git 41 7月  31 21:20 wsyht

 

 

而這個目錄是存儲標籤內容,這裏我就再也不一一演示了

[root@localhost tags]# pwd

/usr/local/wsyht.git/refs/tags

 

報錯展現:

1)由於git服務器裏的git用戶禁止登錄了,本地clone不上因此報的錯

1

2

3

4

Administrator@YYBFVJDMAPF13NB MINGW64 /f/KuGou

$ git clone git@192.168.1.102:/usr/local/wsyht.git

Cloning into 'wsyht'...

fatal: protocol error: bad line length character: This

 

解決方法:讓git能夠登錄,但不用設密碼,由於人咱們是用key去連的,這樣才安全

 

2)由於git權限不夠報的錯

1

2

3

4

5

6

7

8

9

10

11

12

13

14

 Administrator@YYBFVJDMAPF13NB MINGW64 /f/KuGou

yht (master)

$ git push origin master

Counting objects: 3, done.

Writing objects: 100% (3/3), 203 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

error: insufficient permission for adding an object to repository database ./objects

fatal: failed to write object

error: unpack failed: unpack-objects abnormal exit

To git@192.168.1.102:/usr/local

yht.git

 ! [remote rejected] master -> master (n/a (unpacker error))

error: failed to push some refs to 'git@192.168.1.102:/usr/local

yht.git'

 

解決方法:在Git服務器上把Git倉庫的整個目錄和文件子目錄子文件的全部者和所屬組都給Git便可

 

 

二10、鉤子腳本

SVN經常使用腳本:pre-commit,post-commit

Git經常使用腳本:post-receive

 

SVN和Git的pre-commit說明

這是提交要執行的動做,沒什麼好說的,不同的是Git是在本地執行動做

 

SVN和Git的post-commit說明

這是提交後要執行的操做,也沒什麼好說的,同理,不同的是,Git他提交後執行的動做是在本地執行的,由於他提交的內容是提交到本地倉庫,他本地就是一個倉庫

 

Git的 post-receive   => 重點內容到了

Git的post-receive是當接到你在本地push內容到個人Git服務器後,觸發了我Git服務器的post-receive腳本去執行相應的動做,而這個要腳本里面要執行的這個動做就由你自已去定義

 

如今咱們來演示一下

Git服務器做:

必定要先把文件複製出來,把後面的.sample去掉才能使用

 

從上圖能夠看到,複製出來的post-receive權限不對,咱們要先改權限

 

1

2

3

4

5

6

7

8

9

10

11

12

13

[root@bogon hooks]# ll

總用量 48

-rwxr-xr-x 1 git git  452 7月  31 12:45 applypatch-msg.sample

-rwxr-xr-x 1 git git  896 7月  31 12:45 commit-msg.sample

-rwxr-xr-x 1 git git  160 7月  31 12:45 post-commit.sample

-rwxr-xr-x 1 git git  548 8月   2 00:26 post-receive

-rwxr-xr-x 1 git git  548 7月  31 12:45 post-receive.sample

-rwxr-xr-x 1 git git  189 7月  31 12:45 post-update.sample

-rwxr-xr-x 1 git git  398 7月  31 12:45 pre-applypatch.sample

-rwxr-xr-x 1 git git 1578 7月  31 12:45 pre-commit.sample

-rwxr-xr-x 1 git git 1239 7月  31 12:45 prepare-commit-msg.sample

-rwxr-xr-x 1 git git 4951 7月  31 12:45 pre-rebase.sample

-rwxr-xr-x 1 git git 3611 7月  31 12:45 update.sample

 

 

添加腳本要執行的內容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[root@bogon hooks]# echo "/bin/touch /tmp/test_wsyht.txt" >> post-receive  

[root@bogon hooks]# cat post-receive

#!/bin/sh

#

# An example hook script for the "post-receive" event.

#

# The "post-receive" script is run after receive-pack has accepted a pack

# and the repository has been updated.  It is passed arguments in through

# stdin in the form

#  <oldrev> <newrev> <refname>

# For example:

#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master

#

# see contrib/hooks/ for a sample, or uncomment the next line and

# rename the file to "post-receive".

#. /usr/share/git-core/contrib/hooks/post-receive-email

/bin/touch /tmp/test_wsyht.txt

 

由於/tmp目錄下是全部用戶都有讀、寫、執行的,若是要對其餘目錄有讀寫執行權限,必須得先把全部者所屬組改爲Git,而後再給他相應的讀、寫、執行權限便可,

 

再查看一下/tmp目錄下的內容,目前沒有test_wsyht.txt文件

1

2

3

4

[root@bogon hooks]# ls /tmp/

keyring-R8s7ni  pulse-gbg3wjQuvzvI  virtual-root.cNwkfe  vmware-config0

orbit-gdm       pulse-zqLc6oaaNKa9  virtual-root.D0hQX0  VMwareDnD

orbit-root      ssh-hotQgm2726      virtual-root.KIVXJC  vmware-root

 

好,到這一步,咱們的Git服務器就算配置完了

 

本地操做:

提交和push

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Administrator@YYBFVJDMAPF13NB MINGW64 /f/test/wsyht (master)

$ echo "test commit" >> wsyht.txt

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/test/wsyht (master)

$ git add wsyht.txt

warning: LF will be replaced by CRLF in wsyht.txt.

The file will have its original line endings in your working directory.

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/test/wsyht (master)

$ git commit -m'commit test'

[master warning: LF will be replaced by CRLF in wsyht.txt.

The file will have its original line endings in your working directory.

673c27a] commit test

warning: LF will be replaced by CRLF in wsyht.txt.

The file will have its original line endings in your working directory.

 1 file changed, 1 insertion(+)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/test/wsyht (master)

$ git push origin master

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.

Total 3 (delta 1), reused 0 (delta 0)

To git@192.168.1.113:/usr/local/wsyht.git

   f979810..673c27a  master -> master

 

 

查看Git服務器

 

 

 

【拓展知識】

1、別名配置

法1:真接命令行配置

輸入--global是對當前用戶起做用的,若是不加只對當前倉庫起做用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git config --global alias.st status   #把status 配置成別名 st

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git config --global alias.co checkout #把checkout 配置成別名 co

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git config --global alias.ci commit   #把commit 配置成別名 ci

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git config --global alias.br branch   #把branch 配置成別名 br

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git config --global alias.unstage 'reset HEAD'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git config --global alias.last 'log -1'

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git status   #用原來的命令查看狀態

On branch wsyht

Your branch is up-to-date with 'origin/wsyht'.

nothing to commit, working tree clean

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo2 (wsyht)

$ git st       #用別名查看狀態,成功了,其餘別名一樣使用方法,這裏再也不一一演示

On branch wsyht

Your branch is up-to-date with 'origin/wsyht'.

nothing to commit, working tree clean

 

 

法2:配置文件配置

咱們能夠改兩個配置文件

首先先說第一個配置文件,咱們在命令行輸入命令--global設置別名,也是把命令寫進這個文件的,咱們也能夠進到這個文件修改,他就是你家目錄下的.gitconfig,敲cd回車就能夠回到你的家目錄

第一個配置文件配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ pwd

/f/demo1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ cd

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$ ls -a .gitconfig

.gitconfig

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

$ cat .gitconfig

[merge]

        tool = vimdiff

[user]

        name = wsyht

        email = 891958201@qq.com

[color]

        ui = true

[alias]

        st = status

        co = checkout

        ci = commit

        br = branch

        unstage = reset HEAD

        last = log -1

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

 

Administrator@YYBFVJDMAPF13NB MINGW64 ~

 

能夠看到.gitconfig文件寫入了我在命令行設置別名的命令,若是你加--global就會寫入這個配置文件,你也能夠直接進到這個文件添加,而不在終端執行命令,要刪除別名的話進到這個文件把相應的別名命令刪除便可,這個文件是針對當前用戶生效的

 

第二個配置文件

在倉庫目錄下.git/config目錄下,此配置文件,是針對當前倉庫有效,因此在當你在終端執行命令的時候不加--global就會把命令寫入這個文件,你也能夠進到這個文件進行添加刪除別名命令,下面咱們來看配置文件內容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

$ pwd

/f/demo1/.git

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)

$ cat config

[core]

        repositoryformatversion = 0

        filemode = false

        bare = false

        logallrefupdates = true

        symlinks = false

        ignorecase = true

[branch "master"]

[remote "origin"]

        url = git@github.com:wsyht/demo_test.git

        fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]

        remote = origin

        merge = refs/heads/master

[branch "wsyht"]

        remote = origin

        merge = refs/heads/wsyht

#[alias]

        #br = branch

 

添加方法同樣,跟上面的第一個配置文件.gitconfig同樣,這裏我只是爲了演示,因此我把他註釋掉了

 

 

2、標籤操做

1)建立標籤

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git branch

  master

* wsyht

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag v1.0  #建立標籤v1.0

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag

v1.0

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git log --pretty=oneline --abbrev-commit

ca2b12a Merge branch 'wsyht' of github.com:wsyht/demo_test into wsyht

805dcba wo add 111 demo

e4ace0b zhangsan add 111 demo1

924fcc5 commit wsyht

55c5d89 222

6bbbcdf Merge branch 'master' of github.com:wsyht/demo_test

5e368ff test commit

9d505e6 commit wsyht

cd961d2 merged bug

ed1eded commit bug

0631e15 wsyht commit

3a59897 wsyht commint

c1e71b1 test

3020d4c oo

d497da7 Delete demo2.txt

d693375 888

07e6d4c 777

43185ac delete test1.txt

7ecdd08 一次性提交全部文件

fc33a37 1111

85ce2b6 4444

8445579 文件增長2222一行

25e4895 增長1111一行

a7e593c demo1提交

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag v1.1 805dcba  #經過指定他的號碼來給他建立標籤

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git show v1.1

commit 805dcba19f99a6057fc1eabdd75acb7f386d29ed

Author: wsyht <891958201@qq.com>

Date:   Sat Jul 30 22:26:43 2016 +0800

 

    wo add 111 demo

 

diff --git a/demo1.txt b/demo1.txt

index 5ff510a..f1bde10 100644

--- a/demo1.txt

+++ b/demo1.txt

@@ -1,3 +1,4 @@

 test

 222

 333

+111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git show v1.0

commit ca2b12a4cdd0e496bb0dec43c45e2b50336982d0

Merge: 805dcba e4ace0b

Author: wsyht <891958201@qq.com>

Date:   Sat Jul 30 22:28:58 2016 +0800

 

    Merge branch 'wsyht' of github.com:wsyht/demo_test into wsyht

 

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag -a v0.3 -m "個人第三個標籤"

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag -a v0.3 -m "個人第三個標籤" e4ace0b

fatal: tag 'v0.3' already exists

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag -a v0.4 -m "個人第三個標籤" e4ace0b  #-a 指定標籤號,-m 指定描述的內容

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag

v0.3

v0.4

v1.0

v1.1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git show v0.4

tag v0.4

Tagger: wsyht <891958201@qq.com>

Date:   Sat Jul 30 22:53:38 2016 +0800

 

個人第三個標籤

 

commit e4ace0bfa22f9e19913f95282fa5176a0b708be3

Author: wsyht <891958201@qq.com>

Date:   Sat Jul 30 22:20:56 2016 +0800

 

    zhangsan add 111 demo1

 

diff --git a/demo1.txt b/demo1.txt

index 5ff510a..f1bde10 100644

--- a/demo1.txt

+++ b/demo1.txt

@@ -1,3 +1,4 @@

 test

 222

 333

+111

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag  #查看全部標籤

v0.3

v0.4

v1.0

v1.1

 

 

 

2)刪除和push標籤

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag -d v0.3

Deleted tag 'v0.3' (was 784bb96)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag

v0.4

v1.0

v1.1

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git push origin v0.4  #提交單個標籤

Counting objects: 1, done.

Writing objects: 100% (1/1), 180 bytes | 0 bytes/s, done.

Total 1 (delta 0), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

 * [new tag]         v0.4 -> v0.4

 

 Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git push origin  --tags   #提交全部標籤

Total 0 (delta 0), reused 0 (delta 0)

To git@github.com:wsyht/demo_test.git

 * [new tag]         v1.0 -> v1.0

 * [new tag]         v1.1 -> v1.1

 

 

在GitHub上查看

 

標籤都出來了,push成功

 

3)刪除遠程庫的標籤

1

2

3

4

5

6

7

8

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git tag -d v0.4

Deleted tag 'v0.4' (was dea0b58)

 

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (wsyht)

$ git push origin :refs/tags/v0.4

To git@github.com:wsyht/demo_test.git

 - [deleted]         v0.4

 

 

能夠看到,遠程庫的標籤已經刪除成功

 

 

最後再說一下,有的人說SVN有工做能夠提交上傳,可是Git卻要敲命令,如今我告訴你Git也是有工具的,若是你不喜歡命令行的話,你能夠去下載這個工具,github sourcetree 以下圖,這是我網上找來的圖片,界面就是這個樣子

 

 

可是我還想說,我仍是喜歡用命令行,上面那個工具我沒用過,我相信大多數人都是用命令行,由於你經過命令行去作執行的話,你會更有知足感,你操做速度會更快更靈活更有快感,並且你知道每一步的步驟,你也不會混亂,相信我,用命令行吧,我相信大多數人都是用命令git bash的,放棄這個工具吧

 

Git經常使用命令總結:

git help git                #查看git命令幫助

git config --help             #查看config幫助

git config --global user.name 'wsyht'           #設置用戶名標識

git config --global user.email t89191958201@139.com  #設置郵箱標識

git config --list            #查看配置內容

git init                  #建立版本庫

git add 文件名              #添加到暫存區

git commit -m'你描述的內容'  #提交

git status          #查看狀態

git diff  文件名      #查看文件比較

git log            #查看提交的歷史記錄

git log --graph --pretty=oneline --abbrev-commit     #--graph以圖形化顯示,--pretty=oneline省略部分信息輸出,--abbrev-commit則是縮短版本號簡短顯示

git reset --hard HEAD^       #git reset --hard HEAD^

git reset --hard HEAD^^      #git reset --hard HEAD^^

git reset --hard HEAD~100     #回退到前100個版本

git reflog              #獲取版本號

git reset --hard 版本號      #恢復版本

git checkout -- 文件名       #撤銷工做區的修改

git checkout .            #撤銷工做區的修改

git reset HEAD 文件名       #轍消暫存區的修改

git remote add origin git@github.com:wsyht/demo_test.git   #添加遠程url鏈接

git push -u origin master    #第一次push須要加-u

git remote rm origin       #刪除遠程默認倉庫名"origin

git branch     #查看分支

git branch 分支名字          #建立分支             

git checkout 分支名字         #切換分支

git checkout -b 分支名字       #建立切換分支

git merge 分支名字           #合併某分支到當前分支

git branch -d 分支名字        #刪除分支

git branch -D wsyht          #大D強制刪除

git stash         #儲藏當前修改狀態

git stash list      #查看儲藏內容

git stash pop       #恢復原來修改的狀態

git remote -v       #查看遠程庫的詳信息

git pull              #更新本地工做區

git fetch origin         #取全部遠程庫的分支到本地

git fetch origin 分支名    #取某個分支

git branch 分支名 origin/分支名              #建立遠程庫的分支到本地

git clone git@github.com:github註冊名/項目名.git   #克隆GitHub項目到本地

git clone git@server ip:/倉庫路徑/倉庫目錄名字    #克隆Git服務器項目到本地

git check-ignore -v xx.xx      #查看要添加的那個文件在.gitignore的哪一行設置了忽略

git add -f 文件名            #強制添加此文件到暫存區

it tag v1.0               #建立標籤v1.0

git tag v1.1 805dcba          #經過指定他的號碼來給他建立標籤

git tag -a v0.4 -m "個人第三個標籤" e4ace0b    #-a 指定標籤號,-m 指定描述的內容

git tag                            #查看全部標籤

git tag -d v0.3            #刪除標籤

git push origin v0.4         #提交單個標籤

git push origin  --tags       #提交全部標籤

git tag -d v0.4             #刪除遠程標籤第一步

git push origin :refs/tags/v0.4  #刪除遠程標籤第二步

git config --global alias.st status   #把status 配置成別名 st,其餘命令設別名依此類推

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"  #日誌別名配置

 

Git擴展技術:Git flow工做流程

這個工做流程你們能夠去百度上搜索一下文章來看看,這個我目前也不懂,不過他是一個不錯的東西,他可以規範化你的開發流程

 

本文出自 「wsyht的博客」 博客,請務必保留此出處http://wsyht90.blog.51cto.com/9014030/1832284

相關文章
相關標籤/搜索