注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

zorksylar

Nothing is impossible , if distributed.

 
 
 

日志

 
 

【Git】Git 学习之Recording Changes to the Repository  

2012-01-14 11:04:20|  分类: git |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

File status
Remember that each file in your working directory can be in one of two states: 
tracked or untracked.Tracked files are files that were in the last snapshot; they can be unmodifiedmodified, or staged.Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and haven’t edited anything.

就是说在Git看来,目录中的文件,分为两类,一类是tracked,一类是untracked
trached的file,是被跟踪的file,会上传到github上并会进行更新等的
而untrached的file是不被跟踪的,不会上传到github上,在commit的时候也不回commit到,相当于私人的一些file.
默认clone一个repo之后,相应目录下所有的file都是trached的.  

【Git】Git 学习之Recording Changes to the Repository - sylareclipse - sylar
                                                           The lifecycle of the status of your files.


git status

命令用来查看本地当前repo的状态

Tracking New Files

In order to begin tracking a new file, you use the command git add. To begin tracking the README file, you can run this:
$ git add README

If you run your status command again, you can see that your README file is now tracked and staged:

$ git status                                                                                                # On branch master                                                                                          # Changes to be committed:                                                                                  #   (use "git reset HEAD <file>..." to unstage)                                                             #                                                                                                           #        new file:   README                                                                                 #
要跟踪一个file的时候就git add target_file 
同时这个文件就是trached的了.

Staging Modified Files

如果修改了某个文件,在commit之前,也需要将这个文件add上

$ git add modified_file

否则,commit的时候记录的是被修改文件上次add的记录
add后,文件就处于staged状态了

Ignoring Files

被ignore的file,可以理解为是untracked的file
Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore

Here is an example .gitignore file:

$ cat .gitignore *.[oa] *~

在本地repo的目录下有个.gitignore文件,这个文件用于说明那些文件是untrached的.
.gitignore 文件的规则如下:

The rules for the patterns you can put in the .gitignore file are as follows:

  • Blank lines or lines starting with # are ignored.也就是#是注释,空行不考虑
  • Standard glob patterns work.
  • You can end patterns with a forward slash (/) to specify a directory.
  • You can negate a pattern by starting it with an exclamation point (!).

关于上面第二点的 Standard glob patterns:
Glob patterns are like simplified regular expressions that shells use. An asterisk (*) matches zero or more characters; [abc] matches any character inside the brackets (in this case ab, or c); a question mark (?) matches a single character; and brackets enclosing characters separated by a hyphen([0-9]) matches any character in the range (in this case 0 through 9) .

* : 0个或多个字符,
[abc]:a或b或c
? : 任意一个字符
[a-b]:a到b之间的字符.

一个例子:
# a comment - this is ignored *.a       # no .a files !lib.a    # but do track lib.a, even though you're ignoring .a files above /TODO     # only ignore the root TODO file, not subdir/TODO build/    # ignore all files in the build/ directory doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt

Viewing Your Staged and Unstaged Changes

$ git status

命令只能知道 那些文件被修改了,changed but not staged,而不能知道确切被修改的内容,

$ git diff

能够做到显示changed but not staged的详细内容,注意diff 并不会显示所有的change,只会显示unstaged状态的change
另外:

$ git diff --staged

将会显示下次commit中和上次commit的改变
If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged.
 

Committing Your Changes

注意:所有changed之后,没有add的都是unstaged的,commit的时候,不会记录unstaged的文件,只会记录staged的文件。

$ git commit

便会commit所以staged的文件,
同时弹出

# 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:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
# modified: benchmarks.rb

这是git status结果,去掉#后,表示把这个作为commit的message。
如果

$ git commit -v

则会把git diff的结果加入到为commit的message中
如果

$ git commmit -m 'your message'

则用自己定义‘your message’作为的commit的message

Skipping the Staging Area

有时候每次都要在commit之前把所有修改的文件add一下很麻烦,

$ git commit -a -m 'your message'

则让git自动把所有tracked的文件都staged了,也就是都add了

Removing Files

$ git rm file1

把file1从staged area 删除,并且从硬盘中删除了这个文件,repo中将不会有,

$ git status
# On branch master
# Changes to be commited:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: file1
#

状态变为staged
或者

$ rm file1

$ git status

将会是chaged but not staged
还需要

$ git rm file1

$ git status

状态将编程staged

Moving Files

$ git mv file_form file_to

等价于:

$ mv file_form file_to

$ git rm file_from

$ git add file_to

状态变为staged
  评论这张
 
阅读(679)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018