3

I am learning Git commands.

My understanding of git add and git commit is as follows: git add 'file name' takes snapshot of the project(entire working directory) and stage it. git commit saves the staged snapshot to the repository.

I also understand that git differs from SVN etc in the fundamental way of saving the files. While SVN saves incremental changes in individual files, git saves snapshot of the entire working directory. If a file has not changed, git snapshot would contain a reference that file in the previous snapshot.

I have a question here. If git add takes snapshot of entire working directory why do we need to provide individual file name in the command? Git would anyway take snapshot of entire project.

3
  • 2
    git add file [file ...] adds only the specified files, not the entire working directory. But it does allow you to specify a directory, which means "all files in the directory"; and if you are at the top of your work tree and specify ., that amounts to "all files in the work tree". There are some exceptions here due to .gitignore files and the like, but that covers most of it. Commented Jun 10, 2016 at 2:38
  • You can use git add -u to add all previous-tracked files. Commented Jun 10, 2016 at 2:44
  • I wonder why nobody mentioned git add -A. Commented Jun 10, 2016 at 2:51

3 Answers 3

3

Because you may not want to add every single file that's been added or changed.

For example, let's say you've modified your xyzzy.c file but you also downloaded a very nice picture, taylor_swift.jpg, to the current directory (and haven't yet had a chance to move it elsewhere).

You don't want the latter file to find its way into the repo, so you simply do:

git add xyzzy.c 

abd that adds only the file you want added.

In any case, if you want every file in the current directory, you can simply do:

git add . 

I tend to think of it as the three areas.

First, your working area where you have a specific "snapshot" checked out, with whatever changes you've made.

Then the staging area that holds files for the next commit.

And, finally, the git-proper area that holds all committed changes.

If you think in those terms, add then copies individual things from working to staging, and commit copies everything from staging to git-proper.

Sign up to request clarification or add additional context in comments.

5 Comments

So it means when git takes snapshot of the project, snapshot would contain modified files mentioned in git add command, while for other files it would just have a link to previous identical file stored(even if file has changed).
@Mandroid, I tend to think of it as the three areas. Your working area where you have a specific "snapshot" checked out, with whatever changes you've made, the staging area that holds files for the next commit, and the git-proper area that holds all committed changes. add then copies individual things from working to staging, commit copies everything from staging to git-proper.
that explanation helps a lot in visualising the things.
Interesting example.
@Mandroid: copied it into the answer, as you seem to think it's helpful.
1

Your comprehension about snapshot in git is wrong. As shown in document of git website, [Snapshots, Not Differences], the snapshot of your project is an aggregation of snapshot for each files you added in your repository. This is the reason you need specify filename in git add command.

After you tell git which file should be managed by git add <filename>, you can use git add -u to stage your modification, in this case, filename is not necessary.

1 Comment

Thanks for the link. My understanding of snapshot in git was wrong.
0

Checkout the git documentation about git add

simply you can use .

You can use git add . this will take entire directory / new files / modified files - ready to commit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.