专栏名称: 生信媛
生信媛,从1人分享,到8人同行。坚持分享生信入门方法与课程,持续记录生信相关的分析pipeline, python和R在生物信息学中的利用。内容涵盖服务器使用、基因组转录组分析以及群体遗传。
目录
相关文章推荐
51好读  ›  专栏  ›  生信媛

深入理解git逻辑(上)

生信媛  · 公众号  · 生物  · 2023-06-06 14:17

正文

git是分布式版本管理系统,svn为中心化版本管理系统。svn在客户端不维护版本,客户端的改动均需提交到服务器端后才进行受控,也只能从服务器端拉回其他人的修改。git在本地维护版本库,版本库可自由复制,同个项目的不同版本库之间地位相同,当前版本库可向任意其他版本库推送、从其他任意版本库拉回修改。github等远程仓库存在的意义仅为方便交换。

1 单版本库操作

整个git受控的文件夹构成一个版本库。文件在版本库中存在三个阶段:

  • 工作区(working directory)是我们在当前仓库中直接打开文件时能看到的版本;
  • 暂存区(Stage )为数据暂时存放的区域,可在工作区和版本库之间进行友好的数据交换;
  • 版本库(repository)是当前版本库与其他版本库交流(pull,push)时使用的版本。

增量模式

初始化git仓库

$git init
Initialized empty Git repository in /home/USER/git_learn/original_repository/virtual_project/.git/

git add [path...] 用于从工作区向暂存区提交修改, git commit 用于从暂存区向版本库提交修改。

$echo "This line will be the first commit." > readme
$git add readme
$git commit -m "first commit"

$
echo "This line will be the second commit." >> readme
$git add readme
$git commit -m "second commit"

$
echo "This line will be staged but not commited." >> readme
$git add readme

$
echo "This line just modified in the workong directory." >> readme

增量操作结束,此时readme文件中的内容为:

$cat readme
This line will be the first commit.
This line will be the second commit.
This line will be staged but not commited.
This line just modified in the workong directory.

查看git仓库状态,readme中同时存在未暂存和未提交的修改。

$git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
 modified:   readme

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
 modified:   readme

查看git仓库历史,共提交了两个版本。根据提交的时间自动维护顺序,最后一次的提交为HEAD,倒数第二次为HEAD^

$git log
commit c03032e5403cf4b96e30d217e0c39d19d9e92ad1 (HEAD -> master)
Author: wyj1 
Date:   Thu Nov 28 16:07:28 2019 +0800

    second commit

commit 417709fbbc5b3d1f70d24efa2b54edc7f6d3cdbb
Author: wyj1 
Date:   Thu Nov 28 16:06:22 2019 +0800

    first commit

改动查看

在文件夹中打开文件,仅能看到工作区的文件内容,暂存区和版本库中的内容无法直接查看,但可以通过与工作区文件的比较间接实现。

工作区与暂存区:

$git diff readme
diff --git a/readme b/readme
index 7d4a151..7010dbf 100644
--- a/readme
+++ b/readme
@@ -1,3 +1,4 @@
 This line will be the first commit.
 This line will be the second commit.
 This line will be staged but not commited.
+This line just modified in the workong directory.

暂存区与版本库:

$git diff --cached readme
diff --git a/readme b/readme
index 741db84..7d4a151 100644
--- a/readme
+++ b/readme
@@ -1,2 +1,3 @@
 This line will be the first commit.
 This line will be the second commit.
+This line will be staged but not commited.

工作区与版本库,哈希值可由HEAD代替:

$






请到「今天看啥」查看全文