一、什么是Git

Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

二、单机使用Git

1、安装

  1. [root@localhost ~]# yum install -y git

配置基本信息

  1. [root@localhost ~]# git config --global user.name "sxb"
  2. [root@localhost ~]# git config --global user.email "example@qq.com"
  3. [root@localhost ~]#

image.png
初始化仓库

  1. [root@localhost ~]# mkdir -p /data/gitroot
  2. [root@localhost ~]# cd /data/gitroot/
  3. [root@localhost gitroot]# git init
  4. 初始化空的 Git 版本库于 /data/gitroot/.git/
  5. [root@localhost gitroot]#

image.png

2、使用

添加新文件

  1. [root@localhost gitroot]# echo "hello word" >1.txt
  2. [root@localhost gitroot]# git add 1.txt
  3. [root@localhost gitroot]# git commit -m "add new file 1.txt"
  4. [master(根提交) 81dc46b] add new file 1.txt
  5. 1 file changed, 1 insertion(+)
  6. create mode 100644 1.txt
  7. [root@localhost gitroot]#

image.png
查看当前仓库状态是否有改动的文件

  1. [root@localhost gitroot]# echo 'asafa' >> 1.txt
  2. [root@localhost gitroot]# git status
  3. # 位于分支 master
  4. # 尚未暂存以备提交的变更:
  5. # (使用 "git add <file>..." 更新要提交的内容)
  6. # (使用 "git checkout -- <file>..." 丢弃工作区的改动)
  7. #
  8. # 修改: 1.txt
  9. #
  10. 修改尚未加入提交(使用 "git add" 和/或 "git commit -a"
  11. [root@localhost gitroot]#

image.png
对比本次修改了什么内容

  1. [root@localhost gitroot]# git diff 1.txt
  2. diff --git a/1.txt b/1.txt
  3. index dc05884..15e9bf0 100644
  4. --- a/1.txt
  5. +++ b/1.txt
  6. @@ -1 +1,2 @@
  7. hello word
  8. +asafa
  9. [root@localhost gitroot]#

image.png
版本回退

  1. [root@localhost gitroot]# git add 1.txt
  2. [root@localhost gitroot]# git commit -m ">> 1.txt"
  3. [master 8c4e099] >> 1.txt
  4. 1 file changed, 1 insertion(+)
  5. [root@localhost gitroot]# echo '123141' >> 1.txt
  6. [root@localhost gitroot]# git add 1.txt
  7. [root@localhost gitroot]# git commit -m "echo 123141 >> 1.txt"
  8. [master 46048f1] echo 123141 >> 1.txt
  9. 1 file changed, 1 insertion(+)
  10. [root@localhost gitroot]#

image.png
查看日志

  1. [root@localhost gitroot]# git log
  2. commit 46048f1c47a2cea8e483d54824b00ae7d17d73a3
  3. Author: sxb <example@qq.com>
  4. Date: Fri Aug 13 15:13:18 2021 +0800
  5. echo 123141 >> 1.txt
  6. commit 8c4e099db3eae3621ca2fcafdaa2d22c4cd9b82e
  7. Author: sxb <example@qq.com>
  8. Date: Fri Aug 13 15:13:00 2021 +0800
  9. >> 1.txt
  10. commit 81dc46be9e8d91f12878b6481388d159fdfca979
  11. Author: sxb <example@qq.com>
  12. Date: Fri Aug 13 15:10:39 2021 +0800
  13. add new file 1.txt
  14. [root@localhost gitroot]#

image.png
日志一行显示

  1. [root@localhost gitroot]# git log --pretty=oneline
  2. 46048f1c47a2cea8e483d54824b00ae7d17d73a3 echo 123141 >> 1.txt
  3. 8c4e099db3eae3621ca2fcafdaa2d22c4cd9b82e >> 1.txt
  4. 81dc46be9e8d91f12878b6481388d159fdfca979 add new file 1.txt
  5. [root@localhost gitroot]#

image.png
版本回退

  1. [root@localhost gitroot]# git reset --hard 81dc46be9e
  2. HEAD 现在位于 81dc46b add new file 1.txt
  3. [root@localhost gitroot]# cat 1.txt
  4. hello word
  5. [root@localhost gitroot]#

image.png
撤销修改

  1. [root@localhost gitroot]# rm -f 1.txt
  2. [root@localhost gitroot]# git checkout -- 1.txt
  3. [root@localhost gitroot]# ls
  4. 1.txt
  5. [root@localhost gitroot]# cat 1.txt
  6. hello word

image.png
如果1.txt文件修改,add后但没有commit,再想回退到上一次提交的状态,#可以使用git reset HEAD 1.txt,再执行git checkout — 1.txt。

  1. [root@localhost gitroot]# echo "1231224" >> 1.txt
  2. [root@localhost gitroot]# git add 1.txt
  3. [root@localhost gitroot]# git reset HEAD 1.txt
  4. 重置后撤出暂存区的变更:
  5. M 1.txt
  6. [root@localhost gitroot]# cat 1.txt
  7. hello word
  8. 1231224
  9. [root@localhost gitroot]# git checkout -- 1.txt
  10. [root@localhost gitroot]# cat 1.txt
  11. hello word
  12. [root@localhost gitroot]#

image.png
查看所有历史版本

  1. [root@localhost gitroot]# git reflog
  2. 81dc46b HEAD@{0}: reset: moving to 81dc46be9e
  3. 46048f1 HEAD@{1}: commit: echo 123141 >> 1.txt
  4. 8c4e099 HEAD@{2}: commit: >> 1.txt
  5. 81dc46b HEAD@{3}: commit (initial): add new file 1.txt
  6. [root@localhost gitroot]#

image.png
删除文件

  1. [root@localhost gitroot]# echo -e "11111\n2222222222" > 2.txt
  2. [root@localhost gitroot]# git add 2.txt
  3. [root@localhost gitroot]# git commit -m 'add 2.txt'
  4. [master e72a786] add 2.txt
  5. 1 file changed, 2 insertions(+)
  6. create mode 100644 2.txt
  7. [root@localhost gitroot]# git rm 2.txt
  8. rm '2.txt'
  9. [root@localhost gitroot]# git commit -m 'rm 2.txt'
  10. [master b0a0b0f] rm 2.txt
  11. 1 file changed, 2 deletions(-)
  12. delete mode 100644 2.txt
  13. [root@localhost gitroot]# ls
  14. 1.txt
  15. [root@localhost gitroot]#

image.png

三、gitee

3.1添加公钥

先搜索gitee,注册
然后点击设置,选择ssh,把虚拟机的公钥复制上去

虚拟机的公钥

  1. [root@localhost ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. SHA256:/Yjc+k+FcVb9/r74Em7kxIcJf6tVvclYgzjn+NXnxIg root@localhost.localdomain
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. | o|
  14. | ..|
  15. | . o .|
  16. | . ..=. o|
  17. | S .o=o++o|
  18. | . o o=@+=B|
  19. | o o.Eo=*B|
  20. | . ..=++o|
  21. | ....o++++|
  22. +----[SHA256]-----+
  23. [root@localhost ~]# cd .ssh/
  24. [root@localhost .ssh]# ls
  25. id_rsa id_rsa.pub
  26. [root@localhost .ssh]#
  27. [root@localhost .ssh]# cat id_rsa.pub
  28. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrSRqf8oQR5IJvxIWngjgSKU+xAqVs0Y7GoDJK45tMSTh1avZctBAx26KZKGg+qvKA9mTcZF3Rn6QhiFIIoqMD/4RsZlQGjGafGGFMZLCZp78P5Akl1FGtbjs93e7TUSAutszPQ8T1FxGfmdl3go1Ejxffw++02Yt5l9rmOW1hb5FqC1k8b1gFI3aZsPnUuIpTwY55sgcO7F7qcw4mMcX7668PCiCQBEVAnqwAKaffBG3+mXiNvzr5XlmSPLd5gA4m24Ng26MVdMX4I95aAf3Vo81W33vd1yPNqmT0FORyPYN/t3VZed/msvlbNd1imXztQjHWbR82CJni+7FVqIor root@localhost.localdomain
  29. [root@localhost .ssh]#

image.png
image.png

3.2创建仓库

点击加号创建仓库
image.png
image.png

3.3连接远程仓库

  1. [root@localhost .ssh]# cd /data/gitroot/
  2. [root@localhost gitroot]# git remote add origin https://gitee.com/sunxuebing/demo1.git[root@localhost gitroot]# ls
  3. 1.txt
  4. [root@localhost gitroot]# git push -u origin master
  5. Username for 'https://gitee.com': 18747739721
  6. Password for 'https://18747739721@gitee.com':
  7. Counting objects: 7, done.
  8. Compressing objects: 100% (4/4), done.
  9. Writing objects: 100% (7/7), 528 bytes | 0 bytes/s, done.
  10. Total 7 (delta 1), reused 0 (delta 0)
  11. remote: Powered by GITEE.COM [GNK-6.0]
  12. To https://gitee.com/sunxuebing/demo1.git
  13. * [new branch] master -> master
  14. 分支 master 设置为跟踪来自 origin 的远程分支 master
  15. [root@localhost gitroot]#

image.png

3.4克隆远程仓库

从这里找仓库链接
image.png

  1. [root@localhost gitroot]# cd /opt/
  2. [root@localhost opt]# git clone https://gitee.com/sunxuebing/demo1.git
  3. 正克隆到 'demo1'...
  4. remote: Enumerating objects: 7, done.
  5. remote: Counting objects: 100% (7/7), done.
  6. remote: Compressing objects: 100% (4/4), done.
  7. remote: Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
  8. Unpacking objects: 100% (7/7), done.
  9. [root@localhost opt]# ls
  10. demo1
  11. [root@localhost opt]# cd demo1/
  12. [root@localhost demo1]# ls -a
  13. . .. 1.txt .git
  14. [root@localhost demo1]#

3.5推送远程服务端

  1. [root@localhost demo1]# cd /data/gitroot/
  2. [root@localhost gitroot]# ls
  3. 1.txt
  4. [root@localhost gitroot]# echo "1231224" >> 2.sh
  5. [root@localhost gitroot]# git add 2.sh
  6. [root@localhost gitroot]# git commit -m "add 2.sh"
  7. [master de63708] add 2.sh
  8. 1 file changed, 1 insertion(+)
  9. create mode 100644 2.sh
  10. [root@localhost gitroot]# git push
  11. warning: push.default 未设置,它的默认值将会在 Git 2.0 'matching'
  12. 修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
  13. 进行如下设置:
  14. git config --global push.default matching
  15. 若要不再显示本信息并从现在开始采用新的使用习惯,设置:
  16. git config --global push.default simple
  17. 参见 'git help config' 并查找 'push.default' 以获取更多信息。
  18. 'simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git
  19. 为保持兼容,请用 'current' 代替 'simple' 模式)
  20. Username for 'https://gitee.com': 18747739721
  21. Password for 'https://18747739721@gitee.com':
  22. Counting objects: 4, done.
  23. Compressing objects: 100% (2/2), done.
  24. Writing objects: 100% (3/3), 264 bytes | 0 bytes/s, done.
  25. Total 3 (delta 0), reused 0 (delta 0)
  26. remote: Powered by GITEE.COM [GNK-6.0]
  27. To https://gitee.com/sunxuebing/demo1.git
  28. b0a0b0f..de63708 master -> master
  29. [root@localhost gitroot]#

然后刷新网页可以看到刚刚写的脚本上传了
image.png

Git 分支

几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。 在很多版本控制系统中,这是一个略微低效的过程——常常需要完全创建一个源代码目录的副本。对于大项目来说,这样的过程会耗费很多时间。
有人把 Git 的分支模型称为它的“必杀技特性”,也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出。 为何 Git 的分支模型如此出众呢? Git 处理分支的方式可谓是难以置信的轻量,创建新分支这一操作几乎能在瞬间完成,并且在不同分支之间的切换操作也是一样便捷。 与许多其它版本控制系统不同,Git 鼓励在工作流程中频繁地使用分支与合并,哪怕一天之内进行许多次。 理解和精通这一特性,你便会意识到 Git 是如此的强大而又独特,并且从此真正改变你的开发方式。

1.分支创建切换

查看项目

  1. [root@localhost gitroot]# ls
  2. 1.txt 2.sh
  3. [root@localhost gitroot]# ls -a
  4. . .. 1.txt 2.sh .git

查看所有分支

  1. [root@localhost gitroot]# git branch
  2. * master

创建分支

  1. [root@localhost gitroot]# git branch sxb

查看分支

  1. [root@localhost gitroot]# git branch
  2. master
  3. * sxb

切换分支

  1. [root@localhost gitroot]# git checkout sxb
  2. 切换到分支 'sxb'

在新分支下创建一个文件,提交

  1. [root@localhost gitroot]# ls
  2. 1.txt 2.sh
  3. [root@localhost gitroot]# echo "12141" > 123.txt
  4. [root@localhost gitroot]# git add 123.txt
  5. [root@localhost gitroot]# git commit -m "add 123.txt"
  6. [sxb defad3f] add 123.txt
  7. 1 file changed, 1 insertion(+)
  8. create mode 100644 123.txt
  9. [root@localhost gitroot]# ls
  10. 123.txt 1.txt 2.sh

切换回其他分支发现并没有新文件

  1. [root@localhost gitroot]# git checkout master
  2. 切换到分支 'master'
  3. [root@localhost gitroot]# ls
  4. 1.txt 2.sh
  5. [root@localhost gitroot]#

2.分支的合并

如果master分支和lsk分支都对2.txt进行了编辑,当合并时会提示冲突,需先解决冲突才可以继续合并。
解决冲突的方法是在master分支下,编辑2.txt,改为lsk分支里面2.txt的内容。然后提交2.txt,再合并lsk分支。
但是这样有一个问题,万一master分支更改的内容是我们想要的呢?此时可以编辑2.txt内容,改为想要的,然后提交。切换到lsk分支,然后合并master分支到lsk分支即可(倒着合并)。
合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。

  1. [root@localhost gitroot]# git checkout master
  2. 切换到分支 'master'
  3. [root@localhost gitroot]# ls
  4. 1.txt 2.sh
  5. [root@localhost gitroot]# git merge sxb
  6. 更新 de63708..defad3f
  7. Fast-forward
  8. 123.txt | 1 +
  9. 1 file changed, 1 insertion(+)
  10. create mode 100644 123.txt
  11. [root@localhost gitroot]# ls
  12. 123.txt 1.txt 2.sh
  13. [root@localhost gitroot]#

3.分支删除

如果分支没有合并,删除之前会提示,那就不合并,强制删除

  1. [root@localhost gitroot]# git branch -d sxb
  2. 已删除分支 sxb(曾为 defad3f)。

强制删除

  1. [root@localhost gitroot]# git branch -D sxb

4.分支使用原则

① master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
②创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master。
③开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己PC上)里面开发代码,然后合并到dev分支。

image.png

5.远程分支

本地新建的分支如果不推送到远程,对其他人就是不可见的。
查看远程分支命令为git ls-remote origin,可以看到所有分支。

  1. [root@localhost gitroot]# git ls-remote origin
  2. de637084e60b9f81c98fd83cf17842d6a58b7683 HEAD
  3. de637084e60b9f81c98fd83cf17842d6a58b7683 refs/heads/master
  4. [root@localhost gitroot]#

对于git push分支分两种情况:
①当本地分支和远程分支一致时
git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name命令。
②当本地分支比远程分支多
默认git push只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name命令如果推送失败,先用git pull抓取远程的新提交。

git clone的时候默认只把master分支克隆下来。如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name命令,需要注意本地和远程分支的名称要一致。

Git标签与别名

1.标签

标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态

  1. master分支打一个标签
  2. [root@localhost gitroot]# git tag v1.0
  3. 查看标签信息
  4. [root@localhost gitroot]# git show v1.0
  5. commit defad3fd21dba062bbdf57fc7717d1d848ccf43d
  6. Author: sxb <example@qq.com>
  7. Date: Wed Aug 18 18:19:44 2021 +0800
  8. add 123.txt
  9. diff --git a/123.txt b/123.txt
  10. new file mode 100644
  11. index 0000000..ca5cf1c
  12. --- /dev/null
  13. +++ b/123.txt
  14. @@ -0,0 +1 @@
  15. +12141
  16. [root@localhost gitroot]#
  17. 查看所有的标签
  18. [root@localhost gitroot]# git tag
  19. v1.0
  20. Tag是针对commit来打标签的,所以可以针对历史的commit来打tag
  21. 查看历史的commit
  22. [root@localhost gitroot]# git log --pretty=oneline --abbrev-commit
  23. defad3f add 123.txt
  24. de63708 add 2.sh
  25. b0a0b0f rm 2.txt
  26. e72a786 add 2.txt
  27. 81dc46b add new file 1.txt
  28. 针对历史的commit打标签
  29. [root@localhost gitroot]# git tag v0.9 de63708
  30. 可以对标签进行描述
  31. [root@localhost gitroot]# git tag -a v0.8 -m "add tag! " defad3f
  32. [root@localhost gitroot]# git tag
  33. v0.8
  34. v0.9
  35. v1.0
  36. [root@localhost gitroot]#
  37. 删除标签
  38. [root@localhost gitroot]# git tag -d v0.8
  39. 已删除 tag 'v0.8'(曾为 0d8a5da
  40. [root@localhost gitroot]# git tag
  41. v0.9
  42. v1.0
  43. [root@localhost gitroot]#
  44. 推送指定标签
  45. [root@localhost gitroot]# git push origin v1.0
  46. Username for 'https://gitee.com': 18747739721
  47. Password for 'https://18747739721@gitee.com':
  48. Counting objects: 4, done.
  49. Compressing objects: 100% (2/2), done.
  50. Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
  51. Total 3 (delta 0), reused 0 (delta 0)
  52. remote: Powered by GITEE.COM [GNK-6.0]
  53. To https://gitee.com/sunxuebing/demo1.git
  54. * [new tag] v1.0 -> v1.0
  55. [root@localhost gitroot]#
  56. 推送所有标签
  57. [root@localhost gitroot]# git push --tag origin
  58. Username for 'https://gitee.com': 18747739721
  59. Password for 'https://18747739721@gitee.com':
  60. Total 0 (delta 0), reused 0 (delta 0)
  61. remote: Powered by GITEE.COM [GNK-6.0]
  62. To https://gitee.com/sunxuebing/demo1.git
  63. * [new tag] v0.9 -> v0.9
  64. [root@localhost gitroot]#
  65. 本地远程都删除标签
  66. [root@localhost gitroot]# git tag v1.0 -d
  67. 已删除 tag 'v1.0'(曾为 defad3f
  68. [root@localhost gitroot]# git push origin :refs/tags/v1.0
  69. Username for 'https://gitee.com': 18747739721
  70. Password for 'https://18747739721@gitee.com':
  71. remote: Powered by GITEE.COM [GNK-6.0]
  72. To https://gitee.com/sunxuebing/demo1.git
  73. - [deleted] v1.0
  74. [root@localhost gitroot]#

2.Git别名

别名和Linux里的alias类似
设置别名

  1. [root@localhost gitroot]# git config --global alias.ci commit
  2. [root@localhost gitroot]# git config --global alias.co checkout
  3. [root@localhost gitroot]# git config --global alias.br branch
  4. [root@localhost gitroot]# git config --list |grep alias
  5. alias.ci=commit
  6. alias.co=checkout
  7. alias.br=branch

使用别名

  1. [root@localhost gitroot]# git br
  2. * master

小技巧 设置log

  1. [root@localhost gitroot]# git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

再查看日志

  1. [root@localhost gitroot]# git lg
  2. * defad3f - (HEAD, master) add 123.txt (32 分钟之前) <sxb>
  3. * de63708 - (tag: v0.9, origin/master) add 2.sh (39 分钟之前) <sxb>
  4. * b0a0b0f - rm 2.txt (5 天之前) <sxb>
  5. * e72a786 - add 2.txt (5 天之前) <sxb>
  6. * 81dc46b - add new file 1.txt (5 天之前) <sxb>
  7. [root@localhost gitroot]#

取消别名

  1. [root@localhost gitroot]# git config --global --unset alias.br
  2. [root@localhost gitroot]#