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?
What is a Git commit? In Git, a commit is a snapshot of your repo at a specific point in time. To help further understand what a Git commit is, we need to review your Working Directory vs your Staging Directory and how files changes are reflected in your Git repository.
The "commit" command is used to save your changes to the local repository. Note that you have to explicitly tell Git which changes you want to include in a commit before running the "git commit" command. This means that a file won't be automatically included in the next commit just because it was changed.
The git status command tells the current state of the repository. The command provides the current working branch. If the files are in the staging area, but not committed, it will be shown by the git status.
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.
Your understanding is correct: a "commit" is 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With