git 底子操作

金歌  论坛元老 | 2025-3-25 02:33:41 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1000|帖子 1000|积分 3000



1. git 的安装 与 卸载

1.1. git 的安装

判定是否安装 git
git --version
安装 git:
centos: sudo yum -y install git
ubuntu: sudo apt-get install git -y
windows: 3.安装git和图形化界面工具_哔哩哔哩_bilibili

1.2. git 的卸载

判定是否安装 git
git --version
卸载 git:
ubuntu: sudo apt-get remove git -y
2. 初始化创建当地仓库

为什么要创建当地仓库?
要提前说的是,仓库是举行版本控制的一个文件目次。我们要想对文件举行版本控制,就必须先创建一个仓库出来。
创建当地仓库: git init
当前目次下多了一个.git的隐藏文件,.git目次是 git 来跟踪管理仓库的, 不要手动修改这个目次内里的文件,否则改乱了,就把gi仓库给破坏了。
3. 配置当地仓库

先说配置指令: 
  1. git config [--global] user.name "Your Name"
  2. git config [--global] user.email "email@example.com"
  3. git config [--global] --unset user.name
  4. git config [--global] --unset user.email
  5. // 如果你想全局配置就加 [--global] 选项.
  6. // 如果你想取消 [--global] 配置的选项, 同样也需要带 [--global] 选项.
复制代码
 下面举行演示: 


4. 熟悉工作区, 暂存区, 版本库

工作区:是在电脑上你要写代码或文件的目次。
暂存区:英文叫stage或index.一般存放在。 在。git目次下的index文件 (.git/index)中,我们把暂存区有时也叫作索引l(index)
版本库:又名仓库,英文名repository.工作区有一个隐藏目次。git,它不算工作区,而是gi的版本库。这个版本库内里的全部文件都可以被gt管理起来,每个文件的修改,删除,git都能跟踪,以便任何时刻都可以追踪汗青,或者在将来某个时刻可以"还原".
 
下面这个图展示了工作区,暂存区和版本库之间的关系:

图中左侧为工作区,右侧为版本库。g的版本库里存了很多东西,其中最紧张的就是暂存区。
在创建git版本库时,git会为我们主动创建一个唯一的master分支,以及指向master的一个指针叫head.(分支和head的概念后面再说)
当对工作区修改(或新增)的文件执行git add命令时,暂存区目次树的文件索引会被更新。
当执行提交操作git commit时,master分支会做相应的更新,可以简朴明白为暂存区的目次树才会被真正写到版本库中。
对象库: 除了上面所说的.git 中存在的暂存区和版本库之外, 实际上 git 仓库真正存储文件的地方在对于对象库, 内里存放的是一个个对象, 每个对象内里的内容是你每次 add/commit 提交的全部文件的修改环境. 而对于暂存区和版本库实际上都是存储的是对象中文件索引的索引而已!

从上面我们可以总结出两点:


  • 工作区的文件并不为 git 所直接管理, git 是通过 add 和 commit 的方式把工作区修改的文件以对象的情势生存到 git 当中然后联合索引的方式举行管理的!
  • git 仓库中, 真正存储文件内容的是在对象库, 暂存库和 master 库存放的只是索引而已!

5. 场景一: 添加文件

git add filename + git commit
我们先touch一个文件, 然后给内里加两行代码. 

 然后, 我们给他add一下, 然后commit一下. 

我们可以再来写三个文件, 然后 add , commit 一下.


git log 打印日志(从近到远, 以每次 commit 为单位举行打印).


6. 查看第一次 commit 后, .git 的变化


  • 新增了 index

  • 查看 head 指针, 该指针指向 rets/heads/master.


    我们再查看一下rets/heads/master是个什么东西: 


    这个实际上是在我们.git仓库下的一个对象: 

  • 查看 git 对象仓库内里的内容, 不能用 git

    我们可以用 git cat-file filename 来举行查看: 

  • 这个 tree 是啥呢? 是一个 commit 时候我们暂存区的目次结构. 打印一下看看:


    打印出来了 4 行提示, 有 file1, 有 file2, file3, 和 file4.


    我们来查看一下file1和file2中的内容: 

  • 所以 git 仓库是通过对象来记载修改记载的!

tree 存放的内容.
你的明白根本正确。在 Git 中,commit 提交时,会将 暂存区(索引区)的内容 生成一个 tree 对象,这个 tree 记载了本次提交对应的 目次结构、文件列表、文件权限 等信息。
具体来说:

  • 暂存区的作用:用户通过 git add 将文件添加到暂存区,暂存区生存了即将提交的内容。
  • tree 的生成:执行 git commit 时,Git 会基于暂存区的内容生成 tree 对象,它像一个“目次快照”,记载提交时刻的文件构造情势(包罗文件、子目次及其对应哈希值)。
因此,tree 确实是 commit 时暂存区内容所对应的目次结构记载,它是 Git 实现版本管理的核心数据结构之一。
7. git 修改文件

首先先说一个修改的定义: 修改包罗新增, 删除, 修改文件内容
之后, 我们的 git 是管理文件修改的, 而非文件本身

下面举行操作演示:
让我们将 file1.txt 文件举行一次修改:

此时,仓库中的readme和我们工作区的readme是不同的,如何查看当前仓库的状态呢?git 状态呢?git
status命令用于查看在你前次提交之后是否有对文件举行再次修改。


        我们发现, 上面提示我们在暂存区没有修改, 但是在工作区有个修改是 ReadMe 文件. 那修改了啥内容呢? 我们不清楚, 因此需要查询一下:
git diff[file]命令用来表现暂存区和工作区文件的差异,表现的格式正是unix通用的diff 格式。也可以使用git diff HEAD -- [file]命令来查看版本库(这里的版本库指的是暂存区)和工作区文件的区别。

之后, 我们再把这次修改提交到版本库(暂存区)中.

git add之后,就没有看到上面no changes added to commit (use "gita dd" and/or "git commit -a" )的消息了。不过他提示我们暂存区和 master 中有区别, 接下来让我们继续git commit即可:

小结:
git status -> 用来查看工作区 与 暂存区, 暂存区 与 master 分支哪些文件发生了修改
git diff -> 命令用来表现暂存区和工作区文件的内容差异
8. 版本回退

之前我们也提到过,gt能够管理文件的汗青版本,这也是版本控制器紧张的能力。如果有一天你发现之前前的工作做的出现了很大的问题,需要在某个特定的汗青版本重新开始,这个时候,就需要版本回退的功能了。
执行gitreset命令用于回退版本,可以指定退回某一次提交的版本。要解释一下"回退"本质是要将版本库中的内容举行回退,工作区或暂存区是否回退由命令参数决定。
git reset命令语法格式为:git reset [--soft | --mixed | --hard] [HEAD]

  • --mixed




    • 为默认选项,使用时可以不消带该参数。该参数将暂存区的内容退回到指定提交版本内容,工作区文件保持不变。


  • --soft




    • 参数对于工作区和暂存区的内容都不变,只是将版本库回退到某个指定版本。


  • --hard




    • 参数将暂存区与工作区都退回到指定版本。切记工作区有未提交的代码时不要用这个命令,由于工作区会回滚,你没有提交的代码就再也找不回来了,所以使用该参数前肯定要慎重


  • HEAD说明:




    • 可直接写成commit id,表示指定退回的版本
    • HEAD表示当前版本
    • HEAD^上一个版本
    • HEAD^^上上一个版本
    • 以此类推...
    • 可以使用~数字表示:







      • HEAD~0表示当前版本
      • HEAD~1上一个版本
      • HEAD^2上上一个版本
      • 以此类推...



操作演示详解
为了方便测试回退功能, 我们下面先提交三个版本, 然后举行回退操作:

  • 第一次修改提交




    • 查看ReadMe文件内容:

  1. hyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1
复制代码




    • 添加ReadMe文件到暂存区:

  1. hyb@139-159-150-152:~/gitcode$ git add ReadMe
复制代码




    • 提交修改:

  1. hyb@139-159-150-152:~/gitcode$ git commit -m"add version1"
  2. [master cff9d1e] add version1
  3. 1 file changed, 1 insertion(+)
复制代码

  • 第二次修改提交




    • 再次查看ReadMe文件内容:

  1. hyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2
复制代码




    • 添加ReadMe文件到暂存区:

  1. hyb@139-159-150-152:~/gitcode$ git add ReadMe
复制代码




    • 提交修改:

  1. hyb@139-159-150-152:~/gitcode$ git commit -m"add version2"
  2. [master d95c13f] add version2
  3. 1 file changed, 1 insertion(+)
复制代码

  • 第三次修改提交




    • 再次查看ReadMe文件内容:

  1. hyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3
复制代码




    • 添加ReadMe文件到暂存区:

  1. hyb@139-159-150-152:~/gitcode$ git add ReadMe
复制代码




    • 提交修改:

  1. hyb@139-159-150-152:~/gitcode$ git commit -m"add version3"
  2. [master d95c13f] add version3
  3. 1 file changed, 1 insertion(+)
复制代码

  • 查看汗青提交记载




    • 使用git log查看提交记载:

  1. hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline # 打印日志
  2. d95c13ffc87a855a25a3d04e22abfc7d2e31383 (HEAD -> master) add version3
  3. 14c12c32464d6ead7159f5c24e786ce450c89ddd add version2
  4. cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1
复制代码

现在,如果我们在提交完version3后,发现version3编写错误,想回退到version2,重新基于version2开始编写。由于我们在这里希望的是将工作区的内容也回退到version 2版本,所以需要用到--hard参数,示例如下:

  • 执行git reset命令




    • 使用--hard参数将工作区回退到version2版本:

  1. hyb@139-159-150-152:~/gitcode$ git reset --hard 14c12c32464d6ead7159f5c24e786ce450c89ddd
复制代码




    • 此时HEAD指向add version2:

  1. HEAD is now at 14c12c3 add version2
复制代码

  • 查看ReadMe文件内容




    • 查看ReadMe文件,发现内容已经回退到version2:

  1. hyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2
复制代码

  • 查看提交日志




    • 使用git log查看提交日志,发现HEAD指向version2:

  1. hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline
  2. 14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) add version2
  3. cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1
复制代码
文中还提到,惊奇地发现ReadMe文件的内容已经回退到version2,并且HEAD也指向了version2。



不过, 我突然又想归去了, 那有机会归去吗? 如果你执行的 git 操作比较少的话, 是有机会归去的, 但是如果在此底子上你又执行了大量的 git 操作, 就很难回到之前的版本了. 由于我们上面完全没有执行任何操作, 所以可以归去的, 如何归去看下面演示即可.
以下是提取的代码和笔墨:
到这里一般回退功能就演示完了,但现在如果我悔恨了,想再回到version3怎么办?我们可以继续使用git reset命令,回退到version 3版本,但我们必须要拿到version 3的commit id去指定回退的版本。
但我们看到了git log并不能打印出version 3的commit id,运气好的话我们可以从终端上去找找之前的记载,运气欠好的话commit id已经被我们搞丢了=。=
Git还提供了一个git reflog命令能调停一下,该命令用来记载当地的每一次操作。
代码部分:


  • 使用git reflog查看操作记载:
  1. hyb@139-159-150-152:~/gitcode$ git reflog
  2. 14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) HEAD@{0}: reset: moving to 14c12c32464d6ead7159f5c24e786ce450c89ddd
  3. d95c13ffc87a855a25a3d04e22abfc7d2e31383 HEAD@{1}: commit: add version3
  4. 14c12c32464d6ead7159f5c24e786ce450c89ddd (HEAD -> master) HEAD@{2}: commit: add version2
  5. cff9d1e01933318156f8c7d356a78c9e949ae6b7 HEAD@{3}: commit: add version1
  6. 94da6950d27e26230368b22f1ffc4bbf761b5b00 HEAD@{4}: commit: add Modify ReadMe file
  7. 23807c5693698cd868c4fb264997ca575756eed HEAD@{5}: commit: add 3 files
  8. c61428926f38534d4ec6dde9044150be6c1abc6c HEAD@{6}: commit (initial): commit my first file
复制代码

  • 回退到version3:
  1. # 回退到v3
  2. hyb@139-159-150-152:~/gitcode$ git reset --hard d95c13f
  3. HEAD is now at d95c13f add version3
复制代码

  • 查看工作区:
  1. # 查看工作区hyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3
复制代码

  • 查看log:
  1. # 查看log
  2. hyb@139-159-150-152:~/gitcode$ git log --pretty=oneline
  3. d95c13ffc87a855a25a3d04e22abfc7d2e31383 (HEAD -> master) add version3
  4. 14c12c32464d6ead7159f5c24e786ce450c89ddd add version2
  5. cff9d1e01933318156f8c7d356a78c9e949ae6b7 add version1
复制代码

git 为什么版本更换非常快? 几乎不需要时间? 这与他的设计结构有关, 由于 git 是记载的是修改本身, 而非文件本身:


9. 撤销修改

9.1. 环境一:没有add,也没有commit

命令: git checkout -- [file]
情景举例
  1. # 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterNothing to commit, working tree cleanhyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3This piece of code is like shit #新增代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory)    modified:   ReadMeno changes added to commit (use "git add" and/or "git commit -a")# 直接删除代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  6. hello bit
  7. hello git
  8. hello world
  9. hello version1hello version2hello version3hyb@139-159-150-152:~/gitcode$ git statusOn branch masternothing to commit, working tree clean
复制代码
幸亏我们工作服从不高,才写了一行代码就发现不行了,要是你写了3天,不绝都没有提交,该怎么删掉呢?你本身都忘了本身新增过哪些,有同学说,我可以git diff xxx一下,看看差别在哪啊,那你肯定又要花3天时间删代码了,并且很大的概率还会改错。一周过去了,你怎么向你的老板交代呢?
Git其实还为我们提供了更好的方式,我们可以使用git checkout -- [file]命令让工作区的文件回到迩来一次add或commit时的状态。要留意git checkout -- [file]命令中的--很紧张,切记不要省略,一旦省略,该命令就变为其他意思了,后面我们再说。示例如下:
  1. # 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3This piece of code is like shit #新增代码hyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory)    modified:   ReadMeno changes added to commit (use "git add" and/or "git commit -a")# 规复到上一次add或commithyb@139-159-150-152:~/gitcode$ git checkout -- ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  6. hello bit
  7. hello git
  8. hello world
  9. hello version1hello version2hello version3
复制代码
9.2. 环境二:已经add,但没有commit

add 之后生存到了暂存区, 如何撤销呢?
我们下面来举个例子:
  1. # 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3This piece of code is like shit #新增代码# add 存入暂存区hyb@139-159-150-152:~/gitcode$ git add ReadMehyb@139-159-150-152:~/gitcode$ git statusOn branch masterChanges to be committed:  (use "git restore --staged <file>..." to unstage)    modified:   ReadMe
复制代码
让我们往返想一下学过的`git reset`回退命令,该命令如果使用`--mixed`参数,可以将暂存区的内容退回到指定的版本内容,但工作区文件保持不变。那我们就可以回退下暂存区的内容了!!!
以下是提取的代码和笔墨:
  1. # --mixed是默认参数,使用时可以省略
  2. hyb@139-159-150-152:~/gitcode$ git reset HEAD ReadMe
  3. Unstaged changes after reset:
  4. M  ReadMe
  5. # 用 git status 查看一下,发现现在暂存区是干净的,工作区有修改。
  6. # 查看状态
  7. hyb@139-159-150-152:~/gitcode$ 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")
复制代码
还记得如何丢弃工作区的代码吗?
  1. hyb@139-159-150-152:~/gitcode$ git checkout -- ReadMehyb@139-159-150-152:~/gitcode$ git statusOn branch masternothing to commit, working tree cleanhyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3
复制代码
9.3. 已经 add 并且也已经 commit 了

不要担心,我们可以`git reset --hard HEAD^`回退到上一个版本!不过,这是有条件的,就是你还没有把本身的当地版本库推送到远程。还记得Git是分布式版本控制体系吗?我们后面会讲到远程版本库,一旦你推送到远程版本库,你就真的惨了……
  1. # 向ReadMe中新增一行代码hyb@139-159-150-152:~/gitcode$ vim ReadMehyb@139-159-150-152:~/gitcode$ cat ReadMe
  2. hello bit
  3. hello git
  4. hello world
  5. hello version1hello version2hello version3This piece of code is like shit #新增代码# 提交hyb@139-159-150-152:~/gitcode$ git add ReadMehyb@139-159-150-152:~/gitcode$ git commit -m"test quash"[master 5f71ae1] test quash1 file changed, 1 insertion(+)# 回退hyb@139-159-150-152:~/gitcode$ git reset --hard HEAD^HEAD is now at 144a3f8 add filehyb@139-159-150-152:~/gitcode$ cat ReadMe
  6. hello bit
  7. hello git
  8. hello world
  9. hello version1hello version2hello version3
复制代码
10. 删除文件


git 中,删除也是一个修改操作,我们实战一下,如果要删除file5文件,怎么搞呢?如果你这样做故了:
  1. hyb@139-159-150-152:~/gitcode$ ls
  2. file1 file2 file3 file4 file5 ReadMe
  3. hyb@139-159-150-152:~/gitcode$ rm file5
复制代码
但这样直接删除是没有用的,反而徒增烦恼,git status命令会立刻告诉你哪些文件被删除了:
  1. hyb@139-159-150-152:~/gitcode$ git status
  2. On branch master
  3. Changes not staged for commit:
  4.   (use "git add/rm <file>..." to update what will be committed)
  5.   (use "git restore <file>..." to discard changes in working directory)
  6.     deleted:    file5
  7. no changes added to commit (use "git add" and/or "git commit -a")
复制代码
此时,工作区和版本库就不同等了,要删文件,目前除了要删工作区的文件,还要扫除版本库的文件。
一般走到这里,有两种可能:


  • 确实要从版本库中删除该文件
  • 不小心删错了
对第二种环境,很显着误删,需要使用git来举行规复,很简朴,我们刚学过(删除也是修改):
  1. hyb@139-159-150-152:~/gitcode$ git checkout -- file5
  2. hyb@139-159-150-152:~/gitcode$ ls
  3. file1 file2 file3 file4 file5 ReadMe
复制代码
对于第一种环境,很显着是没有删完,我们只删除了工作区的文件。这时就需要使用git rm将文件从暂存区和工作区中删除,并且commit:
  1. hyb@139-159-150-152:~/gitcode$ git rm file5
  2. rm 'file5'
  3. hyb@139-159-150-152:~/gitcode$ git status
  4. On branch master
  5. Changes to be committed:
  6.   (use "git restore --staged <file>..." to unstage)
  7.     deleted:    file5
  8. hyb@139-159-150-152:~/gitcode$ git commit -m"deleted file5"
  9. [master 5476bde] deleted file5
  10. 1 file changed, 0 insertions(+), 0 deletions(-)
  11. delete mode 100644 file5
  12. hyb@139-159-150-152:~/gitcode$ git status
  13. On branch master
  14. nothing to commit, working tree clean
复制代码
现在,文件就从版本库中被删除了。

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表