fernando1979
fernando1979

Reputation: 1947

Does a commit in Git represent the state of a repository?

As I had understood it, when you do a commit in Git, a snapshot of the entire state of the repository is made and that allows me to go back to that state when its necessary. So, to me a commit represents not a change, but a state of a repository. When you want to go back to an old state, you can do git checkout commit-hash

However, I dont understand what happens when you do a cherry-pick, because it gets only the change made by that commit.

How can Git get the difference between two commits during a cherry-pick if a commit represents a state of a whole repository?

Upvotes: 11

Views: 991

Answers (1)

Thomas
Thomas

Reputation: 181755

Your understanding is correct: a "commit" in git is not a change (delta), but represents the entire state. But a commit contains more than just state: it also has a pointer to a parent commit (typically one, but can be any number), i.e. the previous commit in the repository's history.

The parent pointer lets git figure out the differences between the current commit and its parent. This is what cherry-pick does: it computes the diff, then applies just those differences to the current state.

Upvotes: 13

Related Questions