簡體   English   中英

Git:將現有的本地存儲庫連接到現有的遠程存儲庫

[英]Git: Connect existing local repository to existing remote repository

這可能是非常基本的,但我還無法弄清楚:

我有一個 PHP 項目在兩台服務器上運行,我們將它們稱為LiveStaging
兩台服務器顯然都在運行同一個項目,但有一些變化。
該項目在我手中時不在 Github 上,所以這就是我現在首先要做的事情。

我設法在 Github 上創建了一個新的遠程存儲庫,並將Live System 連接到它。
(通過將 Github 回購添加為Live上的“來源”)
git remote add origin https://github.com/path-to-repo/repo.git

所以 Live System 目前在master分支上並且與origin/master保持同步,它有 4 次提交的歷史。

現在我也在嘗試在Staging上連接 Github Repo

所以我做了一個

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

現在,當我執行 git 狀態時,我看到回購協議仍處於初始提交狀態,所有文件和文件夾都列為未跟蹤:

 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/

origin/master中的最后一次提交相比,我如何檢查本地更改
我不想丟失任何本地更改,但也不想提交或推送任何內容
在逐個文件決定提交什么和重置什么之前,我需要先檢查差異

你的方法幾乎是正確的。 看看你的命令:

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

這嘗試將遠程倉庫添加為源並失敗,因為它已經存在。

git remote -v

您會看到這是您的實時存儲庫。 換個名字,比如

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

然后它應該工作。 如果 origin 是 live 並且 staging 是 staging ,那么拉,推到 staging 如下:

git pull staging <branch>

git push staging <branch>

分別。 如果我是你,我會更喜歡 origin 指向 staging 而另一個名為 live 的遠程會指向 live,因為你會更頻繁地使用 staging。

編輯

顯然我誤解了這個問題。 基本上有一個由 GitHub 托管的存儲庫,您也希望將其用於暫存。 你不需要運行

git init

在這種情況下也不要添加遙控器。 您只需要克隆存儲庫,例如

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

可能有更好的方法,但這應該有效。

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

您可以重置您為檢查差異所做的提交

如果你有一個現有的本地倉庫和一個現有的遠程倉庫,你可以只設置遠程倉庫 url:

git remote set-url <name> <newurl> 

例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM