14

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?

  • Related: https://stackoverflow.com/questions/49474035/is-there-multiple-files-stored-in-the-stage-after-git-add/49474111 – jsageryd Apr 06 '18 at 13:44

1 Answers1

14
$ 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.

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
  • 1
    Thanks Greg; actually we are 3 of us; and often happens that someone commit and I need to sync up the repo. So let's say that I write a function and do an add; then I add another function and add again; when I do git commit, will I see both function committed? –  Jul 22 '15 at 19:36
  • 6
    Yes that's correct, specifically because you're doing that second add. When you run add, the contents of the files at that moment are what will be committed. If this answers your question, please accept my answer above. Glad I could help. Also look into branches, it might alleviate some headache for you and your group. – Greg Hilston Jul 22 '15 at 19:54