Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to 'git add' a file each time it gets committed?

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.

like image 230
Chris Avatar asked Feb 18 '13 16:02

Chris


People also ask

Do files get added before or after each commit in Git?

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.

When should I use Git add?

When do you use git add? As you're working, you change and save a file, or multiple files. Then, before you commit, you must git add. This step allows you to choose what you are going to commit. Commits should be logical, atomic units of change - but not everyone works that way.

How do I add a new commit in Git-SCM?

You can see all of the many options with git add in git-scm's documentation. git add usually fits into the workflow in the following steps: Add the files or segments of code that should be included in the next commit: git add README.md Push the changes to the remote branch: git push -u origin update-readme

How to commit files to the staging area in Git?

The files can be committed only if they are added to the staging area. This can be achieved using the git add command. Step 5 − Execute the git add command to add the files to the staging area. Execute the git status command to verify that the files are added to the staging area. Please refer to the following for output.


4 Answers

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.

like image 140
Daniel Hilgarth Avatar answered Oct 16 '22 09:10

Daniel Hilgarth


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.

like image 20
dpkp Avatar answered Oct 16 '22 09:10

dpkp


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.

like image 26
martriay Avatar answered Oct 16 '22 09:10

martriay


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.

like image 39
Phil Miller Avatar answered Oct 16 '22 09:10

Phil Miller