Git系列①之仓库管理互联网托管平台github.com的使用

来源:互联网 发布:sql decode函数用法 编辑:程序博客网 时间:2024/05/20 21:47
互联网项目托管平台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 --listuser.name=jackuser.email=reblue520@163.comcolor.ui=true

 

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

[root@web01 ~]# mkdir chinasoft[root@web01 ~]# cd chinasoft/[root@web01 chinasoft]# git initInitialized empty Git repository in /root/chinasoft/.git/[root@web01 chinasoft]# ll -atotal 8drwxr-xr-x 3 root root 18 Jun 23 14:51 .dr-xr-x---. 12 root root 4096 Jun 23 14:51 ..drwxr-xr-x 7 root root 119 Jun 23 14:51 .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.htmlnothing 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 commit1 file changed, 1 insertion(+)create mode 100644 index.html[root@web01 chinasoft]# git status# On branch masternothing 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.sh1 file changed, 3 insertions(+)create mode 100644 deploy.sh

 

再次编辑index.html

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

 

# 查看变更历史记录

[root@web01 chinasoft]# git logcommit e18b9e0d4f545bcfb47a2d890f7293057240d5acAuthor: jack <reblue520@163.com>Date: Fri Jun 23 15:11:39 2017 +08002th index.htmlcommit c4cb609fde1fd18edc10a032775f2af672abbc13Author: jack <reblue520@163.com>Date: Fri Jun 23 15:06:15 2017 +08002th deploy.shcommit c0c2c393a508d0c59ff2d8934ef3b5055f267edfAuthor: jack <reblue520@163.com>Date: Fri Jun 23 14:55:48 2017 +0800first 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]# lltotal 8-rw-r--r-- 1 root root 34 Jun 23 15:05 deploy.sh-rw-r--r-- 1 root root 19 Jun 23 16:18 index.html[root@web01 chinasoft]# cat deploy.sh #!/bin/bashecho "deploy system"[root@web01 chinasoft]# cat index.html 1.chinasoft itpart

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

[root@web01 chinasoft]# git reflogc4cb609 HEAD@{0}: reset: moving to HEAD^e18b9e0 HEAD@{1}: commit: 2th index.htmlc4cb609 HEAD@{2}: commit: 2th deploy.shc0c2c39 HEAD@{3}: commit (initial): first commit

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

[root@web01 chinasoft]# git reset --hard e18b9e0HEAD is now at e18b9e0 2th index.html[root@web01 chinasoft]# cat deploy.sh #!/bin/bashecho "deploy system"[root@web01 chinasoft]# cat index.html 1.chinasoft itpart2.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:37:3b:d1:1a:da:19:f4:84:a4:99:b3:e9:ad:f7:6c:f8 root@test1_voice_liveThe key's randomart image is:+--[ RSA 2048]----+| . || = . || = 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.comThe authenticity of host 'github.com (192.30.255.112)' 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)? yesWarning: 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.comOpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013debug1: Reading configuration data /etc/ssh/ssh_configdebug1: /etc/ssh/ssh_config line 56: Applying options for *debug1: Connecting to github.com [192.168.1.13] port 22. # 发现是通过192.168.1.13(公司内网DNS)去连接github(内网dns)更换为公网IP服务器问题解决debug1: Connection established.debug1: permanently_set_uid: 0/0debug1: identity file /root/.ssh/id_rsa type 1debug1: identity file /root/.ssh/id_rsa-cert type -1debug1: identity file /root/.ssh/id_dsa type -1debug1: identity file /root/.ssh/id_dsa-cert type -1debug1: identity file /root/.ssh/id_ecdsa type -1debug1: identity file /root/.ssh/id_ecdsa-cert type -1debug1: identity file /root/.ssh/id_ed25519 type -1debug1: identity file /root/.ssh/id_ed25519-cert type -1debug1: Enabling compatibility mode for protocol 2.0debug1: Local version string SSH-2.0-OpenSSH_6.6.1debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: server->client aes128-ctr hmac-md5 nonedebug1: kex: client->server aes128-ctr hmac-md5 nonedebug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_GROUPdebug1: SSH2_MSG_KEX_DH_GEX_INIT sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_REPLYdebug1: Server host key: RSA d4:8f:d5:37:9b:4f:1a:47:12:20:82:03:b7:6d:3e:96debug1: Host 'github.com' is known and matches the RSA host key.debug1: Found key in /root/.ssh/known_hosts:1debug1: ssh_rsa_verify: signature correctdebug1: SSH2_MSG_NEWKEYS sentdebug1: expecting SSH2_MSG_NEWKEYSdebug1: SSH2_MSG_NEWKEYS receiveddebug1: SSH2_MSG_SERVICE_REQUEST sentdebug1: SSH2_MSG_SERVICE_ACCEPT receiveddebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Next authentication method: publickeydebug1: Offering RSA public key: /root/.ssh/id_rsadebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Trying private key: /root/.ssh/id_dsadebug1: Trying private key: /root/.ssh/id_ecdsadebug1: Trying private key: /root/.ssh/id_ed25519debug1: 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 = 0filemode = truebare = falselogallrefupdates = true[remote "origin"]url = git@github.com:reblue520/demo.gitfetch = +refs/heads/*:refs/remotes/origin/*

 

7.2 拉取远程仓库

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

 

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

[root@test1_voice_live chinasoft]# git push -u origin masterCounting objects: 6, done.Delta compression using up to 8 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (5/5), 565 bytes | 0 bytes/s, done.Total 5 (delta 0), reused 0 (delta 0)To git@github.com:reblue520/demo.git8fbed49..b724d04 master -> masterBranch 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.gitCloning into 'demo'...remote: Counting objects: 9, done.remote: Compressing objects: 100% (7/7), done.remote: Total 9 (delta 1), reused 5 (delta 0), pack-reused 0Receiving objects: 100% (9/9), 5.17 KiB | 0 bytes/s, done.Resolving deltas: 100% (1/1), done.[root@test1_voice_live tmp]# cd demo/[root@test1_voice_live demo]# lltotal 16-rw-r--r-- 1 root root 40 Jun 23 19:33 deploy.sh-rw-r--r-- 1 root root 11357 Jun 23 19:33 LICENSE

 

7.5 创建分支dev

[root@test1_voice_live chinasoft]# git branch dev[root@test1_voice_live chinasoft]# git checkout devSwitched to branch 'dev'[root@test1_voice_live chinasoft]# git branch* devmaster

 

提交一个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.txt1 file changed, 1 insertion(+)create mode 100644 dev.txt

 

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

[root@test1_voice_live chinasoft]# git checkout masterSwitched to branch 'master'[root@test1_voice_live chinasoft]# lltotal 16-rw-r--r-- 1 root root 40 Jun 23 19:20 deploy.sh-rw-r--r-- 1 root root 11357 Jun 23 19:22 LICENSE

 

合并到master

[root@test1_voice_live chinasoft]# git checkout masterAlready on 'master'[root@test1_voice_live chinasoft]# git merge devUpdating b724d04..031e757Fast-forwarddev.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 dev.txt[root@test1_voice_live chinasoft]# lltotal 20-rw-r--r-- 1 root root 40 Jun 23 19:20 deploy.sh-rw-r--r-- 1 root root 10 Jun 24 09:37 dev.txt-rw-r--r-- 1 root root 11357 Jun 23 19:22 LICENSE[root@test1_voice_live chinasoft]# git branchdev* master

# 删除分支的命令

# git branch -d dev

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

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

[root@test1_voice_live chinasoft]# git checkout -b prodSwitched 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.txt1 file changed, 1 insertion(+), 1 deletion(-)

 

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

[root@test1_voice_live chinasoft]# git checkout masterSwitched to branch 'master'Your branch is ahead of 'origin/master' by 2 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 change1 file changed, 1 insertion(+), 1 deletion(-)

 

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

[root@test1_voice_live chinasoft]# git merge prodAuto-merging dev.txtCONFLICT (content): Merge conflict in dev.txtAutomatic merge failed; fix conflicts and then commit the result.可以看到冲突的内容[root@test1_voice_live chinasoft]# cat dev.txt <<<<<<< HEAD55555chinasoft20170624=======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 prodAlready up-to-date.[root@test1_voice_live chinasoft]# cat dev.txt 1233333chinasoft20170624

9.给项目打标签

切换到master

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

 

确认下是否在master上

[root@test1_voice_live chinasoft]# git branchdev* masterprodtest打标签为v1.0[root@test1_voice_live chinasoft]# git tag v1.0[root@test1_voice_live chinasoft]# git tagv1.0[root@test1_voice_live chinasoft]# git show v1.0commit 6bd2922a7a8e2ed0319505c06f52651acf92df84Merge: 504c42a 7daacffAuthor: jack <reblue520@163.com>Date: Sat Jun 24 09:46:22 2017 +0800change

 

把标签推送到github上

[root@test1_voice_live chinasoft]# git push origin v1.0Counting objects: 9, done.Delta compression using up to 8 threads.Compressing objects: 100% (5/5), done.Writing objects: 100% (7/7), 609 bytes | 0 bytes/s, done.Total 7 (delta 3), reused 0 (delta 0)remote: Resolving deltas: 100% (3/3), completed with 1 local object.To git@github.com:reblue520/demo.git* [new tag] v1.0 -> v1.0[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)# codewhile 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.md1 file changed, 20 insertions(+)create mode 100644 Help.md[root@test1_voice_live chinasoft]# git push -u origin masterTotal 0 (delta 0), reused 0 (delta 0)To git@github.com:reblue520/demo.gitb724d04..6bd2922 master -> masterBranch master set up to track remote branch master from origin.

 

发现没有推送到远程github上

[root@test1_voice_live chinasoft]# git branch* (detached from v1.0)devmasterprodtest

 

切换到master

[root@test1_voice_live chinasoft]# git checkout masterWarning: you are leaving 1 commit behind, not connected toany of your branches:af013e5 add Help.mdIf you want to keep them by creating a new branch, this may be a good timeto do so with:git branch new_branch_name af013e5Switched to branch 'master'[root@test1_voice_live chinasoft]# lltotal 20-rw-r--r-- 1 root root 40 Jun 23 19:20 deploy.sh-rw-r--r-- 1 root root 25 Jun 24 09:46 dev.txt-rw-r--r-- 1 root root 11357 Jun 23 19:22 LICENSE

 

查看日志

[root@test1_voice_live chinasoft]# git reflog6bd2922 HEAD@{0}: checkout: moving from af013e5f58888418b1739dd66873e02d2269a51d to masteraf013e5 HEAD@{1}: commit: add Help.md6bd2922 HEAD@{2}: checkout: moving from master to v1.06bd2922 HEAD@{3}: checkout: moving from master to master6bd2922 HEAD@{4}: commit (merge): change504c42a HEAD@{5}: commit: master change8f56f76 HEAD@{6}: checkout: moving from prod to master7daacff HEAD@{7}: commit: dev.txt8f56f76 HEAD@{8}: checkout: moving from master to prod8f56f76 HEAD@{9}: merge test: Fast-forward031e757 HEAD@{10}: checkout: moving from master to master031e757 HEAD@{11}: checkout: moving from test to master8f56f76 HEAD@{12}: commit: change dev.txt test031e757 HEAD@{13}: checkout: moving from dev to test031e757 HEAD@{14}: checkout: moving from master to dev031e757 HEAD@{15}: merge dev: Fast-forwardb724d04 HEAD@{16}: checkout: moving from master to masterb724d04 HEAD@{17}: checkout: moving from dev to master031e757 HEAD@{18}: commit: add dev.txtb724d04 HEAD@{19}: checkout: moving from master to devb724d04 HEAD@{20}: pull origin master: Merge made by the 'recursive' strategy.6340b73 HEAD@{21}: commit (initial): first deploy shell

 

恢复到添加Help.md的版本

[root@test1_voice_live chinasoft]# git reset --hard af013e5HEAD is now at af013e5 add Help.md[root@test1_voice_live chinasoft]# lltotal 24-rw-r--r-- 1 root root 40 Jun 23 19:20 deploy.sh-rw-r--r-- 1 root root 25 Jun 24 09:46 dev.txt-rw-r--r-- 1 root root 282 Jun 24 10:43 Help.md-rw-r--r-- 1 root root 11357 Jun 23 19:22 LICENSE

 

阅读全文
0 0