2

I have been using github for couple of months.

I do understand the concept of add and commit. But I never differentiate add and commit and I ended up always using

git add . && git commit -m "comments".

I can see that some of the people has the similar workflow as I (e.g. here: Git add and commit in one command)

The way I see it, if I finish the first "look" of my code, I added and commit it to git, then I start to code again, the second "look" then will have a comment like "extract method, simplify call". But I never do add without commit.

Could you please give me an example from real life experience, where add and commit does not always be done together?

Thanks and best regards, Steve

Steve S.
  • 71
  • 6
  • "Best practice" is an opinion. There's no reason to commit every change (and it can be needless commit history clutter unless you rebase, which brings its own set of potential issues). Committing says "here is a complete unit of work that I've documented with this comment". – Dave Newton Sep 02 '19 at 12:42

2 Answers2

2

The way that Git recommends that committing be done is such that each change is an atomic, logical commit. In other words, it represents one logical change (executed well) and the entire testsuite passes both before and after the change.

One situation in which adding and committing may be done separately is when you're working on perfecting a commit. You may have an initial version of your code which works, but is messy or still has debugging code in it. You can add this change to the index using git add while continuing to refine it in your work tree. If you find that you've broken things, you can roll back to the version in the index and try again. On the other hand, if you find that a change is better, you can add it again, until you're finally ready to commit.

Another, equally valid workflow for this situation is to make multiple commits, one each time the change is an improvement over the last, and then squash them together using git rebase -i or git reset --soft. Which you want to do depends on your preferred development style; they will essentially both produce the same result.

An alternative situation you may want to use git add separately from git commit is when you want to stash only some changes. You can use git add to keep the changes you want in your working tree and then git stash --keep-index to stash the rest of them away.

A question which you didn't ask but which might be on your mind is why the two functions are separate. This is because sometimes when developing you end up with changes from multiple logical commits in your tree and you want to commit only part of them at once, making additional commits for additional logical changes. Being able to add only part of the changes at once means you can commit those, and then add more changes and commit them, and so on.

bk2204
  • 64,793
  • 6
  • 84
  • 100
0

Could you please give me an example from real life experience, where add and commit does not always be done together?

When you add patches.

git add -p will interactively let you choose hunks of patch between the index and the work tree and add them to the index.

This is often done incrementally, file by file.
Then, after review, you commit.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250