Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Automate the cleanup of my feature branch with Git Autosquash

    Juri StrumpflohnerJuri Strumpflohner
    gitgit

    We work on our feature branch and commit a lot, also knowing we’ll have to do an interactive rebase to clean up our history once we’re done. Well, we could also automate that cleanup step to some degree, using Git’s autosquash feature. Let’s have a look.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Assume we're working in that app refactoring branch and we want to make a couple of changes very quickly, and so we commit continuously after each change. Of course, in the end, we could then do an interactive rebase to clean up our history again and merge it in. However, we can also automate those steps using the fixup flag. Let's see how that works.

    I have here editor open on that README which is inside our Git repository. Let's say we want to clean it up. We want to have some instructions for the installation and setup. We might have to interrupt our work and we want to continue later on.

    What I do is I create, first of all, a normal commit with a proper commit message, update README. With that, we have that updated README already here present. Later, we continue with step one. Let's say clone the repo as a first step, and so on.

    This is just a first commit message of a series of commit messages which will go into updating that README. What I really want to have is that all of these commits should finish up in that update README commit message, which I've created before.

    I could obviously commit all of these separately, and then in the end do a Git rebase and cleanup my history, but what I would like to do since I already know these should be discarded and merged into their specific commit, I can tailgate that upfront.

    What we do is we do a normal add such that our file gets into the staging area, and then we do a Git commit -- and do pass that fixup, and we give Git here that identifier of the commit messages to which these should be merged automatically later on. Let's inspect the history again.

    You can now see we have that update README, and then we have that fix up exclamation mark on top of it. Let's continue. Let's create here step two, installation to install the dependencies, npm install.

    Again, we do the same thing. We could do again pick up here our commit hash and then have that fixup applied and merged into the commit message we want in the end.

    Again, we do a git add and then we do a git commit -- fixup just as we did before, but instead of giving it right here after commit, we can say search wherever there's an update text in the commit message and apply the fixup on top of that one. It will take the last one, in this case, it will fix it up on top of the other one which we created before.

    Let's run the command. Let's see the log again. You can now see we have here the first fixup, and then we have another one on top of this one. In the meantime, we might not continue on that README. Why don't we go, for instance, on the app.html and we do some other work.

    For instance, say here we add here a class right here, a full section for our application. We create a normal commit message at app folder, which then goes on top of our commit messages here. Then we resume our work. We go here and we say like step three, execute the tests npm test.

    This one we would again want to have on top of that update README. We can again use a git add and then we use that to fixup which we added before, which is still valid. If we execute that, we will then have the update README, a couple of fixups which we did initially, and then our app at app folder commit message between.

    Then we have another fixup, which will then be applied on top of this one. What we would like to do now is to automatically clean all of these up up until this point here.

    We can do a Git rebase interactive, and we do an autosquash up until this point here. Let's execute this. Now you can see that Git enters in this interactive rebase mode. It picks the first one, which is the correct one, which we actually want to take and keep, and then it has all the fixups already in place here for the commits which we have added before.

    You can also see that it rearranged the commit messages. This one was actually the last commit which we made. We set Git it should pull that on top of the update commit README message. It has also reorganized here our commit messages.

    Let's save everything. If we do now get log, we can see that our history has been cleaned up automatically.