0

Very simple case where there is only main, no branches ever (it's my personal website). I edited a couple of files and then tried calling "git commit -m "my changes"" - and it would not commit.

I finally tried first calling "git add filename" and it "added" them. The called "git commit" and it committed them. Then push and all is good.

So clearly I don't understand what add means. What does it mean? Why do I need to again add a file already in my Git repository?

Update:

So I think, based on the comments and answers below (please correct if wrong) that:

  • add adds the present content of files to the list of changes that are to be committed. As it's the present content added, subsequent changes to the file are not added to the list.
  • commit commits or saves the changed added files to my local repository
  • push pushes the local repository changes to the git server
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • Does this answer your question? [What's the difference between HEAD, working tree and index, in Git?](https://stackoverflow.com/questions/3689838/whats-the-difference-between-head-working-tree-and-index-in-git) – Brian61354270 Feb 10 '23 at 16:48
  • 1
    Also related: [What does the git index contain EXACTLY?](https://stackoverflow.com/q/4084921/11082165) and [What does 'adding to the index' really mean in Git?](https://stackoverflow.com/q/25351450/11082165) and [Git: What is the index](https://stackoverflow.com/q/13167099/11082165) and [What exactly is git index in technical point of view](https://stackoverflow.com/q/46138836/11082165) and [What is the difference between local repository and index?](https://stackoverflow.com/q/18048152/11082165) – Brian61354270 Feb 10 '23 at 16:50

1 Answers1

0

git add doesn't refer to file - it refers to the set of changes made on them. In fact, git doesn't really manage files, it manages changes. Once you made a change (and for this purpose, it doesn't matter if it's on a file you've previously committed on or a new file), you need to add (=stage) this change to be committed.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I just added what I think you told me to my question - is what I added correct? TIA. – David Thielen Feb 10 '23 at 17:08
  • @DavidThielen I agree with your understanding of `commit` and `push`. WRT `add`, I'll add the distinction that you aren't adding files, but the changes to those files. E.g., you could edit a file, `git add` it, and then edit it again. If you then commit it, only the changes made before running `git add` will be committed. – Mureinik Feb 10 '23 at 17:12
  • Got you - I revised what I wrote. And your comment that Git manages changes, not files is very helpful. Is it accurate to also look at it that Git manages file content at specific moments in time. – David Thielen Feb 10 '23 at 17:15