Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force the creation of a remote repository?

Tags:

git

bash

I'm jus trying to clean up my code and eliminate errors. If I run:

git remote add godaddy [email protected]:~/root.git

I get

fatal: remote godaddy already exists.

I know it exists, but I just want it to ignore it and create it again.

For example you can suppress errors for mkdir with the -p

I checked here but could not find anything:

https://git-scm.com/docs/git-remote

This is not a duplicate as I'm looking for a command line option to supress the "error".


1 Answers

You can't tell Git to suppress the error, if it already exists you either need to remove it and re-add it, or update its URL. Suppressing the error for mkdir is OK, because if it exists you don't need to make it. With a Git remote that's not true, because the existing one might have a different URL, so just ignoring the command isn't OK if it fails.

Assuming this is some script that you are trying to change (otherwise if it's a manual process on the command line then just ignore the error and get on with something more important) there are two simple solutions I can see:

  1. check for the remote first, and either update it or add it, as appropriate:

    if git remote | grep -w godaddy ; then git remote set-url godaddy [email protected]:root.git else git remote add godaddy [email protected]:root.git fi

  2. just try adding it and if that fails then update the URL:

    git remote add godaddy [email protected]:root.git || git remote set-url godaddy [email protected]:~/root.git

Another option would be to redirect stderr to /dev/null and make the command return true even git exits with an error, but that's probably not a good idea:

git remote add godaddy [email protected]:root.git 2>/dev/null || true
like image 122
Jonathan Wakely Avatar answered Sep 20 '25 19:09

Jonathan Wakely