Eventially I'm going to push my files from local repository to remote repository. Sounds interesting and exciting because that means someone else on the network can share my files and contribute their talents to the repository, to the project. Let's take a look at the picture below
figure 4.1
Obviously we need the command git push. But before using this command to push files to remote repo, I need to create a repo in remote Git site. There are many Git site on the network, such as Github, bitbucket as so on. You could choose anyone you perfer. To me I choose bitbucket here to take an example.
I won't give your any detail info about how to open the website, create an account and sign in untill we get to the step of creating a repo in the website. Let's see my screen when I'm about to create a repo on the site bitbucket.
figure 4.2
Then create repo by clicking the button 'Create repository' at the bottom. You can see the screen 'Repository Setup' as below..
figure 4.3
The example of command line will be shown out when you click the link "I have an existing project". Why should we choose to click this item rather than the other one "I'm starting from scratch", that's because we have done some steps in our previous episodes, such as create local repo, add and commit files to local repo, we don't need to start from scratch. Easy to understand, right? Before running the command instructions bitbucket provides as above picture shows us. Let's think about what the command git remote does. Run the command to check what you can see
git remote git remote -v
You'll see there's nothing return in your terminal if you've never used it to do anything yet.
For each remote repo you have a full URL to point to and you must use it in you command line when you're doing anything to a remote repo. But full URL is too long to remember and to type quickly, so we need an alias to take the place of an URL as well as we did in any programming language where we call variable. OK, now I'm going to create an alias for the URL of my project gittutorials on bitbucket. First I cd to my working directory and type the following command as you can see in figure 4.3
git remote add origin https://vjianwang@bitbucket.org/vjianwang/gittutorials.git
You can think this line as a definition and initialization for alias 'origin' which now is pointing to project tutorials on bitbucket. Don't worry about how to figure out what the URL is. Git remote site, here is bitbucket, will provide you once you create a repo on it. After running the command, type the git remote to see if any differences there from last running
figure 4.4
Don't be puzzled if you see the URL twice for fetch and push, because Git consider you might be a geek who want ot customize fetch and push to use different protocols. If you don't care about it just skip the point. Just remember to use a different alias next time in case you have another project to be pointed to. Of course if you insist to use the same alias, you can use the command remote rm to remove the previous URL pointing and remote add it again
git remote rm origingit remote add origin https://vjianwang@bitbucket.org/vjianwang/kawayi.git
or you can use remote rename to get retain the URLs pointing to project gittutorials
git remote rename origin newguygit remote add origin https://vjianwang@bitbucket.org/vjianwang/kawayi.git git remote -v
There would be in all 2 projects and 4 URL display
figure 4.5
Now we come back to use alias origin to point to project gittutorials and do the following push work. The push command as below
git push origin master
The command I use is a little bit different from the one bitbucket provides in figure 4.3 . The main difference is that I push only branch master since I don't have any branch else in my local repo and bitbucket's command push all your branches you have in your local repo. When I run the command above, system ask me to type password for my account on bitbucket and then it did the push process untill finish.
figure 4.6
As the picture above shown, a new branch was created and the push was happening between master in local to master in remote. Browse the bitbucket to check my project gittutorials.
figure 4.7
I'm very happy to see all 3 files in my local repo now are locating in the remote repo as we expect.