Search code examples
gitgithubgit-remotegit-fetch

Git: Connect existing local repository to existing remote repository


This is probably pretty basic, but I wasn't able to figure it out yet:

I have a PHP project running on two servers, let's refer to those as Live and Staging
Both servers are running the same project obviously but with some changes.
The project was not on Github when It got into my hands, so that's what I am trying to do first now.

I managed to create a new remote repository on Github and connect the Live System to it.
(by adding the Github repo as 'origin' on Live)
git remote add origin https://github.com/path-to-repo/repo.git

So the Live System is currently on the masterbranch and up to date with origin/masterwhich has a history of 4 commits.

Now I am trying to connect the Github Repo on Staging as well

So I did a

git init
git remote add origin https://github.com/path-to-repo/repo.git
git remote -v

origin  https://github.com/path-to-repo/repo.git (fetch)
origin  https://github.com/path-to-repo/repo.git (push)

git fetch

Now when I do a git status I see that the repo is still on Initial commit and all files and folders are listed as untracked:

 root@staging-host:/var/www/html# git status
 On branch master

 Initial commit

 Untracked files:   (use "git add <file>..." to include in what will be
 committed)

         .htaccess
         README.md
         _index.html
         api/
         app/
         composer.json
         global/
         index.php
         package-lock.json
         package.json
         phpinfo.php
         system/
         vendor/
         view/

How can I check for local changes compared to the last commit in origin/master
I don't want to loose any of the local changes but also not to commit or push anything
I need to check the diff first before I decide file by file what to commit and what to reset


Solution

  • Your approach was almost correct. Take a look at your command:

    git remote add origin https://github.com/path-to-repo/repo.git
    

    This tries to add a remote repo as origin and fails, because that already exists.

    Run

    git remote -v
    

    You will see that it's your live repo. Name it differently, like

    git remote add staging https://github.com/path-to-repo/repo.git
    

    and then it should work. If origin is live and staging is staging, then pulling, pushing to staging is the following:

    git pull staging <branch>
    

    and

    git push staging <branch>
    

    respectively. If I were you, I would prefer origin to point to staging and another remote called live would point to live, because you will use staging more frequently.

    EDIT

    Apparently I have misunderstood the problem. Basically there is a repo hosted by GitHub and you want to use that for staging as well. You do not need to run

    git init
    

    nor to add remotes in that case. You will just need to clone the repo, like

    git clone https://github.com/path-to-repo/repo.git