I’ve used Mercurial before but plan on switching to Git in the near future.
All of the tutorials I’ve seen that explain how Git works show that files are added to the stage (‘git add’) before each commit, regardless of whether they have been tracked before.
Mercurial also has a command that works in a similar way (‘hg add’), but from what I remember, you only need to do ‘add’ once. For example, the steps for a new repository look something like this:
hg init
hg add .
hg commit "Initial commit"
hg push
Is this workflow possible with Git and if it isn’t, what’s the reason for the repeated ‘git add’? Just seems unnecessary.
Yes, the same general workflow can apply, but the “add” command can also be used more selectively, by adding only certain files or only certain portions of files to each commit. This makes it easier to separate logically different patches into different commits, which makes it easier to track and share with others.
But yes, you can just “git add .” and everything will go into the staging area for commit. As mentioned in another answer, “git commit -a” is a partial shortcut (it won’t add new files, but will commit all changes/deletes to already tracked files). Try “git status” to see what will be included in your next commit and what won’t.
UPDATE I should also note that the complement to git add is “git reset HEAD “, which removes the file from the staging area for your next commit. Also, if you are interested in separating edits within a single file into multiple commits, use the “-p” flag to both git add and git reset in order to step through the file interactively and choose which chunks to add/reset.