Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

commit

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?

like image 473
user3254515 Avatar asked Sep 21 '16 08:09

user3254515


People also ask

What does commit in git mean?

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.

What does it mean to commit to a 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.

What git command do you use to show the current state of the repository?

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.

How do you check the state of your local git repository?

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.


1 Answers

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.

like image 190
Thomas Avatar answered Oct 24 '22 03:10

Thomas