git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Question about "git commit -a"

From
Wincent Colaiuta <win@wincent.com>
Date
Oct 5, 2007, 10:48 UTC
Message-ID
<227149BF-1220-43F2-8B0B-B7E9FCA002B1@wincent.com>
In-Reply-To
<4d8e3fd30710050139j45a5a924t5c048994e3457c5f@mail.gmail.com>
El 5/10/2007, a las 10:39, Paolo Ciarrocchi escribió:
Show 13 quoted lines
> So you are used to do something like (please correct me if I'm wrong):
> - modify A
> - modify B
> - modify C
> - modify D
> - modify E
>
> $ git A B E
> $ git add A B E (A, B and E are now in the staging area)
> $ git commit -m "I just modified A,B and E"
> $ git C D
> $ git add C D (C and D are now in the staging area)
> $ git commit -m "I just modified C and D"

The conceptual shift is that in Git your index and not your working directory is your staging area, unlike (most/all?) other SCMs. If you fire up gitk and look at the development history of Git itself you'll see that it's one of the "cleanest" out there, and as you learn Git you learn about the various tools and tricks that it provides that makes it easier for a developer community to produce such a clean history; the index as a staging area is one of the key factors.

The basic workflow is:
   # work on a single change
   edit A
   git add A
   edit B
   git add B
   # see unrelated thing that needs to be fixed, but don't add  
(stage) it yet
   edit C
   # commit first change, then second one
   git commit -s
   git commit -s C

This is just one example of how having a staging area that you can control independently of your working tree can help you. There are other possible workflows and you discover them through use, but they all share the basic idea that you use the staging area to provide you with better control.

Sometimes you're trying to work on a single thing and you see a change within a single file that isn't related. In that case you have an even finer level of granularity available and can use "git add -- interactive" to add only specific hunks.

Finally, closely related to this idea of maintaining a clean history is the newly-added and wonderful "git stash". If you have a relatively complicated work in progress already half-staged in the index and you see something else relatively complicated that you want to attend to straight away then you can easily switch to the second task, commit it, and go back to the first task, thus keeping your development history nice and clean. This is a beautiful example of your SCM facilitating your work and making it easy rather than forcing you to jump through hoops. See the git-stash man page for more details.

I think all of this is incredibly powerful useful stuff, and all of it comes at a very low cost; it's easy to learn and doesn't require you to do any magical and complex history rewriting in order to get a nice clean history.

And on the subject of staging areas, thanks to "git commit --amend" you can even use the last commit as a kind of secondary, addditional staging area, providing you haven't published that commit yet. In other words, I frequently do:

   git show HEAD

Immediately after committing and if I don't like what I see I make modifications as necessary and do:

   git commit --amend

Cheers, Wincent

Previous: Johannes SchindelinNext: Andy Parkins
Message 30 of 35 in “Question about "git commit -a"”
  1. Paolo CiarrocchiOct 4, 2007
  2. Matthieu MoyOct 4, 2007
  3. Paolo CiarrocchiOct 4, 2007
  4. Wincent ColaiutaOct 4, 2007
  5. Nguyen Thai Ngoc DuyOct 4, 2007
  6. Johannes SchindelinOct 4, 2007
  7. Nguyen Thai Ngoc DuyOct 4, 2007
  8. Shawn O. PearceOct 4, 2007
  9. Paolo CiarrocchiOct 5, 2007
  10. Andreas EricssonOct 5, 2007
  11. Paolo CiarrocchiOct 5, 2007
  12. Andreas EricssonOct 5, 2007
  13. Matthieu MoyOct 5, 2007
  14. Andreas EricssonOct 5, 2007
  15. Matthieu MoyOct 5, 2007
  16. Andreas EricssonOct 5, 2007
  17. Paolo CiarrocchiOct 5, 2007
  18. Andreas EricssonOct 5, 2007
  19. Matthieu MoyOct 5, 2007
  20. Kristian HøgsbergOct 5, 2007
  21. Matthieu MoyOct 5, 2007
  22. Marko MacekOct 5, 2007
  23. Andy ParkinsOct 6, 2007
  24. Linus TorvaldsOct 6, 2007
  25. Wincent ColaiutaOct 7, 2007
  26. Dmitry PotapovOct 5, 2007
  27. Marko MacekOct 7, 2007
  28. Dmitry PotapovOct 7, 2007
  29. Johannes SchindelinOct 7, 2007
  30. Wincent ColaiutaOct 5, 2007
  31. Andy ParkinsOct 4, 2007
  32. Miles BaderOct 5, 2007
  33. David SoriaOct 4, 2007
  34. Alex RiesenOct 4, 2007
  35. Johannes SchindelinOct 4, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.