主要包含如下几部分内容:
- 如何初始化一个Git库
- 记录Git库中文件状态的改变:
工作目录中的文件状态主要包括如下5种:
untracked//文件在工作目录中,但是不在Git库中
unmodified//工作目录中和GIt库中是文件保存一致
modified//工作目录中的文件做了修改,但尚未暂存staged
Staged//通过git add命令,把modified状态的文件变为暂存态
Staged Modified//文件修改后,通过git add变为暂存态,然后又做了修改。
1. Getting a Git Repository
1.1 Initializing a Repository in an Existing Directory
If you’re starting to track an existing project in Git, you need to go to the project’s directory and type:
$ git init
This creates a new subdirectory named .git that contains all of your necessary repository files – a Git repository skeleton.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 *.c
$ git add LICENSE
$ git commit -m 'initial project version'
1.2 Cloning an Existing Repository
$ git clone https://github.com/libgit2/libgit2
If you want to clone the repository into a directory named something other than “libgit2”, you can specify that asthe next command-line option:
$ git clone https://github.com/libgit2/libgit2 mylibgit
Git has a number of different transfer protocols you can use. The previous example uses the https:// protocol, but you may also see git:// or user@server:path/to/repo.git, which uses the SSH transfer protocol.
2. Recording Changes to the Repository
2.1. Checking the Status of Your Files
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
2.1.1 Short Status
If you run git status -s or git status --short you get a far more simplified output from the command:
$ git status -s
M README
MM Rakefile//The Rakefile was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.
A lib/git.rb//new files that havebeen added to the staging area
M lib/simplegit.rb//modified
?? LICENSE.txt//untracked
2.2. Tracking New Files
$ git add README
2.3. Staging Modified Files
The files was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.
2.4. Ignoring Files
在.gitignore file中把不需要进行版本控制的文件列出来
$ cat .gitignore
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
2.5. Viewing Your Staged and Unstaged Changes
$ git diff: The command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.
git diff shows you the exact lines added and removed
$ git diff
diff --git a/1.txt b/1.txt
index 767df6f..02f8fa6 100644
--- a/1.txt
+++ b/1.txt
@@ -1,4 +1,4 @@
hello
+world
my name
-world!!
is amber
结果分析:参考读懂diff-合并格式的diff
diff --git a/1.txt b/1.txt
第一行表示结果为git格式的diff。
index 767df6f..02f8fa6 100644
第二行表示两个版本的git哈希值(index区域的767df6f对象,与工作目录区域的02f8fa6对象进行比较),最后的六位数字是对象的模式(普通文件,644权限)。
--- a/1.txt
+++ b/1.txt
第三行表示进行比较的两个文件。
@@ -1,4 +1,4 @@
变动的位置用两个@@作为起首和结束。前面的"-1,4"分成三个部分:减号表示第一个文件(即a),"1"表示第1行,"4"表示连续4行。合在一起,就表示下面是第一个文件从第1行开始的连续4行。同样的,"+1,4"表示变动后,成为第二个文件从第1行开始的连续4行。
hello
+world
my name
-world!!
is amber
第三部分是变动的具体内容。除了有变动的那些行以外,也是上下文各显示3行。它将两个文件的上下文,合并显示在一起,所以叫做"合并格式"。每一行最前面的标志位,空表示无变动,减号表示第一个文件删除的行,加号表示第二个文件新增的行。
git diff --staged: This command compares your staged changes to your last commit:
$ 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
2.6. Committing Your Changes
$ 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
2.6.1. Skipping the Staging Area
git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 file changed, 5 insertions(+), 0 deletions(-)