user393267
user393267

Reputation:

When you do git add on the same file multiple times, is the save done incrementally?

I am not clear reading the documentation, about how multiple add of a file would work in Git.

I would like to make changes in various stage; but I don't want to commit every time; so I thought that I can make changes, use git add, and then make more changes, until I am ready to do a git commit.

Reading the docs it specify that this is possible, but it seems that the second version always override the first; so you last "git add" will always be the version of the file that you will commit.

Is this correct or there are some inner workings that modify this behavior? and what if you want to go back one "add"; is that possible?

Upvotes: 14

Views: 8347

Answers (1)

Greg Hilston
Greg Hilston

Reputation: 2424

$ git add

Does not work in the incremental way you think or would like it to. Commits should be small enough to explain what work was done but big enough that you are not committing constantly.

When you run git add, the files added are then staged for committing, so any other changes made must again be added.

Straight from the description found here:

The "index" holds a snapshot of the content of the working tree, and it is this snapshot that is taken as the contents of the next commit. Thus after making any changes to the working directory, and before running the commit command, you must use the add command to add any new or modified files to the index.

My suggestion to you is to just commit often. There is no harm in it, as Git exists to make your life easier. If you're working on this project alone, there is no downside to committing frequently.

Upvotes: 14

Related Questions