ToB企服应用市场:ToB评测及商务社交产业平台

标题: Git初识 [打印本页]

作者: 用户云卷云舒    时间: 2024-10-18 21:01
标题: Git初识
目录
工作区、暂存库、版本库
查看.git文件
修改文件
版本回退
打消修改
工作区的代码还没有add:
已经add,但没有commit:
已经add,而且也commit了:
删除文件

工作区、暂存库、版本库

   工作区:在电脑上写代码或文件的目录
  暂存区:存放在.git目录下的index文件
  版本库:隐蔽目录.git文件,版本库里的全部文件都可以被Git管理起来,每个文件的修改、删除Git都能跟踪,以便任何时刻都可以追踪历史,或者在某个时刻可以“还原”
  

      图中左侧为⼯作区,右侧为版本库。Git 的版本库⾥存了很多东西,此中最重要的就是暂存区。在创建 Git 版本库时,Git 会为我们⾃动创建⼀个唯⼀的 master 分⽀,以及指向 master 的⼀个指针叫 HEAD。当对⼯作区修改(或新增)的⽂件执⾏    git add    命令时,暂存区⽬录树的⽂件索引会被更新。   当执⾏提交操作    git commit    时,master 分⽀会做相应的更新,可以简单明白为暂存区的⽬录树才会被真正写到版本库中。    查看.git文件

   当执行文件的git add操作后,会在.git/objects/中存储blob对象存储文件内容(blob对象本身只包含文件的内容,不包含文件名或任何其他元数据)并在.git/index/存储对应的二进制文件,当执行git commit操作后,会将暂存区中的文件commit进master中。,同时commit id和tree会存储进.git/object/中
  1. .git/
  2. |-- branches
  3. |-- config
  4. |-- description
  5. |-- HEAD
  6. |-- hooks
  7. |   |-- applypatch-msg.sample
  8. |   |-- commit-msg.sample
  9. |   |-- post-update.sample
  10. |   |-- pre-applypatch.sample
  11. |   |-- pre-commit.sample
  12. |   |-- prepare-commit-msg.sample
  13. |   |-- pre-push.sample
  14. |   |-- pre-rebase.sample
  15. |   `-- update.sample
  16. |-- info
  17. |   `-- exclude
  18. |-- objects
  19. |   |-- info
  20. |   `-- pack
  21. `-- refs
  22.     |-- heads
  23.     `-- tags
复制代码
  HEAD是默认指向master分支的指针
  1. [paper@VM-16-16-centos ~]$ cat .git/HEAD
  2. ref: refs/heads/master
复制代码
  而master指向commit id,是执行提交对象的标识符,使用git cat-file -p [commit id]可以查看commit id中存储的内容,包罗tree、parent(commit id)、author、committer
  1. [paper@VM-16-16-centos ~]$ cat .git/refs/heads/master
  2. 886c3d8a8e78088374d4ecfdcceaf91467f49571
  3. [paper@VM-16-16-centos ~]$ git cat-file -p
  4. 886c3d8a8e78088374d4ecfdcceaf91467f49571
  5. tree cf987c72c4a93e8a2d35d2aab4a4b1085011006f
  6. parent f21b3cfa4aa4d2a1bea886feb4864cc8905b815d
  7. parent 93bc771fc95e137549ba4b58b9b597b3d46256c3
  8. author yuan-peiqi <paper13240347791@163.com> 1725344145 +0800
  9. committer yuan-peiqi <paper13240347791@163.com> 1725344145 +0800
  10. file1
复制代码
  在tree内可查看提交线上的blob对象
  1. [paper@VM-16-16-centos ~]$ git cat-file -p
  2. cf987c72c4a93e8a2d35d2aab4a4b1085011006f
  3. 100644 blob 7a8b68e7de2ee6ef4d404ff8c7d65a083c084d07        file1
复制代码
  在blob内可查看文件内写入的信息
  1. [paper@VM-16-16-centos ~]$ git cat-file -p
  2. 7a8b68e7de2ee6ef4d404ff8c7d65a083c084d07
  3. hello git
  4. hello world
  5. write aaa for new branch
复制代码

修改文件

   当执行commit命令之后,暂存区的内容被提交了,暂存区的文件依然存在,当更改工作区后,工作区的内容不会自动更新至暂存区,必要手动的git add才能实现将修改添加至暂存区,使用git status指令就可以查看修改后的文件是否完成添加和提交。
  1. [paper@VM-16-16-centos ~]$ git status
  2. # On branch master
  3. # Changes not staged for commit:
  4. #   (use "git add <file>..." to update what will be committed)
  5. #   (use "git checkout -- <file>..." to discard changes in working directory)
  6. #
  7. #        modified:   file1
  8. #
  9. # Untracked files:
  10. #   (use "git add <file>..." to include in what will be committed)
  11. #
  12. #        .bash_history
  13. #        .bash_logout
  14. #        .bash_profile
  15. #        .bashrc
  16. #        .cache/
  17. #        .gitconfig
  18. #        .viminfo
  19. #        file
  20. no changes added to commit (use "git add" and/or "git commit -a")
复制代码
  但git status指令只能查看状态,不能确切表现那边改动了,使用git diff [file]和git diff HEAD [file]可以查看那边出现改动,此中git diff [file]可以查看暂存区和工作区的差异,git diff HEAD [file]可以查看版本库和工作区的差异
  1. [paper@VM-16-16-centos ~]$ git diff file1
  2. diff --git a/file1 b/file1
  3. index 7a8b68e..0d99f10 100644
  4. --- a/file1
  5. +++ b/file1
  6. @@ -1,3 +1,4 @@
  7. hello git
  8. hello world
  9. write aaa for new branch
  10. +wirte bbb for new branch
  11. [paper@VM-16-16-centos ~]$ git diff HEAD file1
  12. diff --git a/file1 b/file1
  13. index 7a8b68e..0d99f10 100644
  14. --- a/file1
  15. +++ b/file1
  16. @@ -1,3 +1,4 @@
  17. hello git
  18. hello world
  19. write aaa for new branch
  20. +wirte bbb for new branch
复制代码

版本回退

      执⾏    git reset    命令⽤于回退版本,可以指定退回某⼀次提交的版本。要解释⼀下“回退”本质是要将版本库中的内容进⾏回退,⼯作区或暂存区是否回退由命令参数决定:        git reset    命令语法格式为:    git reset    [--   soft    | --   mixed    | --   hard   ] [   HEAD   ]     
       HEAD    说明:     
  
打消修改

工作区的代码还没有add:

   直接在文件中删除,或者使用git checkout --[file]指令
  1. [paper@VM-16-16-centos ~]$ vim file1
  2. [paper@VM-16-16-centos ~]$ cat file1
  3. hello git
  4. hello world
  5. write aaa for new branch
  6. 11111:
  7. [paper@VM-16-16-centos ~]$ git checkout -- file1
  8. [paper@VM-16-16-centos ~]$ cat file1
  9. hello git
  10. hello world
  11. write aaa for new branch
复制代码
已经add,但没有commit:

   此时在工作区和暂存区修改了,对于工作区可以使用上述方法打消修改,对于暂存区可以使用git reset --mixed指令修改
  1. [paper@VM-16-16-centos ~]$ git add file1
  2. [paper@VM-16-16-centos ~]$ git status
  3. # On branch master
  4. # Changes to be committed:
  5. #   (use "git reset HEAD <file>..." to unstage)
  6. #
  7. #        modified:   file1
  8. #
  9. # Untracked files:
  10. #   (use "git add <file>..." to include in what will be committed)
  11. #
  12. #        .bash_history
  13. #        .bash_logout
  14. #        .bash_profile
  15. #        .bashrc
  16. #        .cache/
  17. #        .file1.swp
  18. #        .gitconfig
  19. #        .viminfo
  20. #        file
  21. [paper@VM-16-16-centos ~]$ git reset HEAD file1
  22. Unstaged changes after reset:
  23. M        file1
  24. [paper@VM-16-16-centos ~]$ git status
  25. # On branch master
  26. # Changes not staged for commit:
  27. #   (use "git add <file>..." to update what will be committed)
  28. #   (use "git checkout -- <file>..." to discard changes in working directory)
  29. #
  30. #        modified:   file1
  31. #
  32. # Untracked files:
  33. #   (use "git add <file>..." to include in what will be committed)
  34. #
  35. #        .bash_history
  36. #        .bash_logout
  37. #        .bash_profile
  38. #        .bashrc
  39. #        .cache/
  40. #        .file1.swp
  41. #        .gitconfig
  42. #        .viminfo
  43. #        file
  44. no changes added to commit (use "git add" and/or "git commit -a")
  45. [paper@VM-16-16-centos ~]$ git checkout -- file1
  46. [paper@VM-16-16-centos ~]$ git status
  47. # On branch master
  48. # Untracked files:
  49. #   (use "git add <file>..." to include in what will be committed)
  50. #
  51. #        .bash_history
  52. #        .bash_logout
  53. #        .bash_profile
  54. #        .bashrc
  55. #        .cache/
  56. #        .file1.swp
  57. #        .gitconfig
  58. #        .viminfo
  59. #        file
  60. nothing added to commit but untracked files present (use "git add" to track)
复制代码
已经add,而且也commit了:

   可以直接使用git reset --hard HEAD^回退到上一个版本
  1. [paper@VM-16-16-centos ~]$ vim file1
  2. [paper@VM-16-16-centos ~]$ git add file1
  3. [paper@VM-16-16-centos ~]$ git commit -m "file1"
  4. [master 9348ccd] file1
  5. 1 file changed, 1 insertion(+)
  6. [paper@VM-16-16-centos ~]$ git reset --hard HEAD^
  7. HEAD is now at 886c3d8 file1
  8. [paper@VM-16-16-centos ~]$ git status
  9. # On branch master
  10. # Untracked files:
  11. #   (use "git add <file>..." to include in what will be committed)
  12. #
  13. #        .bash_history
  14. #        .bash_logout
  15. #        .bash_profile
  16. #        .bashrc
  17. #        .cache/
  18. #        .file1.swp
  19. #        .gitconfig
  20. #        .viminfo
  21. #        file
  22. nothing added to commit but untracked files present (use "git add" to track)
复制代码

删除文件

   删除文件不仅要删除工作区的文件还必要删除暂存区和版本库中的文件,用三部指令分别执行操作
  1. rm -rf file1
  2. git rm file1
  3. git commmit -m "delete file1"
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4