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 Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more