0

I am new to Git, and would like to know where it would be useful to use a staging area, instead of directly committing the changed files.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
asv
  • 3,332
  • 7
  • 24
  • 47
  • 2
    For example if you want to break the changes into several commits so that they make sense. – choroba Jan 02 '18 at 16:14
  • Related: https://softwareengineering.stackexchange.com/q/69178/74113 – Chris Forrence Jan 02 '18 at 16:15
  • I'm going to close this as a duplicate as phd's comment (which will vanish) says, but it's worth noting that when you run `git commit `, what Git is doing is, in effect, `git add`-ing that file *to* the staging area, and then using the staging area to commit. The way Git is built internally, you *always* commit through a stage. Git simply chooses to expose it, so that you have to learn about it, instead of keeping it hidden and automatic as in (e.g.) Mercurial. – torek Jan 02 '18 at 16:45
  • when I'm using git in 80% cases I don't want to commit all changes in a single commit. – Marek R Jan 02 '18 at 17:19

1 Answers1

0

It's the only clean way to "freeze" your local copy state without doing a commit, and so to unstage something is the only way to undo a local edit without "rewriting the history".

I use it a lot, for instance when I'm in a state where things works but what I've done is still not ready for a commit that makes sense.

You can obviously obtain something similar in other ways, for instance doing partial commits in a local, feature branch, and doing a git merge --squash branchname at the end (and then removing the branch), when the feature/fix is ready.

Mauro Piccotti
  • 1,807
  • 2
  • 23
  • 34