简体   繁体   English

如何将已建立的本地git存储库链接到新的远程存储库并将所有文件推送到远程?

[英]How do I link an already established local git repository to a new remote repository and push all files to remote?

My scenario is that I have created a local git repository using SourceTree. 我的场景是我使用SourceTree创建了一个本地git存储库。 As I worked on my project I would commit to my local repository. 当我处理我的项目时,我会提交到我的本地存储库。 Nothing new there with Git. Git没有什么新东西。 So I have a well established Git repository now that I would like to put on a remote server and do the normal Git push/pull to a remote repository so that my website's files will be updated by my push. 所以我现在有一个完善的Git存储库,我想放在远程服务器上并执行正常的Git推/拉到远程存储库,以便我的网站的文件将通过我的推送进行更新。

How can I create a remote repository that also includes all of my projects folders so that I can push from my local repository and the remote repository will get updated with the new files/folders I create in my project locally? 如何创建一个远程存储库,其中还包括我的所有项目文件夹,以便我可以从本地存储库推送,远程存储库将使用我在本地项目中创建的新文件/文件夹进行更新? How does Git handle transferring files? Git如何处理传输文件?

As of right now, I have tried creating a new repository on the remote server and pushing my local repository to it. 截至目前,我已经尝试在远程服务器上创建一个新的存储库并将我的本地存储库推送到它。 It successfully pushes to the repository (updates commits), BUT I can't get the actual files (.html, .php, .js, etc.) to the server through the push. 它成功地推送到存储库(更新提交),但我无法通过推送将实际文件(.html,.php,.js等)提供给服务器。

You need to first add remote repository to your existing repository. 您需要先将远程存储库添加到现有存储库中。

git remote add origin http://your/repository/url

Then you have to stage and commit all your changes. 然后,您必须暂存并提交所有更改。 (optional) (可选的)

git commit -a -m "Some comment"

Then you can push by following command 然后你可以通过以下命令推送

git push origin master

This will create a master branch in your remote repository and your changes will be pushed there. 这将在您的远程存储库中创建一个主分支,您的更改将被推送到那里。 If you are getting some message that you need to pull before you push then you can force your push by 如果你在推动之前得到一些你需要拉的消息,那么你可以强制推动

git push -f origin master

This will forcefully push your local changes. 这将强制推动您的本地更改。 However I'll not recommend pushing forcefully every time, unless you know what you are doing. 但是我不建议每次都强行推,除非你知道你在做什么。

I apologize that my request was not well stated. 我很抱歉我的要求没有得到很好的陈述。 Even though I use git daily at work, I had never set up repositories before on a remote server. 即使我每天都在工作中使用git,我也从未在远程服务器上设置存储库。

What I was really after was how to properly set up a repository on a remote server that allowed me to push to it and then pull into the web root folder. 我真正想要的是如何在远程服务器上正确设置存储库,允许我推送到它,然后进入Web根文件夹。

To achieve this I created a bare repository next to the webroot on my server. 为此,我在服务器上的webroot旁边创建了一个裸存储库。 (By convention bare repositories are named anynamehere.git) (按照惯例,裸存储库名为anynamehere.git)

mkdir <anynamehere>.git
cd <anynamehere>.git
git init --bare

Then, in my local repository I added the remote. 然后,在我的本地存储库中,我添加了远程。

git remote add <name> <url>

Next, I did an upstream push of my local repository branches to get them into the remote repository. 接下来,我对我的本地存储库分支进行了上游推送,以使它们进入远程存储库。

git push -u <remote> <branch>

Now that my remote correctly had my repository I could clone it into the web root folder. 现在我的遥控器正确拥有了我的存储库,我可以将它克隆到web根文件夹中。

git clone /~<username>/public_html/<anynamehere>.git

"anynamehere" was the same name as my web root folder, so when the clone was done I just went into the folder and set the branch I wanted to be showing as my webpage. “anynamehere”与我的web根文件夹名称相同,所以当克隆完成后我就进入文件夹并设置我想要显示为我的网页的分支。

cd <webroot>
git checkout <branch>

Bingo! 答对了! Now my website displays whatever branch I'm on. 现在我的网站显示我所在的任何分支。 When I want to make changes to the site, I just push from my local, ssh into my web root and 当我想对网站进行更改时,我只是从我的本地,ssh推送到我的网站根目录

git pull

All changes are applied right to my website because they get pulled. 所有更改都适用于我的网站,因为它们被拉了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 git push 将本地文件推送到远程 github 存储库? - How do I use git push to push local files to a remote github repository? 如何将新的本地分支推送到远程 Git 存储库并跟踪它? - How do I push a new local branch to a remote Git repository and track it too? Visual Studio Code - 连接到远程 Git 存储库并将本地文件推送到新的远程存储库 - Visual Studio Code - Connect to a remote Git repository and PUSH local files to a new remote repository Git将所有分支的本地存储库推送到远程存储库 - Git push local repository with all branches as-is to remote repository 如何将git存储库中的文件推送到所有远程分支? - how to push in git repository a files to all remote branches? 如何使用 Git 对远程存储库进行初始推送? - How do I do an initial push to a remote repository with Git? 无法将文件推送到本地 Git 服务器上的远程 git 存储库 - Not able to push files to remote git repository on local Git server 如何将修改后的提交推送到远程 Git 存储库? - How do I push amended commit to the remote Git repository? &#39;git push&#39; 上已经存在远程源到新存储库 - Remote origin already exists on 'git push' to a new repository git:删除本地分支上的文件后,推送到远程存储库 - git: push to remote repository, after deleting files on local branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM