前面笔者介绍了自己通过饥人谷网课学习到了本地仓库建立以及相关的知识。今天就来分享自己如何实现远程仓库的推送。
ps:笔者强烈建议使用sshkey进行配置而非https,因为ssh不需要每次都输入密码,比较方便。
远程仓库
其实也就是在网上建立一个属于你自己的仓库,以防止你的代码丢失或者方便你同时在家里和公司进行工作。git官网上是如此分析他的好处与坏处的。
The Pros
The pros of file-based repositories are that they’re simple and they use existing file permissions and network access. If you already have a shared filesystem to which your whole team has access, setting up a repository is very easy. You stick the bare repository copy somewhere everyone has shared access to and set the read/write permissions as you would for any other shared directory. We’ll discuss how to export a bare repository copy for this purpose in Getting Git on a Server.
This is also a nice option for quickly grabbing work from someone else’s working repository. If you and a co-worker are working on the same project and they want you to check something out, running a command likegit pull /home/john/project
is often easier than them pushing to a remote server and you subsequently fetching from it.
The Cons
The cons of this method are that shared access is generally more difficult to set up and reach from multiple locations than basic network access. If you want to push from your laptop when you’re at home, you have to mount the remote disk, which can be difficult and slow compared to network-based access.
It’s important to mention that this isn’t necessarily the fastest option if you’re using a shared mount of some kind. A local repository is fast only if you have fast access to the data. A repository on NFS is often slower than the repository over SSH on the same server, allowing Git to run off local disks on each system.
Finally, this protocol does not protect the repository against accidental damage. Every user has full shell access to the “remote” directory, and there is nothing preventing them from changing or removing internal Git files and corrupting the repository.
可以看出,虽然他的确上传的时候比较慢一些,以及如果网站遭到攻击可能会导致代码流失,但是只要你同时本地和远程仓库都保存你的代码,以及选择一个比较好的远程仓库,总体而言还是利大于弊,可以节省很多时间。
推到哪里
这个仓库是github,相信无论菜鸟或者老手都肯定非常清楚这个网站了,因为他和我们工作一点一滴息息相关。
GitHub is an American company that provides hosting for software development version control using Git. It is a subsidiary of Microsoft, which acquired the company in 2018 for $7.5 billion.[4] It offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features. It provides access control and several collaboration features such as bug tracking, feature requests, task management, and wikis for every project.[5]
GitHub offers plans for free, professional, and enterprise accounts.[6] Free GitHub accounts are commonly used to host open source projects.[7] As of January 2019, GitHub offers unlimited private repositories to all plans, including free accounts.[8] As of May 2019, GitHub reports having over 37 million users[9] and more than 100 million repositories[10] (including at least 28 million public repositories),[11] making it the largest host of source code in the world.[12]
—————————— wikipedia
以上是来自维基百科的解释,简而言之是一个用户多,内容多,覆盖地区多的it网站,这次的推送也是需要推到这个网站上面,所以需要先行注册一个账号。这个不在这里赘述。
如何操作
言归正传,笔者选用的是ssh key的方式上传代码,首先打开github创建一个仓库。
创建好后,会看见这样一个界面,然后将笔者画红色框框的代码复制粘贴即可。
注意这里最好是通过ssh key进行上传,因为这样不用每次输密码比较方便。
注意!!!
这里是要先建立自己的本地仓库才可以上传,如果后面有什么更改的地方直接在commit之后
git push
即可,非常方便快捷。