Tag: visualizations

How to visualize a rebase in the Git Visualization tool – 111

If it wasn’t for this visualization tool, I’d never grasp Git concepts. Today’s tip follows the Git documentation for Rebasing

Suppose you’re experimenting locally on one branch (called creatively, `experiment`) and you want to rebase back onto master. For now, let’s imagine that master is a remote that you’ve cloned from somewhere. When the time comes to push those changes back, you don’t want your local Git history* to show merges commits from your local explorations. You’re going for the linear “look! I got it all right on the first try” history.

visualization setup of master vs experiment branches

First, you’ll want to make sure you’re on the `experiment` branch.

Next, you’ll use the command to rebase your current branch (experiment) onto master. I silently verbalize “onto” as the preposition to help me understand what the rebase command is going to do.

$ git rebase master

rebase visualization

Note how the rebase results in new commit IDs. If you take nothing else away today, note this:

Whenever you do a rebase, you are changing history.

more on this later.

Now to “finish the rebase.” This is where I’m so grateful for this visualization tool. Before seeing what was happening, I’d thought everything would be done for me.  Thanks to the visualization I can see based on the master pointer that I need to merge the experiment branch.

$ git checkout master

$ git merge experiment

moving master up to experiment

This last step should look familiar. The rebase makes our history look like all of our changes were based on the last commit on master. This would be the equivalent of being on the last commit of master, checking out a branch, making commits, and then performing a fast-forward merge from master.

There are just so many moving parts here that a visualization tool helps greatly.

*I mentioned previously about doing rebases on your local git history. The rule of rebasing is you should never do it if those commits have been previously pushed or shared somewhere, since you’re changing history, and Git hates it when you change history. There are ways out of it, but I’m saving that to explore in October timeframe.

A good recap from the documentation (last sentence, of course. always amazing how the most important info is often found at the very end of a document or email. a trick someone taught me once is to write out what you want to say, and the reverse the order of the paragraphs. I guess this blog post is proof of concept 😉 since I’m leaving the most important piece at the bottom!)

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere.

How to make a commit in a detached HEAD state – 098

And one more quote from the same stack overflow answer, “it will still allow you to make commits (which will update HEAD), so the above short definition is still true if you think of a detached HEAD as a temporary branch without a name.”

What? We can still make commits? in a detached HEAD state?

Let’s be brave and give it a try in the visualization tool.

If we do a ‘git commit’ in the detached HEAD state, the visualization tools shows a commit being made to the side.

visualizing commit made in detached HEAD state

P.S. Have I proclaimed my love of this git visualization tool yet today?

So what on earth now? we have this thing floating out in nowhere. Well, we know it is a nameless branch… and we know we can merge branches… let’s try to merge it!

First, we’ll checkout master.

back to non-detached head but commit still floating out there

Then we’ll do the merge and verify the git log.

merged in floating git commit

and thus conclude my mathematical proof I truly have no idea how people are able to keep track of these one-off commits or whatever is going on in git without visualization tools. QED.

How to think about what the HEAD thingy actually is in Git – 097

My learning preference is through hands-on experimentation. I like taking things apart, putting them back together, or trying to break stuff. So let’s break apart this HEAD thingy.

According to this SO answer, it is a file.

saraf@TheBlueDog  ~/Source/Repos/scrap/detachedHeadPractice (master)
$ cat .git/HEAD
ref: refs/heads/master

and that file contains some content. I’d assume “ref” means reference, and “master” is the master branch we’ve been playing with. I’m not sure what “refs/heads/master” actually stands for, but I’ll figure that out later.

I keep hearing HEAD referred to as a pointer. This would make sense if it is a file that only contains the location(?) where to point to. That answers the literal question.

But what does the HEAD pointer do?

According to this stack overflow answer, “HEAD is a reference to the last commit in the currently checked-out branch.” Considering we have master checked out, that makes sense. Doing a git log would show all the commits in the repo.  If we switched to a branch and did a git log, we’d see all the commits for that branch.

But what about the detached HEAD state from yesterday?

Continuing with the same stack overflow answer, “A detached HEAD is the situation you end up in whenever you check out a commit (or tag) instead of a branch.”

Yep, we saw that yesterday. HEAD is pointing at the 2nd commit, whereas master is pointing at the 3rd.

visualization tool showing a detached HEAD state 

Now I had to re-read this line from the same stack overflow answer, “In this case, you have to imagine this as a temporary branch without a name; so instead of having a named branch reference, we only have HEAD.”

A temporary branch without a name??

Okay, what I think is happening is that if you were in a detached state (as shown above) and did a git log, you’d only see 2 commits, not 3.  Since you are seeing 2 commits, you are still technically on a branch. It’s just that this branch doesn’t have a name.

In other words, a Git pointer never points to just a single commit. A git pointer points to the last commit in that series of commits (which I guess is where the temporary nameless branch concept comes from). Which now begs the question… what’s really a git branch?

How to practice getting into and out of a detached HEAD state – 095

I promised that this blog would be a pun-free zone, but I must recommend Terry Pratchett’s The Thief of Time. It’s the last book of the “Death”  series. In fact, I recommend starting with Soul Music. Anyways I kept thinking of that creature as I wrote this tip.

So far, my two biggest lightbulb moments have come from learning about `git reflog` and this git visualization tool. Nothing came close to making sense until I combined those two.

Let’s use the visualization tool to practice getting in and out of a detached HEAD state.

First, let’s make a few commits…

initial setup in visualization tool

Next let’s checkout to the previous commit. Note: it helps to type in the commit id correctly, so ignore the “can’t find commit id”

visualization tool showing a detached HEAD state

Notice in the image above indicates that we’re in a detached HEAD state. Previously, I would have panicked because my last commit (the one master is pointing to) wouldn’t show up in the git log. But now because of this visualization tool, I can see that master is still there and that HEAD is just pointing at the previous commit.

To get out of the detached head state, you’ll need to git checkout master.  Previously, I would do git checkout commitID-master-is-pointing-to and still be in a detached state, frustrating me to no end.

back to a non-detatched head state

Now you’ll see that HEAD is pointing to master and we’re no longer in a detached HEAD state.

Tomorrow we’ll go over getting into and out of a detached HEAD state via the command line.