Unrelated changes in your git code? Avoid them using Patch!

Vinothini B

1 min read

For a very long time, we have been using Git as a code version controller in Spritle Software. Recently, I came across a Git command called git to add -p (where p denotes partial/ patch). We currently have 30+ developers on our team and each person will raise a PR (Pull Request) to merge their current work with the master branch.

In every project, there is one file that is commonly & constantly used by all the developers called the schema.rb. In the schema.rb, we can see all the changes regarding the DB migrations made by each developer.

Simultaneously on my PR, I should also commit to what the schema changes did relate to the PR. For example, the schema.rb shows that 96 new lines are added, and in that 96 lines, only two were added by me. We can use the partial commit feature in the git add -p comment to add my changes to the file.

git add -p schema.rb

When the above command is typed in the terminal you will see a list of options like,

(1/96) Stage this hunk [y, n, q, a, d, j, J, g, /, s, e, ?]?

Here 1 denotes the first change of a total (96) changes.

Do all the other letters seem like they do not make any sense? That’s not the case.

Let me explain them to you!

Say if hit the answer as

  • y – you are going to add this hunk to commit.
  • n – you are not going to add this hunk into the commit.
  • a – you are going to add this hunk and next to all hunk into the commit.
  • d – you are not going to add this hunk and next to all hunk into the commit.
  • q – quit from the flow.

The next thing we have to do is commit and push it to the repository.

git commit -m "Message - Commit Schema changes

NOTE: After doing git add -p, if you try git status, you can see schema.rb in the list of the changed files. You don’t have to perform git add again. Easy right?

As we saw, the partial/ patch feature of git is very useful when it comes to handling large commits. It has a lot of other options that make our workflow flexible.

I hope I have given you a basic idea about what the “git add -p” command is. Keep following my blogs to learn more about such interesting topics.

Reference: https://gist.github.com/mattlewissf/9958704

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *