Git从入门到熟悉

【原理】

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

【拓展知识】

1)别名配置

2)标签操作

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

一、

Git介绍:

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

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

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

二、

Git和SVN的区别

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

SVN的工作流程 

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

SVN工作流程:

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

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

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

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

架构图:

三、

Git优点:

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

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

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

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

架构图:

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

Git文件三种状态

已提交(committed)

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

已修改(modified)

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

已暂存(staged)

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

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

Git的工作目录:

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

暂存区域:

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

本地仓库:

顾名思义,本的版本仓库

Git工作流程:

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

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

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

【实战】

一、Git准备工作

1)下载安装

git下载链接:https://git-scm.com/download/win

我的安装是默认一路向北

2)桌面右健鼠标点击Git Bash Here打开Git Bash,界面如下:

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

3)查看帮助

[email protected] MINGW64 ~/Desktop
$ git help git         #git help <verb>

[email protected] MINGW64 ~/Desktop
$ git config --help    #git <verb> --help

4)设置身份

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

[email protected] MINGW64 /f/demo1 (master)
$ git config --global user.name ‘wsyht‘

[email protected] MINGW64 /f/demo1 (master)
$ git config --global user.email [email protected]

注意:git config 下有三个参数,

--global: 表示对这台机器上的所有Git仓库都会使用这个配置

--system:用户级别配置,针对某个用户生效

--local:针对本地有效

5)设置比较工具(可选)

[email protected] MINGW64 ~/Desktop
$  git config --global merge.tool vimdiff

6)检查配置

[email protected] 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
[email protected]
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true

二、创建版本库

1)(关健字:git init)

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

[email protected] MINGW64 ~/Desktop
$ cd f: 

[email protected] MINGW64 /f
$ mkdir demo1

[email protected] MINGW64 /f
$ cd demo1/

[email protected] MINGW64 /f/demo1
$ git init
Initialized empty Git repository in F:/demo1/.git/

[email protected] MINGW64 /f/demo1 (master)
$

这个时候会多了一个.git的隐藏目录,他是用来管理跟踪管理版本的,里面文件不可以修改

2)提交任务(关健字:git add,git commit)

[email protected] MINGW64 /f/demo1 (master)
$ pwd
/f/demo1

[email protected] MINGW64 /f/demo1 (master)
$ echo "test1" >> demo1.txt

[email protected] 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.

[email protected] MINGW64 /f/demo1 (master)
$ git commit -m "demo1提交"

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

[email protected] MINGW64 /f/demo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (master)
$

4)修改装态查看 (关健字:git status,modify)

[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt

[email protected] MINGW64 /f/demo1 (master)
$ echo "1111" >> demo1.txt

[email protected] 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")
[email protected] MINGW64 /f/demo1 (master)

5)对比查看 (关健字:git diff)

[email protected] 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.

[email protected] MINGW64 /f/demo1 (master)
$

注意:提交前须查看一下文件修改的内容,如不问题,再git add,然后git commit

6)查看提交历史记录(关健字:git log)

[email protected] MINGW64 /f/demo1 (master)
$ git log
commit 8445579cbd259610f94a6c665fc841313a131516   #每次提交的版本号
Author: wsyht <[email protected]>
Date:   Thu Jul 28 23:15:40 2016 +0800
    文件增加2222一行                               #增加内容显示的注释,最近的一次提交,从上到下排序
commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7
Author: wsyht <[email protected]>
Date:   Thu Jul 28 23:15:08 2016 +0800
    增加1111一行
commit a7e593c34a10f0b2e732153c09bce74381e7de35
Author: wsyht <[email protected]>
Date:   Thu Jul 28 22:56:25 2016 +0800
    demo1提交
    
[email protected] MINGW64 /f/demo1 (master)
$

注意:刚才是我自已做实验一共提交了三次,并未贴出来到博客而已,所以出现了三次提交的历史记录

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

Administ[email protected] MINGW64 /f/demo1 (master)
$ git log --pretty=oneline
8445579cbd259610f94a6c665fc841313a131516 文件增加2222一行
25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 增加1111一行
a7e593c34a10f0b2e732153c09bce74381e7de35 demo1提交
[email protected] MINGW64 /f/demo1 (master)
$

三、版本回退

1)回退命令

1、回退到上一个版本

git reset --hard HEAD^

2、回退到上上一个版本

git reset --hard HEAD^^

3、回退到前100个版本

git reset --hard HEAD~100

演示:

回退到上一个版本

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
test1
1111
2222

[email protected] MINGW64 /f/demo1 (master)
$ git reset --hard HEAD^
HEAD is now at 25e4895 增加1111一行

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
test1
1111

[email protected] MINGW64 /f/demo1 (master)
$ git log
commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7
Author: wsyht <[email protected]>
Date:   Thu Jul 28 23:15:08 2016 +0800
    增加1111一行
commit a7e593c34a10f0b2e732153c09bce74381e7de35
Author: wsyht <[email protected]>
Date:   Thu Jul 28 22:56:25 2016 +0800
    demo1提交
    
[email protected] MINGW64 /f/demo1 (master)
$

2)回退到刚才的最新版本

(1)首先获取版本号 (关健字:git reflog)

$ git reflog
25e4895 [email protected]{0}: reset: moving to HEAD^
8445579 [email protected]{1}: commit: 文件增加2222一行    #这个是版本号8445579
25e4895 [email protected]{2}: commit: 增加1111一行
a7e593c [email protected]{3}: commit (initial): demo1提交
[email protected] MINGW64 /f/demo1 (master)
$

(2)恢复版本(关健字:git reset --hard 版本号)

[email protected] MINGW64 /f/demo1 (master)
$ git reset --hard 8445579
HEAD is now at 8445579 文件增加2222一行

[email protected] MINGW64 /f/demo1 (master)
$

(3)查看恢复

$ cat demo1.txt
test1
1111
2222

[email protected] MINGW64 /f/demo1 (master)
$ git log
commit 8445579cbd259610f94a6c665fc841313a131516
Author: wsyht <[email protected]>
Date:   Thu Jul 28 23:15:40 2016 +0800
    文件增加2222一行
commit 25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7
Author: wsyht <[email protected]>
Date:   Thu Jul 28 23:15:08 2016 +0800
    增加1111一行
commit a7e593c34a10f0b2e732153c09bce74381e7de35
Author: wsyht <[email protected]>
Date:   Thu Jul 28 22:56:25 2016 +0800
    demo1提交
    
[email protected] MINGW64 /f/demo1 (master)
$

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

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

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

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

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

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

演示:

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

[email protected] MINGW64 /f/demo1 (master)
$ echo "3333" >> demo1.txt 

[email protected] MINGW64 /f/demo1 (master)
$ echo "1111" >> demo2.txt

[email protected] 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")

[email protected] MINGW64 /f/demo1 (master)
$

(2)添加到暂存区查看

[email protected] 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.

[email protected] 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
        
[email protected] MINGW64 /f/demo1 (master)
$

(3)提交再查看

[email protected] 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(+)
 
[email protected] 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
 
[email protected] MINGW64 /f/demo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (master)
$

(4)一次性提交所有文件方法

[email protected] MINGW64 /f/demo1 (master)
$ echo "5555" >> demo1.txt

[email protected] MINGW64 /f/demo1 (master)
$ echo "5555" >> demo2.txtc
[email protected] 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.

[email protected] 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(+)
 
[email protected] MINGW64 /f/demo1 (master)
$

四、撤销修改

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

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

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

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

第三种:如下 (关健字:git checkout -- 文件名)-- 很重要,如果没有 -- 的话,那么命令变成创建分支了 或者, git checkout .也可以

$ cat demo1.txt
test1
1111
2222
3333
5555
[email protected] MINGW64 /f/demo1 (master)
$ echo "6666" >> demo1.txt

[email protected] 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")

[email protected] MINGW64 /f/demo1 (master)
$ git checkout -- demo1.txt

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
test1
1111
2222
3333
5555

如果,已经add添加到暂存区的,那么撤销方法如下

[email protected] MINGW64 /f/demo1/demo_test (master)
$ echo "666" >> demo1.txt

[email protected] MINGW64 /f/demo1/demo_test (master)
$ cat demo1.txt
test1
1111
2222
3333
5555
666

[email protected]DMAPF13NB 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.

接着再如下图操作,

五、删除文件和恢复

(1)删除演示

$ git status
On branch master
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt  test1.txt

[email protected] MINGW64 /f/demo1 (master)
$ rm -rf test1.txt

[email protected] 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")

[email protected] MINGW64 /f/demo1 (master)
$ git add test1.txt

[email protected] 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
 
[email protected] MINGW64 /f/demo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (master)
$

(2)恢复演示

$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (master)
$ rm -rf demo2.txt

[email protected] 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")

[email protected] MINGW64 /f/demo1 (master)
$ git checkout -- demo2.txt
[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (master)
$

总结:

三种情况

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

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

git checkout .

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

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

第二步: git checkout -- 文件名(辙消工作区的修改)

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密钥对

[email protected] MINGW64 ~
$ cd

[email protected] MINGW64 ~
$ pwd
/c/Users/Administrator

[email protected] 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]-----+

[email protected] MINGW64 ~
$

3、在GitHub上创建项目

4、创建展示

5、完成展示

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

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

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

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

先说第一种连接方式:

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

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

第二种方式展示

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

本地测试

修改配置文件,进到版本库目录.git下,修改config文件
[email protected] MINGW64 ~/.ssh
$ cd f:

[email protected] MINGW64 /f
$ cd demo1/.git/

[email protected] MINGW64 /f/demo1/.git (GIT_DIR!)
$ ls
COMMIT_EDITMSG  description  hooks/  info/  objects/   refs/
config          HEAD         index   logs/  ORIG_HEAD
[email protected] MINGW64 /f/demo1/.git (GIT_DIR!)

$vim config

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

测试连接:

[email protected] MINGW64 /f/demo1 (master)
$ ssh -T [email protected]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.

[email protected] MINGW64 /f/demo1 (master)
$

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

现在我们再更改一个文件,然后再Push到github

先提交到本地

[email protected] MINGW64 /f/demo1 (master)
$ echo "888" >> demo1.txt

[email protected] 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.

[email protected] 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了

[email protected] MINGW64 /f/demo1 (master)
$ git remote add origin [email protected]:wsyht/demo_test.git
fatal: remote origin already exists.

[email protected] MINGW64 /f/demo1 (master)
$ git push -u origin master
fatal: remote error:
  wsyht/demo/demo_test is not a valid repository name
  Email [email protected] for help

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

[email protected] MINGW64 /f/demo1 (master)
$ git remote rm origin

[email protected] MINGW64 /f/demo1 (master)
$ git remote add origin [email protected]:wsyht/demo_test.git

[email protected] MINGW64 /f/demo1 (master)
$ git push -u origin master
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 [email protected]:wsyht/demo_test.git
   43185ac..07e6d4c  master -> master
Branch master set up to track remote branch master from origin.

成功了,哈哈哈。。。

再查看github

OK,已经提交上去了

现在我们再来看一下config这个配置文件

[email protected] 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 = [email protected]: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参数了

七、远程库克隆到本地

点击进去

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

本地操作:关健字: git clone

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

八、GitHub删除项目

九、分支的创建与合并

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

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

[email protected] MINGW64 /f/demo1/demo_test (master)
$ git checkout -b wsyht         #创建并切换分支
Switched to a new branch ‘wsyht‘

[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$ git branch    #查看当前所在分支
  master
* wsyht   

[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$

git branch wsyht   #创建分支

git checkout wsyht #切换分支

在wsyht分支上进行修改内容提交操作

[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$ echo "999" >> demo1.txt

[email protected] 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.
[email protected] 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(+)
 
[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.

[email protected] MINGW64 /f/demo1/demo_test (master)
$ cat demo1.txt
test1
1111
2222
3333
5555
777
888
[email protected] MINGW64 /f/demo1/demo_test (master)
$

发现在wsyht分支上修改提交,再切换回master分支,并不影响master分支文件的内容

现在合并分支

把Master分支和wsyht分支内容合并在一起,只留Master分支,再看Master分支的内容,关健字:git merge wsyht

[email protected] MINGW64 /f/demo1/demo_test (master)
$ git branch
* master
  wsyht
  
[email protected] MINGW64 /f/demo1/demo_test (master)
$ git merge wsyht
Updating 3020d4c..c4ff9ea
Fast-forward
 demo1.txt | 1 +
 1 file changed, 1 insertion(+)
 
[email protected] MINGW64 /f/demo1/demo_test (master)
$ cat demo1.txt
test1
1111
2222
3333
5555
777
888
999

[email protected] MINGW64 /f/demo1/demo_test (master)
$

[email protected] MINGW64 /f/demo1/demo_test (master)
$  git branch -d wsyht
Deleted branch hb (was 5939679).

可查看到,在wsyht分支上修改的内容,合并后,在Master分支上已显示出来

注意:在创建的临时分支上修改了文件内容后,一定要add,commit了然后再在主分区上进行合并操作,此时主分区不能进行合并,合并后,可以删除临时分支

命令总结

查看分支:git branch

创建分支:git branch 分支名字

切换分支: git checkout 分支名字

创建切换分支: git checkout -b 分支名字

合并某分支到当前分支: git merge 分支名字

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

十、分支合并冲突解决

$ git branch
* master

[email protected] MINGW64 /f/demo1/demo_test (master)
$ git checkout -b hb
Switched to a new branch ‘hb‘

[email protected] MINGW64 /f/demo1/demo_test (hb)
$ cat demo1.txt
111

[email protected] MINGW64 /f/demo1/demo_test (hb)
$ echo "333" >> demo1.txt

[email protected] 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.

[email protected] 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(+)
 
[email protected] 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)
  
[email protected] MINGW64 /f/demo1/demo_test (master)
$ cat demo1.txt
111

[email protected] MINGW64 /f/demo1/demo_test (master)
$ echo "222" >> demo1.txt

[email protected] 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.

[email protected] 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(+)
 
[email protected] 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. 

[email protected] MINGW64 /f/demo1/demo_test (master|MERGING)
$ cat demo1.txt
111
<<<<<<< HEAD
222   #master分支的内容
=======
333   #hb分支的内容
>>>>>>> hb   

Admi[email protected] MINGW64 /f/demo1/demo_test (master|MERGING)
$


#修改成主分支修改的内容,删了hb分支的内容再提交就正常了

[email protected] MINGW64 /f/demo1/demo_test (master|MERGING)
$ cat demo1.txt
111
222

[email protected] MINGW64 /f/demo1/demo_test (master|MERGING)
$ git add demo1.txt
[email protected] MINGW64 /f/demo1/demo_test (master|MERGING)

$ git commit -m‘new‘
[master b2a4ccb] new
[email protected] MINGW64 /f/demo1/demo_test (master)

$ cat demo1.txt
111
222

十一、查看历史合并功能

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

[email protected] MINGW64 /f/demo1/demo_test (master)
$ git checkout -b wsyht
Switched to a new branch ‘wsyht‘

[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$ cat demo1.txt
111
222

[email protected] MINGW64 /f/demo1/demo_test (wsyht)
$ echo "333" >> demo1.txt

[email protected] 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

[email protected] 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(+)
[email protected] 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)
[email protected] 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(+)
 
[email protected] 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提交

[email protected] MINGW64 /f/demo1/demo_test (master)
$

十一、分支的注意事项

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

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

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (master)
$ git checkout -b wsyht
Switched to a new branch ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (wsyht)
$ sed -i ‘1s/1111/wsyht/g‘ demo1.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
888

[email protected] 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")

[email protected] MINGW64 /f/demo1 (wsyht)
$

[email protected] 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)
  
[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt
c

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
888

[email protected] 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.

[email protected] 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(-)
 
[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt
c

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (master)
$

[email protected] MINGW64 /f/demo1 (master)
$ git checkout wsyht
Switched to branch ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

十二、双分支操作

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

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

第一步:

#第一个wsyht分支操作

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

[email protected] MINGW64 /f/demo1 (master)
$ git branch
* master

[email protected] MINGW64 /f/demo1 (master)
$ git checkout -b wsyht
Switched to a new branch ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (wsyht)
$ sed -i ‘1s/1111/wsyht/g‘ demo1.txt
[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
888

[email protected] 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")

[email protected] 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

[email protected] MINGW64 /f/demo1 (wsyht)
$ git status     #再次查看状态发现什么都没有
On branch wsyht
nothing to commit, working tree clean

第二步:

第二个bug分支操作

bug分支修改->提交-合并-删除bug分支

[email protected] 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)
  
[email protected] MINGW64 /f/demo1 (master)
$ git checkout -b bug
Switched to a new branch ‘bug‘

[email protected] MINGW64 /f/demo1 (bug)
$ git status
On branch bug
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (bug)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (bug)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (bug)
$ sed -i ‘s/888/bbb/g‘ demo1.txt

[email protected] MINGW64 /f/demo1 (bug)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
bbb

[email protected] 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")

[email protected] 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.

[email protected] 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(-)
 
[email protected] 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)

[email protected] 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(-)
 
[email protected] MINGW64 /f/demo1 (master)
$ git branch -d bug
Deleted branch bug (was ed1eded).

[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt
c
[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
bbb

第三步:

#回到第一个分支wsyht操作

合并master分支->调回wsyht修改状态->add提交

合并master分支主要就是先把刚刚修改的bug先合并了,然后再调回刚才修改的状态,继续开发提交

[email protected] MINGW64 /f/demo1 (master)
$ git checkout wsyht
Switched to branch ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt
c
[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
888

[email protected] MINGW64 /f/demo1 (wsyht)
$ git status
On branch wsyht
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (wsyht)
$ git merge master
Updating 0631e15..cd961d2
Fast-forward
 demo1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 
[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
bbb

[email protected] 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/[email protected]{0} (d1ba7adfb82639ae0ecf1f946349dc064fb30617)
[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
bbb

[email protected] MINGW64 /f/demo1 (wsyht)
$ git add demo1.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ git status
On branch wsyht
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        modified:   demo1.txt
        
[email protected] MINGW64 /f/demo1 (wsyht)
$ git commit -m‘commit wsyht‘
[wsyht 9d505e6] commit wsyht
 1 file changed, 1 insertion(+), 1 deletion(-)
 
[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ git status
On branch wsyht
nothing to commit, working tree clean

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
bbb

[email protected] 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提交
[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
bbb

第四步

#主分支Master操作

切回主分支->合并wsyht分支->add提交->删除wsyht分支

[email protected] 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)

  
[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
1111
1111
2222
3333
5555
777
bbb

[email protected] MINGW64 /f/demo1 (master)
$ git merge wsyht
Updating cd961d2..9d505e6
Fast-forward
 demo1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 
[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
bbb

[email protected] 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内容也删了

十四、强制删除分支

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

如下:

[email protected] MINGW64 /f/demo1 (master)
$ git branch
* master

[email protected] MINGW64 /f/demo1 (master)
$ git checkout -b wsyht
Switched to a new branch ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ rm -rf demo1.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo2.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ git add demo1.txt
[email protected] MINGW64 /f/demo1 (wsyht)
$ git commit -m‘delete demo1‘
[wsyht c83d5a1] delete demo1
 1 file changed, 7 deletions(-)
 delete mode 100644 demo1.txt
 
[email protected] 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)
[email protected] 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强制删除

[email protected] MINGW64 /f/demo1 (master)
$ git branch -D wsyht   #用大D强制删除
Deleted branch wsyht (was c83d5a1).

[email protected] MINGW64 /f/demo1 (master)
$ git branch
* master
[email protected] MINGW64 /f/demo1 (master)
$


十五、多人协作

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

#查看远程库

[email protected] MINGW64 /f/demo1 (master)
$ git remote   #查看远程库的信息
origin

[email protected] MINGW64 /f/demo1 (master)
$ git remote -v    #查看远程库的详信息
origin  [email protected]:wsyht/demo_test.git (fetch)
origin  [email protected]:wsyht/demo_test.git (push)

[email protected] MINGW64 /f/demo1 (master)
$

上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址

#推送分支

推送分支就是把本地的分支提交到对应的远程库的分支中,如果是Master分支就提交到远程库的Master分支,如果是其它分支如wsyht,就提交到远程库的wsyht分支中与之相对应,如果一些bug分支不需要提交的,就把他先主分支先合并了,然后再提交到远程库中

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
wsyht
1111
2222
3333
5555
777
bbb

[email protected] MINGW64 /f/demo1 (master)
$ echo "test" > demo1.txt

[email protected] MINGW64 /f/demo1 (master)
$ cat demo1.txt
test

[email protected] 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.
[email protected] 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(-)
 
[email protected] 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
 
[email protected] MINGW64 /f/demo1 (master)
$ ls
demo1.txt  oo.txt

[email protected] 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 [email protected]: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服务器

我的电脑操作,如下:

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

[email protected] MINGW64 /f/demo1 (master)
$ git branch
* master

[email protected] MINGW64 /f/demo1 (master)
$ git branch wsyht

[email protected] MINGW64 /f/demo1 (master)
$ git branch
* master
  wsyht

[email protected] MINGW64 /f/demo1 (master)
$ git push origin wsyht
Everything up-to-date

张三电脑操作

操作过程:取远程库的分支到本地->创远程分支到本地->修改提交->push

[email protected] MINGW64 /f
$ mkdir demo2

[email protected] MINGW64 /f
$ cd demo2

[email protected] MINGW64 /f/demo2
$ git init
Initialized empty Git repository in F:/demo2/.git/

[email protected] MINGW64 /f/demo2 (master)
$ git remote add origin [email protected]:wsyht/demo_test.git

[email protected] MINGW64 /f/demo2 (master)
$ 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

[email protected] MINGW64 /f/demo2 (master)
$ ls

[email protected] MINGW64 /f/demo2 (master)
$ git branch wsyht origin/wsyht    #创建远程库的分支到本地
Branch wsyht set up to track remote branch wsyht from origin.

[email protected] MINGW64 /f/demo2 (master)
$ ls

[email protected] MINGW64 /f/demo2 (master)
$ git checkout wsyht  #切换到创建的分支
Switched to branch ‘wsyht‘
Your branch is up-to-date with ‘origin/wsyht‘.
l
[email protected] MINGW64 /f/demo2 (wsyht)
$ ls   #可查看到该分支的文件了
demo1.txt  oo.txt

$ cat demo1.txt
test
222
333

[email protected] MINGW64 /f/demo2 (wsyht)
$ echo "111" >> demo1.txt

[email protected] MINGW64 /f/demo2 (wsyht)
$ cat demo1.txt
test
222
333
111

[email protected] 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.

[email protected] 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(+)

[email protected] 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 [email protected]:wsyht/demo_test.git
   924fcc5..e4ace0b  wsyht -> wsyht

我的电脑操作

操作过程:切换分支->修改文件->提交->push->解决冲突->再push

[email protected] MINGW64 /f/demo1 (wsyht)
$ pwd
/f/demo1

[email protected] MINGW64 /f/demo1 (wsyht)
$ git checkout wsyht
Already on ‘wsyht‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
test
222
333

[email protected] MINGW64 /f/demo1 (wsyht)
$ echo ‘111‘ >> demo1.txt

[email protected] 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.

[email protected] 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(+)

[email protected] MINGW64 /f/demo1 (wsyht)
$ git push origin wsyht   #这里push失败是因为冲突了,提示叫你git pull
To [email protected]:wsyht/demo_test.git
 ! [rejected]        wsyht -> wsyht (fetch first)
error: failed to push some refs to ‘[email protected]: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.

[email protected] 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

[email protected] MINGW64 /f/demo1 (wsyht)
$ git branch --set-upstream-to=origin/wsyht
Branch wsyht set up to track remote branch wsyht from origin.

[email protected] MINGW64 /f/demo1 (wsyht)
$ git pull   #执行完上面那一步,终于可以pull了,输出的提示合并也没有问题,如果合并有问题,就修改刚刚修改的文件,然后再push
Merge made by the ‘recursive‘ strategy.

[email protected] MINGW64 /f/demo1 (wsyht)
$ git pull  
Already up-to-date.

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat demo1.txt
test
222
333
111

[email protected] 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 [email protected]:wsyht/demo_test.git
   e4ace0b..ca2b12a  wsyht -> wsyht

现在我们看看GitHub上有没有push成功了

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

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

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

4、再次push就成功了

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

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

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

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

[email protected] MINGW64 /f/demo3
$ git clone [email protected]: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. 

[email protected] MINGW64 /f/demo3
$ ls
demo_test/
cd
[email protected] MINGW64 /f/demo3
$ cd demo_test/

[email protected] MINGW64 /f/demo3/demo_test (master)
$ ls
demo1.txt  oo.txt

[email protected] MINGW64 /f/demo3/demo_test (master)
$ cat oo.txt
111

[email protected] MINGW64 /f/demo3/demo_test (master)
$ echo "222" >>oo.txt

[email protected] 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")

[email protected] 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.

[email protected] 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

[email protected] 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(+)

[email protected] MINGW64 /f/demo3/demo_test (master)
$ ls
demo1.txt  oo.txt

[email protected] MINGW64 /f/demo3/demo_test (master)
$ cat oo.txt
111
222

[email protected] 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 [email protected]: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是张三的工作区目录

[email protected] MINGW64 /f/demo2 (wsyht)
$ echo "333" >> oo.txt

[email protected] 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.

[email protected] 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(+)
 
 [email protected] 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 [email protected]:wsyht/demo_test.git
   c3f4ac0..af8dc1c  wsyht -> wsyht

我的操作: 关健字: git pull #更新本地工作区

记得修改文件之前先git pull一下,再修改和push

[email protected] MINGW64 /f/demo2 (wsyht)
$ cd ../demo1

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  oo.txt

[email protected] MINGW64 /f/demo1 (wsyht)
$ ls
demo1.txt  oo.txt

[email protected] 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(+)

[email protected] MINGW64 /f/demo1 (wsyht)
$ cat oo.txt  #查看已经看到333了
111
222
333

十九、CentOS下搭建Git服务器

#服务器端部署

[[email protected] local]# ifconfig |grep  "inet addr:" | grep Bcast | awk -F":" ‘{print $2}‘ | awk -F‘ ‘ ‘{print $1}‘
192.168.1.102
[[email protected] ~]# yum -y install git
[[email protected] ~]# useradd -s /sbin/nologin git
[[email protected] ~]# git init --bare /usr/local/wsyht.git
[[email protected] ~]# ls /usr/local/wsyht.git/
branches  description  hooks  objects
config    HEAD         info   refs
[[email protected] ~]# chown -R git:git /usr/local/wsyht.git
[[email protected] ~]# cd /home/git/
[[email protected] git]# mkdir 600 .ssh
[[email protected] git]# cd .ssh
[[email protected] .ssh]# echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6y9vxDIo9rqCAW7xebYpFFIL5M0wvwfe/qbcehE5VRvcVvKnlH/TsEygxGJEMCgjfLW7sKo5Me3W5KJNfNFeWQcn6AMSm08ZQk9qCbP1DpyW+AWvnz2KORYzM0+yLSbjd8py6XpWaMO1zX4TFm6VH3IRfaIpe0G/DhIzMxfNnNdCcXw6ou1Bb6tI41Q78ovGZGyPum4Nt03vLQpe0d6p5bqk21h1VeDbnH2mKa5GbS+OLgK+1qMvUy5+q8deASn18gepxhNtk+7LqNWlhTN8hQBFHOsqMuLvb5IGqBTRy/HfEFPiEkmgy9YrSWHiaPqJ/rCIYufaIKEgyAlISIqPj wsyht" >> authorized_keys

解释:上述的--bare意思是只会创建一个裸仓库,而没有工作区,因为服务器上的Git仓库纯碎是为了共享,所以不让用户直接到服务器上去改工作区,并且服务器上的仓库通常以.git结尾,所以我上面起了一个wsyht.git的仓库名字;接着,为了安全,我们得禁止git用户登陆,所以加了/sbin/nologin参数值,而且他也不需要登陆,再把仓库所有者和所属组改成git用户即可,到下面我们就可以在客户端上进行clone,push等之类的操作,前提是要先配好key

#客户端操作

9

$ git clone [email protected]:/srv/sample.git

【拓展知识】

一、别名配置

法1:真接命令行配置

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

[email protected] MINGW64 /f/demo2 (wsyht)
$ git config --global alias.st status   #把status 配置成别名 st

[email protected] MINGW64 /f/demo2 (wsyht)
$ git config --global alias.co checkout #把checkout 配置成别名 co

[email protected] MINGW64 /f/demo2 (wsyht)
$ git config --global alias.ci commit   #把commit 配置成别名 ci

[email protected] MINGW64 /f/demo2 (wsyht)
$ git config --global alias.br branch   #把branch 配置成别名 br

[email protected] MINGW64 /f/demo1 (wsyht)
$ git config --global alias.unstage ‘reset HEAD‘

[email protected] MINGW64 /f/demo1 (wsyht)
$ git config --global alias.last ‘log -1‘

[email protected] MINGW64 /f/demo2 (wsyht)
$ git status   #用原来的命令查看状态
On branch wsyht
Your branch is up-to-date with ‘origin/wsyht‘.
nothing to commit, working tree clean

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

第一个配置文件配置

[email protected] MINGW64 /f/demo1 (wsyht)
$ pwd
/f/demo1

[email protected] MINGW64 /f/demo1 (wsyht)
$ cd

[email protected] MINGW64 ~
$ ls -a .gitconfig
.gitconfig

[email protected] MINGW64 ~
$ cat .gitconfig
[merge]
        tool = vimdiff
[user]
        name = wsyht
        email = [email protected]
[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

[email protected] MINGW64 ~

可以看到.gitconfig文件写入了我在命令行设置别名的命令,如果你加--global就会写入这个配置文件,你也可以直接进到这个文件添加,而不在终端执行命令,要删除别名的话进到这个文件把相应的别名命令删除即可,这个文件是针对当前用户生效的

第二个配置文件

在仓库目录下.git/config目录下,此配置文件,是针对当前仓库有效,所以在当你在终端执行命令的时候不加--global就会把命令写入这个文件,你也可以进到这个文件进行添加删除别名命令,下面我们来看配置文件内容

[email protected] MINGW64 /f/demo1/.git (GIT_DIR!)
$ pwd
/f/demo1/.git

[email protected] 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 = [email protected]: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)创建标签

[email protected] MINGW64 /f/demo1 (wsyht)
$ git branch
  master
* wsyht

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag v1.0  #创建标签v1.0

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag
v1.0

[email protected] 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提交

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag v1.1 805dcba  #通过指定他的号码来给他创建标签
 
[email protected] MINGW64 /f/demo1 (wsyht)
$ git show v1.1
commit 805dcba19f99a6057fc1eabdd75acb7f386d29ed
Author: wsyht <[email protected]>
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

[email protected] MINGW64 /f/demo1 (wsyht)
$ git show v1.0
commit ca2b12a4cdd0e496bb0dec43c45e2b50336982d0
Merge: 805dcba e4ace0b
Author: wsyht <[email protected]>
Date:   Sat Jul 30 22:28:58 2016 +0800

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

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag -a v0.3 -m "我的第三个标签"

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag -a v0.3 -m "我的第三个标签" e4ace0b
fatal: tag ‘v0.3‘ already exists

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag -a v0.4 -m "我的第三个标签" e4ace0b  #-a 指定标签号,-m 指定描述的内容

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag
v0.3
v0.4
v1.0
v1.1

[email protected] MINGW64 /f/demo1 (wsyht)
$ git show v0.4
tag v0.4
Tagger: wsyht <[email protected]>
Date:   Sat Jul 30 22:53:38 2016 +0800

我的第三个标签

commit e4ace0bfa22f9e19913f95282fa5176a0b708be3
Author: wsyht <[email protected]>
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

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag  #查看所有标签
v0.3
v0.4
v1.0
v1.1

2)删除和push标签

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag -d v0.3
Deleted tag ‘v0.3‘ (was 784bb96)

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag
v0.4
v1.0
v1.1

[email protected] 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 [email protected]:wsyht/demo_test.git
 * [new tag]         v0.4 -> v0.4
 
 [email protected] MINGW64 /f/demo1 (wsyht)
$ git push origin  --tags   #提交所有标签
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:wsyht/demo_test.git
 * [new tag]         v1.0 -> v1.0
 * [new tag]         v1.1 -> v1.1

在GitHub上查看

标签都出来了,push成功

3)删除远程库的标签

[email protected] MINGW64 /f/demo1 (wsyht)
$ git tag -d v0.4
Deleted tag ‘v0.4‘ (was dea0b58)

[email protected] MINGW64 /f/demo1 (wsyht)
$ git push origin :refs/tags/v0.4
To [email protected]:wsyht/demo_test.git
 - [deleted]         v0.4

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

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

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

时间: 2024-08-03 02:20:56

Git从入门到熟悉的相关文章

Android 深度探索(卷1)HAL 与驱动开发 第三章 GIT 使用入门 心得体会

Android 深度探索(卷1)HAL 与驱动开发 第三章 GIT 使用入门 心得体会 本章主要介绍GIT的学习,以及介绍GIT用于获取诸多开源项目的源代码. 在使用GIT之前我们首先对其安装,其安装命令: #apt-get  install git #apt-get  install git-doc git-svn git-gui gitk 在Linux 下可以直接使用man命令查看指定命令的帮助文档.这对我们初学者提供了很大的帮助. Git的功能很复杂,为此这章节举例为我们演示如何创建版本库

Git使用入门读书心得

Git使用入门,包含了安装Git.查看Git文档.源代码和提交与获取这三个部分. Git软件包包含了大部分的Git命令,所以必须安装. 在Linux下可以直接使用man的命令直接查看指定命令的帮助文档.安装git-doe 后会安装git的文本格式和HTML格式的文档,所有的文档都存在/usr/share/doe/git-doc目录中. Git的功能很复杂,包括创建版本库.提交源代码.创建分支.向远程服务器上传源代码,从远程服务器获取源代码等技术.创建版本号:任何版本管理员都必须有一个版本库,所不

Android深度探索(卷1)HAL与驱动开发 第三章 Git使用入门 读书笔记

Android深度探索(卷1)HAL与驱动开发 第三章 Git使用入门 读书笔记     本章是对Git的一个概述. Linux是一个开源的系统.事实上,在Linux上许多软件都和底层程序以及内核驱动有关,然而Linux内核的版本非常多,如果每个版本上的软件都采用安装包的形式,则匹配这么多版本的安装包将十分庞大.所以有很多软件不是以二进制安装包的形式来安装和使用的.而是将源代码下载下来,并在每个用户自己的Linux中编译并安装,即使用make 和make install 命令.而Linus作为L

第三章 Git 使用入门

本章主要介绍的是Git使用入门,通过Git来进行源代码的管理,在使用Git之前需要安装Git,在很多的Linux系统中自带Git,也可以通过使用命令来安装Git,然后就是查看Git文档,在Linux下可以使用man命令查看制定命令的帮助文档.接着就是源代码的提交和获取,在本节中通过完整的例子来演示然后创建版本库.提交源代码.创建分支.像远程服务器上传源代码和从远程服务器获取代码.创建版本库需要使用git init,将文件提交到本地版本库使用git commit,创建本地分支使用git branc

Git的入门教程&lt;四&gt;

Git 的入门教程<四> 5> git分支的管理 git默认的有一个主分支叫做master,随着每次的提交,master主分支会形成一条线,而HEAD是指向当前的主分支master的,一般来说,我们将master分支作为向外发布的主分支,而开发的时候会新建一个分支或者好多分支,作为开发分支,等到开发分支完成,在一次性的想master主分支上合并,并进行发布. 5.1 git 分支的创建.删除.切换 git branch dev //创建分支 git checkout dev //切换分支

九个问题从入门到熟悉HTTPS

九个问题从入门到熟悉HTTPS Q1: 什么是 HTTPS? BS: HTTPS 是安全的 HTTP HTTP 协议中的内容都是明文传输,HTTPS 的目的是将这些内容加密,确保信息传输安全.最后一个字母 S 指的是 SSL/TLS 协议,它位于 HTTP 协议与 TCP/IP 协议中间. Q2: 你说的信息传输安全是什么意思 BS: 信息传输的安全有三个方面: 客户端和服务器直接的通信只有自己能看懂,即使第三方拿到数据也看不懂这些信息的真实含义. 第三方虽然看不懂数据,但可以 XJB 改,因此

第三章 Git使用入门 心得体会

第三章 Git使用入门 心得体会     通过学习Android深度探索(卷1)HAL与驱动开发的第三章Git使用入门,首先我对Git有了一定了解git是一个版本控制系统.官方的解释是:版本控制(Revision control)是一种软件工程技巧.其次我掌握了Git的使用流程. 第一步:新建分支 首先,每次开发新功能,都应该新建一个单独的分支 # 获取主干最新代码 $ git checkout master $ git pull # 新建一个开发分支myfeature $ git checko

Android系统移植与驱动开发--第三章 Git使用入门及在学习中有感

第三章 Git使用入门 使用Git的目的是减少各种版本的Linux的压缩大小,提供源代码在Linux上进行编译. 在这一个章节中,其实就是关键步骤的操作,虽然Git与我们学习的android没有很大的联系,但是在开发环境中也是必不可少的.通过学习这个章节,学习到了安装,查看,提取Git的方法.下面将详细讲述. 一.安装Git 可能大多数Linux上已经安装了Git,如果没有可以用下面的命令. Ubuntu10.04版本以下的有-core与原来的区分, 必须的:apt-get install gi

Git的入门教程&lt;二&gt;

Git 的入门教程<二> 4> git远程仓库的使用 我们在使用git进行代码管理的时候,协同办公,需要一个24小时不间断的隐形同事,此时,如果在局域网内,则直接开一台局域网内的24小时开机运行的机器就可以,但是如果在homework的时候就需要这个24小时运行的机器拥有独立的外网IP,这样消耗就比较大,幸好,网上有一个github,提供给我们一个免费的仓库,让我们可以在同事之前推送代码,下面介绍github的使用. 4.1 github的使用 首先 登录github的官网: https