Mac 下git&github 相关配置

去官网下载=最新版本git — git最新版本
在github上注册免费账户—github免费账户

安装git&生成.ssh 文件

Mac 上的安装很简单 直接拖到application应用程序就可以了。
主要是配置问题。
打开终端 Terminal
输入$cd ~/.ssh
如果存在就会直接跳转到文件目录。
不存在就直接创建 ssh-keygen -trsa -C ***@gmail.com (注册github时的邮箱)
过程如下:
fujindongdeMacBook-Pro:~ fujindongwhichgit/usr/local/bin/gitfujindongdeMacBookPro: fujindong cd ~/.ssh
-bash: cd: /Users/fujindong/.ssh: No such file or directory
fujindongdeMacBook-Pro:~ fujindong$ ssh-keygen -trsa -C ***@gmail.com //创建
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fujindong/.ssh/id_rsa):
Created directory ‘/Users/fujindong/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/fujindong/.ssh/id_rsa.
Your public key has been saved in /Users/fujindong/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ExasY+LAHxfnc+BSrbt26v+abiaASQHG8mEF9JhTxLo [email protected]
The key’s randomart image is:
+—[RSA 2048]—-+
| o=*+ … |
|..+=o . =.. |
| =+o.. Boo |
| =.+ *.=.. |
| * B oS+ |
| E = . .. |
| . . |
| + +. |
| ooO=o. |
+—-[SHA256]—–+
那么如何看到这个文件夹在哪儿呢,

显示mac中的隐藏文件

首先这个文件时隐藏的 在Terminal 中输入defaults write com.apple.Finder AppleShowAllFiles YES 显示隐藏文件。关闭显示隐藏文件直接把YES 改成 NO 回车就可以了。
然后 ls 查看目录下的文件。
moreidrsa.pubfujindongdeMacBookPro: fujindong defaults write com.apple.Finder AppleShowAllFiles YES
fujindongdeMacBook-Pro:~ fujindongcd /.sshfujindongdeMacBookPro:.sshfujindong ls
id_rsa id_rsa.pub
fujindongdeMacBook-Pro:.ssh fujindong$ more id_rsa.pub
ssh-rsa AAA*********************@gmail.com

在github上配置ssh keys&检测连接状态

然后将 ssh-rsa 的公钥内容复制到 github personal settings 的SSH keys 中
注意公钥 包含开头 ssh-rsa

Mac 下git&github 相关配置_第1张图片

设置本地git信息
$ssh [email protected]

fujindongdeMacBook-Pro:.ssh fujindong$ ssh [email protected]
The authenticity of host ‘github.com (*)’ can’t be established.
RSA key fingerprint is SHA256:nT*****************.
Are you sure you want to continue connecting (yes/no)? yes //输入yes 设置本地代码库的信息。
Warning: Permanently added ‘github.com,**’ (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi ScottFJD! You’ve successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
fujindongdeMacBook-Pro:.ssh fujindong$

设置本地git个人信息:
gitconfigglobaluser.namenamegit config —-global user.email”email”

检测本地代码库与github账户有没有连接,
$ssh -T [email protected]

fujindongdeMacBook-Pro:.ssh fujindongsshTgit@github.comHiScottFJD!Youvesuccessfullyauthenticated,butGitHubdoesnotprovideshellaccess.fujindongdeMacBookPro:.sshfujindong

git 基本操作

在本地创建一个文件 Terminal 到文件目录下
$git init
然后git 的基本操作都一样

fujindongdeMacBook-Pro:~ fujindongcdAndroidStar/fujindongdeMacBookPro:AndroidStarfujindong ls
gittest
fujindongdeMacBook-Pro:AndroidStar fujindongcdgittest/fujindongdeMacBookPro:gittestfujindong git init
Initialized empty Git repository in /Users/fujindong/AndroidStar/gittest/.git/
fujindongdeMacBook-Pro:gittest fujindong$

提交代码到GitHub仓库

首先在GitHub上建好仓库这个很简单,就不截图说明了。
Mac 下git&github 相关配置_第2张图片
之间我们已经配置过SSH了,也检测过连接状态($ssh -T [email protected])下面就是在本地建仓并上传至github仓库

git init //初始化本地仓库
git add . //添加代码到提交区
git commit -m "代码提交" //将代码提交到本地仓库
git git remote add origin git@github.com:ScottFJD/SFPermission.git //连接远程仓库并建一个叫origin的别名
git pull --rebase git@github.com:ScottFJD/SFPermission.git //这个git pull --rebase 详细说明一下,这里表示把你的本地当前分支里的每个提交(commit)取消掉,并且把它们临时 保存为补丁(patch)(这些补丁放到".git/rebase"目录中),然后把本地当前分支更新 为最新的"origin"分支,最后把保存的这些补丁应用到本地当前分支上。
git push -u origin master //将本地仓库的代码提交到origin的master的分支下。

在Android Studio 中的代码提交参考这篇文章:Android Studio 中Git 应用

技巧:
点击屏幕顶部的“Finder”弹出下拉菜单。
点击“服务”菜单项弹出子菜单。
点击子菜单中的“服务偏好设置”。
点击弹出的服务偏好设置窗口中的“服务”并勾选“新建位于文件夹位置的终端窗口”;点击该项还可以为该操作设置快捷键。
关闭设置窗口后右键单击文件夹,点击弹出的菜单中的“服务”菜单项弹出子菜单,选择其中的“新建位于文件夹位置的终端窗口”菜单项就会打开终端,并且终端的当前工作目录就是文件夹的路径。一定要点中文件夹,右击空白地方弹出的右键菜单是不会有“服务”项的。

之前的这些设置都是以SSH方式连接远端,并且设置了蜜钥,这样就可以在不输入用户名和密码的情况下就可以安全地传输数据;对于http协议来说是不可能的,每一个连接都是需要用户名和密码的。
最新的版本的git 在使用https方式clone代码的时候不需要去记住密码
git clone http://yourname:password@远程仓库地址
就可以了 之后就不用输入密码了。我在win10 和 Mac 上测试都是这样的结果
有关这个具体操作,可以看git 的官方说明
https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%87%AD%E8%AF%81%E5%AD%98%E5%82%A8#_credential_caching

如果远程仓库的代码是有分支的,可以切换分支
git branch -a //查看分支
git checkout [name]

如果有依赖的子库。

执行:
git submodule init //初始化子模块,只在首次建仓时运行一次
git submodule update //每次更新或切换分支后运行一下

可参考:http://blog.csdn.net/ithomer/article/details/7529022

关注我的微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。
Mac 下git&github 相关配置_第3张图片

你可能感兴趣的:(Android,git)