<服务>Git

时间:April 16, 2017 分类:

目录:

Git是分布式版本控制系统。每个client都是一个版本库,通过中央服务器用于代码交换。

git官网https://git-scm.com/

使用git

安装git

[root@why-1 ~]# yum install -y git

设置全局的用户名和邮箱

[root@why-1 ~]# git config --global user.name "why"
[root@why-1 ~]# git config --global user.email "why@whysdomain.com"
[root@why-1 ~]# git config --list
user.name=why
user.email=why@whysdomain.com

开启颜色

[root@why-1 ~]# git config --global color.ui true
[root@why-1 ~]# git config --list
user.name=why
user.email=why@whysdomain.com
color.ui=true

初始git仓库

[root@why-1 ~]# mkdir why
[root@why-1 ~]# cd why
[root@why-1 why]# git init
Initialized empty Git repository in /root/why/.git/
[root@why-1 why]# ls -a
.  ..  .git

添加文件

[root@why-1 why]# vi README.md
[root@why-1 why]# cat README.md 
whysdomain

查看状态

[root@why-1 why]# git status
# On branch master                  
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README.md
nothing added to commit but untracked files present (use "git add" to track)

在主分支上,初始化提交,需要通过git add进行提交

添加到git

[root@why-1 why]# git add README.md 

查看状态

[root@why-1 why]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   README.md
#

发现一个新文件README.md

提交

[root@why-1 why]# git commit -m 'first commit' 
[master (root-commit) f7c4780] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README.md

查看状态

[root@why-1 why]# git status
# On branch master
nothing to commit (working directory clean)

当前工作目录没有任何改变

查看提交日志

[root@why-1 why]# git status
# On branch master
nothing to commit (working directory clean)
[root@why-1 why]# git log
commit f7c47809c3c1e3f69811b49770f7d5c9c0afb5f9
Author: why <why@whysdomain.com>
Date:   Sat Apr 15 02:41:26 2017 +0800

    first commit

修改文件

[root@why-1 why]# vi README.md 
[root@why-1 why]# cat README.md 
whysdomain
whysdomain.com
[root@why-1 why]# git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

README.md被修改了

对比

[root@why-1 why]# git diff README.md 
diff --git a/README.md b/README.md
index 37f1639..bb914dd 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 whysdomain
+whysdomain.com

进行提交

[root@why-1 why]# git add README.md 
[root@why-1 why]# git commit -m 'change README.md'
[master 6bc15d8] change README.md
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@why-1 why]# git log
commit 6bc15d8e67d91e7b73d8861ccb88377e324944b8
Author: why <why@whysdomain.com>
Date:   Sat Apr 15 02:50:09 2017 +0800

    change README.md

commit f7c47809c3c1e3f69811b49770f7d5c9c0afb5f9
Author: why <why@whysdomain.com>
Date:   Sat Apr 15 02:41:26 2017 +0800

    first commit

git有工作区和暂存区,我们直接操作的是工作区,通过git add命令将其提交到暂存区,然后才能进行commit

版本回退

[root@why-1 why]# git reset --hard HEAD^
HEAD is now at f7c4780 first commit
[root@why-1 why]# cat README.md 
whysdomain

HEAD代表当前版本,一个'^'代表往前一个版本,如果是往前三个版本就是HEAD^^^

[root@why-1 why]# git log
commit f7c47809c3c1e3f69811b49770f7d5c9c0afb5f9
Author: why <why@whysdomain.com>
Date:   Sat Apr 15 02:41:26 2017 +0800

    first commit

之前的log也没有了。

回退指定版本

查看当前存在版本的ID

[root@why-1 why]# git reflog
f7c4780 HEAD@{0}: HEAD^: updating HEAD
6bc15d8 HEAD@{1}: commit: change README.md

回退到指定版本

[root@why-1 why]# git reset --hard 6bc15d8
HEAD is now at 6bc15d8 change README.md
[root@why-1 why]# cat README.md 
whysdomain
whysdomain.com

在github上创建项目

选着start a project 输入项目名称,可以添加描述,可以选择语言类型减少上传不必要的文件,例如Python的pyc文件,还可以添加License等,默认为公有仓库,也可以转为私有。

添加语言

添加licence

创建完成

进行克隆可以选择HTTP的方式,也可以使用Use SSH以ssh的方式进行实现。

在本机生产ssh秘钥

[root@why-1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
60:40:f0:28:86:42:02:d3:6d:c9:ed:69:c1:bd:36:dd root@why-1
The key's randomart image is:
+--[ RSA 2048]----+
|=o.=o+ .         |
|+..o=.+ .        |
|+.....oo o .     |
|o.   .+.+ . E    |
|     . .S.       |
|                 |
|                 |
|                 |
|                 |
+-----------------+

查看公钥

[root@why-1 ~]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5Ov5VBTysdAgOoqk0fotUyB+oOdxlioWSDHPmwnaMklk88+8ObAmVdW+T5kqT2dFGpxBqt60bUnqgriocivdnA33GyZQvkPSdoptE94OLXMZwQ6ods9veGL49ISVej0h96myNylnD25TMThDx91fsl0IeY3dy5+cx70+edQFE4rtr/s8N6/Ai3aZdflUkj5a6a5qB99k7u+O5Yg6k3nGyslA/ECtxj9bYkT4thnki3SbHDg0AnlC0LpwIwkfa0lgdU3OSdZS8iB9Jv55Omf7BSLUD4bZiCBkCk9Cm3ZJxDyE3NuQVDv4No5cBpqcf/1Y95ju0eUq9emlY0uorJ7Ypw== root@why-1

添加公钥到github

选择settings——>Deploy——>Add deploy key

  • Title 标题
  • Key 公钥
  • Allow write access 允许push

添加之后会显示我们添加的key,并且注册的邮箱会收到添加key的邮件

回到code中复制ssh方式的路径

[root@why-1 why]# git remote add origin https://github.com/redhadoopwhy/demo.git
[root@why-1 why]# cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/redhadoopwhy/demo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

这个过程就是就是添加下面三行的。

[root@why-1 why]# git pull origin master
warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/redhadoopwhy/demo
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
 .gitignore |   89 ++++++++++++++++++++++++++
 LICENSE    |  201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 290 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 LICENSE
[root@why-1 why]# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/redhadoopwhy/demo.git/info/refs

fatal: HTTP request failed

如果出现以下情况

[root@why-1 why]# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/redhadoopwhy/demo.git/info/refs

fatal: HTTP request failed

如果Git版本是1.7.1,操作系统为CentOS或redhat 6.x

[root@why-1 why]# git --version
git version 1.7.1
[root@why-1 why]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.5 (Santiago)

修改配置文件

[root@why-1 why]# vi .git/config
        url = https://github.com/redhadoopwhy/demo.git
        改为
        url = https://redhadoopwhy@github.com/redhadoopwhy/demo.git
[root@why-1 why]# git push -u origin master
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Counting objects: 12, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (11/11), 1.02 KiB, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@github.com:redhadoopwhy/demo.git
   1e96966..afb007b  master -> master
Branch master set up to track remote branch master from origin.

可以看到刚才上传的内容

创建分支

[root@why-1 why]# git branch dev

切换分支

[root@why-1 why]# git checkout dev
Switched to branch 'dev'

以上创建和切换可以通过git checkout -b dev实现

查看当前分支

[root@why-1 why]# git branch
* dev
  master

在dev分支添加新文件

[root@why-1 why]# vi dev.log
[root@why-1 why]# cat dev.log 
w
h
y

添加文件到暂存区

[root@why-1 why]# git add dev.log 
The following paths are ignored by one of your .gitignore files:
dev.log
Use -f if you really want to add them.
fatal: no files added
需要制定-f参数
[root@why-1 why]# git add -f dev.log 

提交

[root@why-1 why]# git commit -m "add dev.log"
[dev 116a2fb] add dev.log
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 dev.log

合并分支

切换到master分支

[root@why-1 why]# git checkout master
Switched to branch 'master'
[root@why-1 why]# ll
总用量 20
-rw-r--r-- 1 root root     4 4月  15 03:26 demo.py
-rw-r--r-- 1 root root 11357 4月  15 21:52 LICENSE
-rw-r--r-- 1 root root    26 4月  15 03:06 README.md

可以看到master分支没有dev.log文件

合并分支

[root@why-1 why]# git merge dev
Updating afb007b..116a2fb
Fast-forward
 dev.log |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 dev.log
[root@why-1 why]# ll
总用量 24
-rw-r--r-- 1 root root     4 4月  15 03:26 demo.py
-rw-r--r-- 1 root root     6 4月  15 22:58 dev.log
-rw-r--r-- 1 root root 11357 4月  15 21:52 LICENSE
-rw-r--r-- 1 root root    26 4月  15 03:06 README.md
[root@why-1 why]# git branch
  dev
* master

如果需要删除分支,可以git branch -d dev

如果说存在分支和主分支内容冲突的时候,merge合并会失败,如要修改后进行合并。

提交分支

[root@why-1 why]# git push origin dev
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:redhadoopwhy/demo.git
 * [new branch]      dev -> dev

添加标签

[root@why-1 why]# git tag v1.0
[root@why-1 why]# git tag 
v1.0

每次发布版本就可以打一个tag,生产的时候

[root@why-1 why]# git show v1.0
commit 116a2fbafbaa049b1192a6cb3936648ec08ebb3f
Author: why <why@whysdomain.com>
Date:   Sat Apr 15 22:54:22 2017 +0800

    add dev.log

diff --git a/dev.log b/dev.log
new file mode 100644
index 0000000..cc5cac3
--- /dev/null
+++ b/dev.log
@@ -0,0 +1,3 @@
+w
+h
+y
[root@why-1 why]# git push origin v1.0
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:redhadoopwhy/demo.git
 * [new tag]         v1.0 -> v1.0

在code中选择 release就可以看到我们的添加标签的v1.0

如果选择branch就是我们提交的分支

切换到release版本v1.0

[root@why-1 why]# git checkout v1.0
Note: checking out 'v1.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 116a2fb... add dev.log
[root@why-1 why]# git tag
v1.0
[root@why-1 why]# git branch
* (no branch)
  dev
  master

搭建git仓库

可以在https://about.gitlab.com/downloads 页面选择操作系统

这边我选择了CentOS6

https://about.gitlab.com/downloads/#centos6

这里边有详细的安装过程我这边就不过多介绍,这个gitllab-ce的rpm很大,不太好下载,可以去https://packages.gitlab.com/gitlab/gitlab-ce下载后直接用过rpm安装

CentOS6的为https://packages-gitlab-com.s3-accelerate.amazonaws.com/7/8/el/6/package_files/11051.rpm?AWSAccessKeyId=AKIAJ74R7IHMTQVGFCEA&Signature=oTfXEP7n78cUNUiqaN48B8arPao%3D&Expires=1492270218

gitlab-ctl status可以看到gitlab依赖的服务是否正常

gitlab-ctl tail nginx或者status看到的服务都可以。

安装到了/opt/下的github

/etc/gitlab.rb.template

默认用户名和密码为root 5iveL!fe

windows和mac可以使用source tree来进行管理,Linux就直接使用git即可。

更多的可以参考此博客,很受启发Git 最佳实践:分支管理