互联网项目托管平台github.com的使用

1.安装git客户端

# yum install -y git

配置git全局用户以及邮箱

[root@web01 ~]# git config --global user.name "jack"
[root@web01 ~]# git config --global user.email "reblue520@163.com"
[root@web01 ~]# git config --global color.ui true

2.检测git相关配置

[root@web01 ~]# git config --list
user.name=jack
user.email=reblue520@.com
color.ui=true

创建创建本地工作目录chinasoft,并初始化为git工作目录

[root@web01 ~]# mkdir chinasoft
[root@web01 ~]# cd chinasoft/
[root@web01 chinasoft]# git init
Initialized empty Git repository in /root/chinasoft/.git/
[root@web01 chinasoft]# ll -a
total
drwxr-xr-x root root Jun : .
dr-xr-x---. root root Jun : ..
drwxr-xr-x root root Jun : .git

3.创建index.html文件(模拟代码提交)

[root@web01 chinasoft]# touch index.html
[root@web01 chinasoft]# echo "1.chinasoft itpart" > index.html
[root@web01 chinasoft]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# index.html
nothing added to commit but untracked files present (use "git add" to track)

4.提示使用git add添加文件至暂存区

[root@web01 chinasoft]# git add index.html
[root@web01 chinasoft]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
#

5.使用git cmmit提交暂存区文件至git版本仓库 -m:提交描述信息(提交至远程需添加远程仓库)

# 提交

[root@web01 chinasoft]# git commit -m "first commit"
[master (root-commit) c0c2c39] first commit
file changed, insertion(+)
create mode index.html
[root@web01 chinasoft]# git status
# On branch master
nothing to commit, working directory clean

6.模拟版本的回退功能

添加一个deploy.sh文件

[root@web01 chinasoft]# git add deploy.sh
[root@web01 chinasoft]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: deploy.sh
#
[root@web01 chinasoft]# git commit -m "2th deploy.sh"
[master c4cb609] 2th deploy.sh
file changed, insertions(+)
create mode deploy.sh

再次编辑index.html

[root@web01 chinasoft]# cat index.html
.chinasoft itpart
.add deploy.sh

# 查看变更历史记录

[root@web01 chinasoft]# git log
commit e18b9e0d4f545bcfb47a2d890f7293057240d5ac
Author: jack <reblue520@.com>
Date: Fri Jun :: + 2th index.html commit c4cb609fde1fd18edc10a032775f2af672abbc13
Author: jack <reblue520@.com>
Date: Fri Jun :: + 2th deploy.sh commit c0c2c393a508d0c59ff2d8934ef3b5055f267edf
Author: jack <reblue520@.com>
Date: Fri Jun :: + first commit

回退到上一个版本,可以看到最近一次index.html添加的内容2.add deploy.sh没有了

[root@web01 chinasoft]# git reset --hard HEAD^
HEAD is now at c4cb609 2th deploy.sh
[root@web01 chinasoft]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : index.html
[root@web01 chinasoft]# cat deploy.sh
#!/bin/bash echo "deploy system"
[root@web01 chinasoft]# cat index.html
.chinasoft itpart

回退方式2:
git reflog #查看未来历史更新点

[root@web01 chinasoft]# git reflog
c4cb609 HEAD@{}: reset: moving to HEAD^
e18b9e0 HEAD@{}: commit: 2th index.html
c4cb609 HEAD@{}: commit: 2th deploy.sh
c0c2c39 HEAD@{}: commit (initial): first commit

# 直接会退到e18b9e0 commit: 2th index.html这个点,可以看到又到了之前的修改状态

[root@web01 chinasoft]# git reset --hard e18b9e0
HEAD is now at e18b9e0 2th index.html
[root@web01 chinasoft]# cat deploy.sh
#!/bin/bash echo "deploy system"
[root@web01 chinasoft]# cat index.html
.chinasoft itpart
.add deploy.sh

7.项目的推送拉取及其它常用操作

7.1 首先需要在git上使用ssh key创建和github互信使用

①客户端配置生成公钥

[root@test1_voice_live chinasoft]# ssh-keygen
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:
:3b:d1:1a:da::f4::a4::b3:e9:ad:f7:6c:f8 root@test1_voice_live
The key's randomart image is:
+--[ RSA ]----+
| . |
| = . |
| = o . |
| = + |
| S * o |
| . = O |
| o B. |
| .oo. |
| .. +E |
+-----------------+

②拷贝~/.ssh/id_rsa.pub公钥到远程github.com中,如图

[root@test1_voice_live chinasoft]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPbqNyJ9QUHaYLQ70upsNJ8Z2MrOluedIahdwATM9J46iSpWJmQqMPqTlMbjI2Mr7wV0Vl0CtI8YGRu4MqezYosJyxEgpDIZY9XTFSlnpqC4D/gLNIoI9TZAUJJL/6vKm+9lZjxz3TGTpsQP9vM0cBquWk+7PsvRlTHj4gztHh+G9GCGM3ABkTlY+y+dTZk1DT9i6T0sFh7aCNcZf/zUgdmvi1JQDBkCDQAE2arncYb9x12EYoO5Np4f3RpVVaZ0i6DpIYmFaqZZ6+0XNubyYCdo82uidmc0KPvDFoKF71O0A4Wbz0Fl+yJZUpsmwlRXuBl3SlGZY0c2ffQMNBiO7h root@test1_voice_live

③测试:

[root@test1_voice_live chinasoft]# ssh -T git@github.com
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is ::ac:a5:::2d:::1b::4d:eb:df:a6:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Hi reblue520! You've successfully authenticated, but GitHub does not provide shell access.

验证通过后ssh图标会变绿

测试连接git@github.com报错,调试添加参数-v报错如下:

[root@web01 ~]# ssh -T -v git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips Feb
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line : Applying options for *
debug1: Connecting to github.com [192.168.1.13] port . # 发现是通过192.168.1.13(公司内网DNS)去连接github(内网dns)更换为公网IP服务器问题解决
debug1: Connection established.
debug1: permanently_set_uid: /
debug1: identity file /root/.ssh/id_rsa type
debug1: identity file /root/.ssh/id_rsa-cert type -
debug1: identity file /root/.ssh/id_dsa type -
debug1: identity file /root/.ssh/id_dsa-cert type -
debug1: identity file /root/.ssh/id_ecdsa type -
debug1: identity file /root/.ssh/id_ecdsa-cert type -
debug1: identity file /root/.ssh/id_ed25519 type -
debug1: identity file /root/.ssh/id_ed25519-cert type -
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.
debug1: match: OpenSSH_5. pat OpenSSH_5* compat 0x0c000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: kex: diffie-hellman-group-exchange-sha256 need= dh_need=
debug1: kex: diffie-hellman-group-exchange-sha256 need= dh_need=
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(<<) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA d4:8f:d5::9b:4f:1a::::::b7:6d:3e:
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
[root@web01 chinasoft]# git remote add origin git@github.com:reblue520/demo.git

# git remote rm origin # 删除

[root@web01 chinasoft]# cat .git/config
[core]
repositoryformatversion =
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:reblue520/demo.git
fetch = +refs/heads/*:refs/remotes/origin/*

7.2 拉取远程仓库

[root@test1_voice_live chinasoft]# git pull origin master
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
warning: no common commits
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Unpacking objects: % (/), done.
From github.com:reblue520/demo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LICENSE | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
files changed, insertions(+)
create mode .gitignore
create mode LICENSE

7.3 推送到远程仓库,可以看到远程https://github.com/reblue520/demo已经有了我们刚添加的文件

[root@test1_voice_live chinasoft]# git push -u origin master
Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@github.com:reblue520/demo.git
8fbed49..b724d04 master -> master
Branch master set up to track remote branch master from origin.

7.4 项目的clone

克隆demo项目

[root@test1_voice_live tmp]# git clone git@github.com:reblue520/demo.git
Cloning into 'demo'...
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), 5.17 KiB | bytes/s, done.
Resolving deltas: % (/), done.
[root@test1_voice_live tmp]# cd demo/
[root@test1_voice_live demo]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : LICENSE

7.5 创建分支dev

[root@test1_voice_live chinasoft]# git branch dev
[root@test1_voice_live chinasoft]# git checkout dev
Switched to branch 'dev'
[root@test1_voice_live chinasoft]# git branch
* dev
master

提交一个dev.txt

[root@test1_voice_live chinasoft]# vim dev.txt
[root@test1_voice_live chinasoft]# git add dev.txt
[root@test1_voice_live chinasoft]# git commit -m "add dev.txt"
[dev 031e757] add dev.txt
file changed, insertion(+)
create mode dev.txt

切换到master,这时候是没有dev.txt文件的

[root@test1_voice_live chinasoft]# git checkout master
Switched to branch 'master'
[root@test1_voice_live chinasoft]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : LICENSE

合并到master

[root@test1_voice_live chinasoft]# git checkout master
Already on 'master'
[root@test1_voice_live chinasoft]# git merge dev
Updating b724d04..031e757
Fast-forward
dev.txt | +
file changed, insertion(+)
create mode dev.txt
[root@test1_voice_live chinasoft]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : dev.txt
-rw-r--r-- root root Jun : LICENSE
[root@test1_voice_live chinasoft]# git branch
dev
* master

# 删除分支的命令

# git branch -d dev

8.关于文件冲突的模拟和处理

①创建一个分支prod,并编辑文件内容为1233333chinasoft20170624

[root@test1_voice_live chinasoft]# git checkout -b prod
Switched to a new branch 'prod'
[root@test1_voice_live chinasoft]# vim dev.txt
1233333chinasoft20170624
[root@test1_voice_live chinasoft]# git add dev.txt
[root@test1_voice_live chinasoft]# git commit -m "dev.txt"
[prod 7daacff] dev.txt
file changed, insertion(+), deletion(-)

②切换到master,编辑文件dev.txt

[root@test1_voice_live chinasoft]# git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by commits.
(use "git push" to publish your local commits)
[root@test1_voice_live chinasoft]# vim dev.txt
55555chinasoft20170624
[root@test1_voice_live chinasoft]# git add dev.txt
[root@test1_voice_live chinasoft]# git commit -m "master change"
[master 504c42a] master change
file changed, insertion(+), deletion(-)

③合并,因master和prod对同一个文件修改且内容不同无法合并,需要手动处理

[root@test1_voice_live chinasoft]# git merge prod
Auto-merging dev.txt
CONFLICT (content): Merge conflict in dev.txt
Automatic merge failed; fix conflicts and then commit the result. 可以看到冲突的内容
[root@test1_voice_live chinasoft]# cat dev.txt
<<<<<<< HEAD
55555chinasoft20170624
=======
1233333chinasoft20170624
>>>>>>> prod

④手动编辑,再次合并

[root@test1_voice_live chinasoft]# vim dev.txt
[root@test1_voice_live chinasoft]# git add dev.txt
[root@test1_voice_live chinasoft]# git commit -m "change"
[master 6bd2922] change
[root@test1_voice_live chinasoft]# git merge prod
Already up-to-date.
[root@test1_voice_live chinasoft]# cat dev.txt
1233333chinasoft20170624

9.给项目打标签

切换到master

[root@test1_voice_live chinasoft]# git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by commits.
(use "git push" to publish your local commits)

确认下是否在master上

[root@test1_voice_live chinasoft]# git branch
dev
* master
prod
test
打标签为v1.
[root@test1_voice_live chinasoft]# git tag v1.
[root@test1_voice_live chinasoft]# git tag
v1.
[root@test1_voice_live chinasoft]# git show v1.
commit 6bd2922a7a8e2ed0319505c06f52651acf92df84
Merge: 504c42a 7daacff
Author: jack <reblue520@.com>
Date: Sat Jun :: + change

把标签推送到github上

[root@test1_voice_live chinasoft]# git push origin v1.
Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
remote: Resolving deltas: % (/), completed with local object.
To git@github.com:reblue520/demo.git
* [new tag] v1. -> v1. [root@test1_voice_live chinasoft]# vim Help.md
# Help
## git help * spring
* summer
* autumn
* winter > tomcat.war # url
[chinasoft](http://www.chinasoft.com) # image
![chinasoft](https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png) # code
while True:
do echo "hello chinasoft"
done [root@test1_voice_live chinasoft]# git add Help.md
[root@test1_voice_live chinasoft]# git commit -m "add Help.md"
[detached HEAD af013e5] add Help.md
file changed, insertions(+)
create mode Help.md
[root@test1_voice_live chinasoft]# git push -u origin master
Total (delta ), reused (delta )
To git@github.com:reblue520/demo.git
b724d04..6bd2922 master -> master
Branch master set up to track remote branch master from origin.

发现没有推送到远程github上

[root@test1_voice_live chinasoft]# git branch
* (detached from v1.)
dev
master
prod
test

切换到master

[root@test1_voice_live chinasoft]# git checkout master
Warning: you are leaving commit behind, not connected to
any of your branches: af013e5 add Help.md If you want to keep them by creating a new branch, this may be a good time
to do so with: git branch new_branch_name af013e5 Switched to branch 'master'
[root@test1_voice_live chinasoft]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : dev.txt
-rw-r--r-- root root Jun : LICENSE

查看日志

[root@test1_voice_live chinasoft]# git reflog
6bd2922 HEAD@{}: checkout: moving from af013e5f58888418b1739dd66873e02d2269a51d to master
af013e5 HEAD@{}: commit: add Help.md
6bd2922 HEAD@{}: checkout: moving from master to v1.
6bd2922 HEAD@{}: checkout: moving from master to master
6bd2922 HEAD@{}: commit (merge): change
504c42a HEAD@{}: commit: master change
8f56f76 HEAD@{}: checkout: moving from prod to master
7daacff HEAD@{}: commit: dev.txt
8f56f76 HEAD@{}: checkout: moving from master to prod
8f56f76 HEAD@{}: merge test: Fast-forward
031e757 HEAD@{}: checkout: moving from master to master
031e757 HEAD@{}: checkout: moving from test to master
8f56f76 HEAD@{}: commit: change dev.txt test
031e757 HEAD@{}: checkout: moving from dev to test
031e757 HEAD@{}: checkout: moving from master to dev
031e757 HEAD@{}: merge dev: Fast-forward
b724d04 HEAD@{}: checkout: moving from master to master
b724d04 HEAD@{}: checkout: moving from dev to master
031e757 HEAD@{}: commit: add dev.txt
b724d04 HEAD@{}: checkout: moving from master to dev
b724d04 HEAD@{}: pull origin master: Merge made by the 'recursive' strategy.
6340b73 HEAD@{}: commit (initial): first deploy shell

恢复到添加Help.md的版本

[root@test1_voice_live chinasoft]# git reset --hard af013e5
HEAD is now at af013e5 add Help.md
[root@test1_voice_live chinasoft]# ll
total
-rw-r--r-- root root Jun : deploy.sh
-rw-r--r-- root root Jun : dev.txt
-rw-r--r-- root root Jun : Help.md
-rw-r--r-- root root Jun : LICENSE

Git系列①之仓库管理互联网托管平台github.com的使用的更多相关文章

  1. 干货!Git 如何使用多个托管平台管理代码

    考虑到github不能免费创建私有仓库原因,最近开始在使用码云托管项目,这样避免了连接数据库的用户密码等信息直接暴露在公共仓库中.今天突然想到一个点,就是能不能同时把代码推送到github和码云上呢? ...

  2. git几个必知托管平台

      程序员必须知道的几个Git代码托管平台 说到Git代码托管平台,首先推荐的是GitHub,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的Git仓库,私有 仓库要收费,如果你做的 ...

  3. 代码托管平台--GitHub 使用小结

    一.GitHub介绍 GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git作为唯一的版本库格式进行托管.在GitHub,用户可以十分轻易地找到海量的开源代码. 很多IT行业的人到求职的 ...

  4. 如何导入外部Git仓库到中国源代码托管平台(Git@OSC)

    git clone --bare http://git.rcrtm.com/git/dianli.git git clone --mirror https://git.oschina.net/cand ...

  5. Git 时间,将代码托管到GitHub 上

    第一步:在github上创建一个项目,选择所属类型.会自动生成下面的文件. 第二步:使用安卓创建项目 第三步:使用git bash 进入项目目录,通过指令clone到本地 克隆完成后会出现下面的内容 ...

  6. 代码托管平台(Git)

    1,可以说GitHub的出现完全颠覆了以往大家对代码托管网站的认识.GitHub不但是一个代码托管网站,更是一个程序员的SNS社区.GitHub真正 迷人的是它的创新能力与Geek精神,这些都是无法模 ...

  7. 使用IDEA将代码托管到GitHub步骤和错误解决

    一.下载并安装Git版本控制工具 下载地址:https://git-scm.com/downloads 注册GitHub账号:https://github.com/ 为什么托管到GitHub要下载Gi ...

  8. 【Git 系列】基础知识全集

    Git 是一种分布式版本控制系统,它可以不受网络连接的限制,加上其它众多优点,目前已经成为程序开发人员做项目版本管理时的首选,非开发人员也可以用 Git 来做自己的文档版本管理工具. 一.Git 基础 ...

  9. iOS开发-xCode代码托管到GitHub

    xCode默认的是由源代码管理工作Git,Android Studio内置的也有,之前写过两篇关于Window托管Android代码到GitHub的文章,一直想写篇关于Mac上托管代码的到文章,今天终 ...

随机推荐

  1. dubbo监控中心---dubbo-admin

    dubbo监控中心---dubbo-admin: 一:dubbo管理平台搭建: 1-1:下载dubbo-admin.war包 https://pan.baidu.com/s/16jkoSozY68M7 ...

  2. 微信小程序,错误{"errMsg":"request:fail 小程序要求的 TLS 版本必须大于等于 1.2"}

    解决方法一: 开发环境,项目--->勾选不校验即可 解决办法二: 在 PowerShell中运行以下内容, 然后重启服务器 # Enables TLS R2 and Windows # Thes ...

  3. forEach、for in、for of 三者对比

    forEach forEach专门用来循环数组,可以直接取到元素,同时也可以取到index值 存在局限性,不能continue跳过或者break终止循环,没有返回值,不能return let arr ...

  4. vue input添加回车触发

    普通vue input @keyup.enter="onSubmit" element el-input @keyup.enter.native="onSubmit&qu ...

  5. SpringBoot系列: 极简Demo程序和Tomcat war包部署

    =================================SpringBoot 标准项目创建步骤================================= 使用 Spring IDE( ...

  6. C#调用C++导出类的一个实例

    一直认为带导出类dll的只有VC自己可以调用,其它编程语言无法调用,今天看到一篇文章才知道自己错了.https://blog.csdn.net/huiyouyongdeyu2011/article/d ...

  7. tensorflow 学习

    tensorflow: tensor 沿着graph 传递闭包完成flow的过程. 简单运算: import tensorflow as tf # Build a graph. a = tf.cons ...

  8. VUE项目安装

    连接转载:https://www.cnblogs.com/Colwill-Blog/p/6682091.html 刚刚开始学习Vue.js.今天分享一下我的Vue项目安装过程. 我是windows系统 ...

  9. 基于TensorFlow Object Detection API进行相关开发的步骤

    *以下二/三.四步骤确保你当前的文件目录是以research文件夹为相对目录. 一/安装或升级protoc 查看protoc版本命令: protoc --version 如果发现版本低于2.6.0或运 ...

  10. kettle中的合并记录使用记录

    注意:合并记录的使用前提是2个数据源都按比较关键字排过序,否则合并之后的数据不准确,可能会多出很多. 该步骤用于将两个不同来源的数据合并,这两个来源的数据分别为旧数据和新数据,该步骤将旧数据和新数据按 ...