在使用Git时如果需要获取帮助

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

例如,获取config命令的手册

git help config

获取仓库

获取Git仓库的方式有两种:第一种是在现有项目目录直接创建,然后将文件导入Git;第二种是克隆Git仓库。

初始化仓库

在现有目录中初始化Git仓库

mkdir my-project && cd my-project
git init

此时将会创建.git子目录,该子目录包含初始化Git的所有必须文件,是Git仓库的骨干。

如果此时在my-project 创建工程文件,并需要进行版本管理,此时可以对这些文件追踪并提交。可以通过git add命令对指定的文件追踪,并通过git commit提交。

$ git add *.c
$ git add LICENSE
$ git commit -m 'initial project version'

克隆现有仓库

Git克隆的是该Git仓库在服务器上的几乎所有数据,而不是仅仅复制所需的文件。当执行git clone 命令时,默认远程Git仓库的每个文件的每个版本都会被拉去下来。

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

这会在当前目录下创建名为libgit2 目录,并在该目录初始化.git文件夹,而从远程仓库拉取的所有数据放入.git文件夹,并从中读取最新版本的文件的拷贝。

如果想在克隆远程仓库时,自定义本地仓库的名字

$ git clone https://github.com/libgit2/libgit2 mylibgit

git支持多种传输协议,包括https,git,ssh。

更新与提交

项目中文件只有两种状态:已跟踪与未跟踪。

除此克隆的某个仓库,工作目录中的所有文件都属于已跟踪文件,并处于未修改状态。

检查文件状态

要检查文件状态,需要使用git status 命令

$ git status
On branch master
nothing to commit, working directory clean

现在在项目下创建新的README文件,此时它是未跟踪文件

$ echo 'My Project' > README
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    README

nothing added to commit but untracked files present (use "git add" to track)

跟踪新文件

使用命令git add 跟踪文件

git add README

查看文件状态,此时文件处于暂存状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README

暂存已修改的文件

现在修改已经跟踪过的文件CONTRIBUTING.md,查看文件状态,此时修改的文件并未进入暂存状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   CONTRIBUTING.md

要暂存这次修改,需要运行git add 命令

$ git add CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README
    modified:   CONTRIBUTING.md

git status 命令输出十分详细,可以使用更为紧凑的格式输出

$ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

其中新添加的未跟踪文件前面是??标记,新添加到暂存区的文件为A标记,修改过的文件前面未M标记。

而M可能出现在两个位置,右边的M表示文件修改但没有放入暂存区;左边的M表示文件修改且放入暂存区。

遥想查看尚未暂存的文件更新哪些部分,可以使用git diff 命令

$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
 Please include a nice description of your changes when you submit your PR;
 if we have to read the whole diff to figure out why you're contributing
 in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if your patch is
+longer than a dozen lines.

 If you are starting to work on a particular area, feel free to submit a PR
 that highlights your work in progress (and note in the PR title that it's

此命令比较的是工作目录中当前文件和暂存区域快照之间的差异。

如果要查看已暂存的将要添加到下次提交的内容,可以使用git diff --cache 命令,git 1.6.1+还允许使用git diff --staged

$ git diff --staged
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project

提交更新

在每次提交之前,先使用git status 查看,是否所有修改的内容已经暂存起来,然后提交更新

git commit

此时会启动文本编辑器以便输入本次提交说明。它默认会启动shell环境变量$EDITOR 指定的软件,一般是vim或emacs。编辑器显示如下

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#    new file:   README
#    modified:   CONTRIBUTING.md
#
~
~
~
".git/COMMIT_EDITMSG" 9L, 283C

当然也可以使用-m选项,将提交信息与命令放在同一行

$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
 2 files changed, 2 insertions(+)
 create mode 100644 README

尽管使用暂存区可以精心准备提交的细节,但是有时会显得繁琐。git允许跳过暂存区域,将所有已追踪的文件一并提交

$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
 1 file changed, 5 insertions(+), 0 deletions(-)

移除文件

要从Git中移除文件,需要从跟踪文件清单移除,然后提交,git rm 可以完成此项工作,并连带从工作目录删除指定文件。

$ git rm PROJECTS.md
rm 'PROJECTS.md'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    PROJECTS.md

如果在删除之前,修改过且已经放在暂存区域,则必须使用强制删除选项-f。

但如果是想将文件从Git仓库删除,但仍然希望保存在当前工作目录,可以

$ git rm --cached README

git rm 命令可以列出文件或目录名字,也可以使用glob模式

$ git rm log/\*.log

移动文件

git mv 命令相当于运行3条命令:

$ mv README.md README
$ git rm README.md
$ git add README

results matching ""

    No results matching ""