git 命令02

1.怎么添加远程库
2.克隆一个远程库
3.什么是分支管理
4.什么是标签管理
3.搭建git服务器

1.添加远程库。
我以github官网上提供给用户注册https://github.com/,可以在官网了自己新建一个repository为例,是免费,
当然后这个库是public,如果要private化是要付费的。

(1)进入github点击创建New respository  取名为“gitstudy”

github.PNG01

(2)填写New respository 的Name,以及可选的相关描述,勾选“Initialize this repository with a README”选项,初始库的时建立一个README文件,
最后点击Create  respository

github.PNG02
(3)点击账户设置,找“SSH and GPG keys” 下拉选项。点击“New SSH keys”” 添加本地主机的id_rsa.pub,这公钥用于github库与
服务与本地主机进行通信的,必选添加
a.在本地创建SSH keys”
ssh-keygen -t rsa -C "846200045@qq.com"  一路回车,将在家目录下.ssh目录生成id_rsa.pub和id_rsa

[root@www ~]# ssh-keygen -t rsa -C "846200045@qq.com"
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:
9a:c5:10:1b:eb:f4:50:bb:10:31:6e:8f:9d:17:46:77 846200045@qq.com
The key's randomart image is:
+--[ RSA 2048]----+
|      *.. . . E  |
|     . O o . .   |
|      X . o      |
|     + X + .     |
|      o S .      |
|       + .       |
|      o          |
|                 |
|                 |
+-----------------+
[root@www ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@www ~]#

id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
b.第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容.
c.最后,远程github服务就和本地主机可以进行通信了,当然下面就可以进行克隆一个远程库

2.克隆一个远程库
准备在本地主机克隆一份远程主机(默认远程主机名是origin)库
git clone git@github.com:autumn1023/gitstudy

[root@www ~]# git clone git@github.com:autumn1023/gitstudy            #其中autumn1023是在github的账号,gitstudy是新建的repository
Initialized empty Git repository in /root/gitstudy/.git/
The authenticity of host 'github.com (192.30.252.123)' 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.252.123' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@www ~]# ls gitstudy
README.md
[root@www ~]# ls -ld gitstudy
drwxr-xr-x. 3 root root 4096 6月   1 17:52 gitstudy

到此从远程克隆一个库完成。

3 什么是分支管理
软件项目开发过程,由于是分阶段来做的,在没有开发完就提交给其他人,导致其他人其他人无法正常使用代理;
但是如果在项目开发完后一次性提交代码,又存在丢失每天进度的风险。
分支解决了这个问题,开发人员可以创建了一个属于自己的分支,别人看不到,
还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交;
直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又不影响别人工作。

(1)master分支和其他分支
在从远程克隆库的时候,默认带有一个master分支,也称为主分支。显示当前的所有分支及当前所在分支、创建其他分支方法
 
显示当前所有分支及当前所在分支,带有*这分支表示是当前所在分支
git branch
[root@www gitstudy]# git branch
* master

 
创建并切换到其他分支方法一:
git checkout -b branchname
 

[root@www gitstudy]# git checkout -b dev
Switched to a new branch 'dev'
[root@www gitstudy]# git branch
* dev
  master

[root@www gitstudy]#
创建并切换到其他分支方法二:
git branch branchname
git checkout branchname

[root@www gitstudy]# git branch bug
[root@www gitstudy]# git checkout bug
Switched to branch 'bug'
[root@www gitstudy]# git branch
* bug
  dev
  master

[root@www gitstudy]#

(2) 解决分支之间的冲突
这分支的冲突是一个分支master在工作期间修改了文件;git add file;git commit -c “”
另外一个分支dev也在修改次文件;git add file;git committ -c
最后在在主分支上,准备和dev分支时,提示分支之间有冲突,无法合并。导致这个原因可能是两个分在修改文件的同一行,并且相似性很大,
结果不知道在合并的时候不知道以哪个分支为准。出现这情况可以先手动修改文件然后在提交。


3.标签管理。
Git的标签是版本库的快照,但其实它就是指向某个commit的指针(跟分支很像对不对?但是分支可以移动,标签不能移动),所以,创建和删除标签都是瞬间完成的。
我在用在git commit -m “” 以后都生成一个commit_id, 用git log命令可以查看,commit_id是很长的字符串,记起来很麻烦。
这个时候标签管理就很有很大的作用。


(1)给当前分支打标签
git branch  #查看当前所在的分支
git tag  tagname #给当前分支贴个标签
git tag      #查看当前的标签

[root@www gitstudy]# git tag v1.0
[root@www gitstudy]# git tag
v1.0

给历史上忘记打标签的分支贴标签
先用命令git log 或者git log --pretty=oneline --abbrev-commit查看没有打标签的历史版本的commit_id
git tag  tagname commi_id

[root@www gitstudy]# git log --pretty=oneline --abbrev-commit
5a63081 add &
22626a5 Initial commit

git tag v0.2  22626a5

(2)查看标签信息:

[root@www gitstudy]# git show v0.2
commit 5a630814c69d212034ed0df61c70dc45cbe6faec
Author: root <root@www.test.com>
Date:   Wed Jun 1 18:30:41 2016 +0800

    add &

diff --git a/README.md b/README.md
index 88b8e37..b32ba89 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
 # gitstudy
-the respository of  gitstudy
+the respository of &  gitstudy

4.搭建git服务器
可以参考网站:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000

posted @ 2016-06-05 21:18  系统运维  阅读(337)  评论(0编辑  收藏  举报