首页

Linux系统中git

Git 起源

同生活中的许多伟大事件一样,Git 诞生于一个极富纷争大举创新的年代。Linux 内核开源项目有着为数众广的参与者。绝大多数的 Linux 内核维护工作都花在了提交补丁和保存归档的繁琐事务上(1991-2002年间)。到 2002 年,整个项目组开始启用分布式版本控制系统 BitKeeper 来管理和维护代码。到了 2005 年,开发 BitKeeper 的商业公司同 Linux 内核开源社区的合作关系结束,他们收回了免费使用 BitKeeper 的权力。这就迫使 Linux 开源社区(特别是 Linux 的缔造者 Linus Torvalds )不得不吸取教训,只有开发一套属于自己的版本控制系统才不至于重蹈覆辙。他们对新的系统制订了若干目标:

Git 和其他版本控制系统的主要差别在于,Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件内容的具体差异。这类系统(CVS,Subversion,Perforce,Bazaar 等等)每次记录有哪些文件作了更新,以及都更新了哪些行的什么内容.

Git 并不保存这些前后变化的差异数据。实际上,Git 更像是把变化的文件作快照后,记录在一个微型的文件系统中。每次提交更新时,它会纵览一遍所有文件的指纹信息并对文件作一快照,然后保存一个指向这次快照的索引。为提高性能,若文件没有变化,Git 不会再次保存,而只对上次保存的快照作一链接.这是 Git 同其他系统的重要区别。它完全颠覆了传统版本控制的套路,并对各个环节的实现方式作了新的设计。Git 更像是个小型的文件系统

1、创建新仓库

git init

touch test.txt

git add --a

git commit -m "fist commit"

初始化新仓库,在当前目录下由一个.git的目录,所有git需要的数据和资源都放在这个目录中,在当面目录下添加文件后,需要通过git add 添加到文件追踪管理(添加到暂存区,数据存放在.git/index 目录索引,数据内部保存在.git/objects 中), git commit -m "提交说明备注" 提交的信息会提交到数据仓库,数据提交到正式仓库,具体保存在.git/objects 中,如以上提交会包含一个commit,tree ,blob 对象。

2、从现有仓库克隆

git clone url

git clone [email protected]:torvalds/linux.git

如从gitHub上克隆一份linux的源码,不仅是克隆最新版本的源码,还克隆了所有数据仓库的历史版本,每个文件的每一个版本,这个时候及时服务器github 发生故障,可以用本地数据仓库重建服务器上的仓库。可以回复到从服务器克隆或最后更一次从服务器拉去的状态。在.git 目录中,已经保存了所有版本记录,本地文件夹即工作目录的所有文件删除了,然后从中取出最新版本的文件拷贝。

3、检查文件更新状态

要求确定当前工作区和暂存区文件的状态,可以通过git status 命令。在工作区和暂存区的目录状态可以查看。

git status

On branch master nothing to commit, working directory clean

当前在默认master 分支,当前工作目录和暂存区没有任何跟踪的文件,也没有任何文件提交后更改,也没有新增加,未被跟踪的文件。

notepad test.txt

notepad t.txt

修改test.txt文件,新添加一个t.txt 文件,查看当前文件状态。

$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working di
#
# modified: test.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# t.txt
no changes added to commit (use "git add" and/or "git commit -a")

新增加的文件t.txt 在未跟踪文件范围Untracked files 范围,需要通过git add 把改文件添加的暂存区,归入的版本跟踪管理。

4、 添加文件到暂存区

$ git add .
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: h.txt
# new file: test.txt

git add . 把当前所有目录文件所有新文件和修改文件添加到暂存区,如果test.txt 文件提示是 Changes not staged for commit ,说明此跟踪文件已经发生修改,但是还未添加到暂存区,把修改的文件通过git add 命令添加到暂存区后。提示Changes to be committed. 文件已经暂存,随时可以提交到仓库 。h.txt 新添加文件从未跟踪状态Untracked files ,通过git add命令添加到暂存区,已加入跟踪文件的范围。

5、版本提交

$ git commit -m "this is test commit"
[master d4a498a] this is test commit
git commit --amend --reset-author

2 files changed, 3 insertions(+)
create mode 100644 t.txt

通过git commit -m "xxx" 将当前暂存区的内容提交到仓库,本次commit 提交文件是在默认master分支,提交commit 对象存放在.git/objects 的d4/1498a... 的文件中,该文件指向一个树tree对象。

6、查看当前提交日志记录

$ git log
commit d4a498a197c24421acee5c5ff96cfbc7e5c3be9e
Author: andy<[email protected]>
Date: Sat Mar 8 14:23:37 2014 +0800

this is test commit

commit 80071e48614361dc282473d6482e3faa9fec17d9
Author:andy<[email protected]>
Date: Sat Mar 8 13:35:13 2014 +0800

1st commit

git log 命令查看当前版本

7、文件差异比较

工作区和暂存区文件比较用git diff 命令,暂存区和最近一天提交版本之间的差异,可以用git diff --cached/staged. 如下:

目前在test.txt 文件只有1111111 文件内容,我在文件test.txt中添加22222222等内容,比较当前暂存区和工作文件差异

$ notepad test.txt

$ git diff
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working d
#
# modified: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git diff --staged

可以发现工作区比暂存区test.txt文件多增加了22222222222222 内容。暂存区和数据仓库内容是完全相同的。同时看看当前工作区状态。现在我们吧刚刚修改的内容添加到暂存区,同时比较暂存区和数据仓库文件差异。

$ git add test.txt

$ git diff

$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test.txt
#

把工作区修改的内容更新到暂存区后,可以看出此时暂存区和工作区文件完全相同。状态是是已暂存,带提交状态。与此同时,我们可以比较暂存区和数据残酷之间的差异和比较。

$ git diff --staged
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

我们可以很清楚的看到当前暂存区和数据仓库版本比较。暂存区test.txt 内容比最近一次提交内容多22222222222222 一行数据。提交数据到数据仓库。我们现在可以把工作区目录和数据仓库比较,看看test.txt 直接的文件内容差异。

$ git diff head
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222

可以很立即看出工作区文件内容比较仓库有新修改的内容。此时我们提交更新所有文件都没有差异了。看看文件差异。

$ git commit -m "test git diff "
[master fc0166f] test git diff
Committer: andy<[email protected]>

git commit --amend --reset-author

1 file changed, 1 insertion(+)

$ git diff
$ git diff --staged
$ git diff head

8、文件删除和移动

所有的工作区rm xxx删除后,可以直接从数据仓库获取最近一次提交版本的内容 git rm xxx。直接从数据仓库删除此文件内容。

$ ls
h.txt test.txt

$ rm h.txt

$ ls
test.txt

$ git diff
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good

可以通过文某比较当前的工作目录h.txt 文件已经被删除了,工作区目录和暂存区比较文件差异也可以返现文件删除模式。接下来删除暂存区目录

$ git diff --staged

$ git rm h.txt
rm 'h.txt'

$ git diff --staged
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good
-

$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: h.txt
#

通过删除暂存区文件前后可以比较出文件的差异。此时文件也不在跟踪状态。还有我们仅仅希望删除暂存区的内容,不删除工作区的内容

git rm --cached -f h.txt 的方式来实现,特别是针对工作区有修改,要删除暂存区内容。

$ git reset --hard
HEAD is now at fc0166f test git diff

$ ls
h.txt test.txt

$ git rm --cached h.txt
rm 'h.txt'

$ git diff

$ git diff head
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good
-
$ git diff -cached
error: invalid option: -cached

$ git diff --cached h.txt
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good

移动

git 移动文件操作是是相对先移动复制一个新文件,删除原文件,添加新文件到跟踪范围。

$ ls
h.txt test.txt

$ git mv h.txt d.txt 结果是等同于,相当于以下三条命令

$ ls
d.txt test.txt

$ mv d.txt to.txt

$ git rm d.txt
rm 'd.txt'

$ git add to.txt

9、查看提交历史

$ git log
commit fc0166f53a45cfc4c17079e5e3454fb7e3136cb6
Author: andy<[email protected]>
Date: Sat Mar 8 15:52:10 2014 +0800

test git diff

commit d6eab3a38aee0b25ac395899c82edd48723a2ea9

Author: andy<[email protected]>

Date: Sat Mar 8 15:36:53 2014 +0800

enforce commit'

commit 85ec024140442df6db8625a07c2ee7369cceb704

Author: andy<[email protected]>

Date: Sat Mar 8 15:35:44 2014 +0800

com 3

git log 查看提交历史,git log -p -n 最近n 次提交的内能和差异。查看历史记录统计信息 git log --stat,查看提交的历史记录格式可自由选择。

$ git log --pretty=format:"%h - %an, %ar : %s"
fc0166f - yyf, 48 minutes ago : test git diff
d6eab3a - yyf, 63 minutes ago : enforce commit'
85ec024 - yyf, 65 minutes ago : com 3
d4a498a - unknown, 2 hours ago : this is test commit
80071e4 - unknown, 3 hours ago : 1st commit

列出了常用的格式占位符写法及其代表的意义。

选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明

$ git log --pretty=format:"%h %s" --graph
* 78d907a dev branch commit
* fc0166f test git diff
* d6eab3a enforce commit'
* 85ec024 com 3
* d4a498a this is test commit
* 80071e4 1st commit

选项 说明
-p 按补丁格式显示每个更新之间的差异。
--stat 显示每次更新的文件修改统计信息。
--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
--relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
--graph 显示 ASCII 图形表示的分支合并历史。
--pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)

10、撤销操作

可以修改最后一次提交的内容,当你发现最近一次提交内容不正确时候,可以通过 git commit --amend 修改

$ git commit --amend -m "modify commit"
[master c660522] modify commit

$ git log --oneline
c660522 modify commit
fc0166f test git diff
d6eab3a enforce commit'
85ec024 com 3
d4a498a this is test commit
80071e4 1st commit

11、取消文件修改

git reset | git reset head 将head指向的目录树重置的暂存区

git reset --soft head^ 工作区和暂存区不变,但是引用向前回退一次,当对最新提交不满意的时候,撤销最新提交以便更改

git commit -e -F .git/COMMIT_EDITMSG 以上两个命令相当于git commit --amend

git reset head^

git reset --mixed head^ 暂存区和引用回退到上一次提交之前

git reset --hard head^ 引用 工作区 暂存区 彻底消除