LearningHowToCode
LearningHowToCode

Reputation: 125

Why is Git committing to the wrong repository?

I am in Chapter 2 of Michael Hartl's Ruby on Rails tutorial and I typed the following into the command line:

$ git init 
$ git add .
$ git commit -m "Initial commit"
$  git remote add github https://github.com/themaktravels/demo_app.git

fatal: remote github already exists.

$ git push -u github master

Username: 
Password: 

To https://github.com/themaktravels/first_app.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/themaktravels/first_app.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the 
'Note about fast-forwards' section of 'git push --help' for details.

I saw that I got a fatal error before that noted that Github exists, but I thought this was okay because I had committed in Git earlier in a different repository. I noticed that when I $ git push -u Github master, the outcome was that git was trying to commit in the wrong repository (first_app.git) rather than the newly created repository (demo_app.git). Why is this happening?

Before trying to commit, I typed in the following:

$ cd ~/rails_projects
$ rails new demo_app
$ cd demo_app

and then edited my gem file and everything seemed fine. Until I ran into this git issue. Any suggestions? Thank you.

Upvotes: 1

Views: 1923

Answers (3)

Brian Campbell
Brian Campbell

Reputation: 332836

You already have a remote named "github", which points to first_app, so when you try to add the new one, which points to demo_app, it fails.

It looks like you were trying to initialize Git in a repository that already exists and already had a remote. Make sure you do this in a fresh directory.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 993095

The problem is this bit here:

$  git remote add github https://github.com/themaktravels/demo_app.git

fatal: remote github already exists.

If you already have a remote called github, then the git remote add command won't change the existing setting. Use git remote -v to see your current remotes and where they point to. I suspect you already have a remote called github that points to https://github.com/themaktravels/first_app.git.

Upvotes: 1

vgoff
vgoff

Reputation: 11313

To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again.

Like it is trying to tell you, the push is trying to cause a non-fast-forward update.

Upvotes: 0

Related Questions