Jordi
Jordi

Reputation: 23277

Git: staging area workflow

What's really git staging area for?

I'm used to work only with unstaged files and when I think everything is fine I stage them in order to build the commit.

Nevertheless, I guess that it's there for a some good reason, but I'm not able to get which workflow it could help me with.

Any ideas?

Upvotes: 2

Views: 92

Answers (3)

Jan Hudec
Jan Hudec

Reputation: 76366

It is that you can select what you want to include in the commit not just at file, but down to character granularity.

Say you are hunting down a bug and have a lot of temporary debugging statements added all over the place. And now you've fixed some problem and want to commit the fix, but suspect there might be more, so you still need the debug statements around. So you fire up the git gui and use the Stage hunk/Stage line entries in context menu on the diff to stage just the fix and then commit that. You can get the same effect from git add -i or you can edit the diff to stage with git add -e or you can tweak the stage with something like vim-fugitive.

Also sometimes you would use the stage to separate the pending changes. Say you are working on something, have done some parts, but now you need to experiment a bit. So you stage the parts you are confident with, or that you think you may need to return to, and continue with experimentation and then you can easily undo the experiment if it turns out not going where you wanted.

Upvotes: 2

zoul
zoul

Reputation: 104095

It’s hard for programmers to change just the things that should be a part of the next commit. That’s where the staging area helps a great deal – you pick just the right bits, ignoring various logging/debugging statements or unrelated changes, and only then you commit. That way you can create several clean commits out of a batch of changes.

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522541

One useful purpose of the Git stage is that it serves as a place which can hold work which you intend to commit, leaving the working directory available to still be used. This is best explained via an example. Suppose you finished working on a feature, and you completed all testing and your own code review. This work is now ready to be committed, so you git add all the files you changed. This fills the stage with these files, and at this point both the working directory and stage are in sync.

But now suppose you realize at the last minute that you need to make a small change. Or, you get a last minute inspiration to add something else to the feature. The feature itself is fundamentally complete, so you don't want to change the good work you have already done. Since your work is in the staging area, you can modify the working directory as you see fit. If you wish to keep any new work, you can again git add it. Otherwise, you can keep the stage as is, make the commit, and push your work out.

By the way, if midway you want to reset a working directory file to the version in the stage, you can do so via git checkout FILE.

Note that many Git plugins, such as eGit for Eclipse, seem to sort of skip over the stage, often seemingly going straight from working directory file to commit.

Upvotes: 2

Related Questions