You Already Know Git

In celebration of my newest book: Git For Programmers I’m starting a short series of blog posts on some of the more interesting features of Git.

You already know Git

These posts assume you know what Version Control is and why you want it. I even assume you’ve been exposed to Git because Stack Overflow says 93% of programmers have. So these posts will be the fun stuff.

Let’s dive in to one of the most confusing aspect of Git for many programmers: Rebasing. When you say rebasing, many programmers run from the room pulling their hair and crying.

But Rebasing is really not that bad. In fact, it is pretty straight forward.

Rebasing

Let’s say you are working on a branch (you do your work on a branch, right??) and you want to merge the main into your branch to reduce the probability of conflicts later.

If your feature branch branched off of the latest commit from main, no problem, you do the merge, and Git will do a fast forward for you…

Here we see that Feature1 branched off the latest commit of Main and so when we merge, Git does a fast forward and the latest commit has head and feature1 pointing to it. Easy.

Now let’s say that after you branched, someone committed to Main. You do not want to lose the new commit in main, so you must do a full merge.

That resolves the problem but creates a commit in your commit history. The alternative is to rebase. Rebase accomplishes the same thing, but without the new commit. It makes a copy of each commit in your branch, and then plays them back on top of main. What? Think of it as simply making your feature branch off the latest commit of main and you won’t go too far wrong

If you track the arrows, you’ll see that you can just as easily put the bottommost (oldest) Feature1 commit on top of Main and that is how it is usually shown

That’s it. Nothing scary here. You can now go on working on Feature 1 and if Main advances, you can do it again.

One important thing, though: Never Rebase on the Server

Doing so can easily cabbage someone else’s code, and then they will want to hurt you. So don’t do that.

Next up: Interactive Rebasing, which sounds like a kind of rebasing but isn’t.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials and tagged . Bookmark the permalink.