Mac下使用Git上传代码到Github仓库

来源:互联网 发布:洛丽塔电影知乎 编辑:程序博客网 时间:2024/05/21 07:02

本文目录

    • 本文目录
    • 开篇明志
    • 安装Git
    • 创建ssh文件
    • Github账号中添加Key
    • 创建版本库Repository
    • 参考资料
    • 常见异常

开篇明志

想把自己之前做的一些项目开源,本文将记录整个操作过程,直至成功提交代码(或者别的文件)到github。


安装Git

1、下载Git installer,地址:https://git-scm.com/download/mac
2、pkg包下载完成,双击安装。
3、打开终端,使用git version命令查看安装版本,能查看到就是安装成功了

iMac:~ anthony$ git versiongit version 2.11.0 (Apple Git-81)

创建.ssh文件

打开终端,输入以下命令来查看.ssh是否存在:

iMac:~ anthony$ cd ~/.ssh  -bash: cd: ~/.ssh: No such file or directory

如果你的结果如上面代码所示,那么说明.ssh文件夹不存在,
假设你在Github注册账号为: xxxx@xxx.com
Terminal中运行

$ ssh-keygen -t rsa -C xxxx@xxx.com 
//默认直接按 回车 就可以了Generating public/private rsa key pair.Enter file in which to save the key (/Users/anthony/.ssh/id_rsa): Created directory '/Users/anthony/.ssh'.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/anthony/.ssh/id_rsa.Your public key has been saved in /Users/anthony/.ssh/id_rsa.pub.The key fingerprint is:SHA256:dp2r3eAJCUqDTdZguODfPzUnp7CFv9b3CIhFZkQ/a/U xxxx@xxx.com The key's randomart image is:+---[RSA 2048]----+|     .o  .o      ||  . .. o . .     || . . .o . + o .  ||  . .=   + . = . ||   ...+ S.o =   E||    ...+o==+o.   ||      ...*+B+    ||        + +=.=.. ||         oo.+.o..|+----[SHA256]-----+

Github账号中添加Key

这里写图片描述
这里写图片描述
Title:xxx@xx.com
Key:打开你生成的id_rsa.pub文件【/Users/USER_NAME/.ssh/is_rsa.pub】,复制文件里面的Key内容。

可以通过cat 命令查看 id_rsa.pub 里面的key

cat ~/.ssh/id_rsa.pub 

这里写图片描述

如果添加Key成功的话,如下图所示,同时你也会在邮箱里收到一个提醒邮件,内容是你添加了一个Key.
这里写图片描述

创建版本库Repository

首先,返回到主页,www.github.com
这里写图片描述

进入到了 “Create a New Repository”页面:
这里写图片描述

紧接着按照以下步骤进行本地仓库的创建及代码上传。打开终端,输入以下命令:

$ touch README.md  //新建一个README文档,若上一步勾选了创建README.md,提交时导致冲突  $ git init //初始化本地仓库  $ git add README.md   //添加刚刚创建的README文档  $ git commit -m "你的注释...."   //提交到本地仓库,并写一些注释  $ git remote add origin git@github.com:yourname/xxxx.git  //连接远程仓库并建了一个名叫:origin的别名,当然可以为其他名字,但是origin一看就知道是别名,youname记得替换成你的用户名  $ git push -u origin master                              //将本地仓库的文件提交到别名为origin的地址的master分支下,-u为第一次提交,需要创建master分支,下次就不需要了 

也可以按照下面的方式输入:
create a new repository on the command line

//创建 README.md 文件, 并向里面写入`This Is My First Testing Description....`字符串。echo "# This Is My First Testing Description...." >> README.mdgit initgit add README.mdgit commit -m "first commit" //commit备注git remote add origin https://github.com/imthinktwice/TestRepository.gitgit push -u origin master

push an existing repository from the command line

git remote add origin https://github.com/imthinktwice/TestRepository.gitgit push -u origin master

这里写图片描述

参考资料:

《Adding a new SSH key to your GitHub account》

常见异常

1、在设置别名的时候,出现“fatal: remote origin already exists.”错误,说明该别名已经存在,可以另外建一个别名,或者使用“git remote rm origin”命令删除原来的别名,然后重新执行“git remote add origin git@github.com:yourname/xxxx.git”;

2、在提交的时候,出现“error: failed to push some refs to ‘git@github.com:xxx/xxx.git’ hint: Updates were rejected because the remote contains work that you do not have locally….”的错误,说明有冲突,远程仓库的版本比本地仓库的要信,所以要先进行更新,才能提交。使用“git pull git@github.com:xxx/xxx.git”命令进行更新,地址自己相应替换掉。

3.出现 “fatal: refusing to merge unrelated histories”
git pull origin branchname –allow-unrelated-histories
origin: 别名
branchname: 分支名称,一般为master

0 0