DEV Community

Cover image for Git : How to commit a few lines of code from several files?
Raymundo CH
Raymundo CH

Posted on

Git : How to commit a few lines of code from several files?

Most of the times when making a commit with Git the files are staged and then those files are commited. Nevertheless sometimes we need to split a file into chunks and save those chunks into different commits. In other words sometimes we might need to commit a few lines of code only rather than the whole file. Here I share with you how to do it.

Adding the file to te staging area

First we must add the file to the staging area in case the file is untracked by Git. In this case I want to add the file inside the Deactivation folder:

On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   tinyshortcode.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Deactivation/
        FilterCurrency/
Enter fullscreen mode Exit fullscreen mode

So I have to run the next Git command:

git add -N Deactivation/
Enter fullscreen mode Exit fullscreen mode

And the staging area looks as shown below:

On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        new file:   Deactivation/register.php
        modified:   tinyshortcode.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        FilterCurrency/
Enter fullscreen mode Exit fullscreen mode

Picking up the desired lines in the first file

The commit will comprise lines of code from the files register.php and tinyshortcode.php.

Let's work with the file register.php by running the next Git command:

 git add --patch Deactivation/register.php
Enter fullscreen mode Exit fullscreen mode

And let's enter the option e into the command prompt; e stands for edit and enables you to choose the lines of code you want to save in the next commit:

(1/1) Stage addition [y,n,q,a,d,e,?]? e
Enter fullscreen mode Exit fullscreen mode

A text editor is opened in the console where you have to use # at the beginning of the lines which you want to remove from the commit; the remaining lines will be included into the commit:

@@ -0,0 +1,12 @@
+<?php
+
+function rch_register_deactivation_logic(){
+
#    delete_option("fruitsTableData");
+}
+
+
+register_deactivation_hook(
+    WP_PLUGIN_DIR."/tinyShortcode/tinyshortcode.php",
+    "rch_register_deactivation_logic"
+);
Enter fullscreen mode Exit fullscreen mode

Take a closer look at the code above and see how only one line of the code will be removed and the remaining lines will be saved into the commit; the lines of code which start with + will be commited.

Run the next command to see the changes made. In other words you will see the lines of code to be included into the next commit:

git diff --cached
Enter fullscreen mode Exit fullscreen mode

Also check out the staging area and see how it looks different:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Deactivation/register.php

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Deactivation/register.php
        modified:   tinyshortcode.php

Enter fullscreen mode Exit fullscreen mode

Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more.

Picking up the desired lines of the second file

Since the second file is already located in the staging area then we just have to run the Git command necessary to choose a few lines of code for the next commit:

 git add --patch tinyshortcode.php
Enter fullscreen mode Exit fullscreen mode

A promt is displayed where we have to enter the option e:

(1/1) Stage this hunk [y,n,q,a,d,e,?]? e
Enter fullscreen mode Exit fullscreen mode

A text editor is opened where we have to choose the lines to be removed from the commit by adding a # at the beginning of the line:

+require_once WP_PLUGIN_DIR."/tinyShortcode/Deactivation/register.php";
#require_once WP_PLUGIN_DIR."/tinyShortcode/Activation/register.php";
Enter fullscreen mode Exit fullscreen mode

Now the staging area looks as shown below:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Deactivation/register.php
        modified:   tinyshortcode.php

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Deactivation/register.php
        modified:   tinyshortcode.php
Enter fullscreen mode Exit fullscreen mode

Remember that you have to run the next command to see the staging area:

git status
Enter fullscreen mode Exit fullscreen mode

Let's commit the changes

Once the staging area contains the lines of code we want to include in the next commit it's time to save the changes:

git commit -m "Add a description here..."
Enter fullscreen mode Exit fullscreen mode

Don't forget to see the lines of code of the newly created commit with the command:

git show [commit hash]
Enter fullscreen mode Exit fullscreen mode

And in order to see the listing of commits with their corresponding hashes use the command:

git log
Enter fullscreen mode Exit fullscreen mode

Thanks for reading this article!! see you soon! Happy coding

Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more