新建一个新的文件,作为项目的第一个正式文件,因为当前我们的项目中已经包含一个README文件,这个时候做了一次项目的提交,所以我们现在项目的一个版本记录了当前版本的所有文件的镜像,我们称为C1。
查看当前项目状态:
git status
watkins@watkins:~/watkins/finance$ git status # On branch master nothing to commit (working directory clean) watkins@watkins:~/watkins/finance$
然后我们建立一个配置文件,配置项目的应用地点。location, timezone等信息。
watkins@watkins:~/watkins/finance$ mkdir config watkins@watkins:~/watkins/finance$ cd config/ watkins@watkins:~/watkins/finance/config$ touch configuration\ > ^C watkins@watkins:~/watkins/finance/config$ touch configuration watkins@watkins:~/watkins/finance/config$ vim configuration watkins@watkins:~/watkins/finance/config$
watkins@watkins:~/watkins/finance$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # config/ nothing added to commit but untracked files present (use "git add" to track) watkins@watkins:~/watkins/finance$
使用git add 可以跟踪新的文件。
watkins@watkins:~/watkins/finance$ git add config/ watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: config/configuration # watkins@watkins:~/watkins/finance$
这个时候我们的新建里的configuration文件已经被跟踪,并且被保存到了暂存区,在下一次提交的时候就可以被提交到代码仓库。使用git add添加追踪文件的时候,会自动的将新添加的文件添加到暂存区。
同时,git的提示中也给出了将文件从暂存区移除的方法:git reset HEAD <file>
然后这个时候我们提交所有的暂存区的内容,保存一个软件的版本。因为新添加了一个文件,所以现在保存软件的版本为当前所有文件的快照,我们这里称为C2, C1是C2的父节点。
watkins@watkins:~/watkins/finance$ git commit -m 'add configuration file' [master 8c4472f] add configuration file 1 file changed, 2 insertions(+) create mode 100644 config/configuration
watkins@watkins:~/watkins/finance$ git push origin master Username for 'https://github.com': weixingstudio Password for 'https://[email protected]': To https://github.com/weixingstudio/finance.git 79ec82e..8c4472f master -> master watkins@watkins:~/watkins/finance$
然后,我们开始给项目增加一些具体的功能性的代码。
新建一个function1的文件,假设这个文件包含一些有用的功能代码。
watkins@watkins:~/watkins/finance$ touch funciton1 watkins@watkins:~/watkins/finance$ vim funciton1 watkins@watkins:~/watkins/finance$
watkins@watkins:~/watkins/finance$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # funciton1 nothing added to commit but untracked files present (use "git add" to track) watkins@watkins:~/watkins/finance$
追踪文件
watkins@watkins:~/watkins/finance$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # funciton1 nothing added to commit but untracked files present (use "git add" to track) watkins@watkins:~/watkins/finance$ git add funciton1 watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: funciton1 # watkins@watkins:~/watkins/finance$
然后我们再次对function1文件进行功能行的修改。假设添加新的功能。
然后查看状态可以看到:
watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: funciton1 # # 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: funciton1 # watkins@watkins:~/watkins/finance$
这个时候如果提交当前项目,那么提交的是第一次修改的function1 中的内容,第二次修改的内容会在下一次提交的时候进入暂存区后进行提交。
这个时候如果想把第二次修改的内容也一起提交,需要使用git add 重新把修改了的文件加入到暂存区中。
watkins@watkins:~/watkins/finance$ git add funciton1 watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: funciton1 # watkins@watkins:~/watkins/finance$
使用git diff命令可以清楚的查看文件的哪些地方进行了修改。
git diff比较的是暂存区的文件与当前工作目录的文件的差别。
再次修改function1文件,并且不暂存。使得暂存区的数据与工作目录的不一致,然后使用git diff
watkins@watkins:~/watkins/finance$ git diff diff --git a/funciton1 b/funciton1 index f67bd21..239adee 100644 --- a/funciton1 +++ b/funciton1 @@ -7,3 +7,8 @@ int elsefunction() { // inner function } + +int thirdfunction() +{ + // the third function +} watkins@watkins:~/watkins/finance$
使用git diff --cached可以比较在暂存区的文件与上次提交的文件之间的差别。
我们对README进行简单修改。
watkins@watkins:~/watkins/finance$ vim README watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: funciton1 # # 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: README # watkins@watkins:~/watkins/finance$ git add README watkins@watkins:~/watkins/finance$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README # new file: funciton1 # watkins@watkins:~/watkins/finance$
watkins@watkins:~/watkins/finance$ git diff --cached diff --git a/README b/README index 50cf244..e5ea8ca 100644 --- a/README +++ b/README @@ -1,3 +1,5 @@ this is a project that contain a software that calcualte the revenue of a company. and this software may have several versions. by watkins.song +another revise by watkins +hah.~~~&)(* diff --git a/funciton1 b/funciton1 new file mode 100644 index 0000000..239adee --- /dev/null +++ b/funciton1 @@ -0,0 +1,14 @@ +int function1(args[]) +{ + // do some funciton +} + +int elsefunction() +{ + // inner function +} + +int thirdfunction() +{ + // the third function +} watkins@watkins:~/watkins/finance$
然后提交并推送到github
watkins@watkins:~/watkins/finance$ git commit -m 'add function1' [master f2f51bd] add function1 2 files changed, 16 insertions(+) create mode 100644 funciton1 watkins@watkins:~/watkins/finance$ git push origin master To https://github.com/weixingstudio/finance.git 8c4472f..f2f51bd master -> master watkins@watkins:~/watkins/finance$
很多人会觉得暂存区的设置过于麻烦,但是要牢记,每次提交到仓库的文件都必须的在暂存区中的,又可以称为在索引中的文件,提交到仓库的文件至于在暂存区中的文件打交道。
所以必须将修改的文件提交到暂存区,如果觉得每次都提交到暂存区过于繁琐,可以使用简单的提交方法,就是越过暂存区,只要在提交的时候,给 git commit
加上 -a
选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add
步骤
$ git commit -a -m 'added new benchmarks'