What’s The Deal With The Git Index?
来源:互联网 发布:java中extends的用法 编辑:程序博客网 时间:2024/06/06 01:49
源自:http://www.gitguys.com/topics/whats-the-deal-with-the-git-index/
What’s The Deal With The Git Index?
Commands discussed in this section:
- git add
- git commit
- git ls-files
- git status
- git mv
- git rm
The git index defined in 2 sentences
The git “index” is where you place files you want committed to the git repository.
Before you “commit” (checkin) files to the git repository, you need to first place the files in the git “index”.
An index by any other name
The git index goes by many names. But they all refer to the same thing. Some of the names you may have heard:
- Index
- Cache
- Directory cache
- Current directory cache
- Staging area
- Staged files
Why so many names for the same thing? Good question! We don’t cover linguistics questions in this section, but you can be sure the answer would start with “For historical reasons…”
The Index Isn’t The Working Directory
The working directory is where you do your work: You add, remove, and modify files in the working directory.
You can type a command such as git status and git will tell you:
- What files have been added to the git index (for example by using the git add filename command).
- What files git notices that exist in the working directory, but aren’t in the git index.
- What files have different contents between the version in the git index and the version in the working directory.
The Index Isn’t The Git Repository
Files in the git index are files that git would commit to the git repository if you used the git commit command.
However, files in the git index don’t get committed to the repository until you use the git commit command.
The Index: Compared with Legacy VCS’s
Let’s say you have just created a new file named README. How do you add the file to your repository?
In a legacy Version Control System (VCS):
- You tell the VCS what file you want added to the repository (Perforce: p4 add README; Subversion: svn add README)
- Then tell the VCS to save the file to the repository (Perforce: p4 submit; Subversion: svn commit …)
Git behaves differently. With git, to add a new file to the git repository, you:
- Use “git add README”. This causes git to:
- Copy the contents of the README file to the git object store and store its contents as a blob.
- Record in the git index: Filename README and the hash of the README blob in the object store.
- Mark (remember) the README file in the user’s working directory is now “tracked”.
- Use “git commit” which takes the files that are referred to in the “git index” and saves them to the git repository.
Git Index Example
In the below example:
- A git session is shown in the upper right portion of the diagrams.
- The result of the session is shown in the lower right portion (words) and in the left portion (pictures).
- The most recent command typed is shown in a brown font
- The Session begins in a new, empty, directory immediately after a new git repository has been created (e.g. after “git init“).
You can click on the thumbnails in the lower left hand corner to choose which image to display.
You can click on the thumbnails in the lower left hand corner to choose which image to display.
A couple helpful index-related commands
A couple of git commands can be very helpful in showing you what files are in the index, whether or not the filenames in the index have been changed in the working directory, etc.
The git commands are:
- git status: Show what git determines are differences between the working directory, index and the most recent commit. For example:
- git ls-files: Show what files are in the git index
See What’s in the index? What’s Changed? for examples of the git status and git ls-files commands.
You can see examples of git diff which can show differences between the working directory, index, and repository.
Renaming and Removing files
To rename a file in the index and the working directory:
$ git mv README readme$ git ls-filesreadme$ git status# On branch master# Changes to be committed:# (use "git reset HEAD ..." to unstage)## renamed: README -> readme#
And to remove a file from the working directory and the index:
$ rm README$ git rm README
- What’s The Deal With The Git Index?
- What’s The Deal With The Git Index?
- $ What's the deal with the dollar signs?
- What's the deal with Databinder.Eval and Container.DataItem?
- What’s In The Index? What’s Changed?
- what's the Soft3M
- What's the CCIR601?
- What’s the jiffy?
- What's the J2EE?
- What's the JPA?
- What's the CRUD?
- What's the AOP?
- what's the team
- What's the LDAP?
- what's the Glide
- What's the WAR
- What's the difference between Git Revert, Checkout and Reset?
- Four Ways to Deal With the Longer iPhone 5 screen
- 使用Python编写客户端 上传文字or图片至新浪微博
- 使用GDI绘制点
- __declspec(dllimport)和__declspec(dllexport)的区别,以及有关c/c++调用约定
- apk反编译
- PB 函数 中文解释 收集
- What’s The Deal With The Git Index?
- 加速 Microsoft Visual Studio 2008 启动过程
- 我的java思想006:使用双缓冲消除屏幕闪烁现象
- 通过 DomainDataSource 实现数据的添加、查询、更新和删除操作
- 推荐系统中对数据的需求和利用
- 比赛小结
- 怎样为EXCEL2010添加下拉列表
- 开博第一篇
- annotation for hibernate