官网:
官方文档:
比较简略的资料(对基本概念没有解释很清楚):
一、重要概念
1、三个路径:
Working Directory(常被称为工作树):You modify files in your working directory. 也就是说我们要处理的文件就是在这个地方。
staging area/Index(常被称为索引):You stage the files, adding snapshots of them to your staging area. 可以理解为临时存放快照(snapshots) 的地方。
Git directory/Repository (常被称为仓库):You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory(Repository ).可以理解为永久存放快照的地方。
Git的一般工作流程是:
修改文件(modify)--->添加到索引(stage)--->存仓(commit)
注:关于快照的定义
Git thinks of its data more like a set of snapshots of a miniature filesystem.
Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.
To be efficient, if files have not changed, Git doesn’t store the file again, just a link to the previous identical file it has already stored. Git thinks about its data more like a stream of snapshots.
2、工作树中的文件分类
只有两类状态tracked or untracked.(即被跟踪或未被跟踪)
Tracked files are files that were in the last snapshot. they can be unmodified, modified, or staged.
被跟踪的文件,就是存在上一个快照中的文件(即commit后的快照所包含的文件)。
可以有unmodified, modified, staged三个状态。
unmodified:commit后没被修改过。
modified:commit 后被修改过。
staged:modify后被stage过。
Untracked files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area.
未被跟踪过的文件,即不在上一个快照中的文件。
比如新建的文件啊,被移出快照的文件等等。
二、Git设置
有三层配置:
系统级,用户级,项目级。
小的范围覆盖大的范围。
配置文件具体的放置位置,因操作系统不同而异。
git config (命令),用法如下。
设置用户名和邮件地址
$ git config --global user.name "Your name"
$ git config --global user.email youremail
设置别名
设置.gitignore file
设置某些文件不让Git管理。
三、建仓
1、本地
$ git init
初始化,建立骨架,这个时候有.git目录生成。
也就是我们上文所说的Git directory/Repository(仓库)。
$ git add .$ git commit -m 'initial project version'
git add .
添加当前目录(Working derectory)下所有文件(这个时候是Untracked状态)。这个动作是stage。
即保存临时快照到索引,即staging area/Index。
git commit -m 'initial project version'
将快照永久保存到仓库Git directory/Repository。
2、远程
$ git clone https://github.com/libgit2/libgit2$ git clone https://github.com/libgit2/libgit2 mylibgit
前一句是默认创建libgit2目录。
后一句创建自己的目录mylibgit。
三、记录修改
1、git add file
跟踪新文件(Tracking New Files)
更改了文件后生成快照存索引(Staging Modified Files)
2、查看状态
$ git status$ git status -s
3、详细查看状态
$ git diff
This command compares your staged changes to your last commit.
也就是比较staging area/Index和Git directory/Repository 这两个地方的不同。
$ git diff --staged
That command compares what is in your working directory with what is in your staging area.
也就是比较staging area/Index和Working Directory这两个地方的不同。
4、提交
$ git commit
当更改到一定程度,提交到仓库,用git commit。
$ git commit -a -m 'added new benchmarks'
可以用-a选项跳过stage阶段(经典的工作流程是modify->stage->commit)。
5、删除文件
$ git rm PROJECTS.md
相当于从working directory中删除文件,并且从stage area中删除(stage 删除这个动作)。
$ git rm --cached README
Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area.
相当于在working directory中保留文件,但从stage area中删除(stage 删除这个动作)。
$ git rm log/\*.log
This command removes all files that have the .log extension in the log/ directory.
Note the backslash (\) in front of the *.
6、重命名
$ git mv file_from file_to
More importantly,you can use any tool you like to rename a file, and address the add/rm later, before you commit.
注:其实删除和重命名不用上述命令也可以。
四、查看历史版本
git log命令
常用参数:
$ git log -p -2
-p shows the difference introduced in each commit.
-2, which limits the output to only the last two entries
$ git log --stat
see some abbreviated stats for each commit
$ git log --pretty=oneline
The oneline option prints each commit on a single line.
The short, full, and fuller options show the output in roughly the same format but with less or more information.
$ git log --pretty=format
略,要用的时候查看说明
注:The oneline and format options are particularly useful with another log option called --graph. This option adds a nice little ASCII graph showing your branch and merge history.
五、撤销
1、$ git commit --amend
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message.
If you want to try that commit again, you can run commit with the --amend option.
For example:
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
You end up with a single commit – the second commit replaces the results of the first.
2、$ git reset HEAD CONTRIBUTING.md
Unstaging a Staged File.
CONTRIBUTING.md file is modified but once again unstaged.
git reset can be a dangerous command if you call it with --hard.
注:HEAD指当前分支的最新版本 ,后续有需要再查。
3、$ git checkout -- CONTRIBUTING.md
Unmodifying a Modified File.
It is a dangerous command. Any changes you made to that file are gone.
Remember, anything that is committed in Git can almost always be recovered.
Even commits that were on branches that were deleted or commits that were overwritten with an --amend commit can be recovered.
However, anything you lose that was never committed is likely never to be seen again.
六、Tagging
1、Annotated Tags
$ git tag -a v1.4 -m "my version 1.4"
2、Lightweight Tags
$ git tag v1.4-lw
3、Tagging Later
$ git tag -a v1.2 9fceb02
4、Sharing Tags
$ git push origin v1.5
By default, the git push command doesn’t transfer tags to remote servers.
You will have to explicitly push tags to a shared server after you have created
Them.
5、Checking out Tags
$ git checkout -b version2 v2.0.0
you can create a new branch at a specific tag with git checkout -b [branchname] [tagname]
七、项目分支
$ git branch local
创建分支local
$ git branch
查看分支
$ git checkout local
分支切换到local
$ git checkout master # 将当前分支切换为master$ git merge local # 将local分支与当前分支合并
合并分支,当我们用local完成了阶段性任务时,此时可以把local分支合并到master分支中。
$ git branch -d local
删除分支,local分支已经被合并,没有作用了。
$ git branch -D local
强制删除分支。对于未有合并的分支,是不能删除的,我们可以暴力删除。
引入分支的功能,可有效保持了本地项目主分支的干净。
至此,本地代码版本管理基本操作都涉及到了。
八、远程协作
git-clone、git-pull 与 git-push
这三个命令就可以满足基本的需求了。
其余的用到时再查。