Chap 01 Git起步
1.git的三种状态:
a) Commited
b) Modified
c) Staged
2.git文件的三个区域
a) Working directory:工作目录
b) Git directory(reposiory):git本地数据目录
c) Staging directory:暂存目录
状态和工作区域的关系:
3.初次使用需要使用git config进行配置:
a) git config --system 写/etc/.gitconfig,所有用户有效
b) git config --global 写~/.gitconfig,当前用户有效
c) git config 写.git/config,当前项目有效,优先级高于--global
初始化脚本:
#!/bin/sh echo "Init git..." git config --global user.name "xxx" git config --global user.email "xxx@163.com" git config --global core.editor vi git config --global merge.tool vimdiff echo "Init Ok, info is:" git config --list |
4.查看git命令使用方法:
a) Git help xxx
b) Git xxx --help
c) Man git xxx
Chap 02 git基础
1.要将目录及文件纳入版本管理,首先到目录下执行git init,该命令会生成.git目录,用于存储git配置信息。
2.跟踪文件、将文件加入缓存:git add xxx
3.git status查看当前文件状态:
提示信息 | 含义 |
Untracked files | 未纳入管理的文件列表,使用git add加入跟踪管理 |
Changes to be committed | 已经加入缓存目录但尚未提交的文件,使用git commit提交 |
Changes not staged for commit | 已经修改但尚未加入缓存目录的文件,使用git add加入缓存目录 |
对应状态转换图如下:
4.使用git clone url克隆项目。
5.忽略某些文件:编辑.gitignore文件将其不纳入文件管理。例如:
*.[ao] *~ build/ |
6.文件比较:
a) Git diff:比较尚未暂存的文件和上次提交的文件。
b) Git diff --cache:比较已经暂存的文件和上次提交的文件。
7.提交时跳过暂存,直接将未暂存的文件提交:git commit -a
8.提交时git commit -m “commit info”
9.移除文件:git rm xxx。如果改文件已经放入暂存区域,加参数-f强制移除。如果移除文件跟踪但是不删除,使用--cached参数。
10.移动文件:git mv a b <=> mv a b; git rm a; git add b;移动操作在git status不会显示。
11.查看提交日志:git log。使用-p参数查看每次提交差异,-n参数查看最近n测提交记录。更多参数如下:
12.修改最后一次提交:git commit --amend。使用当前暂存区的文件进行提交,如果上次提交后暂存区内容未作修改,可以作为修改上次提交说明的功能使用。这样两次commit算过一次commit操作。
13.取消暂存文件:git reset HEAD <file>
14.取消暂存文件的修改(获取git directory中文件内容):git checkout -- <file>
15.Git clone后使用git remote -v显示当前的远程仓库及其地址,默认原始仓库名称origin。
16.添加远程仓库:git remote add tt url
17.远程抓取数据:git fetch <remote-name>
PS:克隆仓库git clone会自动将远程仓库归于origin名下。git fetch origin会抓取上次克隆或fetch后新增的数据,但是不会合并,只能手动合并。如果设置某个分支跟踪某个远程仓库的分支,可以使用git pull命令自动抓取数据,然后将远端分支自动合并到本地仓库当前分支。Git clone命令的本质是自动创建本地master分支用于跟踪远程仓库的master分支。
18.推送数据:git push <remote-name> <branch-name>。默认服务器为origin,分支为master。
19.查看、重命名、删除远程仓库信息:git remote show/rename/rm <remote-name>
20.显示标签:git tag
21.标签分为轻量级标签(git tag v1.4)和含附注的标签(git tag -a v1.0 -m “my version 1.4”)。
22.Git push不会上传标签,需要使用git push origin tagname明确上传。
Chap 03 Git分支
1.git中的分支实质是指向commit队形的可变指针,默认使用master作为默认分支。
2.Git branch test创建test分支,但是HEAD指针(指向当前分支最新)仍然指向master,使用git checkout test会切换分支到test并使得HEAD指针指向test分支。新建并切换到新分支直接使用git checkout -b test。
3.在master中合并其新分支(Fast forward):git merge test
4.删除分支:git branch -d test,-D强制删除
5.Git merge出现合并冲突,需要手动修改文件,然后使用git status查看是否还有冲突,没有就可以继续运行git merge。
6.Git branch列出所有分支,*表示当前所在的分支。Git branch --merged/--no-merged显示与当前分支合并/未合并的分支。
7.远程分支是对远程仓库的索引。Clone后自动将远程仓库命名为origin,下载数据并在本地创建远程分支origin/master(不可修改)。然后创建一个属于自己的本地master分支,始于origin/master上master相同的位置,这个分支可以修改使用。运行git fetch origin进行同步远程分支origin/master的索引到最新的位置(不可编辑,需要使用git merge origin/serverfix创建自己的分支)。本地master分支的索引不发生变化。
8.Git push 远程仓库 分支名 将分支名推送到远程仓库上,以便别人与你协同开发。
9.删除远程分支:git push origin :serverfix
10.衍和rebase:将分支的更新在需要合并的分支上执行一遍。例如:
git checkout experiment
git rebase mater
将experiment分支的变化衍和在master分支上。
...