什么是GitHub? 什么是Git? 以及如何使用这些开发人员工具。

The first programs you write will probably not be very impressive. You'll make lots of mistakes and you'll never want to revisit the past.

您编写的第一个程序可能不会给人留下深刻的印象。 您会犯很多错误,并且您永远都不想重温过去。

But soon enough, you'll be writing large, complex programs. Maybe you'll delete some stuff now that you want to bring back later. Or maybe you'll bring in a friend to help, and want to gracefully add their changes to your program while you continue to work on your parts.

但是很快,您将编写大型,复杂的程序。 也许您现在要删除一些东西,以后再带回来。 或者,也许您会请一位朋友来帮助您,并希望在继续处理零件的同时将他们所做的更改优雅地添加到您的程序中。

That's where version control comes in, and it's a skill that any employer will expect you to have mastered. It's also extremely useful for anyone working on anything that is saved in pieces - from a computer program to a recipe to a novel.

这就是版本控制的用武之地,也是任何雇主都希望您掌握的一项技能。 这对于任何从事零碎工作的人(从计算机程序到食谱再到小说)都非常有用。

什么是版本控制? (What is version control?)

Version control refers to the ability to save your place in a document or folder and reference previous saves.

版本控制是指将您的位置保存在文档或文件夹中并引用以前的保存内容的功能。

As I write this article, my newest changes are constantly overwriting my previous versions. It's not version control, because I can't go back to the draft I had a week ago. But if I wrote this using Git, that would be possible.

在撰写本文时,我的最新更改是不断覆盖以前的版本。 这不是版本控制,因为我无法返回一周前的草稿。 但是,如果我使用Git编写此代码,那将是可能的。

什么是Git? (What is Git?)

Git is a version control system developed by Linus Torvalds in 2005 (the same guy who wrote Linux). Git helps developers keep track of the state of their code and allows collaboration on a codebase. We'll go over the main components a little later.

Git是Linus Torvalds在2005年开发的版本控制系统(写Linux的那个人)。 Git帮助开发人员跟踪其代码状态,并允许在代码库上进行协作。 我们稍后再讨论主要组件。

If you want to follow along,  you'll have to have Git installed on your computer. Open up a terminal and type git. If you see a list of possible commands, you're good to go!

如果要继续学习,则必须在计算机上安装Git。 打开终端并输入git 。 如果您看到可能的命令列表,那就很好了!

Many computers come with Git already installed. If you need to install, you can follow the instructions here for your computer.

许多计算机已经安装了Git。 如果需要安装,可以按照此处说明操作

使用Git (Using Git)

If you've ever used a computer program or a video game and noticed that you can go back to a previously saved version, you inherently understand the need for Git. It is simply saving a snapshot of your program in time.

如果您曾经使用过计算机程序或视频游戏,并且发现可以返回以前保存的版本,则从本质上了解对Git的需求。 它只是在及时保存程序快照。

But instead of needing to keep track of every single line of code in your program, it instead notes the changes between the code you have now and the last time you saved. It keeps a running note of when each line of code was saved last, and stores them in a special hidden folder.

但是,它不必跟踪程序中的每一行代码,而是记录您现在拥有的代码和上次保存的代码之间的更改。 它会持续记录每行代码的最后保存时间,并将它们存储在一个特殊的隐藏文件夹中。

Let's consider this JavaScript program. It prints three lines to the console (an output you can see in your browser or terminal):

让我们考虑一下这个JavaScript程序。 它向控制台打印三行(您可以在浏览器或终端中看到的输出):

console.log('Hello, this is a git example!')
console.log('And here is another!')
console.log('And yet a third')

git初始化 (git init)

If I want to save versions of my work, I can use Git. First, I'll type git init into my terminal so that I can start using Git. That will create a .git folder, where Git will store its files.

如果要保存工作版本,可以使用Git。 首先,我将git init键入终端,以便可以开始使用Git。 这将创建一个.git文件夹,Git将在其中存储其文件。

git添加 (git add)

git add . will add all the files in our program. If you did git init after you created a file, or any time you make new files, you'll have to tell Git to start tracking changes to them with this command.

git add . 将在我们的程序中添加所有文件。 如果在创建文件后或在创建新文件时执行git init ,则必须告诉Git使用此命令开始跟踪对它们的更改。

git提交 (git commit)

Next, I'll type git commit -am "Initial commit". git commit is the command to save a version of our code. The -am is called a "flag" and signals that there are optional actions we would like to take with this commit. The flag a means we are going to save All of our changes. The flag m denotes that we will provide a Message afterwards, which is "Initial commit".

接下来,我将输入git commit -am "Initial commit"git commit是保存我们代码版本的命令。 -am称为“标志”,它表示我们希望对此提交执行一些可选操作。 标记a表示我们将保存所有更改。 标志m表示我们稍后将提供一条消息,即"Initial commit"

You can write anything you want here - freeCodeCamp has many articles on how to write great commit messages.

您可以在此处编写任何内容-freeCodeCamp提供了许多有关如何编写出色的提交消息的文章

Git如何保存更改 (How Git saves changes)

If we make a change to our program (like changing the text on the first line), we may want to save a version. We could even toggle between versions if we wanted to see how our program has changed over time.

如果我们对程序进行更改(例如更改第一行中的文本),则可能需要保存一个版本。 如果我们想了解程序随着时间的变化,甚至可以在版本之间切换。

console.log('Now I have changed the first line.')
console.log('And here is another!')
console.log('And yet a third')

git diff (git diff)

Here's what this looks like as you run git diff. Git will show you the difference between the code you have now and the last time it was saved.

这是运行git diff样子。 Git将向您显示您现在拥有的代码与上次保存代码之间的区别

It's a little hard to understand what is going on here, but the - are deletions and the + are insertions. We removed text "Hello, this is a git example!" and added text "Now I have changed the first line." This is how Git keeps track of what has changed between versions.

很难理解这里发生了什么,但是-是删除,而+是插入。 我们删除了文本“您好,这是一个git示例!” 并添加了文字“现在我已经更改了第一行”。 这就是Git跟踪版本之间变化的方式。

diff --git a/git.js b/git.js
index eb0f1d1..8dbf769 100644
--- a/git.js
+++ b/git.js
@@ -1,3 +1,3 @@
+console.log('Now I have changed the first line.')
-console.log('Hello, this is a git example!')
 console.log('And here is another!')
 console.log('And yet a third')

Now that we've reviewed the changes we are committing, we can go ahead and make a second commit: git commit -am 'Update first console log'. This will save the changes I made to the first line of text.

现在,我们已经审查了要提交的更改,可以继续进行第二次提交: git commit -am 'Update first console log' 。 这将保存我对文本第一行所做的更改。

git日志 (git log)

We can review the commits we have made with the command git log. If I run it in my program now, I get this output:

我们可以使用git log命令查看所做的提交。 如果现在在程序中运行它,则会得到以下输出:

commit 67627dd44e84a3106a18a19e94cf9f3723e59b3c (HEAD -> master)
Author: amberwilkie <amber@amberwilkie.com>
Date:   Wed Apr 22 16:55:39 2020 -0400

    Update first console log

commit 49fe4152f474a9674a83e2b014a08828209d2690
Author: amberwilkie <amber@amberwilkie.com>
Date:   Wed Apr 22 16:54:59 2020 -0400

    Initial commit

We see our commit messages, the time we committed and a unique ID for our commit, which we can use to reference commits in the future.

我们会看到提交消息,提交时间和提交的唯一ID,将来可以用来引用提交。

git结帐 (git checkout)

If we wanted to go back and see the changes to our code from a previous commit, we'll do so with git checkout 49fe4152f474a9674a83e2b014a08828209d2690. Git will put our code in a temporary state so we can view what the code looked like at that snapshot in time.

如果我们想返回并查看上一次提交对代码的更改,我们将使用git checkout 49fe4152f474a9674a83e2b014a08828209d2690 。 Git会将我们的代码置于临时状态,以便我们可以及时查看该快照中的代码。

I copied the ID for my first commit. If I run this command, my program says "Hello, this is a git example!" on the first line.

我复制了第一次提交的ID。 如果运行此命令,程序将显示“您好,这是一个git示例!” 在第一行。

To get back to the latest code, you'll type git checkout master.

要返回最新代码,请输入git checkout master

分行 (Branches)

If you noticed above, we had to type master to get back to the current state of our code. Why? Because master is the default name for the branch of branches - the place where our code is most up-to-date.

如果您在上面注意到,我们必须键入master才能返回到代码的当前状态。 为什么? 因为master是分支分支的默认名称-我们的代码是最新的位置。

Git relies on branching to maintain our code. You can consider master the trunk of your tree of code. You may break off and make some changes, but the end goal is always to bring them back to the trunk, to master.

Git依靠分支来维护我们的代码。 您可以考虑master代码树的主干。 您可能会休息一下并进行一些更改,但是最终目标始终是将它们重新带回主干,以进行master

You can use git checkout to make a new branch, not just to check out previous versions of your code. Try git checkout -b new-branch. The -b flag is used when we are making a new Branch and after the flag we write the name of our new branch. We can make many commits on this branch and later get them back to master with a process called merging.

您可以使用git checkout创建新分支,而不仅仅是检出以前的代码版本。 尝试git checkout -b new-branch 。 当我们创建一个新的Branch时,使用-b标志,并在该标志之后写入新分支的名称。 我们可以在该分支上进行许多提交,然后通过称为merging的过程使它们重新掌握。

In the diagram below, the dots represent commits. Two branches have been made "off" of master. In software development, we often call these "feature" branches, as opposed to the main master branch. The blue branch has been merged back into master and the yellow branch is still being developed.

在下图中,点代表提交。 两个分支已经脱离主节点。 在软件开发中,我们通常将这些功能称为“功能”分支,而不是主master分支。 蓝色分支已合并回master,黄色分支仍在开发中。

Note that even though the yellow branch was created after the blue branch, only changes from master will be visible in that branch. If we made a third branch sometime in the future, changes from both master and the blue branch would be present in the new, third branch.

请注意,即使黄色分支是在蓝色分支之后创建的,在该分支中也只能看到master的更改。 如果我们将来某个时候创建​​了第三分支,那么新的第三分支中将同时出现master和blue分支的更改。

git合并 (git merge)

git merge will take all the commits you've made on that branch and stick them into the master branch, saving your work.

git merge将接受您在该分支上所做的所有提交,并将它们粘贴到master分支中,从而节省您的工作。

为什么要使用分支? (Why use branches?)

If you're working alone, it may not make a lot of sense to split your work into branches. Why not just save everything on master?

如果您是一个人工作,将工作分成多个分支可能没有多大意义。 为什么不将所有内容都保存在master

The utility of branching does not become very clear until we start thinking about working on a team of developers. If they were all committing to the master branch every time they made a change, things would get very messy very quickly. It would also be difficult to control what code goes "to production" (live to customers) and which code is still being tested or worked on.

直到我们开始考虑与一组开发人员合作时,分支的用途才变得非常清楚。 如果他们每次都更改都提交到master分支,那么事情很快就会变得非常混乱。 也很难控制哪些代码“投入生产”(交付给客户)以及哪些代码仍在测试或使用中。

This way, every developer can have their own branch (or, likely, many), work on their feature for as long as they need, and merge it when the time is right.

这样,每个开发人员都可以拥有自己的分支(或可能有很多),可以在需要时使用自己的功能,并在适当时机将其合并。

什么是GitHub? (What is GitHub?)

GitHub is a free (for personal use), cloud-hosted platform for code. It works with Git on your and your colleagues' computers, serving as the origin, the source of truth for anyone working on the code.

GitHub是一个免费的(供个人使用)云托管的代码平台。 它可以在您和您的同事的计算机上与Git配合使用,是从事代码工作的人的起源 ,真理的源泉。

You and your collaborators upload their code to GitHub periodically, and GitHub provides tooling to help manage changes to the code over time.

您和您的合作者会定期将其代码上传到GitHub,而GitHub提供了工具来帮助管理随着时间的推移对代码所做的更改。

将您的代码上传到GitHub (Uploading your code to GitHub)

First, you'll need to create a GitHub account. You'll use this account for your entire programming career, so hot tip: stick with a professional name, preferably with your actual name in it.

首先,您需要创建一个GitHub帐户 。 您将在整个编程生涯中使用此帐户,所以有个热门提示:坚持使用专业名称,最好使用您的真实姓名。

Once you're in, look for a + in the top corner. Click "New Repository" (the name for Git folders, "repo" for short). Give it a name - probably the same as the folder you created earlier where you've saved your commits. Then click "Create Repository". You can now copy the url you are redirected to and we can set the origin for our code.

进入后,在右上角查找+ 。 单击“新存储库”(Git文件夹的名称,简称“ repo”)。 给它起一个名字-可能与您先前保存提交的文件夹相同。 然后单击“创建存储库”。 现在,您可以复制重定向到的网址,我们可以为代码设置来源

There will be an authentication step at some point here - just follow the directions. Git is very good about giving us clear instructions on the next steps to take.

此处将在某个时候进行身份验证步骤-只需按照指示进行即可。 Git很好地为我们提供了下一步的明确指示。

git remote add origin (git remote add origin)

Now we will tell our codebase (the folder where our code is) where to store our code in the cloud. We'll type git remote add origin <your-url>, which will set an origin for our repository.  Now we can push to our origin to store our cloud at GitHub.

现在,我们将告诉我们的代码库(代码所在的文件夹)将代码存储在云中的位置。 我们将输入git remote add origin <your-url> ,这将为我们的存储库设置一个origin 。 现在,我们可以origin存储到GitHub中。

git推 (git push)

Assuming we're still in our master branch (that is, we have not checked out another branch), we can now type git push and our code will go to GitHub.

假设我们仍在master分支中(也就是说,我们还没有签出另一个分支),我们现在可以输入git push ,我们的代码将转到GitHub。

查看您的代码 (Viewing your code)

Now your code lives in GitHub! Here's what my example from above looks like after I follow the GitHub steps I've explained:

现在,您的代码位于GitHub中! 这是我按照我解释的GitHub步骤操作后的示例示例:

You can click through the files and folders of your repository, viewing the current state of the code. You can also view previous versions of the code, by clicking "X commits" on the right side, middle. You'll see a list of the commits made to the repo and if you click into them, you can browse the files of your project as they existed at that slice of time.

您可以单击存储库中的文件和文件夹,以查看代码的当前状态。 您还可以通过单击中间右侧的“ X commits”来查看代码的先前版本。 您将看到对存储库所做的提交的列表,如果单击它们,则可以浏览项目在该时间段中存在的文件。

拉取请求 (Pull Requests)

There are many other features of GitHub, but the most important in collaborating with colleagues is a pull request. A pull request (very frequently shortened to PR) is a way to manage incoming changes to the code base.

GitHub还有许多其他功能,但是与同事合作最重要的是请求pull 。 拉取请求(通常缩短为PR)是一种管理代码库传入更改的方法。

To make one, you'll make a new branch on your local computer, create at least one commit on that branch, then git push origin head to send that branch to GitHub. (You can put the name of your branch instead of head but it's useful for keeping everything matched up exactly).

要创建一个分支,您将在本地计算机上创建一个新分支,在该分支上创建至少一个提交,然后git push origin head将该分支发送到GitHub。 (您可以输入分支的名称而不是head但这对于使所有内容完全匹配非常有用)。

Now when you go back to GitHub, you should see your branch available to make a PR.

现在,当您回到GitHub时,您应该可以看到您的分支机构可以进行PR。

If you click the "Compare & pull request" button, you'll be able to change many settings for your PR. Most important are generally title and description. If you're working on a team, you can tag colleagues to ask them to review your code, add to projects, and many other features you probably don't care about yet.

如果单击“比较并提取请求”按钮,则可以更改PR的许多设置。 最重要的是标题和描述。 如果您在团队中工作,则可以标记同事以要求他们查看您的代码,添加到项目中以及您可能尚不在意的许多其他功能。

Note that we are comparing branches. Here we are requesting to add the changes from this branch (pr-example) into the master branch. But we could target any of the other branches in our repo. For now, just understand that master isn't the only branch you can "make a pull request against".

请注意,我们正在比较分支。 在这里,我们要求pr-example分支( pr-example )的更改添加到master分支中。 但是我们可以将我们回购中的其他任何分支作为目标。 现在,只需了解master并不是您可以“向其发出请求”的唯一分支。

When you click "Create Pull Request", you'll see this screen:

当您点击“创建请求请求”时,您会看到以下屏幕:

You can see all the commits in this branch (I have only one - "Change third line of program"), and you can also merge your pull request.

您可以看到该分支中的所有提交(我只有一个-“更改程序的第三行”),还可以合并您的请求请求。

Remember how we could merge our code locally when we talked about Git? We can perform the same action with our cloud-hosted code on GitHub. If you click the green "Merge pull request" button, your changes will be merged into master.

还记得我们谈论Git时如何在本地合并代码吗? 我们可以使用GitHub上的云托管代码执行相同的操作。 如果单击绿色的“合并拉取请求”按钮,您的更改将被合并到主数据库中。

git pull (git pull)

The last command you need to know right now is git pull. If you merged your PR into the master branch on GitHub, there are now changes to the origin that you do not have yet on your local computer. If you check out the master branch, then git pull origin master, the changes you have just merged will now be on your local computer.

您现在需要知道的最后一个命令是git pull 。 如果您将PR合并到GitHub上的master分支中,则本地计算机上尚未存在的origin将发生更改。 如果您检出master分支,然后git pull origin master ,那么刚刚合并的更改现在将在本地计算机上。

➜  git-example git:(master) git pull origin master
From https://github.com/AmberWilkie/git-example
 * branch            master     -> FETCH_HEAD
Updating 67627dd..38ad2da
Fast-forward
 git.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

That "Fast-forward" refers to our local master branch "catching up" with the origin branch on GitHub. We have completed the circle:

“快速转发”是指我们的本地主分支与GitHub上的origin分支“追上”。 我们已经完成了这一圈:

  • Local changes

    当地变化
  • Push to GitHub and make PR

    推送到GitHub并进行PR
  • Merge PR into master

    合并公关
  • Pull master to local computer

    将母版拉到本地计算机

Once you are comfortable with these steps, you will be 80% of the way to mastering Git and GitHub!

一旦您对这些步骤感到满意,就可以掌握Git和GitHub的80%的方法!

翻译自: https://www.freecodecamp.org/news/what-is-github-what-is-git-and-how-to-use-these-developer-tools/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值