目录
GitHub简介
使用GitHub,可以借助GitHub来托管我们的项目,GitHub官网:github.com
基本概念
- 仓库(reposiory):项目代码就放在仓库里面,在github上面就叫做仓库 ,那就必须要新建一个repository,如果你的开源项目多了,就拥有多个repositories,仓库用来存放项目代码,每个项目对应一个仓库,多个开源项目则有多个仓库
- 收藏(Star) :仓库主页star按钮,意思为收藏项目的人数,在github上如果你有一个项目过的100个star都算很不容易了,收藏项目,可以方便下次查看
- 复制克隆项目(Fork) :会把别人的仓库完完整整的复制一份,如果点击了fork,那么GitHub的主页就多了一个项目,可以随心所欲的去修改,并且不会影响到原来的项目
- 发起请求(Pull request):在fork的基础上,修改了别人的代码,可以点击pull request,pull request是一种通知机制,通知原来的作者,希望他合并你的修改,这就是 Pull Request
- 关注(Watch):到别人的仓库,关注后,仓库做了什么动作可以知道 ,即关注项目,当项目更新可以接收到通知
- 事物卡片(Issue):发现代码BUG,但是目前没有成型的代码,需要讨论时用,意思就是,你开源了一个项目,别人发现你项目中有BUG,或者哪些地方做得不够好,他就可以给你提lssue,多个Issue就是Issues
创建仓库
首先需要登陆点击页面右上角的sign in,如果没有账号可以注册一个账号,点击页面右上角的sign up,进行GitHub主页,点击Creat repository创建一个仓库
其中.gitignore文件用于在提交项目文件时过滤一些不需要的文件,根据注释填写完成后,点击Creat repository,一个仓库就创建成功,这个操作相当于在git的中进行了一次commit
将gitignore进行一次修改
修改完成然后将页面向下拉,可以看到Commit changes,点击之后相当于在git中又完成了一次提交
添加ssh账户
如果某台机器需要与github上的仓库交互,那么就要把这台机器的ssh公钥添加到这个github账户上
点击账户头像的下拉三角,选择Settings
选择SSH and GPG keys,点击New SSH key
在ubuntu的命令行中,回到用户的主目录下,编辑文件.gitconfig,修改某台及其的git配置,修为注册github时的邮箱,填写用户名
xm@ubuntu:~$ vi .gitconfig
xm@ubuntu:~$ cat .gitconfig
[user]
email = 1006962425@qq.com
name = Xiaoma
执行ssh-keygen -t rsa -C "邮箱地址"生成ssh密钥,默认回车,进入.ssh目录,可以看到id_rsa.pub(公钥),id_rsa(私钥),查看公钥中的内容
xm@ubuntu:~$ ssh-keygen -t rsa -C "1006962425@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xm/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/xm/.ssh/id_rsa.
Your public key has been saved in /home/xm/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:d2nwVSN1K2raKZMAFfJ7nGQOWhtDN+J4uaAq2zG5O9k 1006962425@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| . =.o ..oo|
| B + . ..+|
| + X o. ... |
| . * % .o.o. |
| . . =S=.o= |
| .. o.=o. |
|..+o = o |
|.oo+E o |
|. +o |
+----[SHA256]-----+
xm@ubuntu:~/.ssh$ cd .ssh/
xm@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub known_hosts
将公钥的内容填到Key,自拟标题,然后点击Add SSH key
点击GitHub头像,然后点击我们的test仓库
点击code,然后点击SSH,复制链接
在命令行中复制仓库中的内容,如果克隆出错,执行eval "$(ssh-agent -s)"和ssh-add
xm@ubuntu:~/xm-git/test$ git clone git@github.com:Keine2425/test.git
Cloning into 'test'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), done.
Resolving deltas: 100% (1/1), done.
进入test,可以看到.git,GitHub创建仓库实质是进行了git init,执行git log可以看到之前提交的记录
xm@ubuntu:~/xm-git/test$ cd test/
xm@ubuntu:~/xm-git/test/test$ ls
README.md
xm@ubuntu:~/xm-git/test/test$ ls -al
total 20
drwxr-xr-x 3 xm xm 4096 Apr 7 05:28 .
drwxr-xr-x 3 xm xm 4096 Apr 7 05:27 ..
drwxr-xr-x 8 xm xm 4096 Apr 7 05:28 .git
-rw-r--r-- 1 xm xm 57 Apr 7 05:28 .gitignore
-rw-r--r-- 1 xm xm 20 Apr 7 05:28 README.md
xm@ubuntu:~/xm-git/test/test$ git log
commit e3c2f0f55cc77d3745fa6aee9dc42ddd30908b15 (HEAD -> main, origin/main, origin/HEAD)
Author: Keine2425 <70189906+Keine2425@users.noreply.github.com>
Date: Wed Apr 7 19:52:36 2021 +0800
Update .gitignore
commit 2ea3818652f7c23f0c8175a67ec94fd05bdbedf2
Author: Keine2425 <70189906+Keine2425@users.noreply.github.com>
Date: Wed Apr 7 19:48:33 2021 +0800
Initial commit
上传分支
项目克隆到本地之后,执行如下命令创建分支smart,创建一个code.txt,并提交一个版本
xm@ubuntu:~/xm-git/test/test$ git checkout -b smart
Switched to a new branch 'smart'
xm@ubuntu:~/xm-git/test/test$ git branch
main
* smart
xm@ubuntu:~/xm-git/test/test$ vi code.txt
xm@ubuntu:~/xm-git/test/test$ cat code.txt
creat one line
xm@ubuntu:~/xm-git/test/test$ git add code.txt
xm@ubuntu:~/xm-git/test/test$ git commit -m 'verson 1'
[smart 28c4438] verson 1
1 file changed, 1 insertion(+)
create mode 100644 code.txt
点击branch可以看到推送前
推送前:
推送分支,就是把该分支上的所有本地提交推送到远程库,推送时要指定本地分支,这样,git就会把该分支推送到远程库对应的远程分支上,执行命令git push origin 分支名称
xm@ubuntu:~/xm-git/test/test$ git push origin smart
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'smart' on GitHub by visiting:
remote: https://github.com/Keine2425/test/pull/new/smart
remote:
To github.com:Keine2425/test.git
* [new branch] smart -> smart
再去GitHub网站查看分支,会发现出现了smart分支
本地分支跟踪服务器分支
git branch --set-upstream-to=origin/远程分支名称 本地分支名称
xm@ubuntu:~/xm-git/test/test$ git branch --set-upstream-to=origin/smart smart
Branch 'smart' set up to track remote branch 'smart' from 'origin'.
xm@ubuntu:~/xm-git/test/test$ git status
On branch smart
Your branch is up to date with 'origin/smart'.
nothing to commit, working tree clean
修改code.txt并用git status查看状态,会提示我们与远程分支有歧义,提交之后,用git push推送,由于跟踪的状态所以不需要写分支名称
xm@ubuntu:~/xm-git/test/test$ vi code.txt
xm@ubuntu:~/xm-git/test/test$ cat code.txt
creat one line
creat new line
xm@ubuntu:~/xm-git/test/test$ git status
On branch smart
Your branch is up to date with 'origin/smart'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: code.txt
no changes added to commit (use "git add" and/or "git commit -a")
xm@ubuntu:~/xm-git/test/test$ git add code.txt
xm@ubuntu:~/xm-git/test/test$ git commit -m 'verson 2'
[smart 8043f94] verson 2
1 file changed, 1 insertion(+)
xm@ubuntu:~/xm-git/test/test$ git status
On branch smart
Your branch is ahead of 'origin/smart' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
xm@ubuntu:~/xm-git/test/test$ git push
之后可以在GitHub上看到我们的版本2
从远程分支上拉取代码
git pull orgin 分支名称,使用该命令会把远程分支smart上的代码下载并合并到本地所在分支
xm@ubuntu:~/xm-git/test/test$ git pull origin smart
From github.com:Keine2425/test
* branch smart -> FETCH_HEAD
Already up to date.
xm@ubuntu:~/xm-git/test/test$
题外话
工作中使用git
项目经理
- 项目经理搭建项目的框架
- 搭建完项目框架之后,项目经理把项目框架代码放到服务器
普通员工
- 在自己的电脑上,生成ssh公钥,然后把公钥给项目经理,项目经理把它添加的服务器上面
- 项目经理会给每个组员的项目代码的地址,组员把代码下载到自己的电脑上
- 创建本地的分支dev,在dev分支中进行每天的开发
- 每一个员工开发完自己的代码之后,都需要将代码发布远程的dev分支上