您的位置:首页 > 编程语言

将本地的git项目推送到gitHub中的操作

2017-04-26 10:46 344 查看
如何将你本地的Git项目推送到你的远程中,先将一些我们现在的场景,首先我在本地已经有了一个git的工程,但是是在本地的,所以我们需要的是将这个工程发送到远程github中

//首先,先看本地是否存在SSH的KEY

ls -al ~/.ssh

drwx------   5 lmzqm  staff   170  3 23 10:40 .

drwxr-xr-x+ 48 lmzqm  staff  1632  4 26 09:46 ..

-rw-------   1 lmzqm  staff  3247  3 23 10:40 id_rsa

-rw-r--r--   1 lmzqm  staff   732  3 23 10:40 id_rsa.pub

-rw-r--r--   1 lmzqm  staff   996  4  5 12:36 known_hosts

这里的id_rsa.pub表示你是有key的

好的,直接复制你的key:
pbcopy < ~/.ssh/id_rsa.pub

将你的key复制到剪贴板中,然后直接在git中的setting中创建一个新的ssh key 然后复制进去就可以了

如果没的话,就得跟着节奏一起来 https://help.github.com/articles/connecting-to-github-with-ssh/
这样的话或许会减少一些操作:

接下来,开始吧!

首先现在远程的github先创建一个工程,然后获取git的路径 

eg:https://github.com/lmzqm/springboottest.git

ssh路径:git@github.com:lmzqm/springboottest.git

//添加对远程库的关联操作
git remote add origin git@github.com:lmzqm/springboottest.git

//第一次推送到mater分支上
git push -u origin master //将代码上传,并且和关联

但是这里你可能会遇到一些问题:

首先这里的文件提交发生了一些错误:
➜  test git:(master) ✗ git push -u origin master

error: src refspec master does not match any.

error: failed to push some refs to 'git@github.com:lmzqm/springboottest.git'

这是因为这是一个空的文件夹,所以这里我们需要的是首先创建一个文件,然后提交操作就可以了

touch README

git add README

git commit -m 'first commit'

git push -u origin master

上面处理的链接: http://www.open-open.com/lib/view/open1366080269265.html
这样就可以进行添加操作,当然首先你得有SSH的KEY才能完成这一步,那么如何来创建SSHkey呢?

➜  test git:(master) git push -u origin master

Permission denied (publickey).

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

➜  test git:(master) git push -u origin master

To github.com:lmzqm/springboottest.git

 ! [rejected]        master -> master (fetch first)

error: failed to push some refs to 'git@github.com:lmzqm/springboottest.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

悲剧,又遇到错误了,好吧!继续破解它

上面说的大体意思是叫你要先拉取远程的信息,然后在进行推送的操作

好吧!那就先拉取把!

test git:(master) git pull origin master

warning: no common commits

remote: Counting objects: 5, done.

remote: Compressing objects: 100% (4/4), done.

Unpacking objects: 100% (5/5), done.

remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0

From github.com:lmzqm/springboottest

 * branch            master     -> FETCH_HEAD

 * [new branch]      master     -> origin/master

fatal: refusing to merge unrelated histories

我擦!又出现错误!

好吧!先看看到底是什么错误:

fatal: refusing to merge unrelated histories

致命的:拒绝合并不相关的历史

看的很清楚了吧!不允许合并,没有共同祖先的分支

--allow-unrelated-histories

  By default, git merge command refuses to merge histories that do not share a common ancestor.

需要加上这个就可以搞定了

git pull --allow-unrelated-histories origin master

上面处理的链接:https://github.com/doggy8088/Learn-Git-in-30-days/issues/31

From github.com:lmzqm/springboottest

 * branch            master     -> FETCH_HEAD

Auto-merging .gitignore

CONFLICT (add/add): Merge conflict in .gitignore

Automatic merge failed; fix conflicts and then commit the result.

这样就可以将远程的项目拉取过来了,报冲突了,够倒霉的
git status 看看是那些文件冲突了
Unmerged paths:

  (use "git add <file>..." to mark resolution)

        both added:      .gitignore

还行,就一个
vim .gitignore 处理冲突

然后 git commit -a -m '处理冲突' 

-a 是all的意思

-m 是 message的意思

重新提交

然后就可以执行:
git push -u origin master  //这里的-u主要是关联

➜  test git:(master) git push -u origin master

Counting objects: 70, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (55/55), done.

Writing objects: 100% (70/70), 393.54 KiB | 0 bytes/s, done.

Total 70 (delta 3), reused 0 (delta 0)

remote: Resolving deltas: 100% (3/3), done.

To github.com:lmzqm/springboottest.git

   9bf9a98..7176128  master -> master

Branch master set up to track remote branch master from origin.

这里就说明已经成功了,哈哈

然后去你的github的工程中查看 非常的满意
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git