未登录,请登录后再发表信息
最新评论 (0)
播放视频

怎样使用GitHub

How to Use GitHub

微软收购了GitHub 万事大吉
Microsoft bought GitHub, and everything will be fine!
大家好 我是Siraj
Hello World! It’s Siraj.
这期视频中我会教大家使用GitHub
and in this video I’ll show you how to use GitHub,
一个供全球开发者使用的安全储存空间
the safe space for developers across the world.
我会讲述Git协议的工作原理
I’ll describe how the Git protocol works,
演示如何创建你自己的代码仓库
show you how to make your own repository,
再给一些建议
and give you some tips
教你在社交媒介上推广原创GitHub代码
on how to promote your GitHub code via social media.
10多年前
Over a decade ago,
传奇般的程序员Linus Torvalds
the legendary programmer, Linus Torvalds
正在研究一种操作系统
was working on an operating system
这个项目他已经研究好几年了
that had been his pet project for a couple of years.
他经常需要修改自己的代码
He was constantly making changes to his code,
也有很多程序开发者
and there were other developers
很想为这个项目做点贡献
who were interested in contributing to the project as well.
因此他需要一种方式跟踪代码发生的变化
He needed a way to track how the code changed over time.
这样即便搞砸了
So if he messed up,
也很容易就能回滚到以前的版本
he could easily go back to a previous version.
较简单的方法是
The simple way would be
不断重命名源代码的每个新版本
to continuously rename every new version of his source code,
但是这太麻烦
but this was cumbersome.
更好的方法是
A better way would be
用某种轻量级协议
to use some kind of light-weight protocol
来跟踪代码随时间的变化
to track those changes over time.
你和其他人之间的合作。
This would also help him work with other developers,
因为会产生一个集合
as it would give them a collection of notes
是关于最近工作最多的项目文件的
on the most recently worked on project files,
同时是随着时间变化的修改历史
as well as a nice chronological history of what had changed.
当时已经有一些已有工具用来做这件事
There were existing tools out there,
但Linus不高兴使用他们
but Linus was not happy with any of them,
他固执地认为
as in his hilariously strong opinion,
它们不够好用
they didn’t worked well enough.
因此 他编写了Git协议来解决问题
he wrote the Git protocol to solve his problem.
并用它帮助自己开发Linux操作系统
And he used it to help him work on his Operating system, Linux.
从此以后 尽管还有其他的版本控制系统
Even though, there are other version control systems out there,
Git却广受欢迎还变成了最常用的一个
Git has since exploded in popularity becoming the most used.
现在Git几乎是每个现代开发流程的核心
It’s now at the heart of pretty much every modern development workflow,
而热门网站github.com正是用git协议
and the popular website github.com uses the git protocol
来托管的代码库
to host code bases,
使开发者能在同一个共享接口上合作
and allow developers to work together with a single shared interface.
Git有很多不同的命令
Git has a lot of different commands,
而且如果你是首次尝试理解
and it’s architecture can be confusing,
它的架构 可能会感觉它很混乱
if it’s your first time trying to understand it.
为了能更直观地理解Git
To make git more intuitive,
我们用汽车公司特斯拉做一个类比
let’s use the analogy of an auto company, like Tesla.
假设特斯拉每年都要发售新一版的Model S
Let’s say Tesla has to release a new version of it’s Model S every year,
而且只略微更新下功能
with slightly updated features.
而特斯拉的各汽车设计团队
The different teams of car designers at Tesla
需在一年期计划内在新版本上实现这些功能
have a year-long plan to execute those features for the latest version.
在这个过程中
And along the way,
每个团队都要确保他们的改动
each team has to make sure their changes
能和其他团队的改动兼容
support the changes from other teams.
因此 假如我们在车身设计团队里
So if we’re on the body-design team,
那么车身设计不仅仅要好看
We need to create a body design that doesn’t just look good,
还要跟其他新功能兼容
it has to work in tandem with the new features,
比如 车内设计团队新添加了
that, say, the interiors team is putting in the car,
火焰喷射器
like flamethrowers.
那么该如何组织出最佳的工作流程
How do we best organize our workflow
从而协调所有的团队呢
such that we can accommodate all the teams?
其实在开发2019版车身时
As we are developing the body for a 2019 version,
随时都存在着四版设计
there are actually four versions of the design that exist at any given time.
首先是线上版本
There’s the Live version,
这是已经投放到市场
that’s the current version that’s already on the market,
顾客都知道的版本
that customers know about.
其次是已经计划好明年上线的版本
There is the currently planned version for the next year,
它最终会在所有团队同意后上线
that will eventually go live once all the teams agree.
这代表的是远程仓库
This represents the remote repository.
然后是最新改进后的版本
There’s our own latest improved body design version
它吸收了其他团队的建议
that takes into account suggestions from other teams,
但还未展示给团队其他成员同意
that’s yet to be presented to the rest of the team for approval.
这代表着本地仓库
This represents the local repository.
最后还有很新颖 仍在开发的设计
And finally, there’s a design we are developing and excited about.
但还需要团队其他成员的测试和审核
but still needs to be tested and reviewed by the team.
这是暂存区
This is the staging area.
以上阶段都是对车身设计不同程度的承诺
Each stage represents a different level of commitment to the body design,
在代码库中也一样
and similarly in a code base,
代码上线之前必须通过四个阶段
our code must pass through four stages before it goes live,
就像汽车的设计过程一样
just like in the car design process.
这是件好事
This is a great thing.
我们可不想刚起草一个汽车设计
We wouldn’t want to just draft up a car design,
就草率地决定这是2019版车身的最终设计
and immediately decide, that hey, this is the final 2019 design.
我们的设计必需被整个团队确认
The whole team has to agree upon whether our design works,
能与其他改动兼容才行
in harmony with the other changes that are being made,
这应是一个民主的过程 不像苹果公司
It’s a democratic process unlike Apple.
Git有一组命令
Git has a set of commands
可以通过速查表找到它们
that we can find via a simple cheat-sheet.
每个命令都有自己的用例
Each has it’s own use case.
特别是里面有三个命令
We can use 3 commands in particular,
可以用来标记不同的审查阶段
to highlight the different stages of review,
设计在上线前必须要通过这些审查
that our design must go through before it goes live.
每个审查阶段都进一步降低了
Each stage further lowers the risk,
与其他团队开发的功能产生冲突的风险
that will make a decision that conflicts with features built by another team
“git add”表示我们已经完成了整个设计
“git add” declares that we’ve finished a full design
而且感觉不错
and that we feel good about it.
但它在进入暂存区后
It still needs to be tested and reviewed though,
还需要测试和审核
as this design enters the staging area.
“git commit”意味着该设计通过了所有标准测试
“git commit” means we feel fully confident in the design
我们对它充满信心
we’ve put it through all our standard tests,
而且所有功能 例如旋转后视镜
and our features, like revolving side-mirrors,
都已就绪了
they’re all in place.
然后这个设计会从暂存区复制到本地仓库
This design is now copied from the staging area into the local repository,
准备展示给其他团队
ready to present to the other teams.
等准备好把我们的车展示给其他团队
Once we’re ready to present our car to the other teams,
就可以通过”git push”命令
we can copy our design from the local repository to
把设计从本地仓库复制到远程仓库
the remote repository via the “git push” command.
远程仓库托管在云端
The remote repository is hosted in the cloud,
而本地仓库托管在我们的电脑上
while our local one is hosted on our machine.
一旦所有的团队都对新功能感到满意
And once all the teams are happy with the new features,
就可以作为产品部署到网站或手机应用中了
we can deploy it to our website or mobile app, in production.
这实际上不是一个git命令
This isn’t actually a git command,
它只代表上线一个产品的过程
it just represents the process of going live to production.
到目前为止
So far,
我们假设的是一次只做汽车的一种新版本
we’ve assumed that we are all building just one new version of our car at a time
但事实是
But the reality is,
我们的汽车有三大版本
that our car actually has 3 variations.
每种的车身设计都略有不同
Each with slightly different body designs.
除了车身设计 它们的功能
They have pretty much all their features in common,
几乎完全相同
except for the body-design.
因此尽管可以为每种车身设计新建一个仓库
We could just make new repositories for each,
但它们实在太过相似
but since they share so many in common,
只为每种设计建个新分支就行了
we can just create branches for each.
通过分支可以维护一套代码的多个版本
Branches let us maintain multiple versions of the same code base.
每个分支都会略有不同
Each branch will have a few slight differences.
假设市场团队发现
And if the marketing team learns,
市场对某个版本的汽车没有需求
that hey, there isn’t a demand for a certain version of our car,
但要是能把它的功能用在主版本上却不错
but it would be nice to have a certain feature of that car in our main version.
这样就可以用”merge”命令合并两个分支
We can use the “merge” command to integrate two branches.
虽然会消去一个分支
This eliminates one branch,
但会把那个分支的功能添到新分支上
but brings the features of that branch into a single new branch.
比如说某团队把一个新功能 汽车仪表盘
Let’s say a team pushes a new feature like a car dashboard
推送到了远程仓库
to the remote repository.
我们可以用”git pull”命令更新本地仓库
We can update our local repository with “git pull”,
来拉取新的改动
pulling the new changes.
但如果那些改动跟本地仓库有冲突
But if those changes conflict with our repository,
我们就需要“合并冲突”了
we have what’s called a”merge conflict”.
解决这个问题有两种方式
We have 2 options to resolve this.
要么让那个团队撤销他们的改动
We can either tell that team to revert their changes
要么我们改变自己的车身设计
or we can change our own body design,
以适应新的仪表盘
so that it fits the new dashboard.
然后把更新后的版本推送到远程仓库
Then push an updated version to remote
来解决这个冲突
to resolve the conflict.
Git能帮助我们跟踪所有冲突
Git helps us track all these conflicts,
并快速地指出存在冲突的代码行
and quickly points out the exact lines that are in conflict.
让我们一起来创建自己的仓库
So let’s go ahead and create our own repository,
然后把它放到GitHub上吧
then put it on GitHub.
注册GitHub账号很简单
We can sign up for GitHub pretty easily
只需在GitHub网站上输入用户信息
on the website after entering a few credentials.
添加一张图片和一个描述
We want to add a picture and a description
毕竟本质上它是一个开发者的社交网络
since it’s essentially a social network for developers.
然后
Once we do that,
下载并安装最新版本的Git协议
we can download the latest version of the git protocol and install it.
安装完成后
After that’s done in command line,
配置Git
we can set up git,
把协议跟GitHub连接起来
so it links up with GitHub.
用”git config”命令把用户名和电子邮箱
We define our github username and e-mail using the “git config” command,
定义为同github.com上用到的一样
the same ones we used on github.com.
然后通过”git init”命令
Then we can create our first local git repository,
创建第一个本地Git仓库
via command line using the “git init” command.
查看一下状态
We can check the status
发现现在没有可提交代码
and see that we have absolutely nothing to commit right now.
新建一个文件
So we’ll just create a new file
在里面添加一些代码
and add some code to it.
现在我们要把这些改动推送到GitHub
Now we want to push these changes to GitHub,
把它添加到Git
so we can add it to git,
直接添加到暂存区
straight to the staging area.
这是我们最初的设计
This is our initial design.
然后 如果觉得满意
Then, if we’re satisfied,
可以通过”git commit”命令
we can commit our changes to our local repository
把改动提交到本地仓库
via the “commit” command.
在把代码推送到远程仓库之前
Before we push our code to the remote repository,
为了好玩 我们再创建一个分支
let’s create another branch for fun
然后通过”git checkout”命令切换到新分支
then switch to it via the checkout command.
稍微修改一下代码
We can modify the code a bit here,
然后提交
then commit it.
现在切换回主要分支
Now, if we switch back to our main branch,
也叫做主分支
also called master.
再做一个改动
We can make another change,
只是为了好玩 然后提交改动
just for fun, and commit it.
如果要把改动从新分支合并到主分支
Now if we want to merge the changes from our new branch onto master,
需要用”git merge”命令
we can, via the merge command.
但是 唉 会出现一个合并冲突
But alas, there will be a merge conflict
团队协作时每个开发者都在
This happens when we work with teams,
完善自己的分支 这种事情时有发生
each developer working on their own branch.
幸运的是Git能让我们通过查看
Luckily git lets us resolve these conflicts
造成问题的代码行 来解决这些冲突
by seeing the exact line of code in question.
在本地仓库解决冲突
We can resolve it locally to match,
然后提交解决
then commit the resolution.
这就是为什么说Git就像一棵树
This is why, git resembles a tree.
不同的分支上有不同的代码
Different branches, each with different commits
它们就是树上的一个个节点
can all represent nodes in a tree,
最终汇入主节点
all leading up to the master node.
现在可以把代码推送到GitHub了
Now we can push our code to GitHub.
但是我们先在新账户上创建一个空仓库
But first let’s create an empty repository on our new github account.
复制空仓库链接 添加为新的远程仓库
We’ll copy the link it gives us and add a new remote.
然后用”push”命令把代码推送到该远程仓库
Then we can push to that remote via the push command.
这样在GitHub上打开这个仓库
And if we go to that repository on GitHub,
就能看到我们的代码
we can see our code there,
这酷毙了
how dope is that!
GitHub是一个社交网络
GitHub is a social network
你可以关注其他开发者
and you can follow other developers
来获取他们代码的最新动态
to keep up to date with their latest code.
关注你的开发者越多
The more developers that follow you,
越有可能有开发者为你的开源项目做出贡献
the more likely developers will contribute to your open source project.
目前GitHub上有2800万开发者
Currently, Linus Torvalds is the most followed
Linus Torvalds是其中被关注最多的
developer out of 28 million on GitHub.
而现在我排名第15 Linus你要小心了
And right now I’m number 15, so watch out Linus.
我在推广自己的GitHub简介方面很成功
I found lot of success promoting my github profile
用我的社交媒介账号作GitHub的引流渠道
by using my social media accounts as a distribution channel to GitHub.
好的文档会吸引更多开发者关注你的代码
And good documentation goes a long way to getting developers to follow your code.
直接推广代码吸引不到多少观众阅读
Promoting code directly leaves a very niche audience to view it.
但如果你包装一下
But if you wrap it,
比如用视频 博客或播客
with say, a video or blog post or podcast,
总之用一些内容来解释它
some kind of explainer content in general.
则会吸引到更多人来阅读你的代码
Then you’ll be more likely to get people to actually view the code.
Git和GitHub都很有趣
Git and GitHub are both a lot of fun.
我在视频描述中列举了一些很棒的学习资源
And I’ve listed some great learning resources for you in the video description.
一定要去看看
So definitely check them out.
准备好学习更多了吗
Ready to commit to more?
点击订阅按钮 你就会得到我的真传
Push that subscribe button and my heart will merge with yours.
现在我要去解决合并冲突啦
For now, I’ve got to resolve a conflict.
多谢观看
So, thanks for watching.

发表评论

译制信息
视频概述

GitHub是世界各地开发人员共同分享和协作编程项目的最受欢迎的平台。 在这个视频中,我将通过跟汽车公司(特斯拉)的类比,解释git协议如何工作,解释GitHub如何工作,然后从命令行向您展示如何将您的第一个代码仓库从自己的计算机推送到GitHub。

听录译者

收集自网络

翻译译者

鹿琳

审核员

审核员_MZ

视频来源

https://www.youtube.com/watch?v=Loav1kbA640

相关推荐