上传一个本地新项目到github

来源:互联网 发布:淘宝详情自动生成鲁班 编辑:程序博客网 时间:2024/06/05 08:04

源地址http://blog.csdn.net/chen861201/article/details/8472535


上一次的博客中写道了,创建项目的一个流程,这里还有一种情况就是当我当地有一个现成的文档,我想上传到github上面去怎么办。

其实这种情况github也已经为你想到了,大家可以看到当我们创建一个空的repos的时候上面会有两种情况给我们选择:

Create a new repository on the command line

touch README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin git@github.com:chenbinghuilove/python_redis.gitgit push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:xxx/python_redis.gitgit push -u origin master

这里我们就直接按照上面的例子来操作我们的文件:具体的做法这里我做了一个例子,是我从大师哪里下载的一个项目,然后我先传到我的github上面给大家共享时:

    root@zhou:~/.ssh# git clone git://github.com/xxx/redis-py.git      Cloning into 'redis-py'...      remote: Counting objects: 1906, done.      remote: Compressing objects: 100% (816/816), done.      remote: Total 1906 (delta 1278), reused 1690 (delta 1086)      Receiving objects: 100% (1906/1906), 316.57 KiB | 62 KiB/s, done.      Resolving deltas: 100% (1278/1278), done.      root@zhou:~/.ssh# ls      id_rsa  id_rsa.pub  known_hosts  Lovepython  Loveshell  redis-py  

可以看到redis-py是我从别人的github上面下载的项目,下面我想要上传到我的github上面,首先要做的是先删掉当前.git这个文件,因为它存储的是别人的github信息,下面是我的具体操作:

    root@zhou:~/.ssh/redis-py# ls -a      .   CHANGES  .gitignore  LICENSE      README.md  run_tests  tests      ..  .git     INSTALL     MANIFEST.in  redis      setup.py   .travis.yml      root@zhou:~/.ssh/redis-py# git init      Reinitialized existing Git repository in /root/.ssh/redis-py/.git/      root@zhou:~/.ssh/redis-py# git remote add origin git@github.com:xxx/python_redis.git      fatal: remote origin already exists.      root@zhou:~/.ssh/redis-py# rm -r .git  

这个时候需要做的就是重新初始化一个git文件,并且将github上面的项目git文件给重置上去:

    root@zhou:~/.ssh/redis-py# git init      Initialized empty Git repository in /root/.ssh/redis-py/.git/      root@zhou:~/.ssh/redis-py# git remote add origin git@github.com:xxx/python_redis.git  


如果报错:fatal: remote origin already exists.


则运行:

$ git remote rm origin$ git remote add origin git@github.com:xxx/python_redis.git $ git remote -vorigin    http://git.xxx.com/sqjy/sqjy_android.git (fetch)origin    http://git.xxx.com/sqjy/sqjy_android.git (push)

这时会发现链接是成功的

这时别以为就可以直接push到github上面了,还要需要我们按照以前加载新文件那样重新操作一遍:

root@zhou:~/.ssh/redis-py# git push -u origin master  Enter passphrase for key '/root/.ssh/id_rsa':   error: src refspec master does not match any.  error: failed to push some refs to 'git@github.com:xxx/python_redis.git'  root@zhou:~/.ssh/redis-py# ls  CHANGES  LICENSE      README.md  run_tests  tests  INSTALL  MANIFEST.in  redis      setup.py  root@zhou:~/.ssh/redis-py# ls -a  .   CHANGES  .gitignore  LICENSE      README.md  run_tests  tests  ..  .git     INSTALL     MANIFEST.in  redis      setup.py   .travis.yml  root@zhou:~/.ssh/redis-py# git add . 

    root@zhou:~/.ssh/redis-py# git commit -a -m "study"      [master (root-commit) 4181414] study       22 files changed, 5337 insertions(+)       create mode 100644 .gitignore       create mode 100644 .travis.yml       create mode 100644 CHANGES  

    root@zhou:~/.ssh/redis-py# git push -u origin master       Enter passphrase for key '/root/.ssh/id_rsa':       Counting objects: 26, done.      Delta compression using up to 2 threads.      Compressing objects: 100% (25/25), done.      Writing objects: 100% (26/26), 46.42 KiB, done.      Total 26 (delta 0), reused 0 (delta 0)      To git@github.com:xxx/python_redis.git       * [new branch]      master -> master  

看到这样的信息就可以证明你已经搞定了这一个项目。

实际最上面的操作:会上传到

python_redis

如果想用自己的名字则需要在github中创建一个repository,然后上传到这个项目中即可。


报错:

1. 在push的时候由于文件中有大型文件所以使用http协议传输会报错:

ZhouShuaitongs-iMac:sqwkt-1.0 ZST$ git push origin masterCounting objects: 2075, done.Delta compression using up to 4 threads.Compressing objects: 100% (1975/1975), done.error: RPC failed; result=22, HTTP code = 4132.02 MiB/s   fatal: The remote end hung up unexpectedlyWriting objects: 100% (2074/2074), 14.55 MiB | 8.07 MiB/s, done.Total 2074 (delta 278), reused 0 (delta 0)fatal: The remote end hung up unexpectedlyEverything up-to-date

中途传输会报错,处理这种情况就是用git协议传输,具体方法:

修改成git协议:

$ git remote rm origin$ git push origim masterfatal: 'origim' does not appear to be a git repositoryfatal: Could not read from remote repository.
使用git协议必须在你的git账户中增加你本机的SSH Key,所以会报错。

在自己的git账户中增加SSH Key,寻找本机的SSH Key:

$ ssh -vusage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]           [-D [bind_address:]port] [-e escape_char] [-F configfile]           [-I pkcs11] [-i identity_file]           [-L [bind_address:]port:host:hostport]           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]           [-R [bind_address:]port:host:hostport] [-S ctl_path]           [-W host:port] [-w local_tun[:remote_tun]]           [user@]hostname [command]

表示你本机已经安装了SSH,没有安装自己安装,然后找到ssh所在的目录:

 ssh-keygen
一直回车

Generating public/private rsa key pair.Enter file in which to save the key (/Users/ZST/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/ZST/.ssh/id_rsa.Your public key has been saved in /Users/ZST/.ssh/id_rsa.pub.The key fingerprint is:d7:b7:a1:f5:00:fd:3c:ff:69:4e:41:fe:fc:8c:d3:f2 ZST@ZhouShuaitongs-iMac.localThe key's randomart image is:+--[ RSA 2048]----+|                 ||             .   ||            . .. ||           . .oo ||        S . . =+o||         .   + *=||            . .o=||              ++=||              +*E|+-----------------+

找到了你的ssh key的目录,你的ssh key在id_rsa.pub里面,使用编辑器打开,复制里面的内容到自己的git的SSH Key位置保存即可。

然后在运行代码:

$ git push origin master
这样上传整个项目就不会报错了。

0 0
原创粉丝点击