6. 使用github
6.1 创建github仓库
(1) 注册 github 账户,登录后,点击"New repository"
- *.py
- *.pyc:未被跟踪或管理的文件
6.2 添加ssh账户
(1) 点击账户头像后下拉三角,选择"settings"
如果某台机器要与github上的仓库交互,那么就要把这台机器的ssh公钥添加到这个 github 账户上
点击'SSH and GPG keys',添加ssh公钥
(2) 在ubuntu的命令行中,回到用户的主目录下,编辑文件.gitconfig,修改某台机器的git配置
[root@iZq6rpcx9fo0pxZ ~]# cd
[root@iZq6rpcx9fo0pxZ ~]# vi .gitconfig
(3) 修改为注册github的邮箱,填写用户名
[user]
email = 2859626066@qq.com
name = warmsirius
~
~
(4) 使用如下命令生成ssh密钥
ssh-keygen -t rss -C '邮箱地址'
[root@iZq6rpcx9fo0pxZ ~]# ssh-keygen -t rsa -C '2950626066@qq.com'
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VeymuoC8D2luHpoU7Vlelhg9Rik93iVDTeoadkodneQ 2950626066@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| ..o.++ |
| .o+ o*+. |
| .o+o+=E |
| . +.*..o |
| . . o S +o |
| + * = =. |
| . X o o. |
| . =.+ .. |
| oo+.. .. |
+----[SHA256]-----+
(5) 进入 /root/.ssh/(具体目录根据上一步骤的目录) ,查看文件
[root@iZq6rpcx9fo0pxZ ~]# cd /root/.ssh
[root@iZq6rpcx9fo0pxZ .ssh]# ls
authorized_keys id_rsa id_rsa.pub
[root@iZq6rpcx9fo0pxZ .ssh]# cat id_rsa.pub
- id_rsa:私钥文件
- id_rsa.pub:公钥文件
(6) 回到浏览器,粘贴公钥文件的内容即可
6.3 克隆项目
(1) 在浏览器中点击进入github首页,再进入项目仓库页面

(2) 复制git地址

(3) 克隆出错
执行下面语句
eval "$(ssh-agent -s)"
ssh-add
(4) 再去克隆仓库
6.4 上传分支
(1) 在项目克隆到本地之后,执行如下命令创建分支 warmsirius
[root@iZq6rpcx9fo0pxZ blog]# git checkout -b warmsirius
切换到一个新分支 'warmsirius'
[root@iZq6rpcx9fo0pxZ blog]# git branch
master
* warmsirius
(2) 创建一个views.py,并提交一个版本
[root@iZq6rpcx9fo0pxZ blog]# vi views.py
[root@iZq6rpcx9fo0pxZ blog]# git status
# 位于分支 warmsirius
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# views.py
# "\357\274\201"
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@iZq6rpcx9fo0pxZ blog]# git add views.py
[root@iZq6rpcx9fo0pxZ blog]# git commit -m '创建index视图'
[warmsirius 9473343] 创建index视图
1 file changed, 4 insertions(+)
create mode 100644 views.py
(3) 推送前 github 上文件列表如下:

(4) 推送前 github 分支列表如下:

(5) 推送分支,就是把该分支上的所有本地提交推送到远程库,推送时要指定本地分支,这样,git就会把该分支推送到远程库对应的远程分支上
git push origin 分支名称
[root@iZq6rpcx9fo0pxZ blog]# git push origin warmsirius
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 401 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'warmsirius' on GitHub by visiting:
remote: https://github.com/warmsirius/blog/pull/new/warmsirius
remote:
To git@github.com:warmsirius/blog.git
* [new branch] warmsirius -> warmsirius
(6) 查看推送分支


6.5 将本地分支跟踪服务器分支
git branch --set-upstream-to=origin/远程分支名 本地分支名称
# 例如
git branch --set-upstream-to=origin/warmsirius warmsirius
[root@iZq6rpcx9fo0pxZ blog]# git branch --set-upstream-to=origin/warmsirius warmsirius
分支 warmsirius 设置为跟踪来自 origin 的远程分支 warmsirius。
[root@iZq6rpcx9fo0pxZ blog]# git status
# 位于分支 warmsirius
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# "\357\274\201"
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
现在在blog下面修改views.py文件
[root@iZq6rpcx9fo0pxZ blog]# vi views.py
[root@iZq6rpcx9fo0pxZ blog]# git add views.py
[root@iZq6rpcx9fo0pxZ blog]# git commit -m '创建视图'
[root@iZq6rpcx9fo0pxZ blog]# git status
# 位于分支 warmsirius
# 您的分支领先 'origin/warmsirius' 共 1 个提交。
# (使用 "git push" 来发布您的本地提交)
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# "\357\274\201"
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
如果跟踪分之后,提交的直接git push即可
git push
[root@iZq6rpcx9fo0pxZ blog]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:
git config --global push.default matching
若要不再显示本信息并从现在开始采用新的使用习惯,设置:
git config --global push.default simple
参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 373 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:warmsirius/blog.git
9473343..b68a959 warmsirius -> warmsirius
6.6 从远程分支上拉取代码
git pull origin 分支名称
[root@iZq6rpcx9fo0pxZ blog]# git pull origin warmsirius
来自 github.com:warmsirius/blog
* branch warmsirius -> FETCH_HEAD
Already up-to-date.
[root@iZq6rpcx9fo0pxZ blog]# git status
# 位于分支 warmsirius
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# "\357\274\201"
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
网友评论