Git帮助手册
git命令全面介绍, 可以选择中文版阅读(不过有些内容有丢失, 如果能看懂原版, 建议看原版)
创建一个版本控制的文件, 用到的这些git指令, 可以对照着手册进行阅读
今天尝试git commit, 提示我有untracked file
有些疑惑, 于是去查看了git帮助手册
在第一章1.3 git basics 中, 了解到了git的三种状态
由此引入 Git 项目的三个工作区域的概念:Git 仓库(the Git directory,) 工作目录( the working tree)以及暂存区域(the staging area.)。
The basic Git workflow goes something like this:
基本的 Git 工作流程如下:
You modify files in your working tree.
你修改的文件在working tree(工作目录)中 (也就是你在本地磁盘上想要版本控制的那个文件)
You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.
选择暂存那些你想放到下一次提交(commit)的改变,将文件快照(也就是改变后的文件)
You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
提交更新,找到暂存区域(staging area)的文件,将快照永久性存储到 Git 仓库目录。(git directory)
If a particular version of a file is in the Git directory, it’s considered committed.
如果 Git 目录中保存着的特定版本文件,就属于已提交状态(committed)。
If it has been modified and was added to the staging area, it is staged.
如果作了修改并已放入暂存区域,就属于已暂存状态(staged)。
And if it was changed since it was checked out but has not been staged, it is modified.
如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态。(modified)
回到刚刚我遇到的问题,提示我nothing added to commit but untracked files present
没有被加到提交(commit)中的文件, 但是有untraked(未跟踪)的文件
为了知道这个未跟踪是什么意思, 我又找到了另一节内容。
第二节内容大致概括了Git的大部分操作, 推荐初学者看。
2.1 Git Basics - Getting a Git Repository
2.2 Git Basics - Recording Changes to the Repository
有两种取得 Git 项目仓库的方法。
第一种是在现有项目或目录下导入所有文件到 Git 中;
You can take a local directory that is currently not under version control, and turn it into a Git repository, or
第二种是从一个服务器克隆一个现有的 Git 仓库。
You can clone an existing Git repository from elsewhere.
如果你打算使用 Git 来对现有的项目进行管理,你只需要进入该项目目录并输入:
$ git init
注意现在是在GitTest这个上级目录中做版本控制, 所以要在GitTest上右键,点击 git bush.
该命令将创建一个名为 .git 的子目录,这个子目录含有你初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。 但是,在这个时候,我们仅仅是做了一个初始化的操作,你的项目里的文件还没有被跟踪。
This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet.
所以这里推断出, nothing added to commit but untracked files present 是因为我做了修改的东西没有添加到暂存区域(staging area)
【之后会说到这个暂存区域, 大家多看上面两张图, 就可以理解清楚Git的基本操作原理】
如果你是在一个已经存在文件的文件夹(而不是空文件夹)中初始化 Git 仓库来进行版本控制的话,你应该开始跟踪这些文件并提交。 你可通过 git add 命令来实现对指定文件的跟踪,然后执行 git commit 提交:
If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that specify the files you want to track, followed by a git commit:
$ git add gittest
$ git commit -m 'initial project version'
如下图, 执行git add后, 我的名为gittest的文件就会被跟踪(track), 也就是把它状态变为staged, 也就把它放入了staging area(暂存区域).
三种工作目录与四种文件状态
稍后我们再逐一解释每一条指令的意思。现在,你已经得到了一个实际维护(或者说是跟踪)着若干个文件的 Git 仓库。
We’ll go over what these commands do in just a minute. At this point, you have a Git repository with tracked files and an initial commit.
Git 建立 Local Repository
$ mkdir project; cd project #或者手动创建一个本地文件夹, 用右键git bush
$ git init
$ echo "hello" > hello.txt
$ git add . #将修改(modified)过的文件添加到暂存区(staging area)
$ git commit -m 'initial' #提交commit, 初始化提交信息。
发现一个比较详细的git命令举例:link
> 和 >> 的 区别: What is difference between > and >>
如果你想获得一份已经存在了的 Git 仓库的拷贝,比如说,你想为某个开源项目贡献自己的一份力,这时就要用到 git clone 命令。 如果你对其它的 VCS 系统(比如说Subversion)很熟悉,请留心一下你所使用的命令是"clone"而不是"checkout"。 这是 Git 区别于其它版本控制系统的一个重要特性,Git 克隆的是该 Git 仓库服务器上的几乎所有数据,而不是仅仅复制完成你的工作所需要文件。 当你执行 git clone 命令的时候,默认配置下远程 Git 仓库中的每一个文件的每一个版本都将被拉取下来。 事实上,如果你的服务器的磁盘坏掉了,你通常可以使用任何一个克隆下来的用户端来重建服务器上的仓库(虽然可能会丢失某些服务器端的挂钩设置,但是所有版本的数据仍在。
克隆仓库的命令格式是
git clone [url] 。
git clone https://github.com/Jane-QinJ/Girls-In-AI.git
Git 支持多种数据传输协议。 上面的例子使用的是 https:// 协议,不过你也可以使用 git:// 协议或者使用 SSH 传输协议,比如 user@server:path/to/repo.git 。
记录每次更新到仓库
这个算是版本控制最常用的功能了, 就是记录你的每一次修改。 你还可以为每一次修改添加必要的批注, 每一次的修改都会以快照的形式记录, 所以随时可以恢复成上一状态。
再回顾上面的图,使用 Git 时文件的生命周期如下:
下面详细介绍一下这几个Git中的文件状态:
Remember that each file in your working directory can be in one of two states: tracked or untracked.
请记住,你工作目录下的每一个文件都不外乎这两种状态:*已跟踪或未跟踪。
As you edit files, Git sees them as modified, because you’ve changed them since your last commit. As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats.
编辑(edit)过某些文件之后,由于自上次提交后你对它们做了修改,Git 将它们标记为已修改文件(modified)。 我们逐步将这些修改过的文件放入暂存区(staging area),然后提交(commit)所有暂存了的修改,如此反复。所以使用 Git 时文件的生命周期如上图。
简而言之, 当你增加一个没有进行git add的文件, 它就是untracked状态。
而如果你修改了一个你已经跟踪(tracked)的文件, 它就会变为modified状态。 进一步, 你将这个修改过的文件通过git add【文件名】 加入了staging area, 这时它就可以被提交(commit)了。
最后, 你要将这个提交, push到你的GitHub上, 用git push.
以上各种文件状态, 都是可以查看的, 用到的Git命令为:
$ git status
以我的为例
我的Git跟踪文件目前没有修改, 所以nothing to commit, working tree clean.
没有要提交的内容, working tree(工作目录)很干净。
现在我们新建一个文件, 它就是未被跟踪的(untracked)
熟悉的问题, 看来我之前出现的问题就是在这里。 我新添加的文件仅仅在我本地的工作目录上(working dictionary), 但它要被提交到git directory才可以被Git控制。
推荐大家继续将Git的工作目录和Git的状态结合起来看。
现在更加理解了, 我们工作的地方在本地文件夹, 也就是working directory. 而Git要对我们的文件夹进行监控与控制, 是在git directory,
再重新回顾一下这三个工作区域的概念, 相信你会更加理解。