Check out a free preview of the full Git In-depth course
The "Git Rebase and Amend Solution" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:
Nina walks through the solution to Git Rebase and Amend Exercise.
Transcript from the "Git Rebase and Amend Solution" Lesson
[00:00:00]
>> Nina Zakharenko: Let's go ahead and walk through the solution. So the first thing we'll need to do is checkout exercise7.
>> Nina Zakharenko: And
>> Nina Zakharenko: I am going to make a new commit.
>> Nina Zakharenko: I'm going to
>> Nina Zakharenko: Make a new file called my_name.txt for example.
>> Nina Zakharenko: Actually let me follow through with the
[00:00:37]
>> Nina Zakharenko: With the examples that you use, so
>> Nina Zakharenko: We have our first file and a second file.
>> Nina Zakharenko: And I'm gonna add git add first.txt and commit it.
>> Nina Zakharenko: But when I do a git status, oops, I forgot to add the second file to our commit.
>> Nina Zakharenko: This is fine.
[00:01:21]
As long as we haven't pushed our commit to a remote repository that we're sharing with other people, we can just amend the last commit to include the other file. To do that, we would need to add that file to a staging area and git commit --amend, this is gonna open an editor.
[00:01:39]
It'll let you change the commit message if you wanted to, but I'm gonna leave it as is.
>> Nina Zakharenko: And now, we'll see that our commit, committed two new files has both of the files that we meant to commit.
>> Nina Zakharenko: So let's get things ready for a very simple rebase.
[00:02:08]
I'm going to checkout master.
>> Nina Zakharenko: And I'm gonna checkout a new exercise branch.
>> Nina Zakharenko: This one already exists. I am going to delete it, excuse me, with git branch -D. And I will make a new branch called exercise7-2.
>> Nina Zakharenko: Great, now if we look in our log,
>> Nina Zakharenko: We'll see one or two simple commits.
[00:02:56]
>> Nina Zakharenko: Here, Master has changed. We have our initial commit.
>> Nina Zakharenko: Master, let's pretend that master is going to keep changing as I work on this branch. So I'm gonna checkout master again and I'm going to add some changes to our hello.txt.
>> Nina Zakharenko: When I do double arrows here, it means I'm appending to this file instead of overriding it.
[00:03:31]
>> Nina Zakharenko: So if I look at it now, it says we have some changes. It's continuing to change. We're gonna add this file and commit it.
>> Nina Zakharenko: When we go back to our feature branch, in this case, exercise7-2.
>> Nina Zakharenko: We're going to git rebase master. What that means is it's gonna rewind my work.
[00:04:04]
It's gonna grab the work from master, that commit, and pull it in. And then it's gonna take mine commit, the new commit that I made and apply that change on top.
>> Nina Zakharenko: So if I look at my log now, I'll see I have this commit here, Master has continued to change.
[00:04:28]
If I git rebase from master,
>> Nina Zakharenko: And look at my log again, we'll see that I have this new commit, Master has changed again.
>> Nina Zakharenko: So if I had, let me show this one more time. If I make a new commit here,
>> Nina Zakharenko: And in my exercise7-2 branch and I add it.
[00:05:29]
>> Nina Zakharenko: And I commit it.
>> Nina Zakharenko: If I look at my log, I will see,
>> Nina Zakharenko: Adding hi.txt up here.
>> Nina Zakharenko: If I go back to master, make a new commit.
>> Nina Zakharenko: If I checkout my feature branch,
>> Nina Zakharenko: Exercise7-2, and I look at the log, if I git rebase from master, and I look at the log, we'll see that the change from master has now been pulled in.
[00:06:35]
And that latest change on my branch, adding hi.txt has been applied on top. Cool, I like to do this pretty frequently when I'm working on a branch that's likely to have conflicts. For master often and fix those conflicts one at a time. So that I'm not stuck with the crazy merge commit when I'm ready to merge my feature back into master.
[00:07:03]
>> Nina Zakharenko: We're gonna do a very simple interactive rebase.
>> Nina Zakharenko: We're going to add one more commit in our feature branch.
>> Nina Zakharenko: And I'm gonna add that and commit it.
>> Nina Zakharenko: And now we'll see that, if I look at git log, with git log -n 3, that's gonna let's look at the last three commits.
[00:07:39]
We'll see that we have two commits on top of master, adding hi, and adding a new feature. If we want to squash these commits together and change their commit message, we can do that with an interactive rebase. So remember, I wanna start my interactive rebase from this commit.
[00:08:01]
And I can do that in a few ways. I can say HEAD tilde 2 to go two commits back. I can reference the parent of this commit or I can reference this actual commit.
>> Nina Zakharenko: I'm going to use HEAD-2 to reference two commits back. Now when I do this, an editor's going to open.
[00:08:33]
And we're gonna see the following, it's big, it's scary. There's lots of words but now you all know what they mean. So you shouldn't be too intimidated, right? When you rebase these things list in top down order. So kind of different than what you're used to seeing in git log.
[00:08:51]
It's important to remember. And now it's default to pick, meaning just keep this commit as is.
>> Nina Zakharenko: What we're gonna do is change them. So I'm gonna say reward this commit, meaning, edit the commit message and squash this commit back into the previous one.
>> Nina Zakharenko: Now when I save this, my editor is going to open up two more times.
[00:09:30]
>> Nina Zakharenko: So, it automatically opened again, right?
>> Nina Zakharenko: And, I'm gonna change the commit message. I'm gonna say Added two new features here.
>> Nina Zakharenko: Cool, now, when I say this, my editor is going to open up automatically again to act on the next action in that to-do list. And there's a lot of helpful information here.
[00:09:59]
It's saying this is a combination of 2 commits. This is the first commit message, so that's the one that I had edited. And this is the commit message number 2. We don't care about this commit message. We just wanna combine this commit with the previous one. So I'm gonna comment it out.
[00:10:19]
Now when I save it.
>> Nina Zakharenko: We'll see that I successfully rebased, and if I look at my log
>> Nina Zakharenko: We'll see that adding hi is gone and the other commit is gone. And they've both been combined into this one commit for adding two new features. And that's the end of exercise7.
[00:10:55]
Hopefully, rebase is a lot less scary now. It's not a dirty word, it's incredibly useful. These are all the time, almost everyday, I commit early and often on my local branch. I'm always making updates, always backing up my work. And when I need to share it with coworkers, I rebase it.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops