0

I'm new with and I ask me why I have to use 'git add' and 'git commit' to commit a file into my repository.


With all IDEs I tried it is just one click to commit a file. This means the IDE combines 'add' and 'commit', right?!

And so I can't find any reason why it made sense to program both commands.

Samuel Hulla
  • 6,617
  • 7
  • 36
  • 70
anben
  • 11
  • 3

3 Answers3

0

There are subtle differences between and other cloud-based / repository systems. One of which is the fact has a so-called "staging area".

git add

  • In before a file is updated in your repository, at first it's added to a so called staging area. This is done exactly, as the name of the function would suggest, via the git add command.

  • During the staging stage, you tell your which files you want to update. This also means, you can remove certain updates in staging area, if you change your mind about them.


git commit

  • Once you produce a version, that you think is ready to be posted into a repository, you can commit the changes so you and others can see them.

  • Once again, command named derivated from the action described, so in this case it would be git commit which commits the changes you have proposed in your staging area (via git add).


So the difference is, if you use internet browser to upload files directly into a repository, you essentially skip the git add stage and you do git commit the files do the repository straight away.

Now, if you have a git project you manage only yourself, then yeah, git add is essentially useless for you. However, on a large project where you've got multiple people working, you want to make sure you don't over-write people's code, or just want to make sure you do properly version-manage, it is essential to properly utilize the staging area.

One of main advantages are, that during the commit phase, you don't usually commit a single file, however multiple changes, which makes it easier to keep track of who did what, rather than looking up every single file one by one.

For more info (not only on) git add and git commit I would thoroughly recommend studying this guide. At least personally speaking, I became fluent in from this resource alone

Samuel Hulla
  • 6,617
  • 7
  • 36
  • 70
0

The index in git is like a preflight area, a place where you can prepare the contents of the next commit.

You can basically ignore its existence if you use certain IDEs or if you commit with -a all the time. Some people do. In the simplest contexts, it's probably not a bad option.

However, as complexity grows, it'll become harder and harder not to interact more efficiently with it, which starts with a better understanding.

More details here :

What does the git index contain EXACTLY?

and maybe there?

Understanding git - Index

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
0

Changes can spread over multiple files, and you bundle all those changes in a single commit, with a clear title of what you've done.

For example in web development when you're done creating a new page you've probably changed multiple files. Your commit message could be: Add index page wich contains files:

  • index.html

  • script.js

  • ...

After pushing this to remote the next person that pulls will get a somewhat finished index page, instead of just a single file.

  • 3
    You commit neither files nor changes but snapshots of the entire repo tree. Not to nit-pick, I'm OK with the general sense of your answer, but the first sentence *could* be misleading to some people. – Romain Valeri Mar 13 '19 at 15:21
  • 1
    Totally agree, reading back over it sounds misleading indeed – Jelmer Beerling Mar 13 '19 at 15:26