questions about git branches

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.

what's a git branch?

a reference to a commit!

For example, if you want to see which commit the branch mybranch references, you can run

$ git rev-parse mybranch

or

$ cat .git/refs/heads/mybranch

when you run git log, is there a single file git looks at to find all the commits for that branch?

nope!

git log figures out the history by taking the commit the branch references and looking at that commit's parent(s) / grandparents etc. A commit's parents can't change, so the history is always the same.

is HEAD a branch?

nope!

HEAD is a reference to the current commit or branch you have checked out! Usually it's a reference to a branch. You can see what it is by running cat .git/HEAD.

sometimes git prints the message "You are in 'detached HEAD' state".
What does that mean?

HEAD points to a commit, not a branch

when HEAD points to a commit, no branch gets updated when you make new commits.

git warns you about this because if commits aren't on a branch, it's hard to find them later.

when you run git commit, does it change which commit your current branch points to?

yes!

git commit creates a new commit and then updates the current branch to point at the new commit.

does git save a record of which commits a branch has referred to historically?

yes! the reflog!

you can view it with

$ git reflog BRANCHNAME

so if you want to go back to what you had on that branch last Tuesday, you can!

can creating a new branch use a lot of disk space?

nope!

creating a new branch just creates 2 small files: a reference to the current commit and a history of what you've done with the branch (the reflog).

is it possible to change a branch to point at any commit you want?

yes!

you can make a branch point at a different commit with either git reset or git branch!

if you have 2 copies of a repository with the same branch name (like banana), do both versions of the branch always point at the same commit?

nope!

branch names are local to a repository, and they don't get automatically synced in any way.

more reading

... and you're done! To get the most out of this set of questions, consider taking a minute to write down (in your own words) one or two things you learned!

If you want, you can write down what you learned in this box and anonymously send it to me. Hearing what people are learning helps me build better questions in the future :)