Do I have to "git add" the file every time it is committed?

I have used Mercurial before, but I plan to upgrade to Git in the near future.

All the tutorials I saw that explain how Git works show that files are added to the scene ('git add') before each commit, regardless of whether they were previously tracked.

Mercurial also has a command that works the same way ('hg add'), but from what I remember, you only need to add โ€œaddโ€ once. For example, the steps for the new repository look something like this:

hg init hg add . hg commit "Initial commit" hg push 

This workflow is possible with Git, and if it is not, then what is the reason for re-git add? It just seems unnecessary.

+6
source share
4 answers

Yes, the same general workflow can be applied, but the add command can also be used more selectively, adding only certain files or only certain parts of files for each commit. This makes it easy to split logically different patches into different commits, which makes tracking and sharing easier with others.

But yes, you can just "git add". and everything will go to the intermediate area for fixing. As mentioned in another answer, "git commit -a" is a partial shortcut (it will not add new files, but will commit all changes / deletions of already tracked files). Try "git status" to see what will be included in your next commit and what will not.

UPDATE It should also be noted that the addition to git add is "git reset HEAD", which removes the file from the staging area for the next commit. In addition, if you want to split the editing in one file into several commits, use the โ€œ-pโ€ flag for both git add and git reset to execute the interactive file and choose which fragments to add / reset.

+3
source

The two-step process in git is very powerful. In particular, when editing source code, you often make several parallel changes that are not directly related.

In the intermediate area, you can select the files that you want to commit, and thereby create one commit for each logical change.

If you want to copy all the change files at once, you can use git commit -a as a shortcut.
At this point, all modified and deleted files are written. Please note: it does not automatically add files without a trace. You still need to do this in advance.

+7
source

Since you do not always want to commit all modified files, you can (and you MUST) choose which ones to commit. Depending on the architecture of the project you are working with, sometimes you make additional changes for testing or something else and not necessarily for production.

+1
source

Another shortcut that should be mentioned here is that the git add . hammer is not exactly heavy git add . equal to git add -u . The difference is that the former will process any newly added files that are not explicitly ignored (i.e., all the โ€œraw filesโ€ listed in the git status output), while the latter will only process modified files that have already been tracked.

+1
source

All Articles