简体   繁体   中英

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 master branch and up to date with origin/master which 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

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

Probably there is a better way but this should work.

  1. git add.
  2. git commit -m 'tmp'
  3. git diff HEAD origin/master
  4. git reset --soft HEAD~1

you can just reset commit you did for checking the difference

If you have an existing local repo and an existing remote, you can just set the remote url:

git remote set-url <name> <newurl> 

example:

git remote set-url origin http://github.com/myproject.git 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM