史上最经典的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

相关文章
相关标签/搜索