简体   繁体   中英

Difference between git add and git commit -a

I am trying to better understand how git works.

So we have a working directory (our project files), a staging area (a collection of changes ready to go), and a git repository (where those changes are applied to when committed).

However I am struggling to understand because it seems like files being "tracked" or not refer to some specific thing, and yet we still say each section does its own form of tracking?

Is it correct to say:

  1. If I add or modify a file in my working directory, git detects all of this. Do we say that these files become "tracked" by git at this point? Or are they merely "detected" (or something else)? Is everything in the project folder / working directory "tracked"?

  2. git add {directory or filename} appears to add the contents to the staging area, yes? At this point do we say all staged files are "tracked" files, the ones that show up when we do git ls-files , which I believe shows the files in the staging area? Or no?

  3. git commit -a moves all the changes in the staging area to the repository.

Is this correct?

git add adds files to be tracked as well as staging (the current commit)

git commit -a adds files to staging only if they are already tracked

Git notices files that are not on tracked when they are added to the repo directory because it understands them as a file change. But unless you explicitly add them to the tracking it will not track any change increments

This question may help you: Concept of git tracking and git staging

Also see this https://www.howtogit.net/concepts/types-of-changes.html

I think the main point of our confusion is that git add can do two things (add to tracking if not already, and stage) whereas git commit -a only does one (add tracked files to the stage)

  1. If I add or modify a file in my working directory, git detects all of this. Do we say that these files become "tracked" by git at this point? Or are they merely "detected" (or something else)? Is everything in the project folder / working directory "tracked"?

Git detects modified and deleted files. If they are tracked (ie they were added before and committed) they appear in the output of git status as "modified". The new files and the modified files that are not tracked appear as "untracked".

  1. git add {directory or filename} appears to add the contents to the staging area, yes? At this point do we say all staged files are "tracked" files, the ones that show up when we do git ls-files , which I believe shows the files in the staging area? Or no?

The staging area is used to prepare the next commit. You can say the files it contains are "tracked" but its content is not permanent; it can be changed any time. In order to turn a new file really "tracked" by Git you have to commit the staged changes.

  1. git commit -a moves all the changes in the staging area to the repository.

git commit creates a new commit using the staged content. git commit -a automatically stage tracked files that were changed or deleted and then operates the commit. The new files are not added.

Read more about git commit and about how Git records the changes to the repository .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM