源码

使用 github,知道这些就够了


只要掌握了下面的常用命令,基本上用使用 github 就没有问题。github 有两种认证方式,一种是通过 ssh 私钥的方式,一种通过 https 的账号名和密码。ssh 方式需要创建本地秘钥并且添加到个github 上,操作起来略显麻烦,本篇完全介绍以 https 方式管理。

主要原理说明

git 的管理依赖于在你本地仓库的目录中存在一个 .git 目录的,里面有 config、HEAD 等文件。

HEAD 里面是标识当前所在的分支,内容如下,表示当前在 dev 分支下,之后的拉取和推送都是在 dev 分支下完成:

 ref: refs/heads/dev

 config 里配置一些参数,如仓库地址、remote 等

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

其中,url 参数是重点,表示远程仓库地址,正常的远程仓库格式为https://github.com/huzhicheng/test__11.git,而这里,并且包括下面用到的都是 https://username:[email protected]/huzhicheng/test__11.git 这种格式。其中username 表示 github 账号,后面冒号分隔,接一个密码,也就是 github 密码,然后用 @ 符号连接上远程仓库。

用 https 方式连接 github 项目就是这么简单。

初始化本地仓库

假设我们要在 github 上维护一个项目。首先,要登录 github 账号,点击 “New repository”,输入项目名称,即可成功创建项目仓库,之后会提示如下内容:

 echo "# 初始化仓库" >> README.mdgit initgit add README.mdgit commit -m "first commit"

 做上面这些操作之前,我们需要在机器上创建一个空白目录,然后进入这个目录进行操作。

第一行,是说新建一个 README.md 文件,并且写上你自定义的内容;

第二行,通过 git init 命令做本地仓库的初始化;

第三行,将 README.md 添加到暂存区;

第四行,提交暂存区并写好注释说明。

本地仓库和远程仓库创建联系

本地创建完仓库后,需要把本地仓库和远程仓库建立联系,这样之后才能推送文件到远程仓库。

 git remote add origin https://username:[email protected]/huzhicheng/test__11.git

移除本地仓库和远程仓库的关联

如果远程仓库作废或者添加了错误的远程仓库地址,可以用下面的命令移除掉。注意 origin 后面的内容需要和关联远程仓库时填写的内容一致才可以。

 git remote rm origin https://username:[email protected]/huzhicheng/test__11.git

首次提交

 git push -u origin master

推送本地修改到远程

推送前一定要先拉取最新代码,并且每次修改前及时拉取最新代码是非常好的习惯。

 //拉取最新代码git pull origin master//查看本地仓库状态git status// 将所有修改更新至暂存区git add .// 提交暂存区更改 并写上明确的注释说明git commit -m "注释内容"// 提交修改至主分支git push origin master

 以上操作就可以完成从仓库初始化到文件提交的完整过程了。

那如果是参与已经存在的项目呢,远程仓库已经存在并且已有项目文件在了,下面介绍如何参与已有项目。

将远程仓库同步到本地

首先需要 clone 远程仓库到本地,然后拉取新代码就可以了,就是这么简单。

 git clone https://username:[email protected]/username/xxxxxx.gitgit remote -v   可以查看远程仓库版本git fetch origin master 拉取远程仓库更新

强制覆盖本地文件

有时候临时在本地仓库做了修改,但是不想保留,再拉取更新的时候要强制覆盖本地文件,可以用如下命令。

 git fetch --allgit reset --hard origin/mastergit pull

解决冲突

有时候我们可能和别人同时修改了某一个文件,提交的时候发现文件已经被别人修改,并且提交了。这时,如果我们再提交文件的话会出现如下错误:

 To https://github.com/huzhicheng/test__11.git! [rejected]       dev -> dev (fetch first)error: failed to push some refs to 'https://github.com/huzhicheng/test__11.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.

按照提示,我们尝试 git pull origin dev 获取最新版本,通常又会出现如下问题:

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

0

 意思是说,获取新版本尝试自动合并,但是合并失败,请修复冲突并提交。

这时我们打开冲突的文件,会看到有 <<<<<<< HEAD 、=======>>>>>>> 这些符号,留下要合并的最后内容。然后再执行命令:

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

1

建立并切换到新分支

如果项目中存在多个分支,例如主分支和开发分支,我们在本地创建同样的分支结构使用如下命令,创建 dev 分支,并且切换到 dev 分支 。

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

2

查看所在分支

其中前面带 * 号的为当前所在的分支。

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

3

分支合并

我们之前假设已经在 dev 分支上做了一些修改,可以发布版本之后,想要合并到 master 分支上。

首先使用 git checkout master 切换到 master 分支,然后执行 git merge dev 命令进行 dev 分支到 master 分支的合并。

 [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true[remote "origin"]url = https://username:[email protected]/huzhicheng/test__11.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master

4

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/shiyong-githubzhidaozhexiejiugoule.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 11, 2019 at 06:02 下午

热评文章