您现在的位置是:首页 > 编程语言学习 > 后端编程语言 > 文章正文 后端编程语言

「Git版本控制」git安装、使用、以及创建、克隆github远程仓库,这篇文章就够了

万晓博博客 2019-12-24 16:13:16 后端编程语言

简介做过开发的小伙伴们都知道版本控制,市面上主流的两款版本控制软件SVN和Git,今天万晓博SEO分享下自己最近工作中使用的心得和体验,从版本控制Git安装、使用、以及创建、克隆github远程仓库一条龙教程,让你快速上手版本控制软件。

  本章前言:
 
  做过开发的小伙伴们都知道版本控制,市面上主流的两款版本控制软件SVN和Git,今天万晓博SEO分享下自己最近工作中使用的心得和体验,从版本控制Git安装、使用、以及创建、克隆github远程仓库一条龙教程,让你快速上手版本控制软件。
 
Git版本控制软件
  安装、使用创建和克隆远程仓库实战。
 
  1、安装git
  1. [root@linux ~]# yum -y install git 
  2、单机上使用git
 
  1)创建仓库目录:
  1. [root@linux ~]# mkdir /data/git 
  2)初始化仓库:
  1. [root@linux ~]# cd !$ 
  2. [root@linux git]# git init  
  3. 初始化空的 Git 版本库于 /data/git/.git/ 
  3)定义使用者身份(用户名和邮箱):
  1. [root@linux git]# git config user.name "asnfy" 
  2. [root@linux git]# git config user.email "asnfy@qq.com" 
  4)添加新文件到仓库:
  1. #创建新文件 
  2. [root@linux git]# cat /etc/passwd > test.txt 
  3. #添加add标记 
  4. [root@linux git]# git add test.txt 
  5. #使用commit处理添加了标记的文件 
  6. [root@linux git]# git commit -m " add newfile: test.txt " 
  7. [master(根提交) 425d26f]  add newfile: test.txt 
  8.  1 file changed, 33 insertions(+) 
  9.  create mode 100644 test.txt 
  #与svn用法想通,先对文件添加指定的标记,再通过commit命令执行,-m指定自定义说明,便于日志查看
 
  5)查看当前仓库中的状态:
  1. [root@linux git]# git status  
  2. # 位于分支 master 
  3. 无文件要提交,干净的工作区 
  6)当新增一个文件但未提交到仓库时,状态显示为:
  1. [root@linux git]# echo "123" > new.txt 
  2. [root@linux git]# git status  
  3. # 位于分支 master 
  4. # 未跟踪的文件: 
  5. #   new.txt 
  6. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪) 
  #当产生新文件但未进行标记时,会显示为未跟踪文件
 
  7)添加add标记后显示为:
  1. [root@linux git]# git add new.txt 
  2. [root@linux git]# git status  
  3. # 位于分支 master 
  4. # 要提交的变更: 
  5. #   新文件:    new.txt 
  #当文件被标记后,会显示为待提交的变更
 
  8)对比文件本次修改内容与仓库中的区别:
  1. [root@linux git]# echo "aaaaaaaaaa" >> new.txt  
  2. [root@linux git]# git diff new.txt 
  3. diff --git a/new.txt b/new.txt 
  4. index 190a180..34483aa 100644 
  5. --- a/new.txt 
  6. +++ b/new.txt 
  7. @@ -1 +1,2 @@ 
  8.  123 
  9. +aaaaaaaaaa 
  #当前修改的new.txt文件相比较于仓库中的new.txt,有新增内容,会在新增内容行首显示+号,有删减内容,会在行首显示-号
 
  9)撤销标记:
  1. [root@linux git]# git add 1.txt 
  2. [root@linux git]# git reset HEAD 1.txt 
  3. 重置后撤出暂存区的变更: 
  4. M   new1.txt 
  10)查看git提交记录:
  1. [root@linux git]# git log 
  2. commit 277075277c442544ac98f07f2d5f87197ee34f5b 
  3. Author: asnfy <asnfy@qq.com> 
  4. Date:   Mon Dec 23 14:31:24 2019 +0800 
  5.  
  6.     update new.txt 
  7.  
  8. commit 3c0b9eaae1984220fb73d20f54a653932e939c5a 
  9. Author: asnfy <asnfy@qq.com> 
  10. Date:   Mon Dec 23 14:27:51 2019 +0800 
  11.  
  12.     update new.txt 
  13.  
  14. commit f1eaf59a52b65c512d53487db383e80e0b5f9386 
  15. Author: asnfy <asnfy@qq.com> 
  16. Date:   Mon Dec 23 14:14:17 2019 +0800 
  17.  
  18.         新文件:    new.txt 
  #git日志显示所有变更记录(变更编号,操作用户,时间,自定义的变更说明),空格向下翻页,b键向上翻页,q退出,用法与less命令类似
 
  11)一行显示git日志(提交记录):
  1. [root@linux git]# git log --pretty=oneline  
  2. 277075277c442544ac98f07f2d5f87197ee34f5b update new.txt 
  3. 3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt 
  4. f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt 
  12)git版本回滚:
  1. [root@linux git]# git reset --hard 3c0b9eaae1984220fb73d20f54a653932e939c5a 
  2. HEAD 现在位于 3c0b9ea update new.txt 
  3. [root@linux git]# git log --pretty=oneline  
  4. 3c0b9eaae1984220fb73d20f54a653932e939c5a update new.txt 
  5. f1eaf59a52b65c512d53487db383e80e0b5f9386 add new.txt 
  #回滚版本使用reset - -hard指定变更编码即可,回滚后再次查看提交记录日志,上次的提交记录已消失,当回滚后又想回滚到上次提交的最新版本,可是日志中已经没有那条记录,需要使用reflog命令,查看所有历史变更
 
  13)查看所有历史变更:
  1. [root@linux git]# git reflog  
  2. 3c0b9ea HEAD@{0}: reset: moving to 3c0b9eaae1984220fb73d20f54a653932e939c5a 
  3. 2770752 HEAD@{1}: commit: update new.txt 
  4. 3c0b9ea HEAD@{2}: commit: update new.txt 
  5. f1eaf59 HEAD@{3}: commit: add new.txt 
  #编号2770752的变更就是之前最新提交的变更,通过git reset --hard 2770752即可回滚到之前提交的最新版本
 
  14)git撤销删除:
  1. [root@linux git]# ls 
  2. 1.txt  new.txt  test.txt 
  3. [root@linux git]# rm -f new.txt  
  4. [root@linux git]# ls 
  5. 1.txt  test.txt 
  6. [root@linux git]# git checkout -- new.txt 
  7. [root@linux git]# ls 
  8. 1.txt  new.txt  test.txt 
  #当误删后可以通过checkout指定文件名将误删的文件从git仓库恢复,也可以用于将修改过的文件回退到上一次提交的状态
 
  3、建立GitHub远程仓库
 
  1)在GitHub创建仓库:
Git版本控制软件
 
  2)在本机生成秘钥:
  1. [root@linux ~]# ssh-keygen  
  2. Generating public/private rsa key pair. 
  3. Enter file in which to save the key (/root/.ssh/id_rsa):  
  4. Enter passphrase (empty for no passphrase):  
  5. Enter same passphrase again:  
  6. Your identification has been saved in /root/.ssh/id_rsa. 
  7. Your public key has been saved in /root/.ssh/id_rsa.pub. 
  8. The key fingerprint is: 
  9. SHA256:PN7KUkW6ZJxXyGbgk1MJ1zLEsYcnqf4QsRk24b9zm+8 root@linux 
  10. The key's randomart image is: 
  11. +---[RSA 2048]----+ 
  12. |        +*==     | 
  13. |       o =@+o    | 
  14. |       .@*=+o    | 
  15. |       o*@o+     | 
  16. |       oS+.      | 
  17. |       oo+ .     | 
  18. |       .+ + .    | 
  19. |      .. + o o   | 
  20. |       .o . ooE  | 
  21. +----[SHA256]-----+     
  22. [root@linux ~]# cat /root/.ssh/id_rsa.pub  
  23. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzAgtbiq1f73Cwxm9ra+Z+e5Ki03VFZbn2EhjpDkzjVL7l7Yeux0nJBriM3xjeJHhysFrrbacvMBfaqXpin7i7MQ8+ugGRFSQeSoXlgSq0X7u05LKhLx/Qbut0brBWaI10WchobbjOGsTLoeE0HzaiZj4muFhtAsN8Yvm9BhIfbLPq255s0c4ESLpXqi6gjBDnHaUdLpnD5mqRmyEjhEGiLqnvTNOtMX9Zf5sFi1lwTahvToeMfuQfgC8QgTCXMGGxVafF6uFof4X4TY7B0Fvj05X1Qsoz9+XcWBk4jxOnGeRB8iYxWoupYQQ14cPAYTa+kYqkoGVThA/ru8lv0N// root@linux 
  3)在GitHub点击右上角头像旁边的箭头,选择setting,选择SSH and GPG keys创建ssh秘钥:
 
Git版本控制软件

  4)修改本地git仓库配置文件:
  1. [root@linux ~]# vim /data/git/.git/config 
  5)修改url地址为ssh协议地址:
 
[root@linux ~]# vim /data/git/.git/config

  6)将本地仓库推送远程仓库:
  1. [root@linux ~]# cd /data/git/ 
  2. [root@linux git]# git remote add origin https://github.com/AsnFy/git_test.git 
  3. [root@linux git]# git push -u origin master 
  4. The authenticity of host 'github.com (13.229.188.59)' can't be established. 
  5. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. 
  6. RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. 
  7. Are you sure you want to continue connecting (yes/no)? yes 
  8. Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts. 
  9. Counting objects: 11, done. 
  10. Compressing objects: 100% (8/8), done. 
  11. Writing objects: 100% (11/11), 1.58 KiB | 0 bytes/s, done. 
  12. Total 11 (delta 0), reused 0 (delta 0) 
  13. To git@github.com:AsnFy/git_test.git 
  14.  * [new branch]      master -> master 
  15. 分支 master 设置为跟踪来自 origin 的远程分支 master。 
  7)之后推送执行git push即可:
  1. [root@linux git]# touch github.txt 
  2. [root@linux git]# git add github.txt 
  3. [root@linux git]# git commit -m "add newfile: github.txt" 
  4. [master 3c0a54d] add newfile: github.txt 
  5.  1 file changed, 0 insertions(+), 0 deletions(-) 
  6.  create mode 100644 github.txt 
  7. [root@linux git]# git push  
  8)查看GitHub仓库:
 
Git版本控制软件
 
  #推送的文件已显示
 
  补充:如果需要使用https验证方式,本地仓库配置文件保持默认即可,只是推送本地仓库文件到GitHub需要验证GitHub用户名和密码
 
  4、克隆GitHub远程仓库
 
  1)在GitHub仓库中点击Clone or download获取https/ssh地址:
 
Git版本控制软件
 
  2)克隆远程仓库到本地:
  1. [root@linux ~]# git clone https://github.com/AsnFy/git_test.git 
  2. 正克隆到 'git_test'... 
  3. remote: Enumerating objects: 14, done. 
  4. remote: Counting objects: 100% (14/14), done. 
  5. remote: Compressing objects: 100% (10/10), done. 
  6. remote: Total 14 (delta 1), reused 13 (delta 0), pack-reused 0 
  7. Unpacking objects: 100% (14/14), done. 
  #此步骤不需要验证用户账号密码或ssh秘钥
 
  3)进入目录即可看到远程仓库中的文件:
  1. [root@linux ~]# cd git_test/ 
  2. [root@linux git_test]# ls 
  3. github.txt  new1.txt  new.txt  test.txt 
  4)定义使用者身份:
  1. [root@linux git_test]# git config --global user.name "zhangsan" 
  2. [root@linux git_test]# git config --global user.email "zhangsan@test.com" 
  5)推送新文件到GitHub:
  1. [root@linux git_test]# echo "hello" > README.md 
  2. [root@linux git_test]# git add README.md 
  3. [root@linux git_test]# git commit -m "add README.md" 
  4. [root@linux git_test]# git push 
  #此步骤需要验证用户名密码或ssh秘钥,默认是https验证,如需ssh免密认证,参考前面的步骤修改当前仓库下的.git/config文件
 
  6)查看GitHub:
 
Git版本控制软件
 
  #新文件README.md已显示
 
  7)在GitHub创建新文件:
 
Git版本控制软件
 
  8)在本地仓库拉取GitHub仓库的新文件:
  1. [root@linux git_test]# git pull 
  2. [root@linux git_test]# ls 
  3. abc.sh  github.txt  new1.txt  new.txt  README.md  test.txt 
  #新文件abc.sh拉取成功
 
  本章总结:
 
  以上就是万晓博SEO从最近学习Git版本控制所有心得笔记,从Git创建到上传代码到GitHub平台一条龙服务,我相信通过本章内容学习,你已经对Git以及GitHub这两块有所了解,更多代码开发实用技巧还请继续关注万晓博博客

相关文章

站点信息