git usage
配置ssh
#生成key
$ ssh-keygen -t rsa -C “your email”
#添加已有的key
ssh-add -K [path/to/your/ssh-key]
多个ssh key
git config user.name “用户名”
git config user.email “邮箱”
#生成ssh key时同时指定保存的文件名
ssh-keygen -t rsa -f ~/.ssh/id_rsa.sohu -C “email”
#测试是否可以连接
ssh -T git@github.com
ssh -T -p 443 git@ssh.github.com
#ssh config的格式1
2
3
4
5Host
User github.com-xxx
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
修改.git文件夹下config中的url。
修改前
[remote “origin”]
url = https://github.com/humingx/humingx.github.io.git
fetch = +refs/heads/:refs/remotes/origin/
修改后
[remote "origin"]
url = git@github.com:humingx/humingx.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*
步骤
1 | #git init的时候 创建了.git |
解剖
Git 会创建一个 .git 目录, 这个目录1
2
3
4
5
6
7
8
9
10
11$ ls
HEAD #指向当前分支
branches/ # 新版本的 Git 不再使用
config # 项目特有的配置选项
description # 仅供 GitWeb 程序使用
hooks/ #客户端或服务端钩子脚本。
index # 保存了暂存区域信息 实际上就是一个包含文件索引的目录树,像是一个虚拟的工作区。在这个虚拟工作区的目录树中,记录了文件名、文件的状态信息(时间戳、文件长度等)
info/ # 保存了一份不希望在 .gitignore 文件中管理的忽略模式 (ignored patterns) 的全局可执行文件。
objects/ # 存储所有数据内容
refs/ # 存储指向数据 (分支) 的提交对象的指针
HEAD 及 index 文件,objects 及 refs 目录。这些是 Git 的核心部分
.git/refs 目录下有三个子目录:
heads 保存本地的分支 比如master/deploy/release master内柔是串文本,指向了一个 Commit 对象
remotes 保存远程仓库的分支
tags 保存所有的 tag
详解
底层命令plumbing: Unix风格的:checkout, branch, remote 等共约 30 个 Git 命令
高层命令Porcelain
git add files 把当前文件放入暂存区域。
git commit 给暂存区域生成快照并提交。
git reset – files 用来撤销最后一次git add files,你也可以用git reset 撤销所有暂存区域文件。
git checkout – files 把文件从暂存区域复制到工作目录,用来丢弃本地修改。 会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区的改动<`
> -- History ---
git reset –file ^| | git commit
— Stage —-
git checkout ^| | git add files
____Working—-1
2
3
4
5
6
7
8
9
10
11
12
13
14
15git commit -a 相当于运行 git add 把所有当前目录下的文件加入暂存区域再运行。git commit.
git commit files 进行一次包含最后一次提交加上工作目录中文件快照的提交。并且文件被添加到暂存区域。
git checkout HEAD <files> 回滚到复制最后一次提交。 会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改 动。
git rm --cached <file>" 命令时,会直接从暂存区删除文件
[Display_Text](https://marklodato.github.io/visual-git-guide/conventions.svg)
### 进阶
简单理解:
工作区(Working Directory): 即操作的文件
暂存区 (Stage Index): 即 .git/index保存的目录树 和 对应的 .git/objects里面保存的数据
版本仓库/对象库(History) : 即 .git/refs/master 和其对应内容
> git对象
echo 'test content' | git hash-object -w --stdin
find .git/objects -type f
git cat-file -p d670460b4b4aece5915caf5c68d12f560a9fe3e4
git cat-file -t fdf4fc3
1
2
3
> tree 对象对应于 UNIX 中的目录 一个单独的 tree 对象包含一条或多条 tree 记录 一个单独的 tree 对象包含一条或多条 tree 记录,每一条记录含有一个指向 blob 或子 tree 对象的 SHA-1 指针,并附有该对象的权限模式 (mode)、类型和文件名信息
就是一大坨指针
# 用这个命令可以看到tree对象
git cat-file -p master^{tree}
100644 blob 9f0bc480dad89d2ef1f9983b39554c85eb0d3e79 README
```
blob 对象则大致对应于 inodes 或文件内容
Commit 对象
一个 commit 对象由以下几部分组成
作者
提交者
注释
指向一个 big tree 的指针
怎么理解暂存区 ? http://www.worldhello.net/2010/11/30/2166.html
http://iissnan.com/progit/html/zh/ch9_2.html
Git内部原理
http://www.worldhello.net/2010/11/30/2166.html
参考
http://riny.net/2014/git-ssh-key/
https://segmentfault.com/q/1010000000835302
http://riny.net/2014/git-ssh-key/
http://www.cnblogs.com/hero4china/p/windows_git.html
http://mednoter.com/git-object.html