虚拟项目学习git/github原理与基本操作4

来源:互联网 发布:macbook下载不了软件 编辑:程序博客网 时间:2024/06/06 05:23

新建一个新的文件,作为项目的第一个正式文件,因为当前我们的项目中已经包含一个README文件,这个时候做了一次项目的提交,所以我们现在项目的一个版本记录了当前版本的所有文件的镜像,我们称为C1。


查看当前项目状态:

git status

会给出当前项目中的文件的状态,是未跟踪,还是已经修改没暂存,或者已经暂存,或者已经提交等状态。

watkins@watkins:~/watkins/finance$ git status # On branch masternothing to commit (working directory clean)watkins@watkins:~/watkins/finance$ 

目前我们的项目是一个都已经提交了的项目。

然后我们建立一个配置文件,配置项目的应用地点。location, timezone等信息。

watkins@watkins:~/watkins/finance$ mkdir configwatkins@watkins:~/watkins/finance$ cd config/watkins@watkins:~/watkins/finance/config$ touch configuration\> ^Cwatkins@watkins:~/watkins/finance/config$ touch configurationwatkins@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

然后将本地的仓库推送到远端github中进行保存。

watkins@watkins:~/watkins/finance$ git push origin master Username for 'https://github.com': weixingstudioPassword for 'https://weixingstudio@github.com': To https://github.com/weixingstudio/finance.git   79ec82e..8c4472f  master -> masterwatkins@watkins:~/watkins/finance$ 


然后,我们开始给项目增加一些具体的功能性的代码。

新建一个function1的文件,假设这个文件包含一些有用的功能代码。

watkins@watkins:~/watkins/finance$ touch funciton1watkins@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)##funciton1nothing 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)##funciton1nothing added to commit but untracked files present (use "git add" to track)watkins@watkins:~/watkins/finance$ git add funciton1watkins@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文件已经在暂存区中等待提交。


然后我们再次对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 出现了两次,第一次的出现是因为我们在第一次创建文件的时候已经把文件放入到了暂存区,所以等待提交,第二次出现是因为我们修改了文件,但是没有放入到暂存区,可以看到第二次的提示是:changes not staged for commit,就是说已经修改的文件还没有放入暂存区,也不会作为下一次提交的内容。

这个时候如果提交当前项目,那么提交的是第一次修改的function1 中的内容,第二次修改的内容会在下一次提交的时候进入暂存区后进行提交


这个时候如果想把第二次修改的内容也一起提交,需要使用git add 重新把修改了的文件加入到暂存区中。

watkins@watkins:~/watkins/finance$ git add funciton1watkins@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 diffdiff --git a/funciton1 b/funciton1index 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 READMEwatkins@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$ 

然后加入README到暂存区,比较当前暂存区与上次提交的README之间的差别:

watkins@watkins:~/watkins/finance$ git diff --cached diff --git a/README b/READMEindex 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/funciton1new file mode 100644index 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$ 

包括README与function1文件的差别都列出来了。

然后提交并推送到github

watkins@watkins:~/watkins/finance$ git commit -m 'add function1'[master f2f51bd] add function1 2 files changed, 16 insertions(+) create mode 100644 funciton1watkins@watkins:~/watkins/finance$ git push origin master To https://github.com/weixingstudio/finance.git   8c4472f..f2f51bd  master -> masterwatkins@watkins:~/watkins/finance$ 


这个时候我们的文件的版本称为C3,相当与当前文件的快照C3,C2是C3直接父节点。

很多人会觉得暂存区的设置过于麻烦,但是要牢记,每次提交到仓库的文件都必须的在暂存区中的,又可以称为在索引中的文件,提交到仓库的文件至于在暂存区中的文件打交道。

所以必须将修改的文件提交到暂存区,如果觉得每次都提交到暂存区过于繁琐,可以使用简单的提交方法,就是越过暂存区,只要在提交的时候,给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤

$ git commit -a -m 'added new benchmarks'


原创粉丝点击