- Hey, I'm Cameron Mackenzie,
I'm the Editor In Chief of the TheServerSide.com,
you follow me on Twitter @cameronmcnz.
And I think I got a real treat for you here.
And I'm gonna need about 10-11 minutes your time.
But I'm gonna show you how to clean up a git repository
by using the rebase command and a couple of other
commands in there as well.
But I think I'm gonna do something that you're gonna
find is pretty neat,
I gonna take a repository that's got about 20 commits
in it three different branches, nothing been merged,
and merge it all together.
I'm gonna squash some commits,
I'm gonna do some rebasing.
And in the end, I'm gonna pull this whole thing
down from three branches, and 15 commits
to just two commits, and it's gonna be totally cool.
So anyways, I really hope you enjoy this tutorial,
'cause I really enjoyed putting it together.
So to clean up a git repository, you need a git repository
to clean up, I've got one here over on my Git Lab account,
you can find it if you want it
cameronmcnz/squashcommitsexample,
also on my GitHub account too,
you can find a link in the server side article about this.
And this is a dirty little repository,
or this is dirty as it can get,
if I wanna clean it up in less than 10 minutes.
Being into these three branches there unmerged,
this branch is five commits, this one has three,
this one has two.
And they all branch out from the same point,
this base point right here,
I'd like to squash these commits,
I'd like to merge all of this together, get a rebase done,
and get this cleaned up and maybe even refactor this.
So all of these commits are gone.
And I've got nothing but one commit on here,
but let's not get too aggressive right away.
Now, I suggest that you follow along with this.
So actually clone my repository
and play with this on your local machine,
that's the best way to learn.
First thing I got to do is I got clone this repository.
I mean, saying git clone and passing in the git URL,
which I just copied under this clone option here.
That copies everything into the squash commits example,
and there's gonna cd in there.
And currently, I'm on the master branch,
you do need to check out all of the other branches before
you start this.
So I'm gonna do a little bit of a git checkout feature,
git checkout develop.
And once that's done, I'm gonna take a look
at the graph log that tells me what this whole
repository looks like.
So git log, graph, one line, local branches.
And you'll see this looks a little bit like the fancy dancy
graph that I've got over on Git lab.
But I've got one, two, three branches master, feature
and develop, you can see that there.
And they all branch off from the same point.
Now, they all branch off from nine five three f zero one
eight base, that's this branch point here.
You can see that graphically, there's no doubt about that.
Okay, so what do I wanna do here?
First thing I wanna do is I wanna take these five
different commits and slam them down into one,
all of those commits are on this develop branch here.
And so I want all of these commits here,
to get slammed into this e commit.
In order to do that yet, use the Git rebase
interactive command.
And you have to specify the branch point at which
your branch that you wanna slam
splits off from its parent.
So this is the command here,
git rebase dash I for interactive,
and this is the branch point right there nine five three,
so I'll type that in five three f zero one eight.
That brings up this interactive command.
And you have to pick one commit for everything to merge
into, I'm gonna merge into e here.
And then for the other ones that you don't want to leave
hanging around, you just put an s next to them for squash.
If you're on the Nano editor, at this point,
you have to hit Ctrl + O to write this,
click return, click X to exit.
And of course, it's gonna ask you for a git commit message
and everything is being slammed down into
commit e right here.
So I'm gonna call this e prime,
Ctrl + O to write it, Enter Ctrl + X to get out of here,
I'm gonna take a look at my Git graph again.
And you can see from the Git graph that e prime is now
the only commit on that develop branch
before there were five commits, now there's only one,
that motivates me to go and tackle the feature branch.
So I gonna check out the feature branch.
It's got three files in it b, c, and d.
And I am going to do the exact same command
git rebase dash I
because this and the develop branch both
start off from the same branch point.
So I can use the exact same command.
And this is exactly the same as what I did before.
It's just different.
I put s next to the commits,
that wanna slam into, commit b, I pick one.
Notice that the order of the commits here is reversed
what usually see on the on the log,
so that's sometimes throws people off.
People often pick the last one in this list,
if they're not paying attention,
choose the first one that you wanna pick.
Again, I'm in Nano,
so I gotta do Ctrl + O, Enter, then Ctrl + X,
it's gonna ask me for a commit message,
I'm gonna call this b prime
Ctrl + O, Enter.
And then Ctrl + X to get out of here,
and I wanna see that graph again.
And look at this, things are getting very nice already,
we can see that we've now slam these different branches down
to one commit on each of them.
And things look really, really nice.
Now one of the issues that I have is each of the branches
have a different number of files on them.
And so for every commit a new file was created.
So what branch am I and I'm in the feature branch,
and the feature branch by doing ls has got files
b, c, and d.
And that's because when the feature branch broke off,
there were three commits, the b commit, the c commit,
and the d commit.
And for each of those, I created a file in there.
So b.txt, c.txt, d.txt,
and if you went into the develop branch,
you would see e.txt, f.txt, you get the idea, right?
But none of these files are shared.
So nobody shares any of the files,
I want all these files to come together.
Now, people that are naive, usually do a merge,
but merges are for losers.
So what I'm gonna do is I'm actually I've got three
branches here that are out of sync.
What I'm gonna to do is I'm gonna do three rebases.
So the first rebase that I'm gonna do is a git rebase
of feature onto the develop,
develop onto the feature branch.
Take a look at the log.
And what this does is it takes that develop branch,
it pushes it onto feature.
And by doing so, the develop branch now gets all the files
and feature plus the files that already had,
essentially, you can think of this as bringing
out the same result for the working tree of develop
as a merge with you, it's not a merge, as I said,
merges are for losers.
It's not a merge, but it is pulling all of those files in.
Okay, so we've got three branches,
we gotta do this three times.
Now I'm gonna get rebase, I'm gonna rebase master
on to develop.
And so you see the onto branch first, you see the branch
you're doing it to second.
So git rebase develop master, that's gonna put master
on the develop branch.
So I gonna take a look at the graph once again.
And you can see things are getting very, very orderly here.
So I'm liking the way that all of this is looking.
Okay, but we've still got three separate commits.
So master, develop and feature, they are not quite the same.
So here's what I'm gonna do, I'm gonna do
a git rebase, I'm gonna rebase feature onto master
which means I have to say master first when I say feature,
okay, this is now gonna put feature on top of master
and develop was before master, this is gonna cause all
sorts of fast forwarding and rewinding, but I love it.
If I do a look at the graph, once again,
you can see develop is now below feature and master
I gonna do one more git rebase.
And I'm gonna rebase onto feature
and I'm gonna rebase onto feature that develop branch.
And now when I look at the log, look at this magic,
the magic here is develop master and feature are all
pointing to the same commit, they're at exactly
the same spot.
They've got exactly the same files.
And we've got a nice little history here where I've got
the base commit, I've got b prime, I've got e prime.
And I've got all the files right up here.
So if I do an ls one, two, three, four, five ,six, seven,
I can't even count that high,
but all the files are in there.
That's the develop branch.
If I check out master
and do ls, it's got the same number of files as well.
So we've now completely synchronized these repositories.
So pat on your back.
I think this is pretty awesome where we're getting
right here.
But the magic isn't done yet.
I gonna do something that I'm just gonna get
a little crazy here.
I'm gonna do git branch dash d,
you know where I'm going here, right?
I gonna go to git branch dash d.
We're gonna go to graph and look at this.
I've got rid of my feature and develop branch.
We didn't need those ragtag branches hanging
around anyways, they were no good, do nothing,
but it's done.
So look at this, I got rid of those branches.
And now that I got rid of those branches,
I gonna do some real magic.
I gonna go back to the start of this tutorial
and watch this.
I am gonna rebase.
Oh is really going back here.
I'm gonna do a rebase but I'm gonna do a rebase.
From look at this, turn your head if you don't wanna
see this, cover your eyes four d zero b five b a.
That's the very first commit there.
Interactive rebase.
Here we go.
I'm gonna pick the base,
but I'm gonna squash everything here.
Nobody is safe in this example.
You can run, you can hide, but you cannot get away.
Okay, so that is done Ctrl + X.
Base prime is what I'm gonna call this commit Ctrl + O,
Enter and Ctrl + X.
And I don't know if anybody wants to see this massacre
that's going on here.
But I am now down to two commits.
I've got my master branch, it's going off initial.
And I've got all of the files in here,
I've got everything from the branches,
and I've torn away all of those commits,
I torn away the entire history, we no longer have this here,
we have something beautiful right here.
Now, I would say I would, at this point,
I would go git branch develop.
Let's say git branch feature.
And I would take a look at the graph once again.
And I would see that master feature and develop are now
pointing at the same commit.
But it's right down there at the start of the history.
And so there you go, I mean, how long
was that just over 10 minutes,
and I completely annihilated this dirty little branch here.
And now we've got something that is so clean,
you can eat off it.
So there you go.
That's how you can really clean up Git repository.
So if you enjoyed this tutorial, and you learned a thing
or two here, please share it with your friends
'cause I think there's a lot of good interesting stuff
in here that's worth sharing.
If you're hanging out with software developers.
By the way, I am the Editor of TheServerSide.com,
there's a write up on this on the site.
So you go and check that out if you wanna actually see
a textual description of what I got up to here.
Furthermore, if you wanna keep up to date with my daily
antics and follow me on Twitter @cameronmcnz
and also please subscribe on YouTube.