Git基本操作|青训营笔记

126 阅读6分钟

1. Git是什么

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Git 是一个免费的开源分布式版本控制系统,旨在快速高效地处理从小型项目到大型项目的所有内容。 Git 易于学习,占用空间小,性能快如闪电。 它超越了 Subversion、CVS、Perforce 和 ClearCase 等 SCM 工具,具有廉价的本地分支、方便的临时区域和多个工作流等特性。

1.1 与SVN比较:

类型工具特性
集中式版本控制SVN远程服务器保存代码,本地不保存代码
分布式版本控制Git每个代码仓库都记录版本历史,无论本地或者远端服务器

2. Git基本操作

2.1 Git基本命令

  1. 配置
    • git config
    • git remote
  2. 提交代码
    • git add
    • git commit
  3. 远端同步
    • 拉去代码
      • git clone
      • git pull
      • git fetch
    • 推送代码
      • git push

2.2 Git基本操作

1. 初始化仓库

使用git init命令初始化仓库: 截屏2023-05-26 15.13.18.png

--bare

Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory.

创建一个裸的存储库(纯Git目录,没有工作目录)。如果未设置“ git_dir`环境,则将其设置为当前工作目录。

--template=<template-directory>

Specify the directory from which templates will be used. (See the "TEMPLATE DIRECTORY" section below.)

指定将使用模板的目录。(请参阅下面的“模板目录”部分。)

--initial-branch=<branch-name>

Use the specified name for the initial branch in the newly created repository. If not specified, fall back to the default name (currently master, but this is subject to change in the future; the name can be customized via the init.defaultBranch configuration variable).

使用新创建的存储库中的初始分支的指定名称。如果未指定,请返回默认名称(当前“ Master”,但这可能会在将来更改;该名称可以通过Init.debefaultbranch配置变量进行自定义)。

  • 如此一来,Git仓库的目录结构就是下面这一的👇:

image.png

2. 常用配置

--global 全局配置 写入这个用户的全局配置文件

For writing options: write to global ~/.gitconfig file rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t.

--system 系统配置 写入这台机器的系统配置文件

For writing options: write to system-wide $(prefix)/etc/gitconfig rather than the repository .git/config.

--local 本地配置 写入这个仓库的配置文件 【默认

For writing options: write to the repository .git/config file. This is the default behavior.

如果不同级别的配置有重复,级别低点配置会覆盖高级别低配置,“就近原则”。

配置用户名、邮箱

git config --global user.name "xxxxxx"
git config --global user.email xxxxxxx@yeah.net

image.png 配置Instead Of

git config --global url.git@github.com:.insteadOf https://github.com/

image.png

配置完之后,.gitconfig文件的样子: 截屏2023-05-26 15.47.18.png

3. Git Remote 配置远程仓库

  1. 使用 git remote -v 查看远程仓库配置
  2. 使用 git remote add 添加远程仓库地址
  3. 可以通过 cat .git/config 查看具体配置信息 image.png

4. SSH免密配置

SSH通过公私钥机制,将公钥存放在服务端,实现免密访问。 目前的生成key的加密算法有dsa、rsa、ecdsa、ed25519,默认使用的是rsa,由于安全问题,不推荐使用dsa、rsa,优先推荐使用ed25519.

ssh-keygen -t ed25519 -C "xxxxxxxxx@yeah.net"

默认存放在当先用户的用户目录的.ssh/目录下

cat ~/.ssh/id_ed25519.pub

然后在相关平台添加ssh密钥即可: image.png

5. Git Add

新建一个文件,此时.git仓库还没有变化:

image.png 可以使用git status查看目前仓库的情况:

image.png

使用git add 命令添加追踪文件:

image.png 可以发现,objects目录下有新目录及文件:

image.png 使用git status 发现新建文件已经进入暂存状态。

6. Git Commit

使用git commit命令提交更改后,可以观察到objects目录下新的目录结构23和9b image.png 使用 git log 命令可以看到最新的提交,当前提交:9b1ea81e8a5dbdb510e43a4adbb91f30d8b6f1de image.png

修改readme.md之后再次查看:

image.png

image.png 可见此次objects多了1d、26、d1等文件结构

image.png git log 命令显示最新提交为: 1d6f547c4bbac918112578b5e84900e164a1664e 可以使用git cat-file -p 文件id 的方式读取objects文件 image.png 这些文件中,有些是源文件的副本,有些是commit之后的log文件,还有些是记录commit之后tree结构的文件 image.png

7. Objects

git中objects文件有四种:commit、tree、blob 以及 tag

  • blob : 存储文件内容
  • tree : 存储文件目录信息
  • commit : 存储提交信息,包括tree、parent-commit、author信息,一个commit对应唯一代码版本

可以使用commit信息得到 tree id: image.png

使用tree id得到目录树信息: image.png

最后使用blob id 得到文件信息: image.png

以下图片来自git-scm.com/book/en/v2/… image.png

8. Refs

使用 git checkout -b 命令新建一个分支,并切换到该分支 image.png 此时,观察到refs目录下出现debug image.png 使用cat观察refs文件,可以看到refs存储的就是当前分支到最新commit id image.png

refs/heads代表的是分支,refs/tags代表到是标签

9. Tag & Annotation Tag

  • Branch分支 一般用于开发阶段,可以不断添加commit进行迭代
  • Tag标签 表示的是一个稳定版本,指向对commit id一般不会更改

使用git tag 版本 命令添加tag image.png image.png

可以看到指向当前分支最新commit id image.png

Annotation Tag 一般用于添加一些额外信息: git tag -a v0.0.2 -m "version 0.0.2" image.png

可以观察到Annotation Tag指向了新的objects文件,该文件记录 commit id 以及附注信息: image.png

10. 历史版本

可以通过objects中的commit的parent id一层一层追溯历史版本 image.png

11. 修改历史版本

  1. commit --amend 命令 修改最近一次commit信息
  2. git rebase -i HEAD~n 修改或合并或删除最近n次commit

执行 git commit --amend 命令, 修改最近一次commit信息: image.png 观察到commit id信息发生变化: image.png

12. 悬空Objects

可以使用 git fsck --lost-found 命令寻找悬空的objects文件

13. Git GC

git gc命令可以删除一些不需要的objects文件,以及打包一些objects文件,从而减小仓库体积。

  1. 使用 git reflog expire --expire=now --all 使得操作记录日志马上过期,从而防止误操作导致数据丢失。
  2. 使用 git gc --prune=now 命令整理文件,--prune指定整理的多久之前的文件

image.png

gc之后的树结构:

image.png

14. Git Clone Pull Fetch

  1. Clone : 拉取完整仓库到本地,可以指定分支,深度。
  2. Pull : 拉取远端某分支,并和本地代码进行合并,等同于 git fetch + git merge
  3. Fetch : 仅拉取远端某代码,不进行merge合并操作

15. Git Push

一般使用 git push origin master 即可

image.png