How to redo the previous git commit?

Redo the previous git commit

In this post, I provide the necessary commands to revise or redo the previous git commit. There are cases when you make an incomplete commit, you miss some files, or you would like to change the content of some files in the last commit. With the following command, you can reset your last commit:

git reset --soft HEAD~1
  • --soft Does not touch the index file or the working tree at all. This leaves all your changed files “Changes to be committed”, as git status would put it. [source: man git-branch]

After that, you can further change the content of the file or stage new files. When you are ready, you can stage the changes and craft a new commit.

Update the changes in the remote repository

If you pushed your commit to the remote repository before the redo, you must force-push the revised commit. This can be done as follows:

git push --force origin <branch_name>

Demonstration

The video below demonstrates the described commands above:

How to remove specific elements from an array in JS?

In this post, I provide a solution on how to remove specific elements from an array in JavaScript. Furthermore, I provide an example of how to extend the Array prototype in JavaScript.

Remove a specific element from an array

In the following example, I want to remove the number 3 from the array. Unfortunately, JavaScript does not provide a simple and intuitive way to do so, hence, first, the index of the element is retrieved by calling the indexOf method. Next, the splice method is called, which changes the contents of the array by removing the first element equal to 3.

var arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];

const elIdx = arr.indexOf(3);
if (elIdx > -1) {
  arr.splice(elIdx, 1);
}

console.log(arr); 

Result:

[1, 2, 4, 5, 1, 2, 3, 4, 5]

You will notice that only the first number 3 is removed from the array.

Remove multiple elements from an array

The example below loops through the array and removes the element which is equal to 3.

var arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];

for (let i = arr.length - 1; i >= 0; --i) {
    if (arr[i] === 3) {
        arr.splice(i, 1);
    }
}

console.log(arr); 

Result:

[1, 2, 4, 5, 1, 2, 4, 5]

Extend the Array prototype

Another solution is to extend the Array prototype:

Array.prototype.remove = function (el) {
    const elIdx = this.indexOf(el);
		if (elIdx > -1) {
  		this.splice(elIdx, 1);
		}
};

var arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
arr.remove(3);

console.log(arr);  

Result:

[1, 2, 4, 5, 1, 2, 3, 4, 5]

In the same way, the removeAll method can be implemented to remove all the desired elements from the array.

Using Set

If your array has unique values, you can store the data using Set. It already includes the delete() method which removes a specified value from a Set. Be aware of the performance for both array and Set.

How to get all the node values from an XML in MATLAB?

In the following example, I demonstrate how to get all the node values from an XML based on the XPath in MATLAB and Java. XPath stands for XML Path Language and is a query language for selecting nodes in an XML document. The XPath language is based on a tree representation of the XML document. As I am going to show in this post, XPath provides a straightforward way to navigate the tree and select nodes according to various criteria.

Let’s consider the following example XML file (book.xml):

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <author>Ralls, Kim Inside</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk101">
      <author>Ralls, Kim bk101</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>   
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book> 
</catalog>

What I would like to achieve is to get all the authors names for the books which have id of bk101. This can be easily achieved by using xpath:

mport javax.xml.xpath.*

factory = XPathFactory.newInstance;
xpath = factory.newXPath;

fXML=xmlread('books.xml');
authors = xpath.evaluate('catalog/book[@id="bk101"]/author', fXML, XPathConstants.NODESET);

for i=0:authors.getLength-1
    disp(authors.item(i).getFirstChild().getNodeValue())
end

If you are using the expression in a, for example, for or while loop, I recommend compiling it first to prevent performance drawbacks => The evaluate function will call the compile function each time. After updating the code, by including the compile function, the whole example would be:

import javax.xml.xpath.*

factory = XPathFactory.newInstance;
xpath = factory.newXPath;

fXML=xmlread('books.xml');
expression = xpath.compile('catalog/book[@id="bk101"]/author');
authors = expression.evaluate(fXML, XPathConstants.NODESET);

for i=0:authors.getLength-1
    disp(authors.item(i).getFirstChild().getNodeValue())
end