Reputation: 595
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.
Upvotes: 9
Views: 7477
Reputation: 38138
Another shortcut that should be mentioned here, that's not quite the heavy hammer of git add .
is git add -u
. The difference is that the former will stage any newly added files that aren't explicitly ignored (i.e. all 'untracked files' listed in the output of git status
), while the latter will only stage modified files that were already tracked.
Upvotes: 1
Reputation: 1459
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.
Upvotes: 6
Reputation: 174369
The two stage process in git is very powerful. Especially, when editing source code, you often make several changes in parallel that are not directly related.
With the staging area, you can select which files you want to commit and thus create one commit per logical change.
If you want to commit all changes files at once you can use git commit -a
as a shortcut.
This stages all changed and deleted files and commits them.
Please note: It doesn't automatically add untracked files. You still have to do that beforehand.
Upvotes: 9
Reputation: 5742
Because not always you want to commit all the changed files, you're able (and you SHOULD) to select which ones to commit. Depending on the project arch you are working with, sometimes you perform aditional changes for testing or something and not neccesarily for production.
Upvotes: 1