questions about git commits

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

how does git choose a commit's ID (like 60e2f3...)?

it's a hash!

a commit's ID is the SHA-1 hash of the contents of the commit (the tree + commit message + date + parent commit(s) + a few more things)

do you always need to refer to a commit by its full SHA (40 hex characters)?

nope!

You can refer to it by any prefix, like 60e2f3cdf7b938712a4c3d or 60e2f3c or even 60e2, as long as the prefix is at least 4 characters and there's only 1 commit with that prefix.

is it possible to make a commit have a different commit message?

nope!

commits can't be edited in any way. git commit --amend lets you "amend" a commit, but actually it makes a new commit and your old commit will still be there if you want to go back to it.

is it possible to change a commit so that it has a different parent?

nope!

commits can't be edited in any way, and a commit's parent is part of the commit.

$ git cherry-pick 60e2f3 lets you "move" a commit onto a different branch, but it actually creates a new commit with the new parent.

when you run
$ git checkout $SOME_COMMIT_ID
with the same commit ID are you always guaranteed to get the exact same version of every single file in the repo?

yes!

commit IDs always represent the exact same thing. So if you and your colleague both run

$ git checkout $SOME_COMMIT_ID

in a repository, you'll both see all the same files

ok, technically if you manufacture another commit that hashes to the exact same SHA-1 hash then this isn't true, but this is always true in practice

does git store file permissions (like 400)?

sort of!

git only stores whether the file is executable or not, not the full set of permissions.

what command shows the diff for the commit 60e2f3?

git show 60e2f3

You can also do

$ git show 60e2f3 --stat

to see a summary of files changed.

if you commit a change, is it possible for git to delete that commit?

yes, actually!

As long as the commit is on a branch, you're safe. But if the commit isn't on any branch and you run git gc, git gc will by default remove old commits that aren't on any branch to save disk space.

if you make a commit and somehow lose it (through a bad amend / rebase / merge etc), what command can help you find it again?

git reflog

$ git reflog shows you the history of every commit you've checked out. It can be slow to sift through but everything is there.

more reading