在使用Git之前,需要对Git进行必要的配置,包括用户名、邮件地址等等。
# 用户名 git config --global user.name "your name" # 邮件地址 git config --global user.email "you@example.com" # 禁止行尾符自动转换 git config --global core.autocrlf false注意:
--global 全局选项, 如果省略则只对当前仓库有效。 根据你自己的情况决定是否禁止行尾符转换(Windows/Mac/Linux)。在github上创建一个仓库mitter,用以下代码作第一次提交。
mkdir mitter cd mitter git init touch README git add README git commit -m 'first commit' git remote add origin git@github.com:youraccount/mitter.git git push origin master向mitter添加一个新的文件。
touch hello.c git add hello.c git commit hello.c -m "add hello.c" # 此时hello.c文件已经进入git的本地仓库 git push origin master # 请慎重选择合适的时机提交到远程仓库,对远程仓库进行版本回退将是一个灾难。从远程服务器上获取代码(Git支持多种形式的远程URI形式)。
git clone git@github.com:youraccount/mitter.git git clone /path/to/mitter mitter git clone rsync://host.domain/path/to/repo.git git clone http://host.domain/path/to/repo.git git clone https://host.domain/path/to/repo.git git clone git://host.domain/path/to/repo.git git clone git://host.domain/~user/path/to/repo.git git clone ssh://[user@]host.domain[:port]/path/to/repo.git git clone ssh://[user@]host.domain/path/to/repo.git/ git clone ssh://[user@]host.domain/~user/path/to/repo.git git clone ssh://[user@]host.domain/~/path/to/repo.git git clone /path/to/repo.git git clone file:///path/to/repo.git我们先来看mitter的目录结构1)
mitter ├── .git # Git的核心, 所有Git的数据都存储在这里 │ ├── branches # 分支 │ ├── COMMIT_EDITMSG # 上一次提交的消息, 即"first commit" │ ├── config # Git的本地配置, 即不加--global选项时存储的配置文件 │ ├── description # 仓库的名字, 描述 │ ├── HEAD # 记录当前提交的对象名 │ ├── hooks # Hook脚本目录 │ │ ├── applypatch-msg.sample │ │ ├── commit-msg.sample │ │ ├── post-commit.sample │ │ ├── post-receive.sample │ │ ├── post-update.sample │ │ ├── pre-applypatch.sample │ │ ├── pre-commit.sample │ │ ├── prepare-commit-msg.sample │ │ ├── pre-rebase.sample │ │ └── update.sample │ ├── index # 索引仓库 │ ├── info # │ │ └── exclude # Git排除管理配置 │ ├── logs # 日志, git log │ │ ├── HEAD │ │ └── refs │ │ └── heads │ │ └── master │ ├── objects # 对象目录, Git使用了对象数据库来存储数据 │ │ ├── 0f # Git有四种对象, 提交(commit), 树(tree), 标签(tag), 数据(blob) │ │ │ └── 933da03611f899949e0ab1ee31fd4dc866ca53 # 所有的对象都是以SHA1哈希算法生成的一个40位的十六进制数字命名, 并以此为文件名进行存储 │ │ ├── 54 │ │ │ └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd │ │ ├── e6 │ │ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 │ │ ├── info │ │ └── pack │ └── refs # 引用, 指向所有的提交和标签 │ ├── heads # 当前提交, 记录各个分支的当前提交 │ │ └── master │ └── tags # 标签 └── README # README文件, Git库中当前管理的唯一一个文件Git是一个分布式的源代码管理系统. 它在仓库和源代码之间引入了一个中间坏节 - 索引仓库, 同时增加了本地仓库和远程仓库以实现分布管理. 如果你以前使用过Subversion, 请特别注意这一点.
Git的一般流程, 在当前目录下对源代码实施变更, 对已实施变更的文件使用git add加入索引仓库, 确认变更正确无误后, git commit提交到Git仓库, 最后git push提交到远程仓库.
cd /path/to/mitter vi README # 修改README git diff # 查看README的更改 git add README # 添加修改后的README到索引仓库 git status # 查看状态, 会看到README已更改并加入索引仓库尚未提交到仓库 git diff --cached # 查看当前README与仓库的README的区别 git commit README -m "edit README" # 提交, 并添加开发日志 "edit README" git push origin master # 推送到远程仓库