您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
   
 
     
   
 订阅
  捐助
Git与GitHub的基本使用
 
  1897  次浏览      15
 2018-1-10 
 
编辑推荐:
本文来自尹正杰博客,本文通过创建版本库以及配置文件,详细的介绍了Git与GitHub的基本使用。

一.Git的基本使用

1.版本库创建

a>.什么是版本库呢

版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。现在我们来创建一个git项目(创建一个版本库)

1 [root@yinzhengjie ~]# mkdir ProjcetDir #创建项目
2 [root@yinzhengjie ~]# cd ProjcetDir/
3 [root@yinzhengjie ProjcetDir]# ls -a
4 . ..
5 [root@yinzhengjie ProjcetDir]#
6 [root@yinzhengjie ProjcetDir]# git init #初始化git项目
7 Initialized empty Git repository in /root/ProjcetDir/.git/
8 [root@yinzhengjie ProjcetDir]#
9 [root@yinzhengjie ProjcetDir]# ls -a
10 . .. .git
11 [root@yinzhengjie ProjcetDir]# ls .git/ #用户git的配置文件,勿动!
12 branches config description HEAD hooks info objects refs
13 [root@yinzhengjie ProjcetDir]#

b.把文件添加到版本库

1 [root@yinzhengjie ProjcetDir]# touch ReadMe
2 [root@yinzhengjie ProjcetDir]# mkdir bin conf log
3 [root@yinzhengjie ProjcetDir]# touch conf/{1.5}.conf
4 [root@yinzhengjie ProjcetDir]# git status #查看当前目录的状态,可以查出将哪些文件提交给git进行管理。
5 # On branch master
6 #
7 # Initial commit
8 #
9 # Untracked files: 这里告诉咱们以下有2个文件没有被跟踪。
10 # (use "git add <file>..." to include in what will be committed)
11 #
12 # ReadMe
13 # conf/
14 nothing added to commit but untracked files present (use "git add" to track)
15 [root@yinzhengjie ProjcetDir]#
16 [root@yinzhengjie ProjcetDir]# git add ReadMe #把单个文件提交到stage区域
17 [root@yinzhengjie ProjcetDir]# git status
18 # On branch master
19 #
20 # Initial commit
21 #
22 # Changes to be committed: 这里告诉咱们已经把文件下面的文件给跟踪了
23 # (use "git rm --cached <file>..." to unstage)
24 #
25 # new file: ReadMe
26 #
27 # Untracked files:
28 # (use "git add <file>..." to include in what will be committed)
29 #
30 # conf/
31 [root@yinzhengjie ProjcetDir]#
32 [root@yinzhengjie ProjcetDir]# git add . #把当前目录下的所有文件都提交
33 [root@yinzhengjie ProjcetDir]# git status
34 # On branch master
35 nothing to commit (working directory clean)
36 [root@yinzhengjie ProjcetDir]#
37 [root@yinzhengjie ProjcetDir]# git commit -m "first commit by yinzhengjie" #将文件从stage区提交仓库
38 [master (root-commit) e9ce735] first commit by yinzhengjie
39 Committer: root <root@yinzhengjie.(none)>
40 Your name and email address were configured automatically based
41 on your username and hostname. Please check that they are accurate.
42 You can suppress this message by setting them explicitly:
43
44 git config --global user.name "Your Name" #第一次提交的时候,这个会告诉咱们需要提交一下自己的用户信息,这里自己安装这个格式提交一下即可。继续往下看,我会给出案例。
45 git config --global user.email you@example.com
46
47 If the identity used for this commit is wrong, you can fix it with:
48
49 git commit --amend --author='Your Name <you@example.com>'
50
51 0 files changed, 0 insertions(+), 0 deletions(-)
52 create mode 100644 ReadMe
53 create mode 100644 conf/{1.5}.conf
54 [root@yinzhengjie ProjcetDir]#

2.GIT回滚

a.进行多次提交代码到仓库(目的是创建多个版本库)

[root@yinzhengjie ProjcetDir]# git config --global user.name "yinzhengjie" #提交git用户信息,因为心细的小伙伴会发现第一次提交的时候会有提示信息.
[root@yinzhengjie ProjcetDir]# git config --global user.email "y1053419035@qq.com" #提交邮箱
[root@yinzhengjie ProjcetDir]# git config --global color.ui true #语法高亮
[root@yinzhengjie ProjcetDir]# git config --list #查看配置信息
user.name=yinzhengjie
user.email=y1053419035@qq.com
color.ui=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# echo 3333333333 > ReadMe #修改已经提交到仓库的文件
[root@yinzhengjie ProjcetDir]# touch bin/{1..3}.go
[root@yinzhengjie ProjcetDir]# git add *
[root@yinzhengjie ProjcetDir]# git init .
Reinitialized existing Git repository in /root/ProjcetDir/.git/
[root@yinzhengjie ProjcetDir]# git commit -m "second commit by yinzhengjie" #第二次进行提交,注意,-m表示在提交时的备注信息,也可以理解是对当前版本的一个说明方便用户自己记忆
[master 39fcee5] second commit by yinzhengjie
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 bin/1.go
create mode 100644 bin/2.go
create mode 100644 bin/3.go
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# echo "yinzhengjie is good boy" > ReadMe #修改ReadMe文件
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# echo "test commit" >> ReadMe
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# more ReadMe
yinzhengjie is good boy
test commit
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git add *
[root@yinzhengjie ProjcetDir]# git init .
Reinitialized existing Git repository in /root/ProjcetDir/.git/
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git commit -m "third commit by yinzhengjie"
[master d12c571] third commit by yinzhengjie
1 files changed, 2 insertions(+), 1 deletions(-)
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git log #查看提交日志
commit d12c5714286a81f343ed68fe82406ab72785ea15 #很显然,这是一个md5值,它用来标识当前版本的唯一值,方便用户回滚到该版本。
Author: yinzhengjie <y1053419035@qq.com>
Date: Sat Oct 14 19:22:40 2017 +0800

third commit by yinzhengjie

commit 39fcee5c7aef367ff2092cfe70715857c36fa21d
Author: yinzhengjie <y1053419035@qq.com>
Date: Sat Oct 14 19:13:42 2017 +0800

second commit by yinzhengjie

commit e9ce7354d385ca3c454bec7fa885c47fc4a768ed
Author: root <root@yinzhengjie.(none)>
Date: Sat Oct 14 18:34:34 2017 +0800

first commit by yinzhengjie
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git log --pretty=oneline #简化输出提交日志记录。
d12c5714286a81f343ed68fe82406ab72785ea15 third commit by yinzhengjie
39fcee5c7aef367ff2092cfe70715857c36fa21d second commit by yinzhengjie
e9ce7354d385ca3c454bec7fa885c47fc4a768ed first commit by yinzhengjie
[root@yinzhengjie ProjcetDir]#

b.回滚代码到上一个版本

1 [root@yinzhengjie ProjcetDir]# git log --pretty=oneline #查看当前所拥有的版本,很显然共计3个
2 d12c5714286a81f343ed68fe82406ab72785ea15 third commit by yinzhengjie
3 39fcee5c7aef367ff2092cfe70715857c36fa21d second commit by yinzhengjie
4 e9ce7354d385ca3c454bec7fa885c47fc4a768ed first commit by yinzhengjie
5 [root@yinzhengjie ProjcetDir]#
6 [root@yinzhengjie ProjcetDir]# more ReadMe #查看当前版本的某个文件信息
7 yinzhengjie is good boy
8 test commit
9 [root@yinzhengjie ProjcetDir]#
10 [root@yinzhengjie ProjcetDir]# git reset --hard HEAD^ #回滚到上一个版本信息
11 HEAD is now at 39fcee5 second commit by yinzhengjie
12 [root@yinzhengjie ProjcetDir]#
13 [root@yinzhengjie ProjcetDir]# git log --pretty=oneline
14 39fcee5c7aef367ff2092cfe70715857c36fa21d second commit by yinzhengjie
15 e9ce7354d385ca3c454bec7fa885c47fc4a768ed first commit by yinzhengjie
16 [root@yinzhengjie ProjcetDir]#
17 [root@yinzhengjie ProjcetDir]# more ReadMe #再次查看,发现里面的内容已经回滚到上个版本了
18 3333333333
19 [root@yinzhengjie ProjcetDir]#

c.回滚到指定版本

1 [root@yinzhengjie ProjcetDir]# more ReadMe
2 3333333333
3 [root@yinzhengjie ProjcetDir]#
4 [root@yinzhengjie ProjcetDir]# git log
5 commit 39fcee5c7aef367ff2092cfe70715857c36fa21d
6 Author: yinzhengjie <y1053419035@qq.com>
7 Date: Sat Oct 14 19:13:42 2017 +0800
8
9 second commit by yinzhengjie
10
11 commit e9ce7354d385ca3c454bec7fa885c47fc4a768ed
12 Author: root <root@yinzhengjie.(none)>
13 Date: Sat Oct 14 18:34:34 2017 +0800
14
15 first commit by yinzhengjie
16 [root@yinzhengjie ProjcetDir]#
17 [root@yinzhengjie ProjcetDir]# git reset --hard e9ce7354d385ca3c454bec7fa885c47fc4a768ed #回滚到指定版本
18 [root@yinzhengjie ProjcetDir]# more ReadMe
19 [root@yinzhengjie ProjcetDir]# git log
20 commit e9ce7354d385ca3c454bec7fa885c47fc4a768ed
21 Author: root <root@yinzhengjie.(none)>
22 Date: Sat Oct 14 18:34:34 2017 +0800
23
24 first commit by yinzhengjie
25 [root@yinzhengjie ProjcetDir]#
26 [root@yinzhengjie ProjcetDir]# echo "I'm walking again" >> ReadMe
27 [root@yinzhengjie ProjcetDir]# git add .
28 [root@yinzhengjie ProjcetDir]# git commit -m "我又来溜达了"
29 [master a4c0670] 我又来溜达了
30 1 files changed, 1 insertions(+), 0 deletions(-)
31 [root@yinzhengjie ProjcetDir]# more ReadMe
32 I'm walking again
33 [root@yinzhengjie ProjcetDir]#
34 [root@yinzhengjie ProjcetDir]# git log
35 commit a4c067038c901eb6e5cfc7052984fd389cf06d59
36 Author: yinzhengjie <y1053419035@qq.com>
37 Date: Sat Oct 14 19:55:48 2017 +0800
38
39 我又来溜达了
40
41 commit e9ce7354d385ca3c454bec7fa885c47fc4a768ed
42 Author: root <root@yinzhengjie.(none)>
43 Date: Sat Oct 14 18:34:34 2017 +0800
44
45 first commit by yinzhengjie
46 [root@yinzhengjie ProjcetDir]#
47 [root@yinzhengjie ProjcetDir]# git reflog #表示查看所有的日志,包括你回滚的记录的日志
48 a4c0670 HEAD@{0}: commit: 我又来溜达了
49 e9ce735 HEAD@{1}: e9ce7354d385ca3c454bec7fa885c47fc4a768ed: updating HEAD
50 39fcee5 HEAD@{2}: HEAD^: updating HEAD
51 d12c571 HEAD@{3}: commit: third commit by yinzhengjie
52 39fcee5 HEAD@{4}: commit: second commit by yinzhengjie
53 [root@yinzhengjie ProjcetDir]#
54 [root@yinzhengjie ProjcetDir]# git reset --hard 39fcee5 #回滚到指定版本,通过reflog的信息。
55 HEAD is now at 39fcee5 second commit by yinzhengjie
56 [root@yinzhengjie ProjcetDir]#
57 [root@yinzhengjie ProjcetDir]# git log
58 commit 39fcee5c7aef367ff2092cfe70715857c36fa21d
59 Author: yinzhengjie <y1053419035@qq.com>
60 Date: Sat Oct 14 19:13:42 2017 +0800
61
62 second commit by yinzhengjie
63
64 commit e9ce7354d385ca3c454bec7fa885c47fc4a768ed
65 Author: root <root@yinzhengjie.(none)>
66 Date: Sat Oct 14 18:34:34 2017 +0800
67
68 first commit by yinzhengjie
69 [root@yinzhengjie ProjcetDir]#

3.撤销修改

a.工作区和暂存区

在说撤销修改的操作之前,我个人觉得有必要了解一下原理,我们上面的用到的命令git init,git add 和git commit到底是咋回事,现在我们来进行说明。git的工作机制还是蛮人性化的,为什么要这么说呢?别着急,我们一一揭晓答案。Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。

1 先来看名词解释:
2
3工作目录:
4
5 就是你电脑能任意一个目录都可以称作为工作目录。
6
7 版本库:
8
9 还记得我们上面用的“git init”,没错,就是初始化项目,一旦执行该命令,就会在该工作目录下生成一个隐藏目录“.git”,我们不能称之为它是工作目录,因为我们写代码也不用去编辑它,它就是GIt的版本库。Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。暂存区我下面会通过2张图来进行解释。
10
11   

b.将工作去的代码提交至版本库的过程

我们把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区:

        

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支(注意,一旦执行了这一步操作,就会永久的被git数据库记住,即使你进行回滚操作这个记录也是会存在的,所以说,这一步要慎重操作啊!如果你提交了什么不该提交的数据让你的技术总监发现,它是可以通过命令查看你提交的内容哟~)

c.撤销修改两种姿势

我们刚刚已经了解到,当代码从暂存区提交到版本库时数据将被永久的记录下来。即使你可以回滚到上一个版本,但是照样也可以用同样的方式回滚到你所提交的某个状态的版本库中去。所以在提交的时候,你要特别注意提交的内容,最好配合“git status”命令进行查看,一旦你把数据提交到暂存区,只要执行[git commit -m “本次版本的描述”]就会把数据永久的写入到版本库中去。那么当我们及时发现暂存区有的文件内容我们不想要提交该咋搞呢?我们有两种方法:

姿势一:

1 [root@yinzhengjie ProjcetDir]# git status
2 # On branch master
3 # Changed but not updated: #这行提示可以明显知道是在工作目录
4 # (use "git add <file>..." to update what will be committed)
5 # (use "git checkout -- <file>..." to discard changes in working directory)
6 #
7 # modified: ReadMe #这是告诉咱们该工作区域的代码和版本库之前的区别,该文件以及被修改了
8 #
9 no changes added to commit (use "git add" and/or "git commit -a")
10 [root@yinzhengjie ProjcetDir]#
11 [root@yinzhengjie ProjcetDir]# git add ReadMe #把代码从工作目录提交到暂存区
12 [root@yinzhengjie ProjcetDir]#
13 [root@yinzhengjie ProjcetDir]# git status
14 # On branch master
15 # Changes to be committed: #由于我们以及把文件提交到暂存区了,需要将数据从暂存区删除,可以执行以下命令,下面以及给的有提示了
16 # (use "git reset HEAD <file>..." to unstage)
17 #
18 # modified: ReadMe
19 #
20 [root@yinzhengjie ProjcetDir]#
21 [root@yinzhengjie ProjcetDir]# git reset HEAD ReadMe #将文件从暂存区(stage区域)撤回到工作目录
22 Unstaged changes after reset:
23 M ReadMe
24 [root@yinzhengjie ProjcetDir]#
25 [root@yinzhengjie ProjcetDir]# git status #再次查看工作目录的状态
26 # On branch master
27 # Changed but not updated: #很明显,又回到了工作目录啦~
28 # (use "git add <file>..." to update what will be committed)
29 # (use "git checkout -- <file>..." to discard changes in working directory)
30 #
31 # modified: ReadMe
32 #
33 no changes added to commit (use "git add" and/or "git commit -a")
34 [root@yinzhengjie ProjcetDir]#

姿势二:

1 [root@yinzhengjie ProjcetDir]# git add ReadMe #第一次将文件提交到暂存区
2 [root@yinzhengjie ProjcetDir]# git status
3 # On branch master
4 # Changes to be committed:
5 # (use "git reset HEAD <file>..." to unstage)
6 #
7 # modified: ReadMe
8 #
9 [root@yinzhengjie ProjcetDir]#
10 [root@yinzhengjie ProjcetDir]# echo "I love golang" >> ReadMe #将文件提交到暂存区之后,可以再次编辑提交的文件,我们要做的操作就是再一次提交一下这个文件即可。这种方式比上面那种撤销回再修改之后再次提交更加方便。
11 [root@yinzhengjie ProjcetDir]#
12 [root@yinzhengjie ProjcetDir]# git add ReadMe #再一次提交一下这个文件就好,不过这一次提交的内容会将上一次提交的内容直接覆盖哟~
13 [root@yinzhengjie ProjcetDir]#
14 [root@yinzhengjie ProjcetDir]#
15 [root@yinzhengjie ProjcetDir]# git status
16 # On branch master
17 # Changes to be committed:
18 # (use "git reset HEAD <file>..." to unstage)
19 #
20 # modified: ReadMe
21 #
22 [root@yinzhengjie ProjcetDir]#

d.丢弃工作区

如果对版本库的文件进行了修改,但是你发现你改的内容把服务给弄崩溃了,这个时候你就很着急啊,咋办呢?当然是感觉把之前的修改给删除啊,换句话说,就是把已经修改且还没有提交的代码丢弃。

1 [root@yinzhengjie ProjcetDir]# git status
2 # On branch master
3 nothing to commit (working directory clean)
4 [root@yinzhengjie ProjcetDir]# more ReadMe
5 http://www.cnblogs.com/yinzhengjie
6 I am good boy
7 I love golang
8 [root@yinzhengjie ProjcetDir]# echo `date +%F` >> ReadMe
9 [root@yinzhengjie ProjcetDir]# git status
10 # On branch master
11 # Changed but not updated:
12 # (use "git add <file>..." to update what will be committed)
13 # (use "git checkout -- <file>..." to discard changes in working directory)
14 #
15 # modified: ReadMe
16 #
17 no changes added to commit (use "git add" and/or "git commit -a")
18 [root@yinzhengjie ProjcetDir]# more ReadMe
19 http://www.cnblogs.com/yinzhengjie
20 I am good boy
21 I love golang
22 2017-10-14
23 [root@yinzhengjie ProjcetDir]#
24 [root@yinzhengjie ProjcetDir]# git checkout -- ReadMe #把已经修改且没有提交的代码丢弃。
25 [root@yinzhengjie ProjcetDir]#
26 [root@yinzhengjie ProjcetDir]# more ReadMe
27 http://www.cnblogs.com/yinzhengjie
28 I am good boy
29 I love golang
30 [root@yinzhengjie ProjcetDir]#

4.删除操作

删除是有讲究的,一种是你删除了工作目录中的文件,这种情况好解决,只需要从版本库中checkout一份出来即可,另外一种是删除版本库里面的文件,这种方式删除就没法checkout出来啦~如果非要还原文件的话就只能进行回滚操作了!接下来我们一起来实战一下吧。

a.删除版本库的文件

1 [root@yinzhengjie ProjcetDir]# git status
2 # On branch master
3 nothing to commit (working directory clean)
4 [root@yinzhengjie ProjcetDir]# ls -a
5 , . .. bin conf .git log ReadMe sql
6 [root@yinzhengjie ProjcetDir]# git rm ReadMe #删除版本库的某个文件,如果是多个的话用空格隔开即可。
7 rm 'ReadMe'
8 [root@yinzhengjie ProjcetDir]# ls -a
9 , . .. bin conf .git log sql
10 [root@yinzhengjie ProjcetDir]# git commit -m "remove ReadMe file" #将删除的操作进行提交
11 [master ff06533] remove ReadMe file
12 1 files changed, 0 insertions(+), 3 deletions(-)
13 delete mode 100644 ReadMe
14 [root@yinzhengjie ProjcetDir]#

b.误删除工作目录文件,需要恢复

[root@yinzhengjie ProjcetDir]# ls -a #查看当前目录所有文件
, . .. bin conf .git log sql
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git checkout -- ReadMe #打算从版本库里面copy一个文件出来,但是报错了,很明显是因为版本库没有该文件。
error: pathspec 'ReadMe' did not match any file(s) known to git.
[root@yinzhengjie ProjcetDir]# git reflog
ff06533 HEAD@{0}: commit: remove ReadMe file
79c7e77 HEAD@{1}: commit: update
39fcee5 HEAD@{2}: 39fcee5: updating HEAD
a4c0670 HEAD@{3}: commit: 我又来溜达了
e9ce735 HEAD@{4}: e9ce7354d385ca3c454bec7fa885c47fc4a768ed: updating HEAD
39fcee5 HEAD@{5}: HEAD^: updating HEAD
d12c571 HEAD@{6}: commit: third commit by yinzhengjie
39fcee5 HEAD@{7}: commit: second commit by yinzhengjie
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# git reset --hard 79c7e77 #由于现在的版本没有我需要的ReadMe文件,因此我进行了回滚操作。
HEAD is now at 79c7e77 update
[root@yinzhengjie ProjcetDir]# ls -a
, . .. bin conf .git log ReadMe sql
[root@yinzhengjie ProjcetDir]#
[root@yinzhengjie ProjcetDir]# rm -f ReadMe #强制在工作目录中删除一个ReadMe文件
[root@yinzhengjie ProjcetDir]# ls -a
, . .. bin conf .git log sql
[root@yinzhengjie ProjcetDir]# git checkout -- ReadMe #再次从版本库copy该文件,发现成功了~
[root@yinzhengjie ProjcetDir]# ls -a
, . .. bin conf .git log ReadMe sql
[root@yinzhengjie ProjcetDir]#

二.GitHub的基本使用

1.远程仓库配置

看过上面的操作,可能你会发现自己已经学会了“git”的提交,删除,撤销,回滚等操作,但是这都不是重点,git更好的地方是提供了远程管理仓库,你可以吧代码提交到世界上的一个网站上去,然后你可以在这个网站上对你的代码进行管理,编辑操作等等,想要学习具体的配置情况,可以参考我的一篇博客:http://www.cnblogs.com/yinzhengjie/p/7017036.html

a.第一次下载代码到本地

1 [root@yinzhengjie yinzhengjie_project]# git clone git@github.com:yinzhengjie/jquery.git #第一次把代码下载的时候用clone。
2 Initialized empty Git repository in /yinzhengjie_project/jquery/.git/
3 remote: Counting objects: 42344, done.
4 remote: Compressing objects: 100% (2/2), done.
5 remote: Total 42344 (delta 1), reused 0 (delta 0), pack-reused 42342
6 Receiving objects: 100% (42344/42344), 26.74 MiB | 257 KiB/s, done.
7 Resolving deltas: 100% (29969/29969), done.
8 [root@yinzhengjie yinzhengjie_project]# ll
9 total 4
10 drwxr-xr-x 9 root root 4096 Oct 16 21:57 jquery
11 [root@yinzhengjie yinzhengjie_project]#
12 [root@yinzhengjie yinzhengjie_project]#git pull #第二次把代码下载的时候就可以用pull啦~直接把远程的代码拉下来。

b.提交代码到远程仓库

1 [root@yinzhengjie yinzhengjie_project]# git remote add origin git@github.com:yinzhengjie/GOlang.git #表示添加远程方库的URL
2 [root@yinzhengjie yinzhengjie_project]# git push -u origin master #表示将本地代码推到远程仓库。
3 Counting objects: 2, done.
4 Writing objects: 100% (2/2), 198 bytes, done.
5 Total 2 (delta 0), reused 0 (delta 0)
6 To git@github.com:yinzhengjie/GOlang.git
7 * [new branch] master -> master
8 Branch master set up to track remote branch master from origin.
9 [root@yinzhengjie yinzhengjie_project]#
10 [root@yinzhengjie yinzhengjie_project]# ls -a
11 . .. download.go .git
12 [root@yinzhengjie yinzhengjie_project]#
13 [root@yinzhengjie yinzhengjie_project]# git add .
14 [root@yinzhengjie yinzhengjie_project]# git commit -m "firsh commit by yinzhengjie"
15 [master e14aa25] firsh commit by yinzhengjie
16 1 files changed, 84 insertions(+), 0 deletions(-)
17 create mode 100644 download.go
18 [root@yinzhengjie yinzhengjie_project]# git push origin master
19 Counting objects: 4, done.
20 Compressing objects: 100% (3/3), done.
21 Writing objects: 100% (3/3), 1.88 KiB, done.
22 Total 3 (delta 0), reused 0 (delta 0)
23 To git@github.com:yinzhengjie/GOlang.git
24 cea2ea3..e14aa25 master -> master
25 [root@yinzhengjie yinzhengjie_project]#

2.分支管理

在实际工作中,程序员写完代码都会提交到远程仓库,那么远程仓库都是主库(master)吗?很显然,答案是否定的,真正的答案是程序员把每天写的代码的进度都提交到了分支上去了,我在的上家公司是做金融的,我的工作之一就是将开发写的代码通过Jenkins等自动化工具将代码进行打包,然后在交个测试之后,才会把软件包发给技术支持。我依稀的记得当时开发每天会向不同的分支提交代码,我们一天甚至会接到30多次的部署环境的任务.......好了,都过去了,我们说会正题,什么是分支管理?它有什么作用呢?

我们想一个问题,每个部门都会负责不同的功能,比如处理风控的小组就负责风控的开发和优化,负责前端的程序员就负责写好前端,而且每个小组的每一个程序员负责的具体人物也是不同的,一个软件公司上百人开发一个软件这也是在正常不过的事情了,他们协同开发一个软件,每天都需要敲代码,如果每个人每天提交代码的时候都放在一个库里面,那么很有可能就因为某个程序员提交的代码导致整个程序崩溃了,以至于其他部门的同事没法正常工作了,所以,为了解决这个问题,我们需要采取分支管理。具体的工作流程我们可以参考下图(用系统自带的软件画的,我承认是该学习一下美术了):

a.创建分支并提交

1 [root@yinzhengjie yinzhengjie_project]# git branch test #创建test分支
2 [root@yinzhengjie yinzhengjie_project]# git branch test2 #创建test2分支
3 [root@yinzhengjie yinzhengjie_project]#
4 [root@yinzhengjie yinzhengjie_project]# git branch #查看当前分支
5 * master
6 test
7 test2
8 [root@yinzhengjie yinzhengjie_project]#
9 [root@yinzhengjie yinzhengjie_project]# git checkout test #切换分支,如果你用git checkout -b test命令的话就不用在上面创建该分支了,因为这一条命令的-b参数等效于创建并切换。
10 D jquery
11 Switched to branch 'test'
12 [root@yinzhengjie yinzhengjie_project]# echo "//hello world" >> download.go #修改当前目录下的文件
13 [root@yinzhengjie yinzhengjie_project]#
14 [root@yinzhengjie yinzhengjie_project]# git add .
15 [root@yinzhengjie yinzhengjie_project]#
16 [root@yinzhengjie yinzhengjie_project]# git commit -m "test commit"
17 [test 3ff1035] test commit
18 1 files changed, 1 insertions(+), 0 deletions(-)
19 [root@yinzhengjie yinzhengjie_project]#
20 [root@yinzhengjie yinzhengjie_project]# git push origin test #注意在提交的时候不要选择master库拉~而是要选择我们刚刚创建任何一个库即可。这条命令一执行会自动在远程仓库GitHub里面创建一个名称叫做test的分支
21 Counting objects: 5, done.
22 Compressing objects: 100% (3/3), done.
23 Writing objects: 100% (3/3), 319 bytes, done.
24 Total 3 (delta 1), reused 0 (delta 0)
25 remote: Resolving deltas: 100% (1/1), completed with 1 local object.
26 To git@github.com:yinzhengjie/GOlang.git
27 * [new branch] test -> test
28 [root@yinzhengjie yinzhengjie_project]#

b.合并分支用法展示

1 [root@yinzhengjie yinzhengjie_project]# git branch
2 master
3 * test #当前所在分支
4 test2
5 [root@yinzhengjie yinzhengjie_project]#
6 [root@yinzhengjie yinzhengjie_project]# git checkout master #切换分支到master
7 D jquery
8 Switched to branch 'master'
9 [root@yinzhengjie yinzhengjie_project]#
10 [root@yinzhengjie yinzhengjie_project]# git branch
11 * master #成功切换到主库啦~
12 test
13 test2
14 [root@yinzhengjie yinzhengjie_project]#
15 [root@yinzhengjie yinzhengjie_project]# git pull origin master #拉取远程最新的master代码
16 From github.com:yinzhengjie/GOlang
17 * branch master -> FETCH_HEAD
18 Already up-to-date.
19 [root@yinzhengjie yinzhengjie_project]#
20 [root@yinzhengjie yinzhengjie_project]#
21 [root@yinzhengjie yinzhengjie_project]# git merge test #在master分支上合并test分支(注意,此时是必须要切换到master库哟)
22 Updating e14aa25..3ff1035
23 Fast-forward
24 download.go | 1 +
25 1 files changed, 1 insertions(+), 0 deletions(-)
26 [root@yinzhengjie yinzhengjie_project]#
27 [root@yinzhengjie yinzhengjie_project]# git merge test
28 Updating e14aa25..3ff1035
29 Fast-forward
30 download.go | 1 +
31 1 files changed, 1 insertions(+), 0 deletions(-)
32 [root@yinzhengjie yinzhengjie_project]#
33 [root@yinzhengjie yinzhengjie_project]# git push origin master #把合并后的代码push到远程仓库的master上。
34 Total 0 (delta 0), reused 0 (delta 0)
35 To git@github.com:yinzhengjie/GOlang.git
36 e14aa25..3ff1035 master -> master
37 [root@yinzhengjie yinzhengjie_project]#

c.BUG分支(stash)用法展示

master一般是不会用来开发的,除非你一个人在写代码,可能会直接用来直接写代码。程序员一般都是在其他分支开发代码的,当然每个程序在写代码的时候难免会出现bug,如果你是负责修复线上bug的程序员,但是你修改的这个分支(如operation分支)需要跟master在下班之前进行合并,这个时候你一看手表,还有30分钟就下班了,窝草,这哪能在30分钟搞定一个bug啊,除非Linus附身。这个时候你想咋办呢?嘿嘿,不要慌,这个时候git有一个神奇的工具,你只需要在命令行中敲击“git stash”就能把当前工作区的代码隐藏起来,以便于你提交当前分支(operation)的代码和主分支(master)进行合并。等你合并完成之后,再把你修改的代码恢复过来,具体案例展示如下:

1 [root@yinzhengjie yinzhengjie_project]# pwd
2 /yinzhengjie_project
3 [root@yinzhengjie yinzhengjie_project]#
4 [root@yinzhengjie yinzhengjie_project]# git branch operation ----->创建一个operation分支
5 [root@yinzhengjie yinzhengjie_project]# git branch development ------>创建一个development分支
6 [root@yinzhengjie yinzhengjie_project]# git branch ------>查看当前分支
7 development
8 * master --------->有"*"号表示当前在master分支
9 operation
10 [root@yinzhengjie yinzhengjie_project]#
11 [root@yinzhengjie yinzhengjie_project]# git checkout operation ----->切换当前分支为operation
12 Switched to branch 'operation'
13 [root@yinzhengjie yinzhengjie_project]# git branch ------>再一次查看当前分支
14 development
15 master
16 * operation ------>在operation前面有个*号,说明当前分支为operation。
17 [root@yinzhengjie yinzhengjie_project]#
18 [root@yinzhengjie yinzhengjie_project]# more main.go
19 package main
20
21 import "fmt"
22
23 func main() {
24 fmt.Println("I'm a main branch")
25 }
26 [root@yinzhengjie yinzhengjie_project]#
27 [root@yinzhengjie yinzhengjie_project]# git status ----->查看工作区状态
28 # On branch operation
29 nothing to commit (working directory clean)
30 [root@yinzhengjie yinzhengjie_project]#
31 [root@yinzhengjie yinzhengjie_project]# more main.go -----修改工作区的文件
32 package main
33
34 import "fmt"
35
36 func Bug(s string) {
37 fmt.Printf("这是一个[%s]漏洞\n",s)
38 }
39
40 func main() {
41 fmt.Println("I'm a main branch")
42 Bug("video")
43 }
44
45 [root@yinzhengjie yinzhengjie_project]#
46 [root@yinzhengjie yinzhengjie_project]# git status ---->修改后再次查看。
47 # On branch operation
48 # Changed but not updated:
49 # (use "git add <file>..." to update what will be committed)
50 # (use "git checkout -- <file>..." to discard changes in working directory)
51 #
52 # modified: main.go
53 #
54 no changes added to commit (use "git add" and/or "git commit -a")
55 [root@yinzhengjie yinzhengjie_project]#
56 [root@yinzhengjie yinzhengjie_project]# git checkout master ---->由于修改了内容,因此这里无法切换到主分支去
57 error: You have local changes to 'main.go'; cannot switch branches.
58 [root@yinzhengjie yinzhengjie_project]#
59 [root@yinzhengjie yinzhengjie_project]# git stash ------>把当前的工作区移走。
60 Saved working directory and index state WIP on operation: 6d94c55 second commit by yinzhengjie
61 HEAD is now at 6d94c55 second commit by yinzhengjie
62 [root@yinzhengjie yinzhengjie_project]#
63 [root@yinzhengjie yinzhengjie_project]# git status ----->再一次查看工作区目录,你会发现当前工作区是干净的。
64 # On branch operation
65 nothing to commit (working directory clean)
66 [root@yinzhengjie yinzhengjie_project]#
67 [root@yinzhengjie yinzhengjie_project]# git checkout master ---->并且你还可以切换到主分支上去啦~
68 Switched to branch 'master'
69 [root@yinzhengjie yinzhengjie_project]#
70 [root@yinzhengjie yinzhengjie_project]# git branch bug-1 ----->新建一个bug修复的分支
71 [root@yinzhengjie yinzhengjie_project]# git branch
72 bug-1
73 development
74 * master
75 operation
76 [root@yinzhengjie yinzhengjie_project]# git checkout bug-1 ----->切换到bug分支
77 Switched to branch 'bug-1'
78 [root@yinzhengjie yinzhengjie_project]#
79 [root@yinzhengjie yinzhengjie_project]# more main.go ----->我们查看里面的代码并发是operation分支的修改的内容。而是master的内容
80 package main
81
82
83 func main(){
84 fmt.println("I'm a main branch")
85 }
86 [root@yinzhengjie yinzhengjie_project]#
87 [root@yinzhengjie yinzhengjie_project]# git branch
88 * bug-1
89 development
90 master
91 operation
92 [root@yinzhengjie yinzhengjie_project]#
93 [root@yinzhengjie yinzhengjie_project]# more main.go -----我们再次修改bug-1分支的代码
94 package main
95
96 import "fmt"
97
98 func Bug(s string) {
99 fmt.Printf("这是一个[%s]漏洞\n",s)
100 }
101
102 func main() {
103 fmt.Println("I'm a main branch")
104 Bug("video")
105 fmt.Println("这是修复BUG的分支")
106 }
107
108 [root@yinzhengjie yinzhengjie_project]#
109 [root@yinzhengjie yinzhengjie_project]# git add .
110 [root@yinzhengjie yinzhengjie_project]# git commit -m "first commit bug-1" ---->将修改bug-1分支的代码进行提交
111 [bug-1 8df61d7] first commit bug-1
112 1 files changed, 10 insertions(+), 2 deletions(-)
113 [root@yinzhengjie yinzhengjie_project]#
114 [root@yinzhengjie yinzhengjie_project]# git status
115 # On branch master
116 nothing to commit (working directory clean)
117 [root@yinzhengjie yinzhengjie_project]#
118 [root@yinzhengjie yinzhengjie_project]# git merge bug-1 ---->合并刚刚我们修改的bug-1分支代码。
119 Updating 0fc7a55..8df61d7
120 Fast-forward
121 main.go | 12 ++++++++++--
122 1 files changed, 10 insertions(+), 2 deletions(-)
123 [root@yinzhengjie yinzhengjie_project]#
124 [root@yinzhengjie yinzhengjie_project]# git checkout operation ----->代码合并玩之后,现在你又得去写你自己的代码了,需要切换到你的分支
125 Switched to branch 'operation'
126 [root@yinzhengjie yinzhengjie_project]# git branch
127 bug-1
128 development
129 master
130 * operation
131 [root@yinzhengjie yinzhengjie_project]#
132 [root@yinzhengjie yinzhengjie_project]# more main.go ----->查看你的分支下的代码,发现之前写的代码不翼而飞啦~不要慌,你下面需要做的就是拿回你之前的数据。
133 package main
134
135 import "fmt"
136
137 func main() {
138 fmt.Println("I'm a main branch")
139 }
140 [root@yinzhengjie yinzhengjie_project]# git status ------>我们在没有回复之前,当前工作区应该是干净的,除非你当前目录下有心的目录或文件没有交给git管理。
141 # On branch operation
142 nothing to commit (working directory clean)
143 [root@yinzhengjie yinzhengjie_project]#
144 [root@yinzhengjie yinzhengjie_project]# git stash list ----->该命令可以查看之前我们使用git stash命令将工作区移走的具体位置,便于我们恢复当时的代码,下面写的很清楚,是讲我们的代码移到了:stash@{0}这个位置,我们需要执行的操作就是恢复那个节点即可。
145 stash@{0}: WIP on operation: 6d94c55 second commit by yinzhengjie
146 [root@yinzhengjie yinzhengjie_project]#
147 [root@yinzhengjie yinzhengjie_project]# git stash apply stash@{0} ------>恢复当时提交的节点
148 # On branch operation
149 # Changed but not updated:
150 # (use "git add <file>..." to update what will be committed)
151 # (use "git checkout -- <file>..." to discard changes in working directory)
152 #
153 # modified: main.go
154 #
155 no changes added to commit (use "git add" and/or "git commit -a")
156 [root@yinzhengjie yinzhengjie_project]#
157 [root@yinzhengjie yinzhengjie_project]# more main.go ---->查看恢复后的内容
158 package main
159
160 import "fmt"
161
162 func Bug(s string) {
163 fmt.Printf("这是一个[%s]漏洞\n",s)
164 }
165
166 func main() {
167 fmt.Println("I'm a main branch")
168 Bug("video")
169 }
170
171 [root@yinzhengjie yinzhengjie_project]#
172 [root@yinzhengjie yinzhengjie_project]# git stash list
173 stash@{0}: WIP on operation: 6d94c55 second commit by yinzhengjie
174 [root@yinzhengjie yinzhengjie_project]#
175 [root@yinzhengjie yinzhengjie_project]# git stash drop ----->用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;另一种方式是用git stash pop,恢复的同时把stash内容也删啦~
176 Dropped refs/stash@{0} (5e3a45ebb7f78d8510b126c4357f2bf411fb4ccb)
177 [root@yinzhengjie yinzhengjie_project]#
178 [root@yinzhengjie yinzhengjie_project]# git stash list ------>删除后再次查看stash 的列表,发现我们之前用stash寸的代码列表以及不存在啦~
179 [root@yinzhengjie yinzhengjie_project]#

3.忽略特殊文件.gitignore

有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次 git status 都会显示 Untracked files ... ,有强迫症的童鞋心里肯定不爽。好在Git考虑到了大家的感受,这个问题解决起来也很简单,在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件。

不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置文件,只需要组合一下就可以使用了。所有配置文件可以直接在线浏览:https://github.com/github/gitignore

忽略文件的原则是:

忽略操作系统自动生成的文件,比如缩略图等;

忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库,比如Java编译产生的.class文件;

忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。

   
1897 次浏览       15
相关文章

每日构建解决方案
如何制定有效的配置管理流程
配置管理主要活动及实现方法
构建管理入门
相关文档

配置管理流程
配置管理白皮书
CM09_C配置管理标准
使用SVN进行版本控制
相关课程

配置管理实践
配置管理方法、工具与应用
多层次集成配置管理
产品发布管理