GIT简介
来源
Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
功能
1、从服务器上克隆数据库(包括代码和版本信息)到单机上。 2、在自己的机器上创建分支,修改代码。 3、在单机上自己创建的分支上提交代码。 4、在单机上合并分支。 5、新建一个分支,把服务器上最新版的代码fetch下来,然后跟自己的主分支合并。 6、生成补丁(patch),把补丁发送给主开发者。 7、看主开发者的反馈,如果主开发者发现两个一般开发者之间有冲突(他们之间可以合作解决的冲突),就会要求他们先解决冲突,然后再由其中一个人提交。如果主开发者可以自己解决,或者没有冲突,就通过。 8、一般开发者之间解决冲突的方法,开发者之间可以使用pull 命令解决冲突,解决完冲突之后再向主开发者提交补丁。
优缺点
优点:
1、适合分布式开发,强调个体。 2、公共服务器压力和数据量都不会太大。 3、速度快、灵活。 4、任意两个开发者之间可以很容易的解决冲突。 5、离线工作。
缺点:
1、资料少(起码中文资料很少)。 2、学习周期相对而言比较长。 3、不符合常规思维。 4、代码保密性差,一旦开发者把整个库克隆下来就可以完全公开所有代码和版本信息。
新加GIT用户
用户将作为所有代码仓库和用户权限的管理者
添加GIT用户
[fanmaolin@Centeros ~]$ sudo useradd -m git
[fanmaolin@Centeros ~]$ sudo passwd git
Changing password
for user git.
New password:
BAD PASSWORD: it
is too simplistic/systematic
BAD PASSWORD:
is too simple
Retype
new password:
Sorry, passwords
do not match.
New password:
BAD PASSWORD: it
is too simplistic/systematic
BAD PASSWORD:
is too simple
Retype
new password:
passwd: all authentication tokens updated successfully.
初始化git帐户信息
[fanmaolin
@Centeros ~]
$ su git
Password:
[git
@Centeros fanmaolin]
$ cd
[git
@Centeros ~]
$ git config --global user.name
"git"
[git
@Centeros ~]
$ git config --global user.email
"git@email.com"
把git用户添加到sudo里去
git
@Centeros ~]
$ su fanmaolin
Password:
[fanmaolin
@Centeros git]
$ sudo vim /etc/sudoers
***省略
git
ALL=
NOPASSWD:ALL
***省略
配置ssh公钥访问oschina
把代码放到开源中国社区的码云里
ssh公钥
大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。 首先先确认一下是否已经有一个公钥了。SSH 公钥默认储存在账户的主目录下的 ~/.ssh目录。关键是看有没有用 something或者something.pub 来命名的一对文件,这个 something通常就是 id_dsa 或 id_rsa。有 .pub 后缀的文件就是公钥,另一个文件则是密钥。假如没有这些文件,或者干脆连 .ssh 目录都没有,可以用 ssh-keygen 来创建。 转到.ssh目录下(若没有则新建一个),生成的公钥将放到该目录下
生成公钥
[fanmaolin
@Centeros git]
$ cd ~
/.ssh/
[fanmaolin
@Centeros .ssh]
$ ssh-keygen -t rsa -
C"1300958-2009@163.com"
Generating public/private rsa key pair.
Enter file
in which to save the key (
/home/fanmaolin/.ssh/id_rsa)
:
Enter passphrase (empty
for no passphrase)
:
Enter same passphrase
again:
Your identification has been saved
in /home/fanmaolin/.ssh/id_rsa.
Your public key has been saved
in /home/fanmaolin/.ssh/id_rsa.pub.
The key fingerprint
is:
eb:8d:99:b6:4********
:b9:8f:f5:ef:f5:03:dc:20:7d
1300958-
2009@163.com
The key
's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| . o E |
| S o + |
| . o o . |
| + = . . .|
| o =.O . o.|
| ..Ooo .oo o|
+-----------------+
查看生成的公钥,并将公钥内容拷贝添加到Git @ OSC http://git.oschina.net/keys
[fanmaolin
@Centeros .ssh]
$ cat ~
/.ssh/id_rsa.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAwMcRxQjpb1kLKRO+
JCjWaz1vCXicJIh6AMYzJ+iLXXrUckFDMclPYJEQ+d+
E+
7J*********
AIj5lyIyV3fSM9Egqb5ZddqYiB9/
9Q6RTvwcCE8lf4ke/
/D2NRF+eDS1JunSj6ySvpIZXuJKrMsT4Qzb1NxiXK5Szl15EjrUPYfe5tt/UO5knwuNINplghK6M85i7qHAP4dJ67oTjQmHgW+
V50XXKXWY5mAWk2vlGnHcy5qQ==
1300958-
2009@163.com
检查是否添加公钥成功
fanmaolin@Centeros .ssh]$ ssh -T git@git.oschina.net
The authenticity
of host
'git.oschina.net (
116.211.167.14)' can
't be established.
RSA key fingerprint
is e3:ee:
82:
78:fb:c0:ca:
24:
65:
69:ba:bc:
47:
24:
6f:d4.
Are you sure you want
to continue connecting (yes/no)? yes
Warning: Permanently added
'git.oschina.net,
116.211.167.14' (RSA)
to the list
of known hosts.
Welcome
to Git@OSC, fanmaolinn! 说明成功
关于生成添加公钥可以参考http://git.mydoc.io/?t=154712
创建本地存放代码的仓库
因为是在码云上进行存放,你首先需要注册账号等等
新建并初始化GIT仓库
首先是建立我们本地一个git目录,然后git init使它真正成为一个仓库,会出现一个.git目前,仓库的属性都在里面,它就是一个标志,有它就是一个git仓库。
[fanmaolin
@Centeros ~]
$ mkdir git
[fanmaolin
@Centeros ~]
$ cd git
[fanmaolin
@Centeros git]
$ git init
Initialized empty
Git repository
in /home/fanmaolin/git/.git/
建立联系
[fanmaolin@Centeros git]$ git remote
add origin git@git
.oschina.net:fanmaolinn/test
.git
[fanmaolin@Centeros git]$ git remote -v
origin git@git
.oschina.net:fanmaolinn/test
.git (fetch)
origin git@git
.oschina.net:fanmaolinn/test
.git (
push)
远程仓库与本地仓库的操作
把远程仓库的文件下载
可以用pull把我们远处仓库的文件下下来(刚才我们上面建立了联系,所示我们可以用这个命令:git pull origin master)
[fanmaolin@Centeros git]$ git pull git@git
.oschina.net:fanmaolinn/test
.git
remote: Counting objects:
3, done.
remote: Total
3 (delta
0), reused
0 (delta
0)
Unpacking objects:
100% (
3/
3), done.
From git
.oschina.net:fanmaolinn/test
* branch HEAD -> FETCH_HEAD
[fanmaolin@Centeros git]$ ls
README
.md
把本地仓库文件上传
新建文件x.txt
fanmaolin
@Centeros git]
$ vim x.txt
[fanmaolin
@Centeros git]
$ cat x.txt
xxx
[fanmaolin
@Centeros git]
$ git status
nothing added to commit but untracked files present (use
"git add" to track)
[fanmaolin
@Centeros git]
$ git add x.txt
[fanmaolin
@Centeros git]
$ git status
已经没有未追踪文件了,但是提示我们有改变需要被提交。这个时候,x.txt这个文件就存在于所谓的“暂存区”中,暂存区中的文件可以被真正提交到git仓库。
删除暂存区的文件
use “git rm –cached …” to unstage。
[fanmaolin@Centeros git]$ git rm --cached x.txt
rm
'x.txt'
[fanmaolin@Centeros git]$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# x.txt
nothing added to commit but untracked files present (use
"git add" to track)
再运行git status时就可以发现,又回到文件没有被提交时的状态了
提交x.txt到远程仓库
关于你要提交的地址,可以查看
[fanmaolin@Centeros git]$ git add x.txt
[fanmaolin@Centeros git]$ git
commit -m "add a file"
[master 4634b34] add a file
Committer: fanmaolin <fanmaolin@Centeros.ocaldomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <you@example.com>'
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 x.txt
[fanmaolin@Centeros git]$ git push -f git@git.oschina.net:fanmaolinn/test.git
#注意地址正确
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@git.oschina.net:fanmaolinn/test.git
aa56b2e..4634b34 master -> master
检查提交情况
[fanmaolin
@Centeros git]
$ git status
nothing to commit (working directory clean
提示我们当前没有东西要提交,当前工作目录是干净的,因为我们都提交到git了。到这步后,git就可以完全跟踪已经被提交的文件了,以后,这个文件的任何修改,它都可以记录下来。比如某个人对项目私自修改了一些东西,项目管理者根据git的版本记录是一定可以找到是谁修改了哪些文件的。 进入码云查看
使用clone命令
用clone取得项目的git仓库
[fanmaolin@Centeros git]$ git clone git@git
.oschina.net:fanmaolinn/test
.git
Initialized empty Git repository
in /home/fanmaolin/git/test/
.git/
remote: Counting objects:
6, done.
remote: Compressing objects:
100% (
3/
3), done.
remote: Total
6 (delta
0), reused
0 (delta
0)
Receiving objects:
100% (
6/
6), done.
[fanmaolin@Centeros git]$ ls
README
.md test
x.txt
[fanmaolin@Centeros git]$ ls
.git/
branches COMMIT_EDITMSG config description FETCH_HEAD HEAD hooks index info logs objects refs
可以看见.git这个目录:
使用git log
git log可以看见我们在服务器上做的事情:
[fanmaolin@Centeros git]$ cd test/
[fanmaolin@Centeros test]$ git log
commit
4634b341d432a826086fc03552661259e0558910
Author: fanmaolin <fanmaolin@Centeros
.ocaldomain>
Date: Fri Aug
4 17:
37:
26 2017 +
0800
add a file
commit aa56b2e42a95683583f46eefbcc7255752809e92
Author: fanmaolinn <
1300958-
2009@163.com>
Date: Sat Aug
5 13:
00:
16 2017 +
0800
Initial commit
在该文件夹下创建文件并上传
[fanmaolin
@Centeros test]$ touch a.c
[fanmaolin
@Centeros test]$ git status
nothing added to commit but untracked files present (use
"git add" to track)
[fanmaolin
@Centeros test]$ git add a.c
[fanmaolin
@Centeros test]$ git commit -m
"add a file"
[master
6533c18] add a file
Committer: fanmaolin <fanmaolin
@Centeros.ocaldomain>
Your name
and email address were configured automatically based
on your username
and hostname. Please check that they are accurate.
You can suppress
this message
by setting them
explicitly:
git config --
global user.name
"Your Name"
git config --
global user.email you
@example.com
If the identity used
for this commit
is wrong, you can fix it
with:
git commit --amend --author=
'Your Name <you@example.com>'
0 files changed,
0 insertions(+),
0 deletions(-)
create mode
100644 a.c
[fanmaolin
@Centeros test]$ git push -f git
@git.oschina.
net:fanmaolinn/test.git
Counting
objects:
4, done.
Compressing
objects:
100% (
2/
2), done.
Writing
objects:
100% (
3/
3),
301 bytes, done.
Total
3 (delta
0), reused
0 (delta
0)
To git
@git.oschina.
net:fanmaolinn/test.git
4634b34.
.6533c18 master
-> master
查看上传情况
总结
GIT与SVN各有千秋,看用户的使用习惯吧,关于clone命令,还有很多种用法,可以自行百度一波
参考链接: http://blog.csdn.net/lyx_win/article/details/52231007