GitHub的使用总结

目前GitHub非常的火,无论是国内的还是国外的现在都在使用。抽空研究了一下这个东西的使用方法,并总结出来,虽然有一点晚,但是我觉得还是有需要了解和学习的地方。好了,进入主题,一步一步的去介绍,当然最容易的学习方法,当然是跟着github网站提供的步骤去学习。这里我也是针对这个内容来进行总结。


1、github的网址: https://github.com

2、选择login,输入用户名,密码,登陆。登陆之后,就会进入到主页,这里包含了四个基本的应用:

Set Up Git
Create A Repository
Fork a Repository
Be Social

3、Set Up a git (主要目的就是在本地系统上安装好git工具,因为github其实是服务于git的网站)

1、配置用户名和邮箱。
git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit

git config --global user.email "your_email@youremail.com"
# Sets the default email for git to use when you commit

2、补充说明
git的版本查询 git --version
git的路径查询 which git

4、Create a Repository

在网页中查收到 Create repository的网页绿色按钮后,输入必要的项目名称后,点击,即可完成创建repository的过程。需要记住的是项目名称为hello.git
创建成功后,就涉及如何在本地创建一个客户端的库,以保证与服务断的同步。客户端的库的创建过程如下:
mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory


cd ~/Hello-World
# Changes the current working directory to your newly created directory


git init
# Sets up the necessary Git files

# Initialized empty Git repository in /Users/you/Hello-World/.git/

touch README
# Creates a file called "README" in your Hello-World directory
git add README

# Stages your README file, adding it to the list of files to be committed


git commit -m 'first commit'
# Commits your files, adding the message "first commit"

git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repo


git push origin master
# Sends your commits in the "master" branch to GitHub

上述过程当中,涉及到几个比较关键的点,也是我在研究的过程中花了一些时间才弄清楚的地方。下面一一讲解
git init 实际上是在一个空目录,或者空项目中,建立一个初步的git扩展。
git add README 这个意思,是上传README一个文件,当文件比较多的情况下,需要上传目录当中的所有文件,此时应该的写法为 git ad . 记住要有个点。
git commit -m '注释说明‘ 这个其实就是提交到本地库当中。
git remote add origin https://github.com/username/Hello-world.git 他的含义其实是将本地的库 origin,上传到服务器的Hello-world.git当中。
git push -u origin master 意思就是上传,这个地方才是真正的上传

5、Fork a Repo

fork的理解,其实fork的含义是通知一个git项目的主人,我要将你的代码fork到我的空间去,然后我就可以进行修改了,这样有利于其他未授权的人对代码进行贡献。还有,fork之后,就使用 git clone https://github.com/username/Hello-world.git
修改之后,需要提交到github上,此时使用的方法位:git remote add upstream https://github.com/octocat/hello-world.git
git fetch ustream 获取未改变的内容。

这里有一个概念,就是 git fetch,和git pull,其实都是从服务器获取代码,相当于update,但是fetch是不自动覆盖,而pull就会自动覆盖掉。
此外,还有一个概念,就是当clone之后,origin指向的其实是你自己的repo,而主人的变更是需要通过如下,获得具体的配置跟踪。
cd Spoon-Knife
# Changes the active directory in the prompt to the newly cloned "Spoon-Knife" directory

git remote add upstream https://github.com/octocat/Spoon-Knife.git
# Assigns the original repo to a remote called "upstream"

git fetch upstream
# Pulls in changes not present in your local repository, without modifying your files
我的理解,其实就是分支的问题。所谓分支,其实目的是当你需要同时解决两个问题的时候,而这两个问题为了便于后续在合并,因此会创建一个分支,然后在做meiger,将分支合并,从而解决这样的同时存在的问题。

因此,当你只提交自己的改变给自己的时候,可以如下:
git push origin master
# Pushes commits to your remote repo stored on GitHub




而当你需要与主人的分支进行合并的时候,需要如下做法:
git fetch upstream
# Fetches any new changes from the original repo

git merge upstream/master
# Merges any changes fetched into your working files

创建分支的方法:

git branch mybranch
# Creates a new branch called "mybranch"

git checkout mybranch
# Makes "mybranch" the active branch
其实是实现一个branch,然后将这个branch作为当前获得的branch,进行修改。改变当前branch的方法如下:
git checkout master
# Makes "master" the active branch

git checkout mybranch
# Makes "mybranch" the active branch
两个branch做最后的合并
git checkout master
# Makes "master" the active branch

git merge mybranch
# Merges the commits from "mybranch" into "master"

git branch -d mybranch
# Deletes the "mybranch" branch

Pull request 基本含义是,向主人发送请求,通知他,我做了改变,你可以看看。

Unwatch the main repo 的基本含义,是说,对于很多人都进行更新的项目,我做一些选择,放弃一些更新的跟踪。

最后,还想介绍一下删除一个项目的方法,这个方法在github上其实有时候是很难找到的,具体的方法为:

首先,选择项目 -- 然后选择Setting -- Delete this repository


补充一个关于git的ssh远程访问的配置方法:


首先在本地创建ssh key;

$ ssh-keygen -t rsa -C "your_email@youremail.com"
后面的[email]your_email@youremail.com[/email]改为你的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。

回到github,进入Account Settings,左边选择SSH Keys,Add SSH Key,title随便填,粘贴key。为了验证是否成功,在git bash下输入:

$ ssh -T [email]git@github.com[/email]
如果是第一次的会提示是否continue,输入yes就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。

接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。


$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"
进入要上传的仓库,右键git bash,添加远程地址:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值