Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use git add?

Tags:

git

I'm finding myself using it each time before a commit. Is it right? If so, it should be the same command...

like image 360
Juanjo Conti Avatar asked Sep 09 '10 03:09

Juanjo Conti


People also ask

When should I run 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.

What is the point of git add?

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .

Do you always have to git add?

You have to git add even with plain git to stage files for commit. It's just that if you explicitly list what you want to commit with git commit , it does staging for you. But if you don't specify list of files to commit, then git commit only commits files you staged with git add .

Do you need to git add before git push?

Yes, you do need add and commit your changes before pushing your code to your github repository.


2 Answers

There's git commit -a which is like git add -u -- it adds all modified files, but it does not add untracked files, in contrast to git add ., which adds both tracked and untracked files (not including deletions), provided that they contain modifications.

See Difference between "git add -A" and "git add ."

But yes, in most workflows, you will either git add before you git commit, or you will mostly use git commit -a.

like image 192
Mark Rushakoff Avatar answered Oct 21 '22 21:10

Mark Rushakoff


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. When you then use gitk --all, for example, your index appears as any other commit node and you can see the effects of all your changes as you would a normal commit, before actually committing it to the branch.

What I find extremely useful is git add -i, which goes into interactive mode. You can also use git commit --interactive. Here you can choose which files to add (and even which pieces of your changes to the file to add) one by one, checking the differences for each.

There's no reason to use the commands independently if you don't want to, but there is definitely merit in keeping the commands independent of each other since they do different things and some people like being able to modify their working index without committing.

like image 30
Mark Peters Avatar answered Oct 21 '22 23:10

Mark Peters