1、连接授权
授权的两种方式:HTTPS 和 SSH
- HTTPS 推荐方式,安全、不受防火墙限制
使用HTTPS方式在Git中保存GitHub账号密码的步骤:
1> 确认是否安装Git,以及Git的版本(Git 1.7.10及以上才能使用 osxkeychain credential helper )
2> 打开控制台,验证是否已经安装 osxkeychain credential helper
3> 输入命令:$git credential-osxkeychain (出现:Usage: git credential-osxkeychain <get|store|erase> ,说明已经安装,直接输入)
4> 安装 osxkeychain credential helper
4.1 输入命令下载 $curl -s -O https://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain
4.2 输入命令授权安装 $chmod u+x git-credential-osxkeychain
4.3 安装到Git所在的目录 $sudo mv git-credential-osxkeychain "$(dirname $(which git))/git-credential-osxkeychain"
5> 通过命令行让 Git 使用 osxkeychain credential helper $git config --global credential.helper osxkeychain
2、创建本地Fork副本
1> 在Github中,进入需要创建Fork副本的资源页面
2> 复制Fork资源URL路径
3> 打开终端
4> 在终端中输入命令 $git clone URL
<span style="font-size:14px;">$git clone https://github.com/<span style="color:#FF6600;">YOUR-USERNAME/</span></span><span style="font-size:14px;color:#FF6600;">YOUFORK</span><span style="color:#FF6600;"></span>
5> 根据资源的大小和网速情况,需要等待几分钟,完成后,会出现 .done 的提示,表示完成
<span style="font-size:14px;">$git clone https://github.com/<span style="color:#FF6600;">YOUR-USERNAME/YOUFORK</span>
<span style="color:#009900;"># Cloning into `Spoon-Knife`...
# remote: Counting objects: 10, done.
# remote: Compressing objects: 100% (8/8), done.
# remove: Total 10 (delta 1), reused 10 (delta 1)
# Unpacking objects: 100% (10/10), done.</span></span>
注:此时用户根目录下已经创建了Fork的资源文件
3、通过配置Git来保持本地Fork副本与源资源的同步方式
1> 在Github中,进入源资源页面
2> 复制源资源URL路径
![](https://img-blog.csdn.net/20150322145013732?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW1hemVMaXVrdW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3> 打开终端,通过cd命令进入本地Fork副本的文件夹中
<span style="font-size:14px;">$cd <em><span style="font-size:12px;">主目录</span></em></span>
<span style="font-size:14px;">$ls</span>
<span style="font-size:14px;">$cd <em>your_listed_directory</em></span>
4> 通过 $git remote -v 命令查看Fork的远程地址(此时两个)
<span style="font-size:14px;">$git remote -v
<span style="color:#009900;"># origin https://github.com/<span style="color:#FF6600;">YOUR_USERNAME/YOUR_FORK</span>.git (fetch)
# origin https://github.com/<span style="color:#FF6600;">YOUR_USERNAME/YOUR_FORK</span>.git (push)</span></span>
5> 通过 $git remote add upstream URL 创建本地的upstream资源路径(源资源路径)
<span style="font-size:14px;">$git remote add upstream https://github.com/octocat/FORKREPOSITORY.git</span>
6> 通过 $git remote -v 命令查看Fork的远程地址(此时四个),完成
<span style="font-size:14px;">$git remote -v
<span style="color:#009900;"># origin https://github.com/<span style="color:#FF6600;">YOUR_USERNAME/YOUR_FORK</span>.git (fetch)
# origin https://github.com/<span style="color:#FF6600;">YOUR_USERNAME/YOUR_FORK</span>.git (push)
# upstream https://github.com/<span style="color:#FF6600;">ORIGINAL_OWNER/ORIGINAL_REPOSITORY</span>.git (fetch)
# upstream https://github.com/<span style="color:#FF6600;">ORIGINAL_OWNER/ORIGINAL_REPOSITORY</span>.git (push)</span></span>
4、通过upstream来更新本地Fork副本
1> 打开终端,进入Fork文件夹
2> 通过命令 $git fetch upstream 来更新upstream
<span style="font-size:14px;">$git fetch upstream
<span style="color:#009900;"># remote: Counting objects: 75, done.
# remote: Compressing objects: 100% (53/53), done.
# remote: Total 62 (delta 27), reused 44 (delta 9)
# Unpacking objects: 100% (62/62), done.
# From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
# * [new branch] master -> upstream/master</span></span>
3> 通过命令 $git checkout master 进入本地主分支(master branch)
<span style="font-size:14px;">$git checkout master
<span style="color:#009900;"># Switched to branch 'master'</span></span>
4> 通过命令 $git merge upstream/master 将最新的upstream资源合并到主分支,本地主分支中的变更不会丢失
<span style="font-size:14px;">$git merge upstream/master
<span style="color:#009900;"># Updating a422352..5fdff0f
# Fast-forward
# README | 9 -------
# README.md | 7 ++++++
# 2 files changed, 7 insertions(+), 9 deletions(-)
# delete mode 100644 README
# create mode 100644 README.md</span></span>
注:如果没有使用到源资源的其它分支,可以直接通过命令 $git merge upstream/master 来获取更新源资源并合并
5、通过Push方式更新Fork资源
通过命令 $git push <REMOTENAME> <BRANCHNAME> 将本地的分支更新到Fork资源
注:BRANCHNAME 本地 的branch名称 如 master
REMOTENAME Fork 的资源名称 如 origin
<span style="font-size:14px;">$git push <span style="color:#FF6600;"><REMOTENAME> <BRANCHNAME></span></span>