An healthy git repository, is readable and tracable. We need fully clear what we commit. The Git Index, is what we need, just like a shopping cart. We pick goods(updates) from the shelves(work tree). Then push the cart(index) to cash register, and buy(commit) them. With the Git Index, we have ability to deal with a big heap of updates orderly.
There are three git commands for managing Git Index.
addadd updates from work tree to index
resetremove updates from index
checkoutgive up updates that not be added to index (cover updates in work tree by index) Both
checkoutare multi-purpose. The command
resetalso can use to move current branch. And the command
checkoutalso can use to move HEAD and checkout to the work tree. The difference is the parameter for the command. If the parameter is a commit(hash or branch, tag…), it’s for the branch and HEAD. If the parameter is a path, or there is no parameter, it’s for the index.
checkout can run with option
-p in short.
Git will iterate over the updates and let’s handle them one by one.
You can run with paths as parameters to iterate over one file or several files,
or just run with no parameter to iterate over all updates.
Using there command with
-p option is the best way currently to manage the Git Index.
I name it 3Ps Sorcery.
After run the command, we get a interactive interface like this:
The last Line is different with different command:
As we see, git show an update in the file
And give us several operations
y,n,q,a,d,k,K,j,J,g,/,s,e,?. What are they use for?
By the first sight, I only know the
? provides helps.
So we input ? Enter.
We get the message:
What’s a hugging heap of dazing stuffs! Don’t worry, just let’s reorder them:
We only need know
q for basic using.
For advanced using, we need know one more:
For programmers, that’s enough.
For dictionary keeper, or Webpack configuratician, navigations will be useful.
In this section, we talk about the
To begin the editing, we need an editor.
Just like when you run
git will open vim or nano when you using
And the text in the editor will like this:
Here is an update, we delete the line bar, and insert lines baz and qux.
@@ ... @@ shows the line number and line counts before and after edit.
And follows the content of update.
The first character of each line is the line type, can be space,
The space means this line is a normal line,
- for a deleted line, and an
+ for a new inserted line.
Attention, this space is not indentation.
If you use indent characters for indentation, here is a space at the first.
If you use spaces instead of indentation, one more space each line.
If you just quit the editor without edit anything,
it’s just equal to press a
y instead of
If you recover the update, remove all
+ lines, change
-s to spaces,
it’s just equal to press a
n instead of
We can modify it as you like, but not all modifications are allowed.
It’s easier to understand when we run
add. Here is an example,
if we make modifications like this:
As we see, we don’t delete line bar, but delete line foo. And we edit line baz, and insert qux to another position. That’s not difficult. We’ll found that space lines and
-lines can change to each others, but you can not modify their content;
+lines can make any modifications, remove or insert anywhere when
reset, it’s different. Oppositely, space lines and
+ lines can change to each others,
but you can not modify their content;
- lines can make any modifications, remove or insert anywhere when
It’s confusing. So, example again. the same situation, let’s edit the text to this:
To understand what will happen, we must using words like give up or cancel.
We change line foo from space line to
+ line, means we cancel the adding of line foo,
but the line foo is not added, it’s over there original,
so it’s means delete the line foo in fact.
We change line bar to line Bar, means don’t cancel the deleting of bar,
but cancel deleting of line Bar. And means delete line bar and add line Bar in fact.
At last, insert a line ET.
If you still hard to get it, just assume
+ lines as
- lines and
- lines as
checkout, since the direction is same with
reset, so reverse
What in the paper is abstract. For fully understand, you need try it by your self.
There is a feature lack that, the
add -p doesn’t with new files. However,
reset -p dose.
For the new files, you can use
add filename then
reset -p filename to add it with patching.
File level and global operations
Usually, always check over what you change with
-p is fine.
But some times you need file level or global operations.
git add fileadd all updates in the file from work tree to index
git reset fileremove all updates in the file from index
git checkout filegive up all updates in the file that not be added to index (cover the file in work tree by index)
git add .add all updates from work tree to index
git resetremove all updates from index
git checkoutgive up all updates that not be added to index (cover all updates in work tree by index)
Imperfect ultimate weapon
There is another more fashion option
--interactive on command
-i in short.
By the name, this is a interactive interface too. It seems like this:
status above which like
git status but more clear, and commands below.
There are 8 commands shown, and a
You can just use the index or the first letter instead of full command.
There are 5 core commands:
d. Each of them leads to a second level interface，
the command prompt change from
In this level, we can choose files with index or unique prefix of filename.
Currently, it’s not support segmental matching, fuzzy matching or
Nor tab hints and input history. Wish they will be added in the future.
For now, the index is enough. We can add a
- before the index or file path, to remove adding,
Type Enter to confirm the selection and type Enter again to take the action.
add --i bring us a great interface, we can play fluently, without repeating
On the mode, this is the ultimate weapon. But it’s an imperfect weapon.
reset file and
add -p file within the
reset -p file,
checkout -p file are missing.
So, for right now the best way is still the 3Ps Sorcery.