4

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

git remote add godaddy $user1@foo.com:~/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".

6 Answers6

4

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 $user1@foo.com:root.git else git remote add godaddy $user1@foo.com:root.git fi

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

    git remote add godaddy $user1@foo.com:root.git || git remote set-url godaddy $user1@foo.com:~/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 $user1@foo.com:root.git 2>/dev/null || true
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
3

git remote add godaddy $user1@foo.com:~/root.git

When you execute this command it simply updates your .git/config file with the given url. In your case you already have this remote name in your file.

In this case you can do one of the following depend on your needs?


remote add

# Add new (second) origin
git remote add godaddy1 $user1@foo.com:~/root.git 

remote set-url

# replace the current origin
git remote set-url godaddy $user1@foo.com:~/root.git

List all remotes

# List all remotes
git remote -v
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

Git includes the possibility to send your commits to different remotes. You can change these remotes, for example if your main development server changes. Usually you setup a remote and you won't have to change it again.

If you want to send your cleaned code to the server, make sure to

  1. Commit the Code git commit -am "Cleaned the code up"
  2. Push the changes to the server git push godaddy master

If you're new to git, you probably won't need git-remote.

0xpentix
  • 732
  • 9
  • 22
0

This is a general solution:

function addGitRepos(){
    local name1=godaddy
    local url1=$user1@foo.com:root.git    
    if git remote | grep -w $name ; then
        git remote set-url $name1 $url1
    else
        git remote add $name1 $url1
    fi
}

Later if you need more repos, you can create arrays and a loop to iterate through it.

This is just a starting point.

arc.slate .0
  • 428
  • 3
  • 8
0
git remote remove godaddy 2>&-
git remote add godaddy $user1@foo.com:~/root.git

2>&- is like 2>/dev/null but it's shorter to type for commands that behave well with a closed error log, which git does.

The other commands here will keep any other remote configuration you might have, which probably won't be a problem, but...

jthill
  • 55,082
  • 5
  • 77
  • 137
  • I see 2>/dev/null a bunch, this basically sais send the error stream in to outer space right ? –  Apr 02 '16 at 00:37
0

A script-failsafe, idempotent way to do it:

git remote add origin git@example.com:group/project || git remote set-url origin git@example.com:group/project

Explanation

  • In case origin does not exist yet in this repository, it is set up with the git remote add ... command.
  • In case origin already exists in this repository, the first first command (git remote add ...) fails gracefully. The second command (git remote set-url ...) is then executed to update the repository URL.
Abdull
  • 26,371
  • 26
  • 130
  • 172