Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to use git add?

Tags:

git

As a git noob trying it out on a Rails project, I am wondering if it is bad practice to do git add . (add current directory) before every commit. The intro tutorials I have seen show adding the current directory initially, then using git add new_file to add files after that. If I'm adding a bunch of files from a bunch of different directories, this seems too hard.

Essentially, if you are adding more than one or two files, is it OK to use git add . every time I want to commit? Is using git add . the same as explicitly doing git add new_file for every file that has been created since the last commit?

like image 348
Nick Stamas Avatar asked Sep 30 '09 15:09

Nick Stamas


People also ask

Should you use git add?

The git add command adds new or changed files in your working directory to the Git staging area. git add is an important command - without it, no git commit would ever do anything. Sometimes, git add can have a reputation for being an unnecessary step in development.

When should I do git add?

git add lets you stage your commit in pieces. That's not always necessary if you're committing in properly sized chunks but some times it's inevitable. It also makes it possible to preview a commit. When you use git add the files are checked in to your local index, which is separate from your working directory.

Why git add is taking forever?

This is because the node_modules folder is not ignored and hence when we run git add . command git visit all your directory/files and sub directory/files which is not ignored in . gitignore hence it is taking so long time to process it as node_modules contains a large amount of files and folder.


1 Answers

git add . will add everything that's in the heirarchy, including new files. If you want to track all files that are in the directory, this is fine.

Another (perhaps more common) usage is to do git commit -a, which adds only the files that changed since the last commit before committing, and doesn't include any new files.

EDIT: git add . won't remove any files that were deleted since the last commit. If you're deleting files, I'd use git rm <myfile> so that git is informed of the removed file, and you don't forget to make sure git knows it was deleted. As mentioned in another comment, git commit -a will notice files that have been deleted.

like image 124
James Cassell Avatar answered Sep 28 '22 03:09

James Cassell