DEV Community

Git Staging Area: Explained Like I'm Five

Jonathan Irvin on August 29, 2018

Imagine a box. You can put stuff into the box. You can take stuff out of the box. This box is the staging area of Git. You can craft commits here. ...
Collapse
 
kostassar profile image
Kostas Sar

Clear explanation! Any five-year old could now get how git works! Also had a laugh in the process

I am just a bit confused about "git revert" and the anti-box. Could you please explain more? Does it mean we have the ability to revert the last commit? After typing "git revert" isn't it reverted automatically?

P.S.: Would love to see a second post with the box analogy explaining more about git!

Collapse
 
offendingcommit profile image
Jonathan Irvin

A git revert will create another commit that is an exact mirror opposite of the commit you are reverting.

In my example, a sealed box is a commit. If you have a commit where you add 2 lines and remove one. The reverted commit will remove 2 lines and add one, effectively nullifying your original change.

This is why I was making the juxtaposition to anti-matter where a commit and a reverted commit combined would be the same as no commit.

Does that make sense?

Collapse
 
kostassar profile image
Kostas Sar

Yes, very clear!

But do we have to commit this new anti-box? Or is it pushed automatically?

Thread Thread
 
offendingcommit profile image
Jonathan Irvin

Whenever you perform a git revert the commit is created automagically for you. You can see it if you perform a git log or use a Git GUI like Sourcetree.

IF you want to make your revert public, you would do a git push and your local repo would be synced with the remote repo, assuming no conflicts with the public repo are present.

Thread Thread
 
kostassar profile image
Kostas Sar

Thank you!

Collapse
 
snorkpete profile image
Kion Stephen

Awesome explanation! I'm already quite comfortable with git after a lot of reading and research, but this is the explanation I would've wanted back when I started.

I'd even suggest a follow up article about why the staging area is such a cool idea, using the box analogy. (Other version control systems without a staging area can only create labeled boxes by dumping the entire contents of your desk into the box all at once... git allows you control over what goes into each labeled box...etc)

Collapse
 
offendingcommit profile image
Jonathan Irvin

Great idea!😁

Collapse
 
crs1138 profile image
Honza

How do I open one of the many sealed boxes find some old code and bring it back to life?

Let's say, I deleted a bunch of files in favour of one that does what all these other files were doing. That was 3 or commits ago. Now, I just remembered that one of the files would be useful to me.

Collapse
 
offendingcommit profile image
Jonathan Irvin

Git checkout

Collapse
 
crs1138 profile image
Honza

Ok, I get that this opens the box. Now my HEAD is in the box, but how do I recover the one file?

Thread Thread
 
offendingcommit profile image
Jonathan Irvin

Well, depends on what you mean by recover. If you checked out the commit, you'll see the version of the file you want. If there's a lot of changes you can make a copy and then paste it over the latest commit. If there's a few, you can revert lines or hunks selectively.

Collapse
 
bbasile profile image
Basile B.

Two remarks

  • the opportunity to only add lines or blocks to a commit is important too. Not just the whole file.
  • the staging area is also an opportunity to review your changes.
Collapse
 
adrianhelvik profile image
Adrian
git reset --merge HEAD

Same as --hard except you preserve ignored files.

(Pretty much always better)

Collapse
 
joaonm profile image
JoaoNM

So helpful! Really good for beginners :)

Collapse
 
imben1109 profile image
Ben

How could you resolve conflict?

Collapse
 
offendingcommit profile image
Jonathan Irvin

Stare at the box. You feel compelled to argue with it. It wins. Every time.

Collapse
 
gadrawingz profile image
Gad Iradufasha

Informative peace of information!