Use git add --patch for better commit history and mitigating bugs

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

Let's split our changes into separate commits. We'll be able to check over our changes before staging them all from the terminal. Then, we'll see the positive effect it has on our commit history.

[00:00] Our project consists of an HTML file which is stored in a Git repository. We want to add some animals to the "All About Animals" page that we have. We want to add some information about rhinoceros, lizard and bison. We're just using some example text from Wikipedia.

[00:17] Now that we've made changes to our HTML file, we're going to want to commit them to our repository. Let's open up the terminal and let's use "git status" to show the files that's changed. At this stage, we could just "git add index.html" and commit the whole lot in one, but instead, we're going to use "git add --patch."

[00:37] This is going to allow us to selectively choose which changes we can add to staging. We want to split up our changes into three separate commits. Down here, we have a range of choices. We can choose what to do with the change that we're looking at.

[00:52] If we type "y" for yes, it's going to add all of the changes in one. Instead, we want to type "s" for split. This is going to split each of the changes into separate hunks. I'm going to stage the rhinoceros. Then I'm going to type "q" for quit. Now, I can commit the rhinoceros changes.

[01:12] Let's do "git add --patch" again. Again, it's showing us all of the changes that are there. Let's split those again. Now we can see the lizard changes. Let's stage those, and quit again, and we'll commit those.

[01:24] Let's add patch once more. The last change that's there is the bison so we can stage that and apply our last commit. Now if we look at our commit history, it's all split up nicely into separate commits.

[01:36] This will be much more useful for anyone else that comes to work on our project because all of the features are split up. We also ensured that we're able to check over each change that we staged before we staged it, so it's less likely that bugs will creep into our project.

Neil
Neil
~ 7 years ago

This trick is really awesome! thumbsup!

Tre' Codez
Tre' Codez
~ 7 years ago

Which editor (ide) is that?

Rory Smith
Rory Smithinstructor
~ 7 years ago

Thanks!

Rory Smith
Rory Smithinstructor
~ 7 years ago

It's VS Code :)

Tre' Codez
Tre' Codez
~ 7 years ago

Ah, cool. I've not seen that theme before. ☻

Markdown supported.
Become a member to join the discussionEnroll Today