prev: [[part4-Iterators]]
next:

Intro to Git

Introduction

Version control

V1.0本地版本控制系统 local version control system

V0.0复制整个项目目录
–> V1.0用某种简单的数据库来记录文件的历次更新差异

local

Most popular: RCS (在硬盘上保存补丁集)

V2.0 不同开发者协同工作需求–集中化的版本控制系统(Centralized Version Control Systems,简称 CVCS)

单一的集中管理的服务器,保存所有文件的修订版本

CVCS

例子: CVS…

缺点:过于依靠服务器,服务器宕机就寄

V3.0 分布式版本控制系统(Distributed Version Control System,简称 DVCS)

客户端并不只提取最新版本的文件快照, 而是把代码仓库完整地镜像下来,包括完整的历史记录。

DVCS

例子:git

Git feature

Snapshots

The other version control system are delta-based.

delta

However, the git system are snapshot-based.

snapshot

几乎所有操作都在本地进行

保证数据完整性(Hash表),一般只添加数据

status

commited, modified, and staged

  • 已修改表示修改了文件,但还没保存到数据库中。
  • 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
  • 已提交表示数据已经安全地保存在本地数据库中。
    These made the Git have three areas.
    areas
    基本的 Git 工作流程如下:
  1. 在工作区中修改文件。
  2. 将你想要下次提交的更改选择性地暂存,这样只会将更改的部分添加到暂存区。
  3. 提交更新,找到暂存区的文件,将快照永久性存储到 Git 目录。
    get some help of Git:
    1
    2
    3
    git help <verb>
    git <verb> --help
    man git-<verb>

Basic Git

initialize

a. init

1
git init

b. clone

1
git clone https://github.com/libgit2/libgit2

recording

The status of the file in repo: tracked and untracked.
To get the file tracked: git add . or git add README
To check the status of the file:

1
$ git status

More simple: git status -s

To summarize:

1
2
3
git add . # modified --> staged
git commit -m "initial commit" # staged --> commited
git push origin master # push the commited repo to the remote origin

ignoring

1
2
3
$ cat .gitignore # create a file named .gitinore
*.[oa] # ignore the file ended with .o or .a
*~ # ignore the file ended with ~

difference

查看当前文件和暂存区域快照之间的差异。 也就是修改之后还没有暂存起来的变化内容。

1
git diff

查看已暂存的变化:

1
git diff --cached

commit

先用git status 确认文件都已经暂存,然后提交

1
git commit

如果没有暂存文件,可以用-a命令跳过暂存空间直接commit

1
git commit -a

remove

先删除这个文件,再取消git的跟踪

1
2
rm README.md
git rm README.md

move

1
$ git mv file_from file_to

checking history

default git log: return all the commit history

1
git log

-p: show the difference of each commit
-2: just show the latest 2 commit
--stat: show the simplified version

amend

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令来重新提交:

1
git commit --amend

cancel the file that have been staged

1
git reset HEAD CONTRIBUTING.md

cancel the file that have been commited

1
git checkout -- CONTRIBUTING.md

TO NOTICE: Usually, the behaviors are dangerous. Take attention when using them.

TO BE CONTINUED