实用Linux

git 添加github远程仓库

2018-11-07  本文已影响3人  黄一倚

如果是第一次连接github,需要添加SSH KEY

添加SSHKEY

huanyu@ubuntu:~/.ssh$ ssh-keygen

需要注意的是,要进入到 .ssh 目录下,中间要输入key的名称,就按图中的来就可以

huanyu@ubuntu:~/.ssh$ ssh-add id_rsa
huanyu@ubuntu:~/.ssh$ cat id_rsa.pub

把下面这一长串码复制

在github上找到setting,点击


进入 SSH and GPG keys

image.png

把复制的码粘贴到下面这里

huanyu@ubuntu:~/.ssh$ ssh -T git@github.com

看到下面的信息说明配置成功了

把项目添加到github上

不要选那个勾,直接创建仓库

创建好之后它会告诉你怎么把本地的项目添加到github上

git init

把你的项目添加到git仓库中

git add .   //  . 表示当前目录下的所有文件夹

git commit -m "first commit"  //这个是注释说明
git remote add origin git@github.com:HarryHq/homework.git
git push -u origin master

第二种情况

在github上创建新仓库的时候,勾选了初始化README文件
执行到下面语句的时候会出现不一样的情况

huanyu@ubuntu:~/Downloads/love/love$ git push -u origin master
To github.com:HarryHq/Lovepage.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:HarryHq/Lovepage.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.

hint: (e.g., 'git pull ...') before pushing again.
按照提示,执行以下代码

huanyu@ubuntu:~/Downloads/love/love$ git pull
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:HarryHq/Lovepage
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

git branch --set-upstream-to=origin/<branch> master
按照提示,执行以下代码

huanyu@ubuntu:~/Downloads/love/love$ git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'.

现在可以试一下上传到github了

huanyu@ubuntu:~/Downloads/love/love$ git pull
fatal: refusing to merge unrelated histories

原因是: 两个 根本不相干的 git 库, 一个是本地库, 一个是远端库, 然后本地要去推送到远端, 远端觉得这个本地库跟自己不相干, 所以告知无法合并

huanyu@ubuntu:~/Downloads/love/love$ git pull origin master --allow-unrelated-histories
From github.com:HarryHq/Lovepage
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md

然后再把本地项目上传到github上,这时就成功了

huanyu@ubuntu:~/Downloads/love/love$ git push -u origin master
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (17/17), 64.86 KiB | 5.40 MiB/s, done.
Total 17 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:HarryHq/Lovepage.git
   d899f47..3719996  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

因为第二种操作比较麻烦,所以建议用第一种办法。

上一篇 下一篇

猜你喜欢

热点阅读