版本工具-Git

克隆代码到本地

1
2
3
$ git clone https://git.oschina.net/wuyu/wuyu-plugin.git
$ cd wuyu-plugin
$ git checkout master

已有项目推送仓库

1
2
3
4
5
$ git init
$ git add ./*
$ git commit -m "init"
$ git remote add origin https://git.oschina.net/wuyu/wuyu-plugin.git
$ git push -u origin master

说明: https://git.oschina.net/wuyu/wuyu-plugin.git为示例工程目录,个人的工程以自己的路径为准.

创建分之推送

1
2
3
$ git branch develop
$ git push origin develop
$ git checkout develop

工程提交修改

1
2
3
$ git add ./*
$ git commit -m "init"
$ git push -u origin master

修改提交Commit

如果这是你最近一次提交并且没有push到远程分支,可用以下命令直接修改:

1
[root@localhost vplus (master)]$ git commit --amend -m "your new message"

其他情况可参考 https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits

修改commit的用户名和邮箱

GIT修改当前工程的提交人信息

1
2
3

$ git config user.name "hunts"
$ git config user.email "develop@hunts.work"

Git修改全局的工程提交人信息

1
2
$ git config --global user.name "hunts"
$ git config --global user.email "develop@hunts.work"

或则 或者直接在全局配置文件中修改

1
2
3
4
5
6
7
8
[hunts@localhost logs ]$ cat ~/.gitconfig 
[user]
name = hunts
email = develop@hunts.work
[core]
autocrlf = input
[http]
postBuffer = 524288000

清空git默认的用户名和密码

1
[hunts@localhost ~]$ git config --local --unset credential.helper

Git全局忽略文件

有些场景下我们需要忽略Git项目中的某些文件,对于自己参与的项目,在项目下新建.gitignore文件即可。但如果本地项目较多,或者临时维护别人的项目,一般不会单独新建.gitignore文件,这时就可以选择全局性的忽略这些文件.

在当前用户目录下新建.gitignore_global文件

1
[root@localhost ~]$ vim ~/.gitignore_global

在.gitignore_global文件中添加要忽略的文件,和.ignore里边格式一致(每行一个,支持正则)

Git区分文件名大小写

例如: 创建一个文件readme.md,写入内容,提交到线上仓库,然后修改本地文件名为Readme.md,提交,会发现没有变化,无任何提示信息; 其实Git默认对于文件名大小写是不敏感的. 可以通过如下方式设置

  1. 配置 Git 使其对文件名大小写敏感
1
[root@localhost master]$ git config core.ignorecase false
  1. 修改本地文件名为大写
1
[root@localhost master]$ mv readme.md Readme.md​
  1. 提交修改后的文件, 如果未生效, 请先删除线上仓库中的文件,重新提交
1
2
3
4
5
[root@localhost master]$ git add Readme.md
[root@localhost master]$ git commit -m 'Readme.md'
[root@localhost master]$ git push origin master
[root@localhost master]$ # 如果提交后没变化,执行该命令,之后再执行上述命令,删除本地Git管理的文件,当成新文件提交
[root@localhost master]$ git rm -r --cached readme.md

Git修改远程分支

仓库A:https://github.com/old/json
仓库B:https://gitee.com/new/json

从A切换到B,有3种方式

命令行修改远程地址

1
2
[root@localhost ~]$ cd json
[root@localhost json]$ git remote set-url origin https://gitee.com/new/json.git

先删除再添加新地址

1
2
3
[root@localhost ~]$ cd json
[root@localhost json]$ git remote rm origin
[root@localhost json]$ git remote add origin https://gitee.com/new/json.git

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]$ cd json/.git
[root@localhost .git]$ vim config
[core]
repositoryformatversion = 0
filemode = true
logallrefupdates = true
precomposeunicode = true

[remote "origin"]
# 修改成新的仓库地址
url = https://gitee.com/new/json.git
fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]
remote = origin
merge = refs/heads/master

Git项目创建Tag

查看项目Tag

1
2
3
[root@localhost vplus (master)]$ git tag 
v1.1.1
v1.1.2

创建新的Tag

1
2
3
4
5
6
7
8
9
10
11
[root@localhost vplus (master)]$ git tag v1.1.3 -m '增加工具类,生成实体类打标lombok注解'
[root@localhost vplus (master)]$ git push origin v1.1.3
Counting objects: 1, done.
Writing objects: 100% (1/1), 215 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/dennisit/vplus.git
* [new tag] v1.1.3 -> v1.1.3
[root@localhost vplus (master)]$ git tag
v1.1.1
v1.1.2
v1.1.3

删除指定tag

1
2
3
4
[root@localhost vplus (master)]$ git push origin --delete tag V1.1.3                    
remote: warning: Deleting a non-existent ref.
To https://github.com/dennisit/vplus.git
- [deleted] V1.1.3

上面我们通过git push origin v1.1.3推送了我们新创建的tag v1.1.3, 进度git主页查看

如图,可以看到新推送的tag已经展示在列表

发布Release

点击上图中的【Draft a new release】进行release tag 创建.

查看发布效果

说明

上面的操作仅仅只是将我们最新更新的tag发布成了release, 如果想让别人直接引用你最细版本的包, 需要将最新更新版本RELEASE到中央仓库中(后续有时间补充如何推送)。

Git命令一览

Github作为图床

Github作为图床比较简单,只需要一个地址映射即可. 如下:

原始文件

图床地址

发布中央仓库

我们可以把自己沉淀的通用工具代码RELEASE到Maven中央仓库, 然后后期直接Maven引入使用, 方便统一管理更新.

创建发布工单

创建审核单, 网址: https://issues.sonatype.org/secure/Dashboard.jspa, 工单按照提示填写即可, 工单只有等审核通过后自己包对应的groupId才能进行包推送到中央仓库.

版本包防篡改

我们的工程包,安全方面上讲是不允许被非法篡改的,所以我们需要在打包的时候进行签名处理.

安装Gnupg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# yum install gnupg
[root@localhost ~]# gpg --version
gpg (GnuPG) 2.0.22
libgcrypt 1.5.3
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: ~/.gnupg
支持的算法:
公钥:RSA, ?, ?, ELG, DSA
对称加密:IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256,
TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256
散列:MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
压缩:不压缩, ZIP, ZLIB, BZIP2

生成密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@localhost ~]# gpg --list-keys
[root@localhost ~]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

请选择您要使用的密钥种类:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (仅用于签名)
(4) RSA (仅用于签名)
您的选择?
RSA 密钥长度应在 1024 位与 4096 位之间。
您想要用多大的密钥尺寸?(2048)
您所要求的密钥尺寸是 2048 位
请设定这把密钥的有效期限。
0 = 密钥永不过期
<n> = 密钥在 n 天后过期
<n>w = 密钥在 n 周后过期
<n>m = 密钥在 n 月后过期
<n>y = 密钥在 n 年后过期
密钥的有效期限是?(0)
密钥永远不会过期
以上正确吗?(y/n)y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

真实姓名:苏若年
电子邮件地址:dennisit@163.com
注释:suruonian
您正在使用‘utf-8’字符集。
您选定了这个用户标识:
“苏若年 (suruonian) <dennisit@163.com>”

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?O
您需要一个密码来保护您的私钥。

我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数

说明: 上面的步骤如果不需要特别选定直接使用默认的即可,在设定密码过程中会弹出对话框让你设定私钥密码,该密码需要记住后面发布包时候需要用.

查看密钥

1
2
3
4
5
6
[root@localhost local]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/30E287D1 2016-02-25
uid 苏若年 (suruonian) <dennisit@163.com>
sub 2048R/FA4E2A5C 2016-02-25

密钥推送服务器

注意: 这里发送的是公钥,和上面密钥列表中对应pub模块对应.

1
2
3
4
5
6
7
[root@localhost local]# gpg --keyserver hkp://keys.gnupg.net --send-keys 30E287D1
gpg: 将密钥‘30E287D1’上传到 hkp 服务器 keys.gnupg.net
[root@localhost local]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 30E287D1
gpg: 下载密钥‘30E287D1’,从 hkp 服务器 keys.gnupg.net
gpg: 密钥 30E287D1:“苏若年 (suruonian) <dennisit@163.com>”未改变
gpg: 合计被处理的数量:1
gpg: 未改变:1

Maven配置

该配置主要用于配置deploy包到oss.sonatype.org时的账号授权信息.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost apache-maven-3.5.2]# tree conf/
conf/
├── logging
│   └── simplelogger.properties
├── settings.xml
└── toolchains.xml
[root@localhost apache-maven-3.5.2]# vim conf/settings.xml
[root@localhost apache-maven-3.5.2]# cat conf/settings.xml -n | grep -C 3 un-zone
124
125 <server>
126 <id>sonatype</id>
127 <username>un-zone</username>
128 <password>登录oss.sonatype.org的密码</password>
129 </server>
130

这里的un-zone为我的oss.sonatype.org账号ID

工程配置

引入仓库

1
2
3
4
5
6
7
8
9
10
11
12
<distributionManagement>
<snapshotRepository>
<id>sonatype</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

注意: 这里的推送地址id和maven的setting.xml中配置的server节点中的id保持一致.

发布插件

工程pom.xml文件中增加如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<developers>
<developer>
<id>Elon.Su</id>
<name>苏若年</name>
<email>dennisit@163.com</email>
<timezone>+8</timezone>
</developer>
</developers>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<charset>${project.build.sourceEncoding}</charset>
<docencoding>${project.build.sourceEncoding}</docencoding>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

执行发布并添加签名

1
[root@localhost vplus]# mvn clean deploy -Prelease -Dmaven.test.skip=true -Dgpg.passphrase=密钥生成时设定的私钥密码

包公有化

上面我们将包大到maven仓库中, 但是此时并不能直接引入使用. 需要在https://oss.sonatype.org/中进行包正式发布.

  1. 登录https://oss.sonatype.org
  2. 选择菜单【Staging Repositories】
  3. 搜索自己相关包关键词
  4. 选中要正式发布的包依次执行【close】、【release】(【close】会进行包发布合规性检测. 只有各个指标都通过了才能真正的release成功).

参考文档