github官方网站:https://github.com
需要创建并注册一个账号并登录
创建项目仓库
配置本地环境与github进行交互
配置以下操作等同于修改~/.gitconfig文件,会显示在git log里面信息
[root@wcy ~]# git config --global user.name "wcy" [root@wcy ~]# git config --global user.email "****@163.com"
生成git公私钥
[root@wcy git]# ssh-keygen -t rsa -C "***@163.com"
拷贝公钥内容到github上
[root@wcy git]# cd ~/.ssh/
[root@wcy .ssh]# ls
id_rsa id_rsa.pub
[root@wcy .ssh]# cat id_rsa.pub
···················
添加
测试一下,看到下面消息则为成功
[root@wcy .ssh]# ssh -T git@github.com The authenticity of host 'github.com (13.229.188.59)' 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,13.229.188.59' (RSA) to the list of known hosts. Hi Chuyio! You've successfully authenticated, but GitHub does not provide shell access.
克隆项目到本地
[root@Check1 ~]# mkdir /usr/local/git/debug [root@Check1 ~]# cd /usr/local/git/debug/ [root@Check1 debug]# git clone git@github.com:Chuyio/debug.git Initialized empty Git repository in /usr/local/git/debug/debug/.git/ Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts. remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. [root@Check1 debug]# ls debug
克隆出错
克隆的时候可能会出错
出错则执行下
eval "$(ssh-agent -s)" ssh-add
执行后再重新克隆项目
项目克隆到本地后,创建一个文件,进行编辑,提交
[root@Check1 debug]# git log commit d883a4392110de7b98c332f392e147b1b18e5f9c Author: Chuyio <goodmoodwjl@163.com> Date: Tue Dec 18 10:25:26 2018 +0800 Initial commit [root@Check1 debug]# git branch * master [root@Check1 debug]# git checkout -b wcy Switched to a new branch 'wcy' [root@Check1 debug]# git branch master * wcy [root@Check1 debug]# vim wcy.sh [root@Check1 debug]# git status # On branch wcy # Untracked files: # (use "git add <file>..." to include in what will be committed) # # wcy.sh nothing added to commit but untracked files present (use "git add" to track) [root@Check1 debug]# git add wcy.sh [root@Check1 debug]# git commit -m "创建wcy.sh主脚本文件" [wcy 4bf120d] 创建wcy.sh主脚本文件 1 files changed, 542 insertions(+), 0 deletions(-) create mode 100644 wcy.sh [root@Check1 debug]# git log commit 4bf120df209803e62272205bd1a5407650798bda Author: wcy <goodmoodwjl@163.com> Date: Tue Dec 18 20:55:15 2018 +0800 创建wcy.sh主脚本文件 commit d883a4392110de7b98c332f392e147b1b18e5f9c Author: Chuyio <goodmoodwjl@163.com> Date: Tue Dec 18 10:25:26 2018 +0800 Initial commit
推送分支
将本地的分支推送到github上
使用git push origin 分支名称即可
push --推送
origin --代表远程(这里指的是github)
[root@Check1 debug]# git push origin wcy Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts. Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 4.63 KiB, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'wcy' on GitHub by visiting: remote: https://github.com/Chuyio/debug/pull/new/wcy remote: To git@github.com:Chuyio/debug.git * [new branch] wcy -> wcy
本地的分支跟踪远程的某一个分支
本地的分支提交和远程分支不一致的话git会给出一些提示
[root@Check1 ConfigLinux]# git branch -vv #查看本地分支与远程分支的映射关系 master beda7c3 [origin/master] Initial commit * wcy 24e5350 创建主脚本setup.sh [root@Check1 ConfigLinux]# git branch --set-upstream wcy origin/wcy Branch wcy set up to track remote branch wcy from origin. [root@Check1 ConfigLinux]# git branch -vv master beda7c3 [origin/master] Initial commit * wcy 24e5350 [origin/wcy] 创建主脚本setup.sh
git branch --set-upstream 用法
git branch --set-upstream [本地分支] [远程主机名]/[远程分支名]
--set-upstream最新版本貌似不再支持,使用--track和--set-uptream-to来替代
和原本的--set-upstream不同的是git branch --track dev origin/dev是创建一个新分支,并关联,若本地已存在,则会报本地分支已存在的错误
git branch --track用法
git branch --track [新建分支] [远程主机名]/[远程分支名]
如果本地分支已存在,那使用--set-upstream-to来代替
git branch --set-upstream-to用法
git branch --set-upstream-to=[远程主机名]/[远程分支名] [本地分支名]
这两种方法与之前的--set-upstream都有所不同,需要注意
推送代码
跟踪远程分支后,可以直接使用git push推送到远程分支
[root@Check1 ConfigLinux]# echo "test" >> 说明.txt [root@Check1 ConfigLinux]# git add 说明.txt [root@Check1 ConfigLinux]# git commit -m "增加说明文档" [wcy affc5a0] 增加说明文档 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 shuoming.txt create mode 100644 "\350\257\264\346\230\216.txt" [root@Check1 ConfigLinux]# git push Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 346 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:Chuyio/ConfigLinux.git 24e5350..affc5a0 wcy -> wcy
拉取代码
git pull origin 分支名称
例:
git pull origin wcy
操作如下:
[root@Check1 ConfigLinux]# git pull origin wcy From github.com:Chuyio/ConfigLinux * branch wcy -> FETCH_HEAD Already up-to-date.
总结
一般使用git环境介绍
项目负责人
1、由项目负责人或项目经理搭建项目的框架架构
2、搭建好项目框架之后,项目经理或负责人把项目框架代码放到服务器上(服务器可以上github或gitlab等)
员工操作部分
1、在自己电脑上,生成ssh公钥,然后把公钥给项目经理,项目经理把它添加到服务器上面
2、项目经理会给每个组员的项目代码的地址,组员把代码下载到自己的电脑上
3、创建本地分支,在本地分支中进行每天的工作开发
4、每一个员工开发完自己的代码之后都需要将代码发布到远程服务器对应的分支上
一般项目里面会出现的两个分支
Master:用于保存发布的项目代码,如版本1.0、版本2.0
Dev:用于保存开发过程中的代码。每个组员内开发完都都需要把自己的代码发布到远程的dev分支(保证写完没有BUG,再发布推送)