互联网项目托管平台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@163.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 8 drwxr-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.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 1 file changed, 1 insertion(+) create mode 100644 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 1 file changed, 3 insertions(+) create mode 100644 deploy.sh
再次编辑index.html
[root@web01 chinasoft]# cat index.html 1.chinasoft itpart 2.add deploy.sh
# 查看变更历史记录
[root@web01 chinasoft]# git log commit e18b9e0d4f545bcfb47a2d890f7293057240d5ac Author: jack <reblue520@163.com> Date: Fri Jun 23 15:11:39 2017 +0800 2th index.html commit c4cb609fde1fd18edc10a032775f2af672abbc13 Author: jack <reblue520@163.com> Date: Fri Jun 23 15:06:15 2017 +0800 2th deploy.sh commit c0c2c393a508d0c59ff2d8934ef3b5055f267edf Author: jack <reblue520@163.com> Date: Fri Jun 23 14:55:48 2017 +0800 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 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/bash echo "deploy system" [root@web01 chinasoft]# cat index.html 1.chinasoft itpart
回退方式2:
git reflog #查看未来历史更新点
[root@web01 chinasoft]# git reflog c4cb609 HEAD@{0}: reset: moving to HEAD^ e18b9e0 HEAD@{1}: commit: 2th index.html c4cb609 HEAD@{2}: commit: 2th deploy.sh c0c2c39 HEAD@{3}: 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 1.chinasoft itpart 2.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_live The 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.com The 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)? 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 11 Feb 2013 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /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/0 debug1: identity file /root/.ssh/id_rsa type 1 debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_dsa type -1 debug1: identity file /root/.ssh/id_dsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: identity file /root/.ssh/id_ed25519 type -1 debug1: identity file /root/.ssh/id_ed25519-cert type -1 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.3 debug1: match: OpenSSH_5.3 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=16 dh_need=16 debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16 debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) 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:37:9b:4f:1a:47:12:20:82:03:b7:6d:3e:96 debug1: Host 'github.com' is known and matches the RSA host key. debug1: Found key in /root/.ssh/known_hosts:1 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 = 0 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: 4, done. remote: Compressing objects: 100% (4/4), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), done. From github.com:reblue520/demo * branch master -> FETCH_HEAD Merge made by the 'recursive' strategy. .gitignore | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE
7.3 推送到远程仓库,可以看到远程https://github.com/reblue520/demo已经有了我们刚添加的文件
[root@test1_voice_live chinasoft]# git push -u origin master Counting 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.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: 9, done. remote: Compressing objects: 100% (7/7), done. remote: Total 9 (delta 1), reused 5 (delta 0), pack-reused 0 Receiving 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]# ll total 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 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 1 file changed, 1 insertion(+) create mode 100644 dev.txt
切换到master,这时候是没有dev.txt文件的
[root@test1_voice_live chinasoft]# git checkout master Switched to branch 'master' [root@test1_voice_live chinasoft]# ll total 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 master Already on 'master' [root@test1_voice_live chinasoft]# git merge dev Updating b724d04..031e757 Fast-forward dev.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 dev.txt [root@test1_voice_live chinasoft]# ll total 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 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 1 file changed, 1 insertion(+), 1 deletion(-)
②切换到master,编辑文件dev.txt
[root@test1_voice_live chinasoft]# git checkout master Switched 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 change 1 file changed, 1 insertion(+), 1 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 5 commits. (use "git push" to publish your local commits)
确认下是否在master上
[root@test1_voice_live chinasoft]# git branch dev * master prod test 打标签为v1.0 [root@test1_voice_live chinasoft]# git tag v1.0 [root@test1_voice_live chinasoft]# git tag v1.0 [root@test1_voice_live chinasoft]# git show v1.0 commit 6bd2922a7a8e2ed0319505c06f52651acf92df84 Merge: 504c42a 7daacff Author: jack <reblue520@163.com> Date: Sat Jun 24 09:46:22 2017 +0800 change
把标签推送到github上
[root@test1_voice_live chinasoft]# git push origin v1.0 Counting 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) # 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 1 file changed, 20 insertions(+) create mode 100644 Help.md [root@test1_voice_live chinasoft]# git push -u origin master Total 0 (delta 0), reused 0 (delta 0) 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.0) dev master prod test
切换到master
[root@test1_voice_live chinasoft]# git checkout master Warning: you are leaving 1 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 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 reflog 6bd2922 HEAD@{0}: checkout: moving from af013e5f58888418b1739dd66873e02d2269a51d to master af013e5 HEAD@{1}: commit: add Help.md 6bd2922 HEAD@{2}: checkout: moving from master to v1.0 6bd2922 HEAD@{3}: checkout: moving from master to master 6bd2922 HEAD@{4}: commit (merge): change 504c42a HEAD@{5}: commit: master change 8f56f76 HEAD@{6}: checkout: moving from prod to master 7daacff HEAD@{7}: commit: dev.txt 8f56f76 HEAD@{8}: checkout: moving from master to prod 8f56f76 HEAD@{9}: merge test: Fast-forward 031e757 HEAD@{10}: checkout: moving from master to master 031e757 HEAD@{11}: checkout: moving from test to master 8f56f76 HEAD@{12}: commit: change dev.txt test 031e757 HEAD@{13}: checkout: moving from dev to test 031e757 HEAD@{14}: checkout: moving from master to dev 031e757 HEAD@{15}: merge dev: Fast-forward b724d04 HEAD@{16}: checkout: moving from master to master b724d04 HEAD@{17}: checkout: moving from dev to master 031e757 HEAD@{18}: commit: add dev.txt b724d04 HEAD@{19}: checkout: moving from master to dev b724d04 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 af013e5 HEAD is now at af013e5 add Help.md [root@test1_voice_live chinasoft]# ll total 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