DEV Community

Spencer Lepine
Spencer Lepine

Posted on

Creating Custom Git Commands

Blog Post Thumbnail

Every time I clone a repository from GitHub, I always run the same set of commands. This is prone to typos and simply inconvenient. There is a simple solution of combining each step into a single command that automatically runs everything for us.

In this example, I need to clone a GitHub repository, move into the new directory, and then open the project in VSCode.

Instead of multiple commands:

  git clone https://github.com/spencerlepine/readme-crawler
  cd readme-crawler
  code .
Enter fullscreen mode Exit fullscreen mode

It would great to run one command:

    clone https://github.com/spencerlepine/readme-crawler
Enter fullscreen mode Exit fullscreen mode

To achieve this, we can create a script in the


 directory. Make sure this path matches up with your configuration for the terminal (e.g.

 ```PATH=$PATH:$HOME/bin```

).

Let’s create a custom script to combine the git commands.



```sh
  #!/bin/bash

  ((!$#)) && echo missing git URL argument! && exit 1

  git clone $1
  basename=$(basename $1)
  reponame=${basename%.*}
  cd $reponame
  npm install
  code .
Enter fullscreen mode Exit fullscreen mode

Use this script or create your own, and follow these steps to set up the custom command:

  • Navigate to usr/local/bin ->

    cd ~/../../usr/local/bin

  • Run

    vim clone

    • Paste the script
  • Save the file:

    • *press ‘ESC’
    • *press ‘SHIFT’ + ‘:’
    • *type ‘wq’ + ENTER
  • Create an executable


    chmod +x clone

  • Run the command!


    clone https://github.com/spencerlepine/manyshiba-bot.git

Viola! This script will accept one command line argument of the destination repo URL. It will automatically open the new project in VSCode in one command.

Follow my journey or connect with me here:

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay