0


【Git】初识Git

本篇文章的环境是在

  1. Ubuntu/Linux

环境下编写的

文章目录

版本控制器

在日常工作和学习中,老板/老师要求我们修改文档,但修改可能不尽人意,多次修改后可能还不如最初的版本,但如果是直接在原文档上修改,那我们是否还能找回最初的版本呢?
若每一次大改,都是在复制出的副本上修改,可以解决上述原文档丢失的问题,但随着版本数量不断增多,我们还能记得每个版本各自修改了什么吗?
版本控制器就是为了解决上述问题而诞生的。所谓版本控制器,就是一个

  1. 可以记录工程的每一次改动和版本迭代的管理系统,同时也方便多人协同作业

Git 基本操作

安装 Git

可以使用

  1. git

  1. git --version

查看是否已经安装了GIt
如果bash 响应的是

  1. git: command not found

类似的话语,那就是没有安装git

  • 安装 Git:
  1. #Centossudo yum -yinstallgit#Ubuntusudoapt-getinstallgit-y
  • 查看 Git 版本
  1. git--version

创建 Git 本地仓库

Git 进行版本控制的方式是,使用仓库对代码进行管理

  • 创建一个Git 本地仓库的命令为git init,注意命令要在文件目录下执行,例如:在这里插入图片描述

创建本地仓库后,会多出来一个

  1. .git

隐藏文件,这个目录就是Git用来跟踪管理仓库的
其目录结构如下:

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ tree .git/
  2. .git/
  3. ├── branches
  4. ├── config
  5. ├── description
  6. ├── HEAD
  7. ├── hooks
  8. ├── applypatch-msg.sample
  9. ├── commit-msg.sample
  10. ├── fsmonitor-watchman.sample
  11. ├── post-update.sample
  12. ├── pre-applypatch.sample
  13. ├── pre-commit.sample
  14. ├── pre-merge-commit.sample
  15. ├── prepare-commit-msg.sample
  16. ├── pre-push.sample
  17. ├── pre-rebase.sample
  18. ├── pre-receive.sample
  19. ├── push-to-checkout.sample
  20. └── update.sample
  21. ├── info
  22. └── exclude
  23. ├── objects
  24. ├── info
  25. └── pack
  26. └── refs
  27. ├── heads
  28. └── tags

配置 Git

当安装 Git 后很重要的一步是配置 用户名称 和 e-mail地址,命令如下:

  1. git config -l#以列表形式显示配置git config user.name "Your name"#配置用户名称git config user.email "email"#配置email

使用

  1. --unset

选项删除配置,例如:

  1. git config --unset user.name
  2. git config --unset user.email

可以使用

  1. --global

这个选项,该选项的效果是使得当前机器上所有的 Git 仓库都使用这个配置

  1. git config --global user.name "Your name"#配置用户名称git config --global user.email "email"#配置email

使用

  1. --global

设置的配置不能直接使用

  1. --unset

删除,而是也需要携带

  1. --global
  1. git config --global--unset user.name

认识工作区、暂存区、版本库

  • 工作区:和.git同级的目录下的文件/目录
  • 暂存区:stageindex。一般存放在.git目录下的 index 文件(.git/index)中,暂存区有时也就索引(index)
  • 版本库:又名仓库,英文名repository.git就是Git的版本库。版本库里的所有文件都可以被Git管理起来,每个文件的修改、删除,Git 都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”在这里插入图片描述
  • 图中左侧为工作区,右侧为版本库。其中我们重点关注暂存区
  • 在创建 Git 版本库时,Git 会为我们自动创建一个唯一的 master分支,以及指向 master 的一个指针HEAD
  • 当对工作区修改(或新增)的文件执行git add命令,暂存区目录树的文件索引会被更新
  • 当执行git commit 提交操作,master分支会做出相应的更新,可以简单理解为暂存区的目录树才真正被写到版本库中

小总结:必须通过

  1. git add

  1. git commit

命令才能将工作区的文件添加到仓库(版本库)中进行管理

添加文件

在包含

  1. .git

的目录下新建一个ReadMe文件,使用

  1. git add

命令将文件添加到暂存区

  1. gitadd[file1][file2]#可添加一个或多个文件到暂存区gitadd[dir]#添加指定目录到暂存区gitadd.#添加当前目录下的所有文件改动到暂存区gitadd-f[file]#-f选项表示强制添加

再使用

  1. git commit

命令将暂存区内容添加到本地仓库中
注意:

  1. 提交时要对本次提交进行“描述”

,记录提交的细节,方便后续查看,得知此次提交改动了些什么,这是很重要的一步,也绝对不能省略

  1. git commit -m"描述"#提交暂存区全部内容到本地仓库中git commit [file1][file2]-m"描述"#提交暂存区的指定文件到本地仓库

成功提交后,Git会告诉我们一些改动的细节,示例:

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ touch ReadMe
  2. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ vim ReadMe
  3. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cat ReadMe
  4. hello git
  5. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ gitadd.
  6. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git commit -m"new file:ReadMe"[master (root-commit) 0008577] new file:ReadMe
  7. 1file changed, 1 insertion(+)
  8. create mode 100644 ReadMe

使用

  1. git log

命令可以查看历史提交记录

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git log
  2. commit 000857774f55793cf3ba54f014aaa239dc226609 (HEAD -> master)
  3. Author: bao-bao-hai-mian <1076847758@qq.com>
  4. Date: Sun Sep 1520:12:53 2024 +0800
  5. new file:ReadMe
  1. git log

显示从最近到最远的提交日志,并且可以看到commit 的描述信息
还可以加上

  1. --pretty=oneline

选项,让消息简洁些

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git log --pretty=oneline
  2. 000857774f55793cf3ba54f014aaa239dc226609 (HEAD -> master) new file:ReadMe

日志消息的一大串数字,是每次提交的

  1. commit id(版本号)

,是用 SHA1 计算出的一个很大的数字,用十六进制表示


再添加几个文件

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ touch file1 file2 file3
  2. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ gitadd.
  3. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git commit -m"add three file:file1 file2 file3"[master 7c61b82]add three file:file1 file2 file3
  4. 3 files changed, 0 insertions(+), 0 deletions(-)
  5. create mode 100644 file1
  6. create mode 100644 file2
  7. create mode 100644 file3

此时我们查看

  1. .git

的目录结构

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ tree .git
  2. .git
  3. ├── branches
  4. ├── COMMIT_EDITMSG
  5. ├── config
  6. ├── description
  7. ├── HEAD
  8. ├── hooks
  9. ├── applypatch-msg.sample
  10. ├── commit-msg.sample
  11. ├── fsmonitor-watchman.sample
  12. ├── post-update.sample
  13. ├── pre-applypatch.sample
  14. ├── pre-commit.sample
  15. ├── pre-merge-commit.sample
  16. ├── prepare-commit-msg.sample
  17. ├── pre-push.sample
  18. ├── pre-rebase.sample
  19. ├── pre-receive.sample
  20. ├── push-to-checkout.sample
  21. └── update.sample
  22. ├── index
  23. ├── info
  24. └── exclude
  25. ├── logs
  26. ├── HEAD
  27. └── refs
  28. └── heads
  29. └── master
  30. ├── objects
  31. ├── 00
  32. └── 0857774f55793cf3ba54f014aaa239dc226609
  33. ├── 0e
  34. └── 6b1780b73cd9220ec5073dc64b42f7ad4bd945
  35. ├── 15
  36. └── a37e9ef171cca4a5d985fccd1fcf9414b2c7cf
  37. ├── 4b
  38. └── 825dc642cb6eb9a060e54bf8d69288fbee4904
  39. ├── 7c
  40. └── 61b823e2c036bc9e37ec0d9676fab511b1db1e
  41. ├── 8d
  42. └── 0e41234f24b6da002d962a26c2495ea16a425f
  43. ├── e6
  44. └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
  45. ├── info
  46. └── pack
  47. └── refs
  48. ├── heads
  49. └── master
  50. └── tags

相比于之前我们看到的 .git 结构,多了一些东西

  1. index就是暂存区,git add后的内容都是添加到这里的
  2. HEAD是指向当前分支的指针,默认指向master
  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cat .git/HEAD
  2. ref: refs/heads/master

  1. master分支

,其实就是最近一次提交的

  1. commit id
  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cat .git/refs/heads/master
  2. 7c61b823e2c036bc9e37ec0d9676fab511b1db1e
  1. objects为 Git 的对象库,里面包含了创建的各个版本库对象及内容。当执行git add命令时,暂存区的目录树被更新,同时工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,就位于.git/objects目录下

这些文件不能直接使用

  1. cat

查看,都是经过

  1. sha(安全哈希算法)

加密过的文件,不过可以使用

  1. git cat-file

查看版本库对象内容

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git cat-file -p 000857774f55793cf3ba54f014aaa239dc226609
  2. tree 0e6b1780b73cd9220ec5073dc64b42f7ad4bd945
  3. author bao-bao-hai-mian <1076847758@qq.com>1726402373 +0800
  4. committer bao-bao-hai-mian <1076847758@qq.com>1726402373 +0800
  5. new file:ReadMe

显示正是一次提交的相关信息
其中还有一行

  1. tree commit id

,我们使用同样的方式查看

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git cat-file -p 0e6b1780b73cd9220ec5073dc64b42f7ad4bd945
  2. 100644 blob 8d0e41234f24b6da002d962a26c2495ea16a425f ReadMe

再看 ReadMe 对应的

  1. commit id
  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git cat-file -p 8d0e41234f24b6da002d962a26c2495ea16a425f
  2. hello git

这正是我们提交的 ReadMe 中的内容

小总结:本地 git 仓库中,有几个文件和目录很特殊

  • index:暂存区,git add后会更新其内容
  • HEAD:指向当前分支的指针
  • refs/heads/master:保存最近一次提交的commit id
  • objects:包含了创建的各个版本库对象及内容,此处可以简单理解为存放了 git 维护的所有修改

修改文件

  1. Git 跟踪并管理的并不是文件,而是修改

新增文件,删除文件,修改文件内容都是修改


示例:
往 ReadMe 文件中添加一些内容

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cat ReadMe
  2. hello git
  3. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ vim ReadMe
  4. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cat ReadMe
  5. hello git
  6. i am coding
  7. i am coding
  8. i am coding

此时工作区的ReadMe 与 暂存区和版本库的 ReadMe 内容是不同的。

  1. git status

命令用于查看当前仓库状态

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git status
  2. On branch master
  3. Changes not staged for commit:
  4. (use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)
  5. modified: ReadMe
  6. Untracked files:
  7. (use "git add <file>..." to include in what will be committed)
  8. .ReadMe.swp
  9. no changes added to commit (use "git add" and/or "git commit -a")

Git 检查到 工作区的 ReadMe 被修改了,但没有

  1. 添加和提交

,并建议我们更新修改

  1. gitdiff[file]#显示暂存区和工作区文件的差异gitdiff HEAD -- [file]#显示版本库和工作区文件的差异

在这里插入图片描述

版本回退

回退是版本控制器很重要的能力
使用

  1. git reset

回退版本,具体如何回退根据参数而定

  1. git reset [--soft |--mixed| --hard][HEAD]
  • --soft:对于工作区和暂存区的内容都不变,只是将版本库回退到某个指定版本
  • --mixed:默认选项,使用时可以不带该参数。将暂存区和版本库回退,工作区不变
  • --hard:工作区、暂存区、版本库都会回退
  • HEAD:要回退到的版本 - 指定commit id- HEAD 表示当前版本- HEAD^表示上个版本- HEAD^^表示上上个版本,以此类推- 以可以使用~数字表示- HEAD~0表示当前版本,HEAD~1表示上个版本,以此类推

如果当前有三个版本,我们想回退到版本二,可以通过

  1. git log

查看版本二的

  1. commit id

,但当我们回退到版本二后,再使用

  1. git log

将不会看到版本三的

  1. commit id

这是否意味着我们不能反悔,回到版本三呢?其实不是,可以使用

  1. git reflog

,其记录了本地的每一个日志

  1. ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git reflog
  2. 7c61b82 (HEAD -> master) HEAD@{0}: commit: add three file:file1 file2 file3
  3. 0008577 HEAD@{1}: commit (initial): new file:ReadMe

查出来的是部分的

  1. commit id

,Git 支持使用部分

  1. commit id

实现回退
回退的效果图如下:
在这里插入图片描述

撤销修改

撤销修改在不同的场景下有不同的操作

场景一
只撤销工作区的修改
如果我们对工作区的代码进行了较大幅度的改动,但没有添加与提交,此时我们想撤销这些修改,如何操作呢?
手动修改一定是不推荐的,可能会出现该删的没删,不该删的删了的情况
使用

  1. git diff

可以知道修改了什么,但还是需要手动修改,仍可能出现问题
Git 提供了

  1. git checkout -- [file]

命令让工作区的文件回到最近一次

  1. add

或者

  1. commit

时的状态,该命令一定要记得带上

  1. --

,不然会是完全不一样的效果

  1. git checkout -- [file]

场景二
只撤销暂存区的内容
直接使用

  1. git reset
  1. git reset --mixed[commit id]
  1. --mixed

选项就是只回退暂存区内容

场景三
只撤销版本库的内容
直接使用

  1. git reset
  1. git reset --hard HEAD^

使用

  1. --hard

选项,只回退版本库内容,

  1. HEAD^

表示回退到上一个版本

删除文件

在 Git 中,删除文件也是一次修改,可以使用

  1. add

  1. commit

,如果误删,那么还可以按照场景对修改进行撤销
Git 提供命令可以删除工作区文件,并

  1. add

,也删除暂存区内容

  1. gitrm[file]

不过还没有提交,所以还需要

  1. git commit

如此,文件就从版本库中被删除了

以上就是本篇博客的所有内容,感谢你的阅读
如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。
在这里插入图片描述

标签: git elasticsearch c++

本文转载自: https://blog.csdn.net/m0_72563041/article/details/142283911
版权归原作者 好想有猫猫 所有, 如有侵权,请联系我们删除。

“【Git】初识Git”的评论:

还没有评论