Using Git Index
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.
Basic Commands
There are three git commands for managing Git Index.
They are add
, reset
and checkout
.
-
add
add updates from work tree to index -
reset
remove updates from index -
checkout
give up updates that not be added to index (cover updates in work tree by index) Bothreset
andcheckout
are multi-purpose. The commandreset
also can use to move current branch. And the commandcheckout
also 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.
Meticulous operations: --patch
Each of add
, reset
, checkout
can run with option --patch
, or -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 index.md
.
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:
add
reset
checkout
What’s a hugging heap of dazing stuffs! Don’t worry, just let’s reorder them:
We only need know y
, n
, s
, q
for basic using.
For advanced using, we need know one more: e
.
For programmers, that’s enough.
For dictionary keeper, or Webpack configuratician, navigations will be useful.
In this section, we talk about the e
.
To begin the editing, we need an editor.
Just like when you run commit
without -m
opiton,
git will open vim or nano when you using e
.
And the text in the editor will like this:
Here is an update, we delete the line bar, and insert lines baz and qux.
The line @@ ... @@
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, -
or +
.
The space means this line is a normal line,
a -
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 e
.
If you recover the update, remove all +
lines, change -
s to spaces,
it’s just equal to press a n
instead of e
.
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 whenadd
.
But 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 add
.
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 +
lines.
On checkout
, since the direction is same with reset
, so reverse +
and -
too.
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 file
add all updates in the file from work tree to index -
git reset file
remove all updates in the file from index -
git checkout file
give 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 reset
remove all updates from index -
git checkout
give up all updates that not be added to index (cover all updates in work tree by index)
Imperfect ultimate weapon add --interactive
There is another more fashion option --interactive
on command git add
, -i
in short.
By the name, this is a interactive interface too. It seems like this:
It’s shows status
above which like git status
but more clear, and commands below.
There are 8 commands shown, and a ?
hidden:
You can just use the index or the first letter instead of full command.
There are 5 core commands: u
r
a
p
d
. Each of them leads to a second level interface,
the command prompt change from >
to >>
.
In this level, we can choose files with index or unique prefix of filename.
Currently, it’s not support segmental matching, fuzzy matching or *
, ?
yet.
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.
The add --i
bring us a great interface, we can play fluently, without repeating git status
.
On the mode, this is the ultimate weapon. But it’s an imperfect weapon.
There are add file
, reset file
and add -p file
within the add -i
.
But reset -p file
, checkout file
, checkout -p file
are missing.
So, for right now the best way is still the 3Ps Sorcery.