简体   繁体   中英

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. As I worked on my project I would commit to my local repository. Nothing new there with 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.

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?

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.

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.

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.

To achieve this I created a bare repository next to the webroot on my server. (By convention bare repositories are named 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.

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.

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

git pull

All changes are applied right to my website because they get pulled.

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