Git中的各个分区

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、Git的各个分区
    • 1. 工作区(Working Directory)
    • 2. 暂存区(Stage/Index)
    • 3. 资源库(Repository或Git Directory)
    • 4. git仓库(Remote Directory)
  • 二、文件的几种状态
  • 三、Basic Usage


前言

因为工作的原因,离开了一线开发岗,虽然不怎么写代码,但是和GIT的接触更多了起来,经历过SVN和GIT两个最重要的版本管理工具,感觉GIT太棒了,特此记录觉得自己对GIT不了解的地方。

一、Git的各个分区

1. 工作区(Working Directory)

在git管理下的正常目录都算是工作区。就是你平时存放项目代码的地方。

2. 暂存区(Stage/Index)

暂存区,用于临时存放你的改动。

3. 资源库(Repository或Git Directory)

资源库(或版本库),就是安全存放数据的位置,这里面有你提交到所有版本的数据。其中HEAD指向最新放入仓库的版本。

4. git仓库(Remote Directory)

远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑存放Git代码的机器。

Git中的各个分区_第1张图片
git的工作流程一般是这样的:
1、在工作目录中添加、修改文件;
2、将需要进行版本管理的文件放入暂存区域;
3、将暂存区域的文件commit到资源库;
4、 将资源库的文件push到git远程仓库;
因此,git管理的文件有三种状态:已修改(modified),已暂存(staged),已提交(committed).

二、文件的几种状态

Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.
Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified.如果使用git rm移出版本库, 则成为Untracked文件。
Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过,返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改。
Staged: 暂存状态。执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存,文件状态为Modified。
下面的图很好的解释了这四种状态的转变:
Git中的各个分区_第2张图片

三、Basic Usage

Git中的各个分区_第3张图片
The four commands above copy files between the working directory, the stage (also called the index), and the history (in the form of commits).

  • git add files copies files (at their current state) to the stage.
  • git commit saves a snapshot of the stage as a commit.
  • git reset – files unstages files; that is, it copies files from the
    latest commit to the stage. Use this command to “undo” a git add
    files. You can also git reset to unstage everything.
  • git checkout – files copies files from the stage to the working
    directory. Use this to throw away local changes. You can use git
    reset -p, git checkout -p, or git add -p instead of (or in addition
    to) specifying particular files to interactively choose which hunks
    copy. It is also possible to jump over the stage and check out files
    directly from the history or commit files without staging first.
    Git中的各个分区_第4张图片
  • git commit -a is equivalent to running git add on all filenames that
    existed in the latest commit, and then running git commit.
  • git commit files creates a new commit containing the contents of the
    latest commit, plus a snapshot of files taken from the working
    directory. Additionally, files are copied to the stage.
  • git checkout HEAD – files copies files from the latest commit to
    both the stage and the working directory.

你可能感兴趣的:(Git,git,github)