Git 联机版注册使用

简介:

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

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

Git 单机版安装使用 http://www.68idc.cn/Linux/2016-03/129648.htm

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

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

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

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

shell > ssh-keygen -t rsa -C “linuxidc@foxmail.com” # 全部默认回车

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

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

-> Add key

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

## Github 地址:https://github.com/linuxidc

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

Repository name : MyTest # 仓库名称

Description(optional) : # 仓库描述

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

> Create repository

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

二、与远程仓库交互

1、git 配置

shell > git config –global user.name ‘linuxidc’shell > git config –global user.email ‘linuxidc@foxmail.com’shell > git config –global color.ui true

2、创建本地库

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

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

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

shell > git remote add origin git@github.com:linuxidc/MyTest.git # 本地仓库跟远程仓库关联shell > git push -u origin master # 将本地仓库推送到远程仓库,第一次推送时要加 -u 参数Counting objects: 3, done.Writing objects: 100% (3/3), 237 bytes, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:linuxidc/MyTest.git* [new branch] master -> masterBranch master set up to track remote branch master from origin.

## 现在去 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)? yesWarning: 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.pyshell > git commit Student_management_system.py -m ‘Second submission'[master a946cf0] Second submission1 files changed, 124 insertions(+), 0 deletions(-)create mode 100644 Student_management_system.py

shell > git remote origin master # 将最新的修改推送到远程仓库Warning: Permanently added the RSA host key for IP address ‘192.30.252.129’ to the list of known hosts.Counting objects: 4, done.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 1.33 KiB, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:linuxidc/MyTest.git5fdb1c4..a946cf0 master -> master

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

4、从远程仓库克隆

## 登陆 GitHub 创建远程仓库

Repository name : GithubTest # 仓库名称

Description(optional) : # 仓库描述

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

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

> Create repository

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

shell > cd /git/shell > git clone git@github.com:linuxidc/GithubTest.git # 从远程仓库克隆一份到本地Initialized empty Git repository in /git/GithubTest/.git/remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0Receiving objects: 100% (3/3), done.

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

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

shell > git add README.mdshell > git commit README.md -m ‘First submission’shell > git push origin master

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

三、分支管理

shell > git clone git@github.com:linuxidc/MyTest.git # 克隆一个远程库到本地

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

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

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

shell > echo ‘This is a testing’ > T.txt # 新建一个测试文件,并提交shell > git add T.txtshell > git commit T.txt -m ‘dev commit'[dev 59b4093] dev commit1 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..59b4093Fast-forwardT.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

shell > git push origin master # 将修改推送到远程仓库Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 279 bytes, done.Total 3 (delta 1), reused 0 (delta 0)To git@github.com:linuxidc/MyTest.gita946cf0..59b4093 master -> 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 教程系列文章:

GitHub 使用教程图文详解 http://www.68idc.cn/Linux/2014-09/106230.htm

Git 标签管理详解 http://www.68idc.cn/Linux/2014-09/106231.htm

Git 分支管理详解 http://www.68idc.cn/Linux/2014-09/106232.htm

Git 远程仓库详解 http://www.68idc.cn/Linux/2014-09/106233.htm

Git 本地仓库(Repository)详解 http://www.68idc.cn/Linux/2014-09/106234.htm

Git 服务器搭建与客户端安装 http://www.68idc.cn/Linux/2014-05/101830.htm

Git 概述 http://www.68idc.cn/Linux/2014-05/101829.htm

分享实用的GitHub 使用教程 http://www.68idc.cn/Linux/2014-04/100556.htm

Ubuntu下Git服务器的搭建与使用指南 http://www.68idc.cn/Linux/2015-07/120617.htm

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

沿途跟着一条河,你看着它在晨光暮霭中变换着色彩,

Git 联机版注册使用

相关文章:

你感兴趣的文章:

标签云: