Creating a Custom Alias to Update Git Remote URLs for any repository

Rahul Kumar
3 min readSep 1, 2024

--

Poster image

Updating Git remote URLs can be repetitive, especially when managing multiple projects. To simplify this, you can create a custom terminal function that automatically sets the remote URL for your GitHub repositories using a fixed username. With just one command, you can update the remote URL by passing the repository name, saving time and minimizing errors.

Step 1: Define the Function in Your Shell Configuration

The first step is to create a function that automates the process of setting the remote URL. This function will take in the repository name as a parameter and use your GitHub username to construct the full URL.

Open your shell configuration file (~/.zshrc for zsh or ~/.bashrc for bash) in a text editor:

nano ~/.zshrc  # For zsh users

or

nano ~/.bashrc  # For bash users

Add the following code to define your function:

# Function to set Git remote URL using a fixed GitHub username
set_git_remote() {
if [ $# -ne 1 ]; then
echo "Usage: set_git_remote <repository>"
return 1
fi

local repository=$1
local username="bumblebee338"

git remote set-url origin git@github.com:${username}/${repository}.git
echo "Remote URL updated to git@github.com:${username}/${repository}.git"
}

Step 2: Reload Your Shell Configuration

After adding the function to your shell configuration file, you need to reload the shell so that the changes take effect. This can be done with the following command:

source ~/.zshrc  # For zsh users

or

source ~/.bashrc  # For bash users

Step 3: Use Your New Command

With the function now defined, you can use your custom command to set the remote URL for any Git repository. Simply navigate to your repository’s directory and run:

set_git_remote your-repository

For example, if your repository name is my-repo, you would run:

set_git_remote my-repo

This will execute the following command behind the scenes:

git remote set-url origin git@github.com:bumblebee338/my-repo.git

The remote URL will be updated automatically, and you’ll see a confirmation message.

Why This Helps:

This custom function simplifies the process of updating remote URLs, especially if you frequently create new repositories or change their URLs. By automating the process, you reduce the likelihood of making mistakes and save time that can be better spent coding.

Conclusion:

Customizing your terminal with functions like this is a powerful way to streamline repetitive tasks in your development workflow. With just a few lines of code in your shell configuration, you can eliminate the need to manually type out remote URLs, making your work faster and more efficient.

By automating the setup for your GitHub repositories, you’re one step closer to a smoother, more productive coding experience.

--

--

Rahul Kumar

I'm a Frontend Developer. I have a strong passion for UI effects, animations, and creating intuitive, dynamic user experiences.