mac使用git管理Github

前言

学习笔记 -- 使用git管理Github

正文

最新版本Xcode,在terminal下可以发现git已经被安装。

MacBookPro:~ huwenkuan$ git --version
...
git version 2.9.3 (Apple Git-75)

需要知道的是本地的git仓库和Github服务器之间是通过ssh加密的。

在终端执行

MacBookPro:~ huwenkuan$ ssh -v
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-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]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

表示Mac已经安装了ssh

1 接下来是创建SSH Key
(这里直接借鉴下大牛的代码)

QQ20170217-2@2x.png

接着可以在用户主目录里找到.ssh目录,里面有github_rsa和github_rsa.pub两个文件,这两个就是SSH Key的秘钥对

MacBookPro:~ huwenkuan$ cd .ssh
MacBookPro:.ssh huwenkuan$ ls
github_rsa  github_rsa.pub  known_hosts

2 在Github设置ssh key

登陆Github, “Settings”->SSH keys->Add SSH key

QQ20170217-3@2x.png
QQ20170217-4@2x.png

这里我已经设置过了

title:可以随便填名字

key:在Key文本框里粘贴github_rsa.pub文件的内容

点击add key 配置完成

3 测试本地是否和Github连接上

MacBookPro:.ssh huwenkuan$ ssh -T git@github.com
Hi yun591855479! You've successfully authenticated, but GitHub does not provide shell access.

第一次链接Github,会有一个确认,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

一 使用git在本地建立的项目更新到Github
MacBookPro:github仓库 huwenkuan$ mkdir hellogithub
MacBookPro:github仓库 huwenkuan$ cd hellogithub/
MacBookPro:hellogithub huwenkuan$ ls -ah
.   ..
MacBookPro:hellogithub huwenkuan$ git init
Initialized empty Git repository in /Users/huwenkuan/Documents/github仓库/hellogithub/.git/
MacBookPro:hellogithub huwenkuan$ ls -a
.   ..  .git
MacBookPro:hellogithub huwenkuan$ touch newfile
MacBookPro:hellogithub huwenkuan$ ls
newfile
MacBookPro:hellogithub huwenkuan$ git add newfile
MacBookPro:hellogithub huwenkuan$ git commit -m 'first commit'
[master (root-commit) e1e2cb8] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfile

配置本地用户和邮箱

用户名邮箱作用 : 我们需要设置一个用户名 和 邮箱, 这是用来上传本地仓库到GitHub中, 在GitHub中显示代码上传者;
使用命令 :

MacBookPro:hellogithub huwenkuan$ git config --global user.name xxx //设置用户名 
MacBookPro:hellogithub huwenkuan$ git config --global user.email xxx@xx.com //设置邮箱

为了把本地库推到远程服务器,首先需要在Github上面也建一个项目

7FA56C39-2AEC-42B8-8910-5D686C2F5D80.png

在Repository name填上我们的项目名字,description随便填,别的大家可以暂时参照我的选择勾选。

然后会生成项目

QQ20170217-5@2x.png

(其实他有提示你接下来该怎么做)

然后把远程项目和本地库关联

MacBookPro:hellogithub huwenkuan$ git remote add origin git@github.com:yun591855479/hellogithub.git
MacBookPro:hellogithub huwenkuan$ git push -u origin master

<tips:
“origin”经常被用作远端仓库别名,就因为 git clone 默认用它作为克隆自的链接的别名。
“git remote add xxx 远程服务器仓库地址aaa”:
该方法是关联本地与服务器的仓库,为服务器的仓库取个别名叫xxx,一般情况下该别名就叫origin,此时他关联的就是远程服务器地址aaa这个仓库。
而删除某个别名用:“git remote rm tt” 删除现存的某个别名tt,也就是断开别名tt所对应的远程仓库的连接,不再需要它了、项目已经没了,等等。
例如:git remote rm origin

可能会报以下错

To github.com:yun591855479/hellogithub.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:yun591855479/hellogithub.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.

这是本地没有README文件造成的
现在去将该文件pull下来

MacBookPro:hellogithub huwenkuan$ git pull --rebase origin master
warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From https://github.com/yun591855479/hellogithub
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: first commit

更新github代码仓库到本地。
使用下面的命令即可更新代码

git pull origin master

然后查看是否已经同步下来

MacBookPro:hellogithub huwenkuan$ ls
LICENSE     README.md   newfile

然后再执行

MacBookPro:hellogithub huwenkuan$ git push -u origin master

<tips:想要与他人分享你牛鼻的提交,你需要将改动推送到远端仓库。 执行 git push [alias] [branch],就会将你的 [branch] 分支推送成为 [alias] 远端上的 [branch] 分支。>

遇到的问题

当我再次关联时会报错

MacBookPro:hellogithub huwenkuan$ git remote add origin git@github.com:yun591855479/hellogithub.git
fatal: remote origin already exists.

我们需要移除

MacBookPro:hellogithub huwenkuan$ git remote rm origin

再执行

MacBookPro:hellogithub huwenkuan$ git remote add origin https://github.com/yun591855479/hellogithub.git


MacBookPro:hellogithub huwenkuan$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To https://github.com/yun591855479/hellogithub.git
   546c5fe..f355d1c  master -> master
Branch master set up to track remote branch master from origin.
二 直接将github上的仓库clone下来

直接在gitHub上创建仓库,本地不创建。
在终端输入’git clone’命令就能下载github的代码了:
例如:我们在github上创建了一个仓库test2
命令行执行:

MacBookPro:github仓库 huwenkuan$ git clone https://github.com/yun591855479/test2.git
Cloning into 'test2'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.
MacBookPro:github仓库 huwenkuan$ ls

此时就能查看到test2

MacBookPro:github仓库 huwenkuan$ cd test2
MacBookPro:test2 huwenkuan$ ls
LICENSE     README.md
补充

在下载下来的下面做一些修改并提交。
$ 进入刚下载的代码仓库:cd example/
$ 修改”README.md”文件:echo "hello github" > README.md
$ 添加修改的代码:git add README.md
$ 查看修改的状态:git status
$ 填写提交信息:git commit -m "add test message"
$ 正式提交代码:git push origin master

MacBookPro:hellogithub huwenkuan$ echo "#hello github">> README.md
MacBookPro:hellogithub huwenkuan$ vim README.md 
MacBookPro:hellogithub huwenkuan$ cat README.md 
# hellogithub
My pro first time visit you!
#hello github

我们可以通过vim查看README.md的内容,也可以通过cat 来直接查看。

参考文献:

github常见操作和常见错误!
Git版本控制软件结合GitHub从入门到精通常用命令学习手册
mac使用git管理Github
如何解决failed to push some refs to git
git pull和git pull --rebase的不同

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容