【原理】

1)Git介绍

2)Git和SVN的区别、SVN工作流程、架构图讲解

3)Git优点、架构图、文件三种状态、三个工作区域、工作流程讲解


【实战】

1)准备工作

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常用命令总结


前言:本人技术有限,如有说得不对或做的不对的地方,请大家提出来,谢谢大家


一、

Git介绍:

Git:是可以提供版本控制的一个工具

Github:提供了你用git这个工具的一个平台,公共代码仓库

git服务器:自建仓库,私有仓库(企业用)


二、

Git和SVN的区别

Git是分布式版本控制系统,SVN是集中式版本控制系统


SVN的工作流程 

SVN有一台中央服务器,下面有一台至多台的客户端,完全依赖于中央服务器工作

SVN工作流程:

客户端通过把内容Check out到本地,然后提交到中央服务器,后面再更新,然后再提交


弊端:1、如果中央服务器断网了,则会导致无法更新

    2、如果网络不好,则会导致更新缓慢

    3、如果中央服务器单点故障,又没及时更新的话,则会导致数据丢失


架构图:

wKioL1ec0nvz9JkEAABynam-fVk782.png-wh_50



三、

Git优点:

1、Git没有中央管理器,每个人的电脑就是一台完整的版本库

2、工作不需要联网,因为版本都在自已的电脑上

3、如果多个人共同协作,只需要push到github仓库上即可进行多人协作

4、项目提交到GitHub,每一次提到到本地操作,都是一次对代码仓库的完整备份


架构图:

wKiom1ed4gqRK6OZAABmsyIuhHw977.png-wh_50

上图Git服务器是企业内部自已搭的私有的,如果是自已玩的,我们自已的电脑就是自已的仓库,我们不需要联网,然后有一个GitHub是公共仓库,我们还可以把自已的项目提交到公共仓库,相当于对自已代码又做了一个备份,而企业内部,则需要自建一台私有的Git服务器,大家通过先把代码提交到自已电脑的仓库,然后再提交到Git服务器这样的一个工作流程



Git文件三种状态

已提交(committed)

表示该文件已经被安全地保存在本地数据库中了


已修改(modified)

表示修改了某文件,但还没有提交保存


已暂存(staged)

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



Git文件流转时的三个工作区域:

Git的工作目录:

保存着特定的版本文件,属于提交状态


暂存区域:

做了修改并已放入暂存区域


本地仓库:

顾名思义,本的版本仓库


Git工作流程:

1、在工作目录中修改某些文件

2、对修改后的文件进行快照,然后保存到暂存区域

3、提交更新,将保存在暂存区域的文件快照永久转储到Git目录中


【实战】

一、Git准备工作

1)cygwingit bash的对比

由于我们在Windows上执行git的命令需要一个仿Unix终端的软件,在这里,我给大家介绍两款软件,一款是cygwin,一款是git bash,git bash是git提供给你用的软件,那么这两款软件有啥不一样呢,请看图:

wKioL1ed5RmAGrZ9AAC3TTpcMCA953.jpg-wh_50

1、左边那款是git bash,他能够把你当前用户家目录所有文件包括windows的都列出来,而右边的cygwin只列出他用户家目录的文件,并没有列出windows的文件

总结第一点:cygwin简洁性强,而bit bash把windows家目录的东西都列出来,让你也可以进行操作,至于你觉得哪个好,这个看你个人喜好


再来看第二张图

wKioL1ed5avyKI8tAADDm-PiStg626.jpg-wh_50

2、左边那款还是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,界面如下:

wKioL1eax3WQRgkWAACl-GSLWyc229.png-wh_50

它是在Windows下模仿Shell命令行的一个终端


4)查看帮助

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop
$ git help git         #git help <verb>

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop
$ git config --help    #git <verb> --help


5)设置身份

因为git是分布式版本控制系统,所以需要填写用户名和邮箱做为一个标识

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)设置比较工具(可选)

Administrator@YYBFVJDMAPF13NB MINGW64 ~/Desktop
$  git config --global merge.tool vimdiff


7)检查配置

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


二、创建版本库

1)(关健字:git init

版本库:又名仓库,你可以简单的理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,包括修改、删除、提交、回滚

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)

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

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

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

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

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

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)
$ git log --pretty=oneline
8445579cbd259610f94a6c665fc841313a131516 文件增加2222一行
25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 增加1111一行
a7e593c34a10f0b2e732153c09bce74381e7de35 demo1提交
Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)
$


三、版本回退

1)回退命令

   1、回退到上一个版本

      git reset --hard HEAD^

   2、回退到上上一个版本

      git reset --hard HEAD^^

   3、回退到前100个版本

      git reset --hard HEAD~100

演示:

回退到上一个版本

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

$ 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 版本号

Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1 (master)
$ git reset --hard 8445579
HEAD is now at 8445579 文件增加2222一行

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


(3)查看恢复

$ 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)
$


四、工作区与暂存区的区别

工作区:就是在我创建的demo1的版本库目录里的文件,.git目录除外

版本库:.git目录就是版本库,版本库存着很多东西,其中最重要的就是stage(暂存区),还有Git为我们自动创建了第一个分支Master,以及指向Master的第一个指针HEAD,


提交Git文件到版本库有两步;

第一步:是使有tgit add把文件添加进去,实际上就是把文件添加到暂存区

第二步:使用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支上


演示:

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

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)添加到暂存区查看

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)提交再查看

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)一次性提交所有文件方法

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)
$


四、撤销修改

如果在一行里面添加了新内容,现在要撤消

总结一下一共有三种方法:

第一种:把要删除的内容添删掉,然后add,commit

第二种:恢复上一个版本,git reset --hard HEAD^

第三种:如下 (关健字:git checkout -- 文件名)-- 很重要,请注意,这里说的是两个'-',如果没有 -- 的话;那么命令变成切换分支了 或者用 git checkout . 推荐使用这个,可以批量还原修改的文件,前提是未add和commit,但是不包括新增的文件,只是针对已经存在文件且修改了内容的文件,进行撤消

$ 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添加到暂存区的,那么撤销方法如下

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.

接着再如下图操作,

wKiom1ebFiXCWU5_AACt19VZ7Q0650.png-wh_50


如果新添加的文件,但是还没有add,现在我们不想用这个新文件了,那么我们直接删除就行了,如下:

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)
$



五、删除文件和恢复

(1)删除演示

$ 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)恢复演示

$ 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)
$


总结:

三种情况

1、未add想辙消(包括增删改文件内容,也包括删文件)

   git checkout -- 文件名  (辙消工作区的修改)    或

   git checkout .

2、add到暂存区后,需分两步操作

   第一步:git reset HEAD 文件名  (辙消暂存区的修改)

3、commit之后

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

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

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

      git reflog                                      #获取版本号

      git reset --hard 版本号                #恢复版本号

     

六、远程仓库(GitHub公共仓库)

1)先注册github账号

省略...


2)生成ssh-keygen密钥对

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 p_w_picpath 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 ~
$


3、在GitHub上创建项目

wKioL1ebAXnj0hAcAAAdHI_1sI4513.png-wh_50

4、创建展示

wKiom1ebAfGjJgk8AACWBDmViPA287.png-wh_50


5、完成展示

wKiom1ebAlnzqzOFAACT2QJnhwc863.png-wh_50

在如上截图显示内容我要说一下了

通过本地仓库和GitHub仓库的连接方式有两种:

第一种:通过HTTPS方式连接,就是如上红框显示,这种方式在本地远程GitHub仓库,需要用输入你的GitHub账号和密码,而且每次都要输入  缺点:不安全,麻烦

第二种:通过ssh方式连接,在本地创建密钥对, 再把公钥上传到GitHub服务器,然后实现无密码连接如下截图这是通过ssh的连接方式:     优点:安全,方便

wKioL1ebA7qB2N07AACSKjXXAMk695.png-wh_50


先说第一种连接方式:

wKioL1ebBW2Bjg8QAAEBiSqKPOQ491.png-wh_50

这种方式是通过第一种https连接方式,先进入到我的工作目录下,也就是创建的版本库目录下,

然后输入远程命令,用户名和密码即可


第二种方式展示

上面已经生成了密钥对了,然后我们把id_rsa.pub的公钥复制到GitHub上去

wKiom1ebBi-xnOdaAABzXIaGA0o811.png-wh_50

wKiom1ebBkWTtHMKAAAvQrTVOTc834.png-wh_50

wKiom1ebBtOyD95ZAACI8tcRUSs306.png-wh_50


wKioL1ebBrvDP3gSAAB3sJPN0-8443.png-wh_50

wKiom1ebB1LCc1I5AABG0jx8lWs172.png-wh_50

本地测试

修改配置文件,进到版本库目录.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

wKiom1ebCAPRH04OAABX1TIeXnc662.png-wh_50

如上图所示,原本默认是https的连接方式,现在改成通过ssh方式去连接,只需要把giuhub刚创建项目时他显示的连接命令考到config文件url后面即可

测试连接:

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

先提交到本地

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了

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


如果报如上错,解决办法如下:

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是远程仓库默认的库名

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

wKioL1ebDAXDDaR5AAAguMPGeIo981.png-wh_50

wKioL1ebDBPxyuVCAAANVPmk3DI514.png-wh_50

wKiom1ebDBPyRAbeAAA5aIKkXXs435.png-wh_50

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参数了


七、远程库克隆到本地

点击进去

wKiom1ebDhSwGhsLAAAph7wWCtY580.png-wh_50

wKioL1ebDkOw8k4-AAARGt-Btvw228.png-wh_50


wKiom1ebDkOgNYMqAAAjzcqXS3Q313.png-wh_50

删除提交后,现在我们在把GitHub克隆到本地,你会发现少了一个文件


本地操作:关健字: git clone

wKiom1ebD66TfdA8AAB2wbdW7jg619.png-wh_50

可以看到多了一个目录出来


八、GitHub删除项目

wKioL1ebEPqQ_O2hAAAamRJ76gE521.png-wh_50

wKioL1ebEPqRtKLOAAAV_VMalF4933.png-wh_50

wKiom1ebEPqQYYZVAABzEVRhs18485.png-wh_50

wKiom1ebEPqgwwZ9AABSrm3Z0iQ792.png-wh_50

wKioL1ebEPqRbneUAABe47WF4Ww793.png-wh_50


九、分支的创建与合并

首先我们来讲一下什么是分支,分支就相当于我现在在做一个项目A,然后项目里面有一个功能的BUG我需要去做修改,然后为了安全,不影响我的Master分支,我就在我的Master分支上再创建一个分支出来,然后我就在我这个分支上修改我的代码,修改完成后,再与我的Master主分支合并,这样就不会影响到我的原文件,分支其实就是基于某一时刻的代码进行修改,这个分支实际上只存储这些修改,也可以说是项目某个时期的快照,但他不是真实赋值文件,修改完成后,合并到Master分支,这样,Master分支的内容从而也会跟着发生改变,而创建的临时文件,则怎么修改也不会影响我的源文件,即Master分支的内容,相当于你在虚拟机的一个测试环境,你爱昨玩昨玩,都不会影响到我的真实系统


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

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分支上进行修改内容提交操作 

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

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 分支名字


十、分支合并冲突解决

$ 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分支的内容再提交就正常了

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


十一、查看历史合并功能

在合并分支时,默认会用fast forware模式,这样的话在我们删除分支后,会删掉我们曾经合并过的信息,此时,你再用git log就查看不了我们跟哪些分支做过合并了,所以在合并的时候我们要加上一个参数,--no-ff

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)
$


十一、分支的注意事项

第一个要注意的地方:如下可以看到git切换到wsyht分支,但是wsyht分支没有add和commit,所以再切回master,他就相当于直接改了Master的文件,所以修改了一定要,add再commit后再切回Master

第二个要注意的地方:当你在其他分支上修改了文件提交后,合并到Master后,一定要删了开出来的分支,然后再开新的分支用,毕免二次污染

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


十二、双分支操作

我在这里讲一句:实践出真知,有时候想一些东西,还不如亲自动手操作明白来得快,有些地方你想来想去不明白,但你动手操作了,你就明白他是怎么一回事了,所以不要光看不练,相信我,下面对于属于开发比较复杂的项目的开发来说很重 ,请大家认真看


#这里是指使用第一个分支开发,但是还没开发完新功能,所以并未能add提交,这个时候上头又说要马止改一个很重要的bug,要快速完成,所以我们在把这个分支储藏起来,下次回来再拿出来继续开发


第一步:

#第一个wsyht分支操作   

wsyht分支开发->储藏-暂停

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分支

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先合并了,然后再调回刚才修改的状态,继续开发提交

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分支

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内容也删了


十四、强制删除分支


当我们在某第二个分支开发某一个功能,提交分支之后,回头策划却说还是不要改了,这个时候我们不能合并分支,只能删除第二个分支

如下:

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)
$


十五、多人协作

当在你提交或帮隆的时候会自动帮你把本地和远程的master分支对应起来,远程库的默认名称是origin


#查看远程库

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分支不需要提交的,就把他先主分支先合并了,然后再提交到远程库中

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成功同步上去了

wKiom1eck4KR2rjvAAAktDgPcWY109.png-wh_50

2)多人同推冲突

现在假如有两个人两台电脑共同推一个分支的文件,假如我先推了,张三也又接着推了,张三就会报错推不上去,这个时候,我们来看看怎么操作


角色:我”“张三”


首先我的在我的电脑上创建一个wsyht分支用来做开发,因为master分支张三是无法拉取下来做开发的,所以只能搞一个wsyht分支,在wsyht分支上做开发,开发完成功能后,提交wsyht分支,然后push到git服务器,再合并master分支,然后把master分支提交,push到git服务器


的电脑操作,如下:

流程:创建分支->切换分支->推送   #此分支一般工作中事先存在,而我这里为了做事验,所以临时创建

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

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

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成功了

wKiom1ecxiqSZqE8AABEumGy6Cc426.png-wh_50


wKioL1ecxiqgHGXmAAA1zLwQfaY875.png-wh_50


wKiom1ecxiry6UlLAABFo2XzZhw932.png-wh_50


多人协作冲突解决总结:1、如果我push报错,则是别人先push了

                      2、这个时候我就提示git pull,或进行下一步操作

                      3、如果git pull的时候合并有问题,就要修改文件,解决冲突

                      4、再次push就成功了



十六、创建工作目录的两个方法

第一种、就是上面张三用的方:,git init后,然后使用取得远程库的分支,创建远程库的分支

现在我们来演示第二种方法:git clone

当来了一位新员工,我们就给克隆git的项目下来给他开发推送

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


十七、忽略需要push的文件

忽略push的文件很简单,只需要在你的工作区目录下创建一个特珠的.gitignore文件,然后把忽略的文件名或文件尾辍填进去即可,但在windows下你是创建不了.xxx的文件,他会提示你输入文件名,这个时候你可以用编辑器另存为就可以创建出这个文件

如:

#python
*.so
*.egg


如果你添加的某个文件被忽略了,但你又想添加,

有两种方法解决:第一:修改.gitignore文件

                第二:就是强制执行他


第一种方法:查看要添加的那个文件在.gitignore的哪一行设置了忽略,然后去修改.gitignore文件

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


第二种方法:加上-f 强制添加执行

git add -f xx.xx


GitHub也为我们准备了各种配置文件,大家可以参考一下

https://github.com/github/gitignore



十八、更新本地工作区

张三操作:  #demo2是张三的工作区目录

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

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


十九、CentOS下搭建Git服务器

#服务器端部署

[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:/仓库路径/仓库目录名字

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的普通文件

[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不上所以报的错

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权限不够报的错

 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即可



二十、钩子脚本

SVN常用脚本:pre-commitpost-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去掉才能使用

wKiom1efd8TRjPtkAAD55tUEG1s874.png-wh_50

从上图可以看到,复制出来的post-receive权限不对,我们要先改权限


[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


添加脚本要执行的内容

[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文件

[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

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服务器

wKioL1efeizQTkLJAAA6ZuB0rPQ036.png-wh_50



【拓展知识】

一、别名配置

法1:真接命令行配置

输入--global是对当前用户起作用的,如果不加只对当前仓库起作用

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回车就可以回到你的家目录

第一个配置文件配置

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就会把命令写入这个文件,你也可以进到这个文件进行添加删除别名命令,下面我们来看配置文件内容

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一样,这里我只是为了演示,所以我把他注释掉了



二、标签操作

1)创建标签

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标签

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上查看

wKioL1ecyPaBHKEQAAAttvRsY_c430.png-wh_50

标签都出来了,push成功


3)删除远程库的标签

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

wKioL1ecyaXRNOA3AABFcfr4XO4435.png-wh_50

可以看到,远程库的标签已经删除成功



最后再说一下,有的人说SVN有工作可以提交上传,但是Git却要敲命令,现在我告诉你Git也是有工具的,如果你不喜欢命令行的话,你可以去下载这个工具,github sourcetree 如下图,这是我网上找来的图片,界面就是这个样子

wKioL1edb0mAhGeVAAKxdS1wuRM925.jpg-wh_50


但是我还想说,我还是喜欢用命令行,上面那个工具我没用过,我相信大多数人都是用命令行,因为你通过命令行去做执行的话,你会更有满足感,你操作速度会更快更灵活更有快感,而且你知道每一步的步骤,你也不会混乱,相信我,用命令行吧,我相信大多数人都是用命令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工作流程 

这个工作流程大家可以去百度上搜索一下文章来看看,这个我目前也不懂,不过他是一个不错的东西,他能够规范化你的开发流程