git原理-hash与文件管理与分支管理

一、hash

哈希是一个系列的加密算法,各个不同的哈希算法虽然加密强度不同,但是有以下几个共同点:
①不管输入数据的数据量有多大,输入同一个哈希算法,得到的加密结果长度固定。
②哈希算法确定,输入数据确定,输出数据能够保证不变
③哈希算法确定,输入数据有变化,输出数据一定有变化,而且通常变化很大
④哈希算法不可逆
Git 底层采用的是 SHA-1 算法。
哈希算法可以被用来验证文件。原理如下图所示:

二、Git 保存版本的机制

1、集中式版本控制工具的文件管理机制

以文件变更列表的方式存储信息。这类系统将它们保存的信息看作是一组基本文件和每个文件随时间逐步累积的差异。

2、Git 的文件管理机制

Git 把数据看作是小型文件系统的一组快照。每次提交更新时 Git 都会对当前的全部文件制作一个快照并保存这个快照的索引。为了高效,如果文件没有修改,
Git 不再重新存储该文件,而是只保留一个链接指向之前存储的文件。所以 Git 的工作方式可以称之为快照流。

3、Git 文件管理机制细节

(1)Git 的“提交对象”

(2)提交对象及其父对象形成的链条

三、Git 分支管理机制

1、分支的创建

2、分支的切换

 

git客户端-eclipse项目的git操作

最开始的时候,应该先从远程库fetch 到本地,本地merge后,才能提交到远程库,否则远程库会拒绝接收提交。

一、提交到本地库

1、新项目首先需要添加到暂存区

2、提交至本地库

3、图形化界面拖拽和快捷键

新建好的类可以,可以不用 Add to index 直接 commit 就行了,此时

还可以使用快捷键  cmmand + alt + 3

二、推送到远程库

三、从远程库clone到本地

eclipse 因为新建版本稍有变化,有的版本不能直接clone到工作区,需要在clone到别的地方,然后再从外部导入到工作区。

四、转换clone到本地的项目

因为上传到远程仓库的文件不包括eclipse的项目配置文件,所以clone到本地后,我们需要convert项目。

五、解决冲突

冲突文件→右键→Team→Merge Tool
修改完成后正常执行 add/commit 操作即可。

六、新建分支与合并分支

项目-->Team-->Switch To Other 页面中:可以对分支进行新建、删除、重命名和切换。 

主开发者:

本地新建 hot_fix分支:

修改好后,提交到本地库,然后push到远程仓库。【push到远程库方法也可以是如下方法】

协开发者:

从远程库下载hot_fix分支:

从本地master分支 切换到 远程hot_fix分支:

然后先切换到本地master分支:

接着再合并本地的 hot_fix分支:

最后 push 到 远程库。

 

 

git客户端-eclipse项目的git配置

一、eclipse查看git用户配置文件

二、eclipse项目初始化git

工程→右键→Team→Share Project→Git【我这边已没有git选项了,直接进入git配置页面】

三、eclipse项目配置git

1、配置项目的本地签名

2、配置git项目忽略文件

eclipse中的 Quick Access  搜索框中 输入  navigator,就能查看项目的所有文件了。

概念:Eclipse 特定文件
这些都是Eclipse 为了管理我们创建的工程而维护的文件,和开发的代码没有
直接关系。最好不要在Git 中进行追踪,也就是把它们忽略。
.classpath 文件
.project 文件
.settings 目录下所有文件
为什么要忽略Eclipse 特定文件呢?
同一个团队中很难保证大家使用相同的IDE 工具,而IDE 工具不同时,相关工
程特定文件就有可能不同。如果这些文件加入版本控制,那么开发时很可能需要为
了这些文件解决冲突。

GitHub 官网样例文件
https://github.com/github/gitignore
https://github.com/github/gitignore/blob/master/Java.gitignore
编辑本地忽略配置文件,文件名任意。

Java.gitignore 忽略配置文件如下:

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.classpath
.project
.settings
target

注意啦:下面这些是自己添加的忽略文件,其余的忽略文件是模板提供的。

.classpath
.project
.settings
target

在~/.gitconfig 【git的用户配置文件】文件中引入上述文件
[core]
excludesfile = /Users/cool/Java.gitignore
[注意:如果是Windows系统,这里的路径中就要使用“\”,不能使用“/”]

git基础-(7)-使用SSH登录github仓库

一、新建 SSH 的密钥

运行命令生成.ssh 密钥目录
$ ssh-keygen -t rsa -C [email protected]

打开用户根目录的 .ssh文件夹,查找里面的公钥文件:选中并复制

二、 SSH 的密钥

打开github账号的setting设置,找到  SSH and GPG keys 并将 步骤一中的 公钥信息,复制到 SSH keys 栏目中。

三、获取SSH连接

1、在仓库的下载中,Clone or download–>Use SSH–> Clone with SSH 获得项目仓库的 SSH链接。

2、在本地客户端中,添加远程仓库的SSH连接别名:

cooldeMacBook-Pro:git-learn cool$ git remote add origin_ssh [email protected]:sky123/huashan.git

3、执行提交到远程库操作

cooldeMacBook-Pro:git-learn cool$ git push origin_ssh master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 848 bytes | 848.00 KiB/s, done.
Total 9 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To github.com:sky123/huashan.git
   e416c03..a4fe921  master -> master
cooldeMacBook-Pro:git-learn cool$ 

 

git基础-(5)-远程团队内部协作

一、主力开发登录github创建项目仓库

参考图片:

创建完后,拷贝仓库地址 比如 https://github.com/sky123/huashan.git

二、主力开发将本地库提交到github远程仓库

1、设置远程仓库别名

git remote -v 查看当前所有远程地址别名
git remote add [别名] [远程库地址]
git remote add rr https://github.com/i12dream/huashan.git
【rr 是 远程仓库 自己取的 别名,以后还是取 origin 好,这样方便统一】

cooldeMacBook-Pro:git-learn cool$ git remote add rr https://github.com/sky123/huashan.git
cooldeMacBook-Pro:git-learn cool$ git remote -v
rr https://github.com/sky123/huashan.git (fetch)
rr https://github.com/sky123/huashan.git (push)
cooldeMacBook-Pro:git-learn cool$

2、将本地库上传到远程服务器

git push [远程库地址别名] [远程库分支名]

### 上传做法:osx 有弹窗 获取密码,我拒绝了 然后就在 命令行 输入了用户名和密码
cooldeMacBook-Pro:git-learn cool$ git push rr master
Username for 'https://github.com': sky123
Password for 'https://[email protected]': 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 471 bytes | 471.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/sky123/huashan/pull/new/master
remote: 
To https://github.com/sky123/huashan.git
 * [new branch]      master -> master
cooldeMacBook-Pro:git-learn cool$ 

三、协作开发下载修改并提交到远程

1、协作开发先clone到本地

git clone [远程库地址]   作用:
1、完整的把远程库下载到本地
2、创建origin 远程地址别名
3、初始化本地库

新建一个目录,直接 git clone 就好了。git clone 会新建一个 仓库名文件夹,里面项目已经下载好了,git已经初始化好了 ,远程地址别名也设置好了。
特别注意:远程库 别名这里是 origin 【所以下次都用orign 当做远程库别名好了】

cooldeMacBook-Pro:desktop cool$ git clone
...
..

### 特别注意:远程库 别名这里居然是 origin 【所以下次都用orign 当做远程库别名好了】
cooldeMacBook-Pro:huashan cool$ git remote -v
origin	https://github.com/sky123/huashan.git (fetch)
origin	https://github.com/sky123/huashan.git (push)
cooldeMacBook-Pro:huashan cool$

2、修改了一些文件后,就先提交到本地库

3、提交到远程库的一些问题

当协作开发直接 推送到远程库时,发现 新创建的用户 需要邮箱验证一下。

cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
remote: You must verify your email address.
remote: See https://github.com/settings/emails.
fatal: unable to access 'https://github.com/sky123/huashan.git/': The requested URL returned error: 403
cooldeMacBook-Pro:huashan cool$

登录邮箱验证完后,再一次推送提交,发现无法授权 ,需要仓库创建者【主力开发】的邀请。

邀请怎么添加呢?


“主力开发”通过各种方式把邀请链接发送给“协作开发”,“协作开发”登录自己的 GitHub账号,访问邀请链接。

成为合作者后,协作开发就能提交到远程仓库了。
cooldeMacBook-Pro:huashan cool$ git push origin master

四、主力开发从远程仓库拉取并合并到本地

 pull=fetch+merge
 git fetch [远程库地址别名] [远程分支名]
 git merge [远程库地址别名/远程分支名]
 git pull [远程库地址别名] [远程分支名]

1、可以使用 fetch 和 merge  两步操作

远程仓库管理者【主力开发】 :拉取远程已经修改的仓库。rr 是 之前远程仓库地址的别名。

cooldeMacBook-Pro:git-learn cool$ git remote -v
rr	https://github.com/sky123/huashan.git (fetch)
rr	https://github.com/sky123/huashan.git (push)

cooldeMacBook-Pro:git-learn cool$ git fetch rr master
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/sky123/huashan
 * branch            master     -> FETCH_HEAD
   4a234e2..97806e9  master     -> rr/master
cooldeMacBook-Pro:git-learn cool$ 

下载到本地的目录是 rr/master ,也就是说本地有 master  rr/master 两个分支。

cooldeMacBook-Pro:git-learn cool$ git checkout rr/master
Note: checking out 'rr/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 97806e9 令狐冲
cooldeMacBook-Pro:git-learn cool$

接下来就是合并操作了:【如果合并时出现冲突,可以根据分支冲突一文来处理】
cooldeMacBook-Pro:git-learn cool$ git merge rr/master

2、也可以使用 pull 操作 一步到位

pull 等于 fetch + merge
cooldeMacBook-Pro:git-learn cool$ git pull rr master

五、主力开发和协作开发同时提交到远程问题

两个开发者 同时向 远程库 提交的情况下:第二个 开发者 提交时 会报错 。

cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
To https://github.com/sky123/huashan.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/sky123/huashan.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
cooldeMacBook-Pro:huashan cool$ 

我们需要从远程库中将最新的版本下载下来,先合并 然后再提交!!!

cooldeMacBook-Pro:huashan cool$ git pull origin master
From https://github.com/sky123/huashan
 * branch            master     -> FETCH_HEAD
Auto-merging apple.txt
CONFLICT (content): Merge conflict in apple.txt
Automatic merge failed; fix conflicts and then commit the result.
cooldeMacBook-Pro:huashan cool$ vim apple.txt
cooldeMacBook-Pro:huashan cool$ git add apple.txt
cooldeMacBook-Pro:huashan cool$ git commit -m "conflict resolve"
[master e416c03] conflict resolve
cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 687 bytes | 687.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/sky123/huashan.git
   ff6f1a1..e416c03  master -> master
cooldeMacBook-Pro:huashan cool$ 

 

git基础-(4)-分支管理合并与冲突

一、创建分支

git branch [分支名]

创建出来的分支和原分支是一模一样的,里面的内容都没有变。注意创建出来的分支不是空分支。

二、查看所有分支

git branch -v

三、切换分支

git checkout [分支名]

四、删除分支

git branch -d [分支名]

五、合并分支内容

 当前分支合并被合并分支:  git merge [被合并分支]

核心总结:如果两个分支合并时,产生了新节点【判断是否产生新节点的依据是,A和B合并时,是否之前已经出现了合并结果了】,就会需要-m 参数 来 添加 提交备注,否则不需要  -m 参数 来 添加提交备注。

不存在行冲突时:git 分支合并是以 作为单位的,不存在行冲突就自动合并了。
如果 A和B一开始是一模一样的,A修改了提交生成 A+。
此时 A+ merge B,结果是没有变化,仍是 A+。
如果接着 B merge A+,则 B变成了A+ ,且没有产生新节点,不需要添加提交信息。

如果 A和B一开始是一模一样的,A修改了提交生成 A+,且 B修改成了B+。
此时 A+ merge B+,生成 A++,需要添加提交备注,也可以直接输入 A+ merge B+ -m "XX"
如果接着 B+ merge A++,则B+ 变成了 A++,且没有产生新节点,不需要添加提交信息。

存在行冲突时:在同一行 合并和被合并分支 都修改过了。

1、AB合并之前已经有合并结果了,比如A分支合并了B分支,产生了A+节点,此时B分支再合并A分支时,也只会变成A+节点。

2、AB合并之前还未出现合并结果,此时需要将所有冲突文件都添加到暂存区,然后提交(这里的 git commit 不需要再写文件名了,但是提交备注还是需要的)产生新的合并后节点。

aaaaaaa
bbbbbbb
ccccccc   ### modify by master
ddddddd
eeeeeee
fffffff

git commit apple.txt -m "modify apple by master"

===========上面是主分支进行的修改提交====下面是被合并分支进行的修改提交===========

aaaaaaa
bbbbbbb
cccccc  ### modify by hot_fixc
ddddddd
eeeeeee
fffffff

git commit apple.txt -m "modify apple by hot_fix"

结果显示:存在冲突。

cooldeMacBook-Pro:git-learn cool$ git merge hot_fix
Auto-merging apple.txt
CONFLICT (content): Merge conflict in apple.txt
Automatic merge failed; fix conflicts and then commit the result.
cooldeMacBook-Pro:git-learn cool$ git branch -v
  hot_fix f411b30 modify apple by hot_fix
* master  44bc206 modify apple by master
cooldeMacBook-Pro:git-learn cool$

查看当前合并冲突的状态信息:

cooldeMacBook-Pro:git-learn cool$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   apple.txt

no changes added to commit (use "git add" and/or "git commit -a")
cooldeMacBook-Pro:git-learn cool$ 

需要手动修改提交:下面就是 冲突文件的信息,需要自己修改。

aaaaaaa
bbbbbbb
<<<<<<< HEAD
ccccccc   ### modify by master
=======
cccccc  ### modify by hot_fixc
>>>>>>> hot_fix
ddddddd
eeeeeee
fffffff

合并冲突时需要修改所有有冲突的文件,然后对每一个冲突文件执行  git add [冲突文件名]  ,当所有冲突文件都添加到暂存区后,执行  git commit -m “日志信息”
注意:此时commit 一定不能带具体文件名。

如果想直接git commit -m "信息" 就会报错了。

cooldeMacBook-Pro:git-learn cool$ git commit -m "测试一下"
U	apple.txt
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
cooldeMacBook-Pro:git-learn cool$ 

所以必需要先将冲突文件 添加到 暂存区,然后再执行 git commit -m “xxx”【后面不加 -m 参数时,需要进入 vim 编辑 添加提交信息】


下面是修改好,合并到本地库的文件。

aaaaaaa
bbbbbbb
<<<<<<<
ccccccc   ### modify by master
=======
cccccc  ### modify by hot_fixc
>>>>>>>
ddddddd
eeeeeee
fffffff

[注意master合并了hot_fix后,master 现在已经提交了合并,生成了新节点,此时再合并 hot_fix 会提示 已经合并过了,不能合并]

而此时,hot_fix 还是 :

aaaaaaa
bbbbbbb
cccccc  ### modify by hot_fixc
ddddddd
eeeeeee
fffffff

转到 hot_fix 分支, git merge master ,是可以合并的,如下结果

cooldeMacBook-Pro:git-learn cool$ git merge master
Updating f411b30..2faa553
Fast-forward
 apple.txt | 4 ++++
 1 file changed, 4 insertions(+)
cooldeMacBook-Pro:git-learn cool$ vim apple.txt

显示合并后的文件信息

aaaaaaa
bbbbbbb
<<<<<<<
ccccccc   ### modify by master
=======
cccccc  ### modify by hot_fixc
>>>>>>>
ddddddd
eeeeeee
fffffff

 

 

 

mac软件问题和优化-旧版sublime的插件包下载失败

github 最近禁用了 tls1.0 加密协议,而此时mac系统上,旧版sublime,下载时会出现报错。查看sublime控制台报错信息:

Package info was unavailable last time crawler ran. Error downloading repository. HTTP exception InvalidCertificateException (Host api.github.com returned an invalid certificate ([SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:548))) downloading https://api.github.com/repos/rust-lang/sublime-rust.

然后查到:github 的问题贴

简单来说就是,mac版 sublime 用的下载模块是 urllib ,而urllib 是python自带的标准库,里面有ssl模块。而旧版python的ssl模块,采用ssl1.0连接服务器,所以导致高版本的ssl服务器拒绝连接。

解决办法是:打开sublime的
preferences->Package settings->Package control->Settings – User
添加如下代码:【也就是知道下载模块的第一优先级是使用 crul 模块】

"downloader_precedence":
	{
		"osx":
		[
			"curl",
			"urllib",
			"wget"
		]
	},

urllib是Python自带的标准库,无需安装,直接可以用。
提供了如下功能:

  • 网页请求
  • 响应获取
  • 代理和cookie设置
  • 异常处理
  • URL解析

爬虫所需要的功能,基本上在urllib中都能找到,学习这个标准库,可以更加深入的理解后面更加便利的requests库。

python 2.7  代码使用如下:

# request:GET
import urllib
response = urllib.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))

英文问题讨论解析:


The problem is that the crawler is running on an old OS and Python 3.3, which doesn’t support TLS 1.2. wbond is already on it.

=====================================================

Python 3.3’s _ssl module uses the OpenSSL API in such a way that a connection to a TLS 1.2 server is not possible. It doesn’t use the TLS 1.1 or TLS 1.2 constants, but rather a constant for TLS 1.0. This results in GitHub rejecting the connection since it uses an insecure version of the TLS protocol.

Windows users by default use the WinInetDownloader, which uses the wininet library, and Microsoft’s TLS stack, so as long as they are running a recent build of Windows, they will have TLS 1.2.

Mac and Linux users default to using urllib, which utilizes the Python _ssl module.

mac系统问题和优化-openssl的更新问题

因为mac 现在主要用 LibreSSL,openssl的更新没有及时。

如果需要更新,可以用brew来安装 openssl,并将其设置为 系统默认的openssl。

cooldeMacBook-Pro:~ cool$ brew install openssl
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2p.el_capitan.bottle.tar.
######################################################################## 100.0%
==> Pouring openssl-1.0.2p.el_capitan.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
?  /usr/local/Cellar/openssl/1.0.2p: 1,793 files, 12.2MB
cooldeMacBook-Pro:~ cool$ openssl version
OpenSSL 0.9.8zh 14 Jan 2016
cooldeMacBook-Pro:~ cool$ 

调用方法一:更新系统环境变量:

cooldeMacBook-Pro:~ cool$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
cooldeMacBook-Pro:~ cool$ source  ~/.bash_profile
cooldeMacBook-Pro:~ cool$ openssl version
OpenSSL 1.0.2p  14 Aug 2018

调用方法二:

1、删除了.bash_profile中 的用户配置路径 openssl,
2、找到系统 /usr/bin/openssl 重命名乱连接,做备份 /usr/bin/openssl.bak
3、查看 新版openssl的安装目录:

cooldeMacBook-Pro:~ cool$ ls -l /usr/local/opt/openssl
lrwxr-xr-x  1 cool  admin  24 Oct 29 21:06 /usr/local/opt/openssl -> ../Cellar/openssl/1.0.2p

4、备份原来的/usr/bin/openssl,并重新设置系统openssl的软连接:

cooldeMacBook-Pro:~ cool$ sudo ln -s  /usr/local/Cellar/openssl/1.0.2p/bin/openssl  /usr/bin/openssl 
Password:
cooldeMacBook-Pro:~ cool$

关于python中的 ssl版本问题:

urllib是Python自带的标准库,无需安装,直接可以用。而urllib 是python自带的标准库,里面有ssl模块。也就是说python自带的的ssl模块与系统的openssl软件是无关的,版本号当然可以不相同。

cooldeMacBook-Pro:~ cool$ python -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 0.9.8zh 14 Jan 2016
cooldeMacBook-Pro:~ cool$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 0.9.8zh 14 Jan 2016'
>>>

所以使用 旧版本的 urllib 库的 爬虫只支持ssl 1.0协议,导致ssl高版本协议的服务器拒绝与其连接。

HTTP exception InvalidCertificateException (Host codeload.github.com returned an invalid certificate ([SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:548))) downloading https://codeload.github.com/seanliang/ConvertToUTF8/zip/1.2.11.

git基础-(3)-文件比较之diff命令

diff命令都是以行作为比较单位的,如果在某行添加了信息,文件差异比较相当于删除了这行,然后又在此处重新添加了一行。

比较文件差异:
git diff [文件名]
将工作区中的文件和暂存区进行比较

git diff [本地库中历史版本] [文件名]   比如:  git diff head bbb.txt
将工作区中的文件和本地库历史记录比较

不带文件名可以比较多个文件


一、添加了两个新文件并提交到本地库

cooldeMacBook-Pro:git-learn cool$ git commit a.txt bbb.txt -m "两个新文件"
[master ee9a560] 两个新文件
 2 files changed, 11 insertions(+)
 create mode 100644 a.txt
 create mode 100644 bbb.txt
cooldeMacBook-Pro:git-learn cool$ git status
On branch master
nothing to commit, working tree clean
cooldeMacBook-Pro:git-learn cool$ 

接着工作区修改了a.txt ,但是还没有添加到暂存区

cooldeMacBook-Pro:git-learn cool$ 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 directory)

	modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
cooldeMacBook-Pro:git-learn cool$ 

二、工作区与暂存区比较

采用了diff 命令:

cooldeMacBook-Pro:git-learn cool$ git diff a.txt
diff --git a/a.txt b/a.txt
index 07b3c9e..f9bbd73 100644
--- a/a.txt
+++ b/a.txt
@@ -1,6 +1,6 @@
 sd
 asdsad
-dfsdf
+dfsdf   ##这一行末尾添加了信息 
 
 asd
 soft commit
cooldeMacBook-Pro:git-learn cool$

三、工作区与本地库比较

当前工作区 修改了 a.txt ,没有修改 bbb.txt

1、比较a.txt

cooldeMacBook-Pro:git-learn cool$ git diff head a.txt
diff --git a/a.txt b/a.txt
index 07b3c9e..f9bbd73 100644
--- a/a.txt
+++ b/a.txt
@@ -1,6 +1,6 @@
 sd
 asdsad
-dfsdf
+dfsdf   ##这一行末尾添加了信息 
 
 asd
 soft commit

============上面是工作区与当前游标本地库比较===================

============下面是工作区与上一个游标本地库比较===================

cooldeMacBook-Pro:git-learn cool$ git diff head^ a.txt
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..f9bbd73
--- /dev/null
+++ b/a.txt
@@ -0,0 +1,7 @@
+sd
+asdsad
+dfsdf   ##这一行末尾添加了信息 
+
+asd
+soft commit
+
cooldeMacBook-Pro:git-learn cool$ 

 2、比较b.txt

cooldeMacBook-Pro:git-learn cool$ git diff head bbb.txt
cooldeMacBook-Pro:git-learn cool$ 

####  git diff head bbb.txt 当前本地仓库的当前游标下的  bbb.txt 与工作区的 bbb.txt 文件差异比较

==========================
cooldeMacBook-Pro:git-learn cool$ git diff head~1 bbb.txt
diff --git a/bbb.txt b/bbb.txt
new file mode 100644
index 0000000..221c50e
--- /dev/null
+++ b/bbb.txt
@@ -0,0 +1,4 @@
+bob
+sd
+主线程
+主线程
cooldeMacBook-Pro:git-learn cool$ 

==========================
git diff head~1 bbb.txt 代表当前游标的上一个版本的  bbb.txt 与工作区的bbb.txt 差异比较