GitHub一个提供Git仓库服务的网站。
在使用GitHub的服务之前,需要注册相应的账号。
本地的Git和GitHub的连接,传输可以使用ssh
和https
进行,本文以ssh
为例。
创建SSH秘钥,连接GitHub
Step 1
在主目录中查看有没有./ssh
文件夹,若没有该文件夹,打开Terminal,输入一下命令。
ssh-keygen -t rsa -C "email_name@email.com"
leeguo@leeguo-PC:~/.ssh$ ls
id_rsa id_rsa.pub
- id_rsa:私钥,不要泄露出去
- id_rsa.pub:公钥,里面的内容一会需要使用
Step 2
登录GitHub,setting
=>SSH and GPG keys
=>New SSH key
,输入Title
,把id_rsa.pub
中的内容复制到Key中,点击Add SSH Key。这样就完成了。
一个GitHub可以添加多个Key,也就以为这可以使用多个电脑进行推送。
添加远程库
在上一个文章,Git基本教程中在本地创建了一个Git仓库,现在在GitHub创建一个仓库,将两个仓库进行同步,这样文件就不会丢失了。
同样是登录GitHub,点击New Repository,创建一个新的仓库。
在Repository name中写入一个名字(我写的是learngit),其他的可以保持默认,Create Repository,创建完成。
现在GitHub上创建的仓库是空的,现在可以从这个空的仓库中克隆(clone)出新的仓库,也可以把本地的仓库与其相关联,也就是把本地仓库的内容推送到的GitHub上去,具体的方法如下。
在本地的仓库下(工作区)输入以下的指令:
leeguo@leeguo-PC:~/Nutstore/Code$ git remote add origin git@github.com:idealcitier/learngit.git
上面的idealcitier就是自己GitHub的名字,origin可以认为是一个指令(远程库)。
推送到远程库
下面把本地的内容推送到的GitHub上去,第一次会出现这样的情况,输入yes
就可以了
leeguo@leeguo-PC:~/Nutstore/Code$ git push -u origin master
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:********************
Are you sure you want to continue connecting (yes/no)? Y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,*********' (RSA) to the list of known hosts.
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (19/19), 1.47 KiB | 0 bytes/s, done.
Total 19 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To git@github.com:idealcitier/learngit.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
把本地库的内容推送到的GitHub上,使用git push
,实际上就是把当前的分支master
推送到的GitHub。
由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
总的来说,使用命令git push -u origin master第一次推送master分支的所有内容;在以后每次的本地提交,使用git push origin master
推送最近的修改。
从远程库克隆(clone)
克隆一个远程的仓库(GitHub),使用git clone
命令。
例如使用git clone
去clone一个大佬的项目。
leeguo@leeguo-PC:~/Nutstore/Code$ git clone git@github.com:mikepenz/MaterialDrawer.git
Cloning into 'MaterialDrawer'...
Warning: Permanently added the RSA host key for IP address '********' to the list of known hosts.
remote: Counting objects: 18576, done.
remote: Total 18576 (delta 0), reused 0 (delta 0), pack-reused 18576
Receiving objects: 100% (18576/18576), 19.85 MiB | 1.12 MiB/s, done.
Resolving deltas: 100% (10161/10161), done.
Checking connectivity... done.
项目下载在使用指令的目录下的,这个要注意。
分支管理
创建分支
利用git checkout -b new_branch
。
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
* master
leeguo@leeguo-PC:~/Nutstore/Code$ git checkout -b new_branch
Switched to a new branch 'new_branch'
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
master
* new_branch
利用git branch
查看当前的分支,当前的分支前面有一个*
。
切换分支
在分支之间切换,git checkout branch_name
leeguo@leeguo-PC:~/Nutstore/Code$ git checkout new_branch
Switched to branch 'new_branch'
leeguo@leeguo-PC:~/Nutstore/Code$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
合并分支
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
master
* new_branch
leeguo@leeguo-PC:~/Nutstore/Code$ vim readme.txt
leeguo@leeguo-PC:~/Nutstore/Code$ git add readme.txt
leeguo@leeguo-PC:~/Nutstore/Code$ git commit -m "add a branch"
[new_branch 83ac9ac] add a branch
1 file changed, 1 insertion(+)
通过上面的命令,在新的分支下,对文件的进行了修改。在两个分支下,readme.txt
文件是不同的。
现在想把new_branch分支中的内容合并到master分支上。可以利用git merge
命令,合并某分支到当前的分支。
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
* master
new_branch
leeguo@leeguo-PC:~/Nutstore/Code$ git merge new_branch
Updating 9eb57a6..83ac9ac
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
删除分支
在合并完分支之后,或这个分支没有用的时候,将分支删除。
删除分支的方法,git branch -d new_branch
。
leeguo@leeguo-PC:~/Nutstore/Code$ git branch -d new_branch
Deleted branch new_branch (was 83ac9ac).
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
* master
推送分支
把分支上的所有内容提交到GitHub。推送时要指定本地的分支,这样Git就可以把该分支推送到GitHub上去了。
例如,推送主分支使用:
leeguo@leeguo-PC:~/Nutstore/Code$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:idealcitier/learngit.git
9eb57a6..83ac9ac master -> master
推送某个非主分支,使用:
leeguo@leeguo-PC:~/Nutstore/Code$ git push origin new_branch
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 317 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:idealcitier/learngit.git
* [new branch] new_branch -> new_branch
创建标签
在发布一个版本,通常会给版本库添加一个标签tag
。
给Git打标签十分的简单,首先切换到的打标签的分支上,使用git tag tag_name
就可以添加一个标签。
leeguo@leeguo-PC:~/Nutstore/Code$ git branch
master
* new_branch
leeguo@leeguo-PC:~/Nutstore/Code$ git tag v1.0
可以使用git tag
查看所有的标签。
leeguo@leeguo-PC:~/Nutstore/Code$ git tag
v1.0
默认标签是打在最新的commit上的,如果忘记打标签,可以的找对应的commit id,运行相应的命令。
leeguo@leeguo-PC:~/Nutstore/Code$ git log --pretty=oneline --abbrev-commit
83ac9ac add a branch
9eb57a6 say something
3bb6dbc add a line twice and create a file
b4285df add a line again
700118e add a line
e3c4b85 add distributed
3cae6f8 wrote a readme file
leeguo@leeguo-PC:~/Nutstore/Code$ git tag v0.9 9eb57a6
leeguo@leeguo-PC:~/Nutstore/Code$ git tag
v0.9
v1.0
使用命令git show tag_name
查看标签的信息。
leeguo@leeguo-PC:~/Nutstore/Code$ git show v0.9
commit 9eb57a617518c315a3c9ab726cf1c6afe3cbc0e7
Author: leeguo <leeguo@outlook.com>
Date: Fri Oct 20 23:05:19 2017 +0800
say something
diff --git a/readme.txt b/readme.txt
index 43f0f36..c952b2f 100644
--- a/readme.txt
+++ b/readme.txt
@@ -3,3 +3,4 @@ Git is free sofeware.
Git is useful.
Git is best tool.
I don't know what to say.
+Say something.
不仅仅可以添加标签,也可以添加带有说明的标签,例如
leeguo@leeguo-PC:~/Nutstore/Code$ git log --pretty=oneline --abbrev-commit
83ac9ac add a branch
9eb57a6 say something
3bb6dbc add a line twice and create a file
b4285df add a line again
700118e add a line
e3c4b85 add distributed
3cae6f8 wrote a readme file
leeguo@leeguo-PC:~/Nutstore/Code$ git tag -a v0.8 -m "version 0.8" 3bb6dbc
leeguo@leeguo-PC:~/Nutstore/Code$ git show v0.8
tag v0.8
Tagger: leeguo <liguo7@foxmail.com>
Date: Sat Oct 21 16:29:00 2017 +0800
version 0.8
commit 3bb6dbcd977669afdcae2897aa983ca58e8955f5
Author: leeguo <leeguo@outlook.com>
Date: Fri Oct 20 22:29:00 2017 +0800
add a line twice and create a file
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
diff --git a/readme.txt b/readme.txt
index 8f9a33e..43f0f36 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,3 +2,4 @@ Git is a distribution version control system.
Git is free sofeware.
Git is useful.
Git is best tool.
其中git tag -a v0.8 -m "version 0.8" 3bb6dbc
中,-a
后面是标签名,-m
指定说明的文字。
标签的操作
标签可以进行删除:
leeguo@leeguo-PC:~/Nutstore/Code$ git tag
v0.8
v0.9
v1.0
leeguo@leeguo-PC:~/Nutstore/Code$ git tag -d v0.8
Deleted tag 'v0.8' (was 2c48ae5)
leeguo@leeguo-PC:~/Nutstore/Code$ git tag
v0.9
v1.0
标签同样可以推送到GitHub:
leeguo@leeguo-PC:~/Nutstore/Code$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:idealcitier/learngit.git
* [new tag] v1.0 -> v1.0
一次性推送所有的tag到GitHub:
leeguo@leeguo-PC:~/Nutstore/Code$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:idealcitier/learngit.git
* [new tag] v0.9 -> v0.9
如果标签推送到了GitHub,现在想把标签(tag)删除,先在本地把本地的标签删除。
leeguo@leeguo-PC:~/Nutstore/Code$ git tag -d v0.9
Deleted tag 'v0.9' (was 9eb57a6)
在进行远程的删除,删除的命令仍是使用push
,具体的操作如下。
leeguo@leeguo-PC:~/Nutstore/Code$ git push origin :refs/tags/v0.9
To git@github.com:idealcitier/learngit.git
- [deleted] v0.9