Git Book读书笔记--Git的基础

来源:互联网 发布:淘宝如何买二手东西 编辑:程序博客网 时间:2024/06/05 21:53

Git Book中文版

获取Git仓库

获取git项目仓库的方法有2种,一是在现有项目或目录下导入所有文件到Git;二是从一个服务器克隆一个git仓库。

  • 在现有目录中初始化仓库

创建一个目录,在目录下执行下面git命令

$ git init

然后会生成一个.git文件,这个目录写的所有文件都会在git仓库中。在通过git addgit commit添加到git本地仓库。

  • 从服务器中克隆一个
$ git clone [url]

git的三种状态:

  • 已提交(git本地仓库)

  • 已修改(非暂存区)

  • 已暂存(staged/index)

image

记录每次更新到仓库

在工作目录下的每个文件都不外乎这2种状态:已跟踪或未跟踪。

  • 已跟踪文件:是指那些被纳入了版本控制的文件,在上一次快照(提交)中有他们的记录,在工作一段时间后,他们的状态可能处于未修改,已修改或已放入暂存区。使用git add命令添加到git工作区的文件

初次克隆某个仓库的时候,工作目录中的所有文件都属于已跟踪文件,并处于未修改状态。

  • 未跟踪文件:未使用git add命令添加到git暂存区中。
    (未跟踪文件和.gitignore文件的区别?)

  • 已修改文件:某些文件自上次提交后对它们做了修改,git将它们标记为已修改文件。
    (如果第一次add一个文件,之后对他进行修改,算已修改文件?)

  • 未修改文件:提交到git本地仓库的文件或是第一次add进入暂存区的文件。

检查当前文件的状态

$ git status  # 查看文件的状态(git有3种状态,已提交,已暂存,已修改)

git status操作结果

  • 1.新建一个git仓库(执行git init命令)后,执行git status,结果是:
$ git statusOn branch master  # 表示分支为master分支Initial commit  # 初始commitnothing to commit (create/copy files and use "git add" to track) # 没有东西提交(创建或复制一个文件,使用git add 命令添加到已追踪文件)
  • 2.创建一个1.txt文件,执行git status命令,结果为:
$ git statusOn branch masterInitial commit# 未追踪的文件1.txt(使用git add命令添加到暂存区中)Untracked files:  (use "git add <file>..." to include in what will be committed)        1.txt# 当前是一个未追踪的文件(使用git add命令添加到已追踪文件),nothing added to commit but untracked files present (use "git add" to track)
  • 3.对1.txt执行git add命令后,执行git status命令,结果为:
$ git statusOn branch masterInitial commit# 想变回commited(提交)状态(使用 git rm --cache 命令从暂存区移除),这时已经是处于暂存区。Changes to be committed:  (use "git rm --cached <file>..." to unstage)        new file:   1.txt
  • 4.对1.txt执行git commit命令后。执行git status命令,结果为:
$ git statusOn branch master# 没有东西提交(暂存区没有东西),工作区干净了nothing to commit, working tree clean
  • 5.创建一个新文件2.txt,执行git add命令添加2.txt,然后对2.txt文件进行修改,在执行git status命令。结果为:
$ git statusOn branch master# 2.txt文件已经添加到暂存区,想要变回commited状态(使用git reset命令)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   2.txt# 不在暂存区(使用 git add添加到暂存区,或者使用 git checkout从工作区丢弃)Changes not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   2.txt

忽略文件

在git管理中,总是有一些自动生成的文件,比如日志文件,编译过程中创建的临时文件等,是我们不需要纳入git版本控制中的,那么我们可以创建一个.gitignore文件,将需要忽略的文件写入,这样这些忽略的文件就不会进入git版本控制。

.gitignore文件规范

  • 所有空行或者#开头的行都会被git忽略

  • 可以使用glob模式匹配

  • 匹配模式可以以/开头,防止递归
  • 匹配模式可以以/结尾,指定目录
  • 要忽略制定模式以外的文件或目录,可以在模式前加上!取反。
# 忽略以.a结尾所有文件*.a# 不忽略lib.a文件,即使使用了*.a!lib.a# 忽略当前目录的README文件,不包括子目录下的README文件/README# 忽略build目录下的所有文件build/# 忽略doc目录下的.txt文件,不包括子目录下的.txt文件doc/*.txt# 忽略doc目录下的所有.txt文件,包括子目录doc/**/*.txt

查看已暂存和未暂存的修改

$ git diff  # 查看文件修改前后的不同

git diff:查看的是未暂存区的文件(已修改文件)与之前提交的文件(未修改文件)之间的差异。

diff --git a/1.txt b/1.txtindex 6848c67..6b2ee64 100644--- a/1.txt+++ b/1.txt@@ -1 +1,3 @@-first--- 1.txt  # 未修改文件的内容\ No newline at end of file  # 文件结束+first--- 1.txt++second   # 修改的文件内容\ No newline at end of file

提交更新

$ git commit  # 提交到git本地仓库
$ git commit 1.txt -m'commit 1.txt '[master c92181b] commit 1.txt  # c92181b是SHA-1值,表示本地git仓库存储的标识# 一个文件改变过,这个文件插入5条数据,删除1条数据 1 file changed, 5 insertions(+), 1 deletion(-)

移除文件

$ git rm # 移除commit之后的文件,直接删除文件

如果当前文件处于已修改状态,执行git rm命令,结果为:

$ git rm 2.txt# 本地文件已经修改,删除报错error: the following file has local modifications:    2.txt# --cached:追踪文件移除为未追踪文件# -f:暴力删除文件(use --cached to keep the file, or -f to force removal)

如果文件处于未修改状态(已提交状态),执行git rm命令,结果为

$ git rm 1.txtrm '1.txt'$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)        deleted:    1.txt

这样文件就会删除。

移动文件

$ git mv 
  • 移动
git mv a.txt 1/a.txt # 将根目录下的a.txt文件移动到1目录下$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)        renamed:    a.txt -> 1/a.txt
  • 重命名
git mv file_from file_to # 对文件进行重命名
$ git mv a.txt b.txt # 由a.txt改为b.txt$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)        renamed:    a.txt -> b.txt

查看提交历史

$ git log

输出结果:

# 这是其中一次的提交记录commit 6c2d4f8b2a5c119629dd5143816deb50f1aa5305 # 表示SHA-1值,表示提交记录的标识Author: XXX <XXX@XXX.com> # 表示git用户名和邮箱Date:   Thu Apr 27 13:07:34 2017 +0800    2.txt commit # 提交的message

git log 的常用语

-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 log的输出一般都比较长,我们可以显示最近几条记录,如:git log -2显示最近2条提交记录

git log --stat:输出提交的简略信息,修改了多少个文件,文件修改了多少行等+

执行:$ git log -2 --stat输出:commit 985733dce4d0378a37cfd963c4ac2fcf183139fdAuthor: xxx <xxx@xxx.com>Date:   Thu Apr 27 13:20:52 2017 +0800    4 2.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)

git log --pretty这个命令指定使用不同于默认格式的方式展示提交历史。

如:

$ git log -3 --pretty=oneline # 一行显示471d3d4be04e5a9e63d5d5fc23ea1f8f609a58e3 1985733dce4d0378a37cfd963c4ac2fcf183139fd 46c2d4f8b2a5c119629dd5143816deb50f1aa5305 2.txt commit

处理oneline之外,还有short,full,fuller可用,还有自定义format格式。

如:

$ git log -2 --pretty=format:"%h - %an, %ar : %s"471d3d4 - xxx, 44 minutes ago : 1  # xxx表示作者Author985733d - xxx, 58 minutes ago : 4

git撤销操作

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错。这是我们可以使用下面的命令,尝试重新commit。

$ git commit --amend 

示例:

# 第一次提交信息$ git commit 2.txt -m'2.txt 第一次提交'   [master 9675435] 2.txt 第一次提交 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 2.txt# 对上一次提交信息进行修改$ git commit --amend -m'上一次提交信息有误,2.txt第一次提交确定'   [master 64644c2] 上一次提交信息有误,2.txt第一次提交确定 Date: Thu Apr 27 14:37:15 2017 +0800 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 2.txt# 执行git log后只看到git commit --amend的提交信息$ git log  commit 64644c2ec618962b5a3632cf22c749e90b9f6564Author: ZhangZhiQiang <zhangzhiqiang@pegasus-mobile.com>Date:   Thu Apr 27 14:37:15 2017 +0800    上一次提交信息有误,2.txt第一次提交确定

如果你提交后发现暂存了某些需要修改的文件,可以这样操作

git commit -m'first commit'git add other_filegit commit --amend -m'重新提交'

最终你只有一次提交,第二次提交会代替第一次提交的结果。

取消暂存的文件

git reset

如果我们将文件修改后,通过git add命令添加到暂存区后,执行git reset会从新将文件从暂存区退回到非暂存区,如:

# 将修改的文件后,添加到暂存区$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)        modified:   2.txt# 重置2.txt$ git reset HEAD 2.txtUnstaged changes after reset:M       2.txt# 重置后的文件状态,显示not staged(不在暂存区)$ git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   2.txt

撤销对文件的修改

git checkout -- file
# 修改2.txt文件后,执行git status命令$ git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   2.txtno changes added to commit (use "git add" and/or "git commit -a")# 撤销对2.txt的修改$ git checkout -- 2.txt# 执行git status命令,这个是时候发现,修改的文件内容没有了,被撤回了$ git statusOn branch masternothing to commit, working tree clean

Git Book中文版

0 0
原创粉丝点击