0. 命令速查

Git - 图1

Git - 图2

1. 创建版本库

  1. # 进入一个准备用作版本库的文件夹内
  2. Ahang@Ahang MINGW64 ~
  3. $ cd Documents/Git/
  4. # 初始化创建该库
  5. Ahang@Ahang MINGW64 ~/Documents/Git
  6. $ git init
  7. Initialized empty Git repository in C:/Users/Ahang/Documents/Git/.git/
  1. // 复制一个已创建的仓库:
  2. git clone ssh://user@domain.com/repo.git

2. 将文件添加到版本库中

  • 先在版本库的文件夹里面添加文件如:README,可以写入些内容
  • 再通过git add README添加至库中
  • 最后通过git commit -m "wrote a README file" 后面的" "内容可以根据实际情况填写
  1. # 可以通过dir, ls 查看当前目录下文件
  2. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  3. $ dir
  4. README
  5. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  6. $ ls
  7. README
  8. # 添加到版本库
  9. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  10. $ git add README
  11. # 提交到库内,并加以注解,方便自己和他人查看
  12. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  13. $ git commit -m "wrote a README file"
  14. [master (root-commit) ae27363] wrote a README file
  15. 1 file changed, 2 insertions(+)
  16. create mode 100644 README

对于同时添加多个文件时

  1. # 创建3个文件
  2. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  3. $ touch a
  4. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  5. $ touch c
  6. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  7. $ touch b
  8. # 报错示例:直接提交还没有添加时
  9. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  10. $ git commit -m "add 3 files"
  11. On branch master
  12. Untracked files:
  13. (use "git add <file>..." to include in what will be committed)
  14. a
  15. c
  16. b
  17. nothing added to commit but untracked files present (use "git add" to track)
  18. # 先添加,可以同时添加多个,空格隔开即可
  19. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  20. $ git add a b c
  21. # 再提交,会将目前添加还没有提交的全部提交
  22. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  23. $ git commit -m "add 3 files"
  24. [master 7602874] add 3 files
  25. 3 files changed, 0 insertions(+), 0 deletions(-)
  26. create mode 100644 a
  27. create mode 100644 b
  28. create mode 100644 c

3. 对仓库文件修改并保存

  • 查看仓库状态:git status
  • 查看修改后文件的区别:git diff README
  • 然后对文件保存通过git add READMEgit commit -m <detail>
  1. # 查看状态
  2. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  3. $ git status
  4. On branch master
  5. nothing to commit, working tree clean
  6. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  7. $ git status
  8. On branch master
  9. Changes not staged for commit:
  10. (use "git add <file>..." to update what will be committed)
  11. (use "git restore <file>..." to discard changes in working directory)
  12. modified: README
  13. no changes added to commit (use "git add" and/or "git commit -a")
  14. # 查看区别
  15. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  16. $ git diff README
  17. diff --git a/README b/README
  18. index 75977d0..682c059 100644
  19. --- a/README
  20. +++ b/README
  21. @@ -1,2 +1,4 @@
  22. Hello world!!!
  23. -This is my frist example!
  24. \ No newline at end of file
  25. +This is my second examples!
  26. +
  27. +add some word
  28. \ No newline at end of file
  29. # 文件保存
  30. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  31. $ git add README
  32. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  33. $ git status
  34. On branch master
  35. Changes to be committed:
  36. (use "git restore --staged <file>..." to unstage)
  37. modified: README
  38. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  39. $ git commit -m "add some word"
  40. [master adde561] add some word
  41. 1 file changed, 3 insertions(+), 1 deletion(-)
  42. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  43. $ git status
  44. On branch master
  45. nothing to commit, working tree clean

4. 对修改过的文件还原到修改前和修改后

  • 修改前后跳转:git reset --hard commit_id

    • 跳到修改前一次:commit_id 改为HEAD^
    • 跳到修改前两次:commit_id 改为HEAD^^
    • 跳到修改前10次:commit_id 改为HEAD~10
    • 跳转到修改后某个点:commit_id改为该版本对应得hash值前几个唯一的字母如 a3798
  • 查看提交的版本记录,对修改前后,查看每个版本对应得hash值:git loggit log --pretty=oneline

  • 回到跳转前版本,此时查看log没有,需要查看历史记录: git reflog

  • 原理:对多个版本的会记录下多个节点,通过HEAD指针指向当前版本文档,对文档的版本指定修改实质上也就是修改了HEAD指针的位置

—-vision1—-vision2—-vision3 <—HEAD

  1. # 查看提交的版本记录
  2. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  3. $ git log
  4. commit a47393fb96dea41a21d9d674b6e12faff06b6edc (HEAD -> master)
  5. Author: ahang1598 <hcie_zpc@163.com>
  6. Date: Fri Jul 17 21:44:58 2020 +0800
  7. add GPL
  8. commit adde56105ebed13fe13dd735343dff32d6ddb8a9
  9. Author: ahang1598 <hcie_zpc@163.com>
  10. Date: Fri Jul 17 21:34:55 2020 +0800
  11. add some word
  12. ~
  13. # 查看提交的版本记录简易版
  14. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  15. $ git log --pretty=oneline
  16. a47393fb96dea41a21d9d674b6e12faff06b6edc (HEAD -> master) add GPL
  17. adde56105ebed13fe13dd735343dff32d6ddb8a9 add some word
  18. ae27363a081d748911a88b6bbf86054f2373baf3 wrote a README file
  19. # 还原上一个版本
  20. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  21. $ git reset --hard HEAD^
  22. HEAD is now at adde561 add some word
  23. # 查看内容
  24. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  25. $ cat README
  26. Hello world!!!
  27. This is my second examples!
  28. add some word
  29. # 此时再查看提交的版本记录,就没有了最后一次提交的了
  30. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  31. $ git log
  32. commit adde56105ebed13fe13dd735343dff32d6ddb8a9 (HEAD -> master)
  33. Author: ahang1598 <hcie_zpc@163.com>
  34. Date: Fri Jul 17 21:34:55 2020 +0800
  35. add some word
  36. # 此时可以通过查看历史记录找到对应得commit_id
  37. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  38. $ git reflog
  39. adde561 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
  40. a47393f HEAD@{1}: reset: moving to a4739
  41. adde561 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
  42. a47393f HEAD@{3}: commit: add GPL
  43. adde561 (HEAD -> master) HEAD@{4}: commit: add some word
  44. cbbddf8 HEAD@{5}: commit: add 6 files
  45. 7602874 HEAD@{6}: commit: add 3 files
  46. ae27363 HEAD@{7}: commit (initial): wrote a README file
  47. # 跳转到指定的commit_id
  48. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  49. $ git reset --hard a4739
  50. HEAD is now at a47393f add GPL

5. 撤销修改

  • 命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

    • 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
    • 一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态

撤销修改☞回到最后一个状态

6. 撤销删除

  1. 对于已经在仓库/工作区里面的文件删除:git rm README
  2. 对于删除的文件恢复,先要从版本区恢复到暂存区:git restore --staged README
  3. 再从暂存区恢复到工作区和仓库中:git checkout -- README
  1. # 先删除
  2. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  3. $ git rm README
  4. rm 'README'
  5. # 查看状态,已删除
  6. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  7. $ git status
  8. On branch master
  9. Changes to be committed:
  10. (use "git restore --staged <file>..." to unstage)
  11. deleted: README
  12. # 此时还没有提交,文件仍在在仓库里面,但是工作区没有了
  13. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  14. $ git log --pretty=oneline
  15. 492042c21840a7feb35984cd8a782c2cac7711f1 (HEAD -> master) remove a
  16. 43ede64fbf10c501c8969c831aa92e53ef4bae07 add boss
  17. 385781f7b45fab5e48f51addc205ac982b7542ae add a delete
  18. 714b14f8c577a4b4dedc0b650c01f322bc127080 delete chinese character
  19. 0f77a88b30fbfe27f50a2255cffb646abdfc82a4 add some
  20. a47393fb96dea41a21d9d674b6e12faff06b6edc add GPL
  21. adde56105ebed13fe13dd735343dff32d6ddb8a9 add some word
  22. cbbddf8cf9394d2ed57523d87ca3a3546088bc5d add 6 files
  23. 7602874655e60aed97b4b7f0c3e5eec1a5f97a1b add 3 files
  24. ae27363a081d748911a88b6bbf86054f2373baf3 wrote a README file
  25. # 工作区已被删除
  26. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  27. $ ls
  28. a 'a[a-c]' b bb cc cc,dd dd
  29. # 直接从暂存区无法恢复
  30. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  31. $ git checkout -- README
  32. error: pathspec 'README' did not match any file(s) known to git
  33. # 从仓库里面恢复到暂存区
  34. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  35. $ git restore --staged README
  36. # 再从暂存区恢复到工作区
  37. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  38. $ git restore README
  39. Ahang@Ahang MINGW64 ~/Documents/Git (master)
  40. $ ls
  41. a 'a[a-c]' b bb cc cc,dd dd README

如果删除后提交了的话需要git reset --hard commit_id去回退操作

7. 连接github操作

7.1 首次将本地库同步到远程库中

  1. echo "# resume" >> README.md
  2. git init
  3. git add README.md
  4. git commit -m "first commit"
  5. git branch -M main
  6. git remote add origin git@github.com:ahang1598/resume.git
  7. git push -u origin main
  1. git remote add origin git@github.com:ahang1598/resume.git
  2. git branch -M main
  3. git push -u origin main

7.2 将本地库同步到远程库中

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改

7.2 将远程库克隆到本地

$ git clone git@github.com:ahang1598/spider.git

7.3 修改部分本地代码后同步远程库

思路是将整个git上的代码都clone下来,然后手动把自己的文件拉进指定文件夹,再上传上去

一.创建一个文件夹”text”来右击点git bash

  1. 1.git init
  2. 2.git clone [https].git

假设clone下来的文件名叫A

二.clone成功后我们打开A,将本地要上传的文件拖到指定文件夹内,然后git bash clone下来的文件A

  1. 1.git init 默认下载下来的文件中包含了初始化的文件,无需初始化
  2. 2.git add . 注意add后面有一个空格和一个点
  3. 3.git commit -m "提交的注释"
  4. git branch -M main 如果本地还是master
  5. 4. git push origin main

7.4 获取远程仓库并更新到本地仓库

git pull:相当于是从远程获取最新版本并merge到本地

  1. $ git checkout branch2
  2. $ git pull origin branch2

7.5在远程仓库和本地仓库都发生了变化后想要将本地修改提交到远程的流程:

  1. git add .,然后git commit提交本地仓库
  2. git pull拉取远程仓库,提示不能自动合并
  3. 手动解决矛盾,修改文件后再次git add.git commit
  4. git push成功