Git 联机版注册使用

简介:

之前研究了 Git 单机版 ( 单兵作战 ),今天来研究一下 Git 联机版 ( 团队协作 )!

GitHub 是一个开源的代码托管平台,可以分享自己的代码到该平台上,让大家参与开发或供大家使用,等。( 也可以搭建自己的 Git 仓库,相关产品:gitlab )

一、GitHub ( 使用开源的代码托管仓库 )

1、创建 GitHub 账号:https://github.com/

## 创建好用户后,点击右上角 -> Settings -> Emails 这里需要验证邮件地址 ( 建议不要写网易旗下的邮箱地址,如:163 / 126 否则你会崩溃的,收不到 github 发送的验证邮件 )

2、打开 Sehll / git Shell 生成公钥、私钥 ( Linux / Windows )

3、登陆 GitHub 点击 Settings -> SSH keys -> Add an SSH key ( 有了 key 就能证明你就是你了~ ,可以添加多个 key )

输入 Title ( 任意 )
输入 公钥 ( id_rsa.pub 中的内容 )

-> Add key

4、这样就在 GitHub 安好家了,GitHub 上的内容默认公开,别人可读。

5、登陆 GitHub 点击右上角的 '+' 号 -> New Repository 创建远程仓库

Repository name : MyTest  # 仓库名称

Description(optional) :      # 仓库描述

Public : 只能勾选这个,不花钱的情况下

> Create repository

## 现在你可以看到创建好的一个空仓库,上面有提示可以这么、那么操作!

二、与远程仓库交互

1、git 配置

2、创建本地库

shell > mkdir -p /git/MyTest
shell > git init                                        # 初始化 git 仓库
Initialized empty Git repository in /git/MyTest/.git/

shell > echo "This is a test repository" > README.md    # 生成测试文件,并提交到本地库
shell > git add README.md
shell > git commit README.md -m 'first commit'

3、关联远程仓库、推送本地仓库

## 现在去 GitHub 就可以看到 MyTest 仓库中有 README.md 文件了

注意:第一次执行时,会有如下提示,属正常

The authenticity of host 'github.com (192.30.252.128)' 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.128' (RSA) to the list of known hosts.

shell > cp /script/20150902/Student_management_system.py . # 拷贝一个文件来本地仓库,并提交
shell > git add Student_management_system.py
shell > git commit Student_management_system.py -m 'Second submission'
[master a946cf0] Second submission
1 files changed, 124 insertions(+), 0 deletions(-)
create mode 100644 Student_management_system.py

## 现在远程仓库中也有了新的文件了~

4、从远程仓库克隆

## 登陆 GitHub 创建远程仓库

Repository name : GithubTest  # 仓库名称

Description(optional) :              # 仓库描述

Public : 只能勾选这个,不花钱的情况下

Initialize this repository with a README : 勾选,自动生成 README.md

> Create repository

## 现在就创建了一个远程仓库,并生成了 README.md 文件

shell > cd GithubTest/ ; ls # README.md 文件已经克隆到了本地
README.md

shell > 修改 README.md 文件,添加一行 Local change

shell > git add README.md
shell > git commit README.md -m 'First submission'
shell > git push origin master

## 将修改推送到远程仓库

三、分支管理

shell > git branch # 查看本地分支
* master

shell > git checkout -b dev # 新建、并切换分支 ( git branch dev # 新建分支 git checkout dev # 切换分支 )
Switched to a new branch 'dev'

shell > git branch # 再次查看分支,前面有 * 的代表当前分支
* dev
master

shell > echo 'This is a testing' > T.txt # 新建一个测试文件,并提交
shell > git add T.txt
shell > git commit T.txt -m 'dev commit'
[dev 59b4093] dev commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 T.txt

shell > git checkout master # 切换回主分支
Switched to branch 'master'

shell > git branch # 可以看到当前分支是 master,现在是看不到刚才在 dev 分支创建的 T.txt 文件的,因为还没有合并分支
dev
* master

shell > git merge dev # 将 dev 分支合并到当前分支,也就是 master (主) 分支
Updating a946cf0..59b4093
Fast-forward
T.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 T.txt

shell > git branch -d dev # 删除 dev 分支,也可以不删
Deleted branch dev (was 59b4093).

shell > git branch # 仓库中再次只剩下主分支了
* master

## 如果合并分支有冲突的话,要自己手动编辑文件,之后重新提交 ( git add File.txt , git commit -m 'commit' )
## 如果有别的分支修改文件,没有合并前不要在 master 分支修改文件,否则会合并冲突
## 查看合并日志:git log --graph --pretty=oneline --abbrev-commit

## 合并分支的时候:git merge --no-ff -m "merge with no-ff" dev ,采用 --no-ff 参数,可以在删除分支后也能查到合并日志

Git 教程系列文章: 

Git 的详细介绍请点这里
Git 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-03/129651.htm
今日推荐