git笔记梳理及命令复习|青训营笔记

119 阅读4分钟

git笔记梳理

git抽象出的的基本结构

截屏2023-06-04 11.55.32.png

git具有三个分区

  • 工作区(Working Directory)开发者直接编辑的地方
  • 暂存区(Staging Area)数据暂时存放的地方
  • 仓库(Repository)存放已提交数据的地方,分为本地仓库和远程仓库

Git仓库

.git

截屏2023-06-04 11.55.25.png

  • head:存储当前指向的分支
  • config:存储当前仓库的配置
  • hooks:配置hook
  • objects:一些文件信息,将在下面重点介绍
  • refs:存储分支信息

objects

commit/tree/blob在git中统称为objects,除此之外还有tag

(可以用git tag v0.0.1给当前状态打上一个tag,代表一个版本,有tag的commit一般不会变化)

  • blob存储文件内容
  • tree存储文件的信息
  • commit存储提交信息,一个commit可以对应唯一版本的代码

三种文件的组织方式如下:

  1. 通过commit寻找到tree信息,每个commit都会存储对应的ID信息
  2. 通过tree存储的信息,获取到对应的目录树信息
  3. 从tree中获得blob的ID,通过Blob ID获取对应的文件内容

截屏2023-06-04 14.59.36.png

完整的git视图

截屏2023-06-04 15.20.21.png

代码合并

Fast-forward

不会产生新的分支,使用方式:git merge test(分支名) --ff-only

截屏2023-06-04 15.32.59.png

Three-way merge

会产生新的分支,使用方式:git merge (test)分支名 --no-ff

截屏2023-06-04 15.33.27.png

使用原则

截屏2023-06-04 15.35.05.png

git命令复习(以平时使用的逻辑)

0. overview

    start a working area (see also: git help tutorial)
       clone     Clone a repository into a new directory
       init      Create an empty Git repository or reinitialize an existing one

    work on the current change (see also: git help everyday)
       add       Add file contents to the index
       mv        Move or rename a file, a directory, or a symlink
       restore   Restore working tree files
       rm        Remove files from the working tree and from the index

    examine the history and state (see also: git help revisions)
       bisect    Use binary search to find the commit that introduced a bug
       diff      Show changes between commits, commit and working tree, etc
       grep      Print lines matching a pattern
       log       Show commit logs
       show      Show various types of objects
       status    Show the working tree status

    grow, mark and tweak your common history
       branch    List, create, or delete branches
       commit    Record changes to the repository
       merge     Join two or more development histories together
       rebase    Reapply commits on top of another base tip
       reset     Reset current HEAD to the specified state
       switch    Switch branches
       tag       Create, list, delete or verify a tag object signed with GPG

    collaborate (see also: git help workflows)
       fetch     Download objects and refs from another repository
       pull      Fetch from and integrate with another repository or a local branch
       push      Update remote refs along with associated objects

1. 创建git仓库

git init

初始化创建空git仓库。本质上就是产生一个.git文件夹。

git clone

用于从远端拉取一个git仓库到当前文件夹下,有两种方式:http以及ssh

2.从修改一个文件到commit到remote库的全流程

  1. 修改.gitignore,将不想commit的文件写入,git会将其自动忽略

  2. git add filename 或*(代表有改动的全部文件)将文件放入暂存区

    git status可以查询当前状态

    git restore可以将已经add的文件从暂存区取出

  3. git commit -m 将暂存区中的文件放入本地库中,-m参数后为本次commit的消息

  4. git pull 将本次commit推入远端仓库

3.如何从远端库拉取更新

git fetch

用于查看远端仓库是否有更新。如果有更新将会下载下来,如果没有更新,就没有反应

但值得注意的是fetch指令只会将更新下载到本地库的远端分支中,工作区不会看到改变

git merge

git merge后加上分支名可以将该分支合并到当前分支。可以配合git checkout使用,git checkout会将分支版本与其他分支进行对比。

git pull

拉取远端仓库的更新并合并到当前分支上,相当于git fetchgit merge的合体。

4.分支

git branch -v

查看所有分支信息

git branch

用于创建新的分支,分支名需要加在后面

git checkout

用于切换不同分支

5.撤销提交

git rest(撤销本地提交)

git reset分为是否保留修改

  • 保留修改:git reset --soft,你之前所作的更改都在,这也称为最安全的撤销。
  • 舍弃修改:git reset --hard,你之前所作的更改都会消失,所以要谨慎使用。

git revert(撤销远程操作)

使用git revert可以回滚远程仓库的commit,但会在远程仓库产生一个新的commit,名为Revert“···”。

如果不想让产生这个提交,可以用git rebase -i HEAD~x整理合并提交