一、建立新仓库 linux
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 对象。
二、从现有仓库克隆
git clone url
git clone git@github.com:torvalds/linux.git
如从gitHub上克隆一份linux的源码,不只是克隆最新版本的源码,还克隆了全部数据仓库的历史版本,每一个文件的每个版本,这个时候及时服务器github 发生故障,能够用本地数据仓库重建服务器上的仓库。能够回复到从服务器克隆或最后更一次从服务器拉去的状态。在.git 目录中,已经保存了全部版本记录,本地文件夹即工做目录的全部文件删除了,而后从中取出最新版本的文件拷贝。
三、检查文件更新状态
要求肯定当前工做区和暂存区文件的状态,能够经过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 把改文件添加的暂存区,纳入的版本跟踪管理。
四、 添加文件到暂存区
$ 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命令添加到暂存区,已加入跟踪文件的范围。
五、版本提交
$ 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对象。
六、查看当前提交日志记录
$ git log
commit d4a498a197c24421acee5c5ff96cfbc7e5c3be9e
Author: andy<andy@gmail.com>
Date: Sat Mar 8 14:23:37 2014 +0800
this is test commit
commit 80071e48614361dc282473d6482e3faa9fec17d9
Author:andy<andy@gmail.com>
Date: Sat Mar 8 13:35:13 2014 +0800
1st commit
git log 命令查看当前版本
七、文件差别比较
工做区和暂存区文件比较用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<andy@gmail.com>
git commit --amend --reset-author
1 file changed, 1 insertion(+)
$ git diff
$ git diff --staged
$ git diff head
八、文件删除和移动
全部的工做区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
九、查看提交历史
$ git log
commit fc0166f53a45cfc4c17079e5e3454fb7e3136cb6
Author: andy<andy@gmail.com>
Date: Sat Mar 8 15:52:10 2014 +0800
test git diff
commit d6eab3a38aee0b25ac395899c82edd48723a2ea9
Author: andy<andy@gmail.com>
Date: Sat Mar 8 15:36:53 2014 +0800
enforce commit'
commit 85ec024140442df6db8625a07c2ee7369cceb704
Author: andy<andy@gmail.com>
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(后跟指定格式)
十、撤销操做
能够修改最后一次提交的内容,当你发现最近一次提交内容不正确时候,能够经过 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
十一、取消文件修改
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^ 引用 工做区 暂存区 完全消除