歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Git 聯機版注冊使用

Git 聯機版注冊使用

日期:2017/2/28 13:53:39   编辑:Linux教程

簡介:

之前研究了 Git 單機版 ( 單兵作戰 ),今天來研究一下 Git 聯機版 ( 團隊協作 )!

GitHub 是一個開源的代碼托管平台,可以分享自己的代碼到該平台上,讓大家參與開發或供大家使用,等。( 也可以搭建自己的 Git 倉庫,相關產品:gitlab )

Git 單機版安裝使用 http://www.linuxidc.com/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 "[email protected]" # 全部默認回車

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 '[email protected]'
shell > git config --global color.ui true

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、關聯遠程倉庫、推送本地倉庫

shell > git remote add origin [email protected]: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 [email protected]:linuxidc/MyTest.git
* [new branch] master -> master
Branch 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)? 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

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 [email protected]:linuxidc/MyTest.git
5fdb1c4..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 [email protected]: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 0
Receiving objects: 100% (3/3), done.

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 clone [email protected]: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 # 再次查看分支,前面有 * 的代表當前分支
* 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

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 [email protected]:linuxidc/MyTest.git
a946cf0..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.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

Ubuntu下Git服務器的搭建與使用指南 http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的詳細介紹:請點這裡
Git 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved