Jess Wang
July 29, 2022
10 min

Answer Popular Questions on Stack Overflow Without Google

A TikTok video brought my attention to the Most Asked Questions page on Stack Overflow recently, and I realized 3 major things:

A screenshot of Stack Overflow's most popularly asked questions.

  1. The amount of people who view these questions are on the order of the MILLIONS.
  2. It's not immediately obvious what command is the correct answer. I need to read through threads, explanations, and external links to make sure that what I'm copying is the "answer" to the original question.
  3. These questions can be answered by AI Command Search.

Can I leverage tools in Warp to answer popular Stack Overflow questions from the terminal itself and save people a lot of time from Googling things?

Curious to see how Warp did? I lay it all out, question-by-question, in the sections below.

How Do I Undo the Most Recent Local Commits in Git

Screenshot of Stack Overflow question. Note the amount of views - 11.4 million!

1) First, let's set up our environment so we can test out this scenario.

  1. Create a new git branch

git checkout -b newBranch

  1. Use vim to make an arbitrary change to an arbitrary file in "newBranch"

vi pride.yaml

(and then some vim-related commands to make changes within the file)

  1. Verify the changes were made by checking the status of the newBranch

git status

  1. Save all the changes you made

git add .

  1. Commit these changes locally

git commit -m 'random changes'

2) Okay, we have a local commit in Git set up. Now we want to know how to undo it.

I am going to use AI Command Search to get the command I need.

Using AI Command Search to see how to undo a local commit.

The output it gives me is:

git reset --soft HEAD~1

3) BUT WAIT. How can I trust this command to ACTUALLY do what I want?

I'm going to use Command Inspector (enabled automatically) to hover over each part of the command to tell me what it does:

Using Command Inspector in Warp to test the command for undo-ing a local git commit.

  • git: The stupid content tracker
  • reset: Reset current HEAD to the specified state
  • --soft: Remove the last commit from the current branch, but the file changes will stay in your working tree

4) Seems like the command lives in the ballpark of what I want to do. Let's run it!

  1. Undo the most recent commit

git reset --soft HEAD~1

  1. Check the status to see if the changes are actually uncommitted

git status

Here's the output of successfully undo-ing a local git commit. You can see my file is back as a staged file.

✅ I'd consider this a success!


How Do I Delete a Git Branch Locally and Remotely

Screenshot of Stack Overflow question for how to delete a git branch locally and remotely. Viewed 10.1 million times.

1) First, let's set up my environment so we can test out this scenario.

  1. Creating a new git branch locally

git checkout -b newBranch

  1. Push the branch to create it remotely.

git push origin newBranch

2) Okay. We have a local and remote git branch. Now we want to know how to delete both of them.

I'm going to use AI Command Search and see what it outputs:

Using AI Command Search to see how to delete a local and remote git branch.

It spits out this command:

git branch -d branch git push origin --delete branch

3) BUT WAIT. How can I trust this command to ACTUALLY do what I want?

I am going to use Command Inspector (enabled automatically) to hover over each part of the command to tell me what it does:

Using Command Inspector to verify the command for deleting a local and remote git branch.

  • git: The stupid content tracker
  • branch: List, create, or delete branches
  • -d: Delete fully merged branch
  • push: Update remote refs
  • origin: Remote
  • --delete: All listed refs are deleted from the remote repository. This is the same as prefixing all refs with a colon

4) From the descriptions, it seems that this command will indeed undo my most recent commit. So let's go ahead and run it.

  1. I need replace 'branch' with the actual name of my branch, which is 'newBranch'. Then run the command.

git branch -d newBranch git push origin --delete newBranch

  1. Check if the local 'newBranch' branch is deleted.

git branch

  1. Check if the remote 'newBranch' branch is deleted. When I checked, it was not there anymore!


✅ I'd consider this a success!

What Is The Difference Between Git Pull And Git Fetch

Screenshot of Stack Overflow question for the difference between git pull and git fetch

  1. Type `git pull` into the input box.
  2. Hover over it or press CMD + I to activate Command Inspector.

Using Command Inspector to see what git pull does

  1. Type in `git fetch` into the input box.
  2. Hover over it or press CMD + I to activate Command Inspector

Using Command Inspector to see what git fetch does

So `git pull` integrated with another repository while `git fetch` downloads objects and refs from another repository.

I think this is helpful for developers who already understands these concepts and just needs a reminder. For beginners, it will probably be more helpful in this case to read through the Stack Overflow threads and familiarize yourself with the conceptual difference of what these commands do.

(BTW, adding the reply I found to be most helpful below)

The difference between git fetch and git pull according to one answer on Stack Overflow


👍 MINOR SUCCESS :)

How Can I Remove A Specific Item From A Array Javascript

Stack Overflow question for how to remove a specific item from an array in Javascript. 10.2 million views!

1) I am going to use AI Command Search on this question and see what it outputs as my terminal command.

Using AI Command Search to see how to remove a specific item from an array in Javascript

It spits out this command:

array.splice(index, 1)

Let's try it out.

  1. Create an array and assign it to a variable named 'array'

var array = [1, 2, 3, 4, 5]

  1. Run the command that AI Command Search outputted - first replace 'index' with the index you want to remove from the array.

array.splice(1, 1)

Output of running array.splice in Javascript

As we can see, the number at the specified index was removed from the array. Seems about right!

2) BUT WAIT. Is this what the OP wanted?

As I revisited the thread, it seemed that the OP wanted to be able to input the value to remove, not the index of the value. On top of that, they wanted to remove ALL elements with the value, not just one.

I didn't think the title of the Stack Overflow question correctly reflected the OP's intention, so I reworded it and tried it again in Warp's AI Command Search.

This time I asked "How to remove all specific values from an array Javascript?"

Using AI Command Search to see how to remove all specific values from an array in Javascript

It spits out this command:

var arr = [1,2,3,4,5,6,7,8,9,10];

arr = arr.filter(function(n){return n%2==0});

console.log(arr);

When I run the code, this is what I see:

The result of removing all odd numbers from an array

WOW. AI Command Search literally just wrote a function for you in Javascript that will filter out all odd values from an array. If you wanted to filter out all elements of a specific value, you could try:

var arr = [1,2,3,4,5,6,7,8,9,10];

arr = arr.filter(function(n){return n!="THE_SPECIFIC_VALUE"});

console.log(arr);

And this is what it looks like when you run it:

The result of removing all elements of a specific value from an array


✅ I'd consider this a success!

How Do I Rename A Local Git Branch

Screenshot of Stack Overflow question for how to rename a local git branch

1) Let me first set up my environment so I can test out this scenario.

  1. Creating a new git branch locally

git checkout -b newBranch

2) Okay. Now that we've created a branch called 'newBranch', we want to rename it to something else.

I'm going to use AI Command Search and see what it outputs:

Using AI Command Search to rename a local git branch

It outputs this command:

git branch -m old-branch-name new-branch-name

3) BUT WAIT. How can I trust this command to ACTUALLY do what I want?

I'm going to use Command Inspector (enabled automatically) to hover over each part of the command to tell me what it does:

Using Command Inspector to see what git branch -m <old-branch-name> <new-branch-name> does

  • git: The stupid content tracker
  • branch: List, create, or delete branches
  • -m: Move/rename a branch and its reflog

4) Let's run the command!

  1. Replace <old-branch-name> with `newBranch`
  2. Replace <new-branch-name> with, let's say, `superNewBranch`
  3. Run the command.

git branch -m newBranch superNewBranch

And this is the result of running the command:

The result of running a command to rename a local branch


✅ I'd consider this a success!

Conclusion

My main takeaway from this experiment is that AI Command Search and Command Inspector are pretty darn good for generating any basic commands you want to execute.

With AI Command Search, you need to be specific with how you phrase your questions. For example, I had to play around with my English to get the exact result I was looking for. Command Inspector is a good gut-check, but if you want to dive deeper into topics you're unfamiliar with, that may be a good time to consult the Internet.

If you're interested with playing around with AI Command Search and Command Inspector, you can download Warp (Mac-only) and try them out for free:

Download Warp

I hope this was helpful to you. I am thinking about doing a part 2 that tests these features on more Stack Overflow questions - let me know if you're interested!

Share

View More