Carl Raymond
Carl Raymond

Reputation: 4479

Staging a file more than once before commit

If I stage a changed file, then change it some more and re-stage it, and then commit, am I committing two changes, or just the net change? I'm assuming the answer is two, but I can't find this in the Git book.

This comes up in the context of not wanting to commit unencrypted passwords in my web.config file. I'm building a pre-commit hook that checks whether the relevant section is encrypted, and if not, aborts the commit. But I've realized that if I just then encrypt the file, and commit (without staging it again), I would still be committing the unencrypted file. But what if I re-stage it again and then commit? Would I still be committing the unencrypted data?

Upvotes: 0

Views: 206

Answers (4)

Da Ha Song
Da Ha Song

Reputation: 428

When you run commit, git takes a snapshot of your staged area of moment, and stores them in the repository. So, it is always the latest staged version that goes into commit.

Upvotes: 0

Ajedi32
Ajedi32

Reputation: 48368

Assuming you didn't move or rename the file before staging it again, you're only committing the "net change". You can see exactly what's in the staging area at any given time by running git diff --cached.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

Just the net change; the first one is replaced by the second version. Eventually, you'll garbage collect the now irrelevant staged copy.

Upvotes: 1

Simon Boudrias
Simon Boudrias

Reputation: 44619

You only have one staged area buffers. There's no notion of changes hierarchy or time in the staged area.

So if you change a file, stage it, overwrite previous changes, restaged it. Only the final diff with the previous commit is commited.

In short, only the changes you see in git diff are commited.

Upvotes: 1

Related Questions