Merge pull request #151 from wenchaoh997/zh_cn

translate zh_cn
This commit is contained in:
Michael Cade 2022-06-26 12:53:04 +02:00 committed by GitHub
commit ea4e703a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 198 additions and 191 deletions

View File

@ -1,36 +1,38 @@
--- ---
title: '#90DaysOfDevOps - Variables & Constants in Go - Day 11' title: '#90DaysOfDevOps - 变量、常量和数据类型 - 第十一天'
published: false published: false
description: 90DaysOfDevOps - Variables & Constants in Go description: 90DaysOfDevOps - 变量、常量和数据类型
tags: 'devops, 90daysofdevops, learning' tags: 'devops, 90daysofdevops, learning'
cover_image: null cover_image: null
canonical_url: null canonical_url: null
id: 1048862 id: 1048862
--- ---
Before we get into the topics for today I want to give a massive shout out to [Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I) and this fantastic concise journey through the fundamentals of Go. 在我们开始今天的学习之前,我想向[Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I)和这段精彩简洁的Go基础之旅欢呼。
On [Day8](day08.md) we set our environment up, on [Day9](day09.md) we walked through the Hello #90DaysOfDevOps code and on [Day10](day10.md)) we looked at our Go workspace and went a little deeper into compiling and running the code. 在[第8天](day08.md)我们设置了环境,[第9天]我们学习了Hello #90DaysOfDevOps 的代码,[第10天]我们查看了Go工作区并更深入了解了编译和运行代码。
Today we are going to take a look into Variables, Constants and Data Types whilst writing a new program. 今天我们要学习变量(Variables),常量(Constants)和数据类型(Data Types),并编写一个新的程序。
## Variables & Constants in Go ## Go中的变量和常量
Let's start by planning our application, I think it would be a good idea to work on a program that tells us how many days we have remained in our #90DaysOfDevOps challenge.
The first thing to consider here is that as we are building our app and we are welcoming our attendees and we are giving the user feedback on the number of days they have completed we might use the term #90DaysOfDevOps many times throughout the program. This is a great use case to make #90DaysOfDevOps a variable within our program. 让我们从规划这个应用程序开始,我觉得开发一个程序是告诉我们#90DaysOfDevOps挑战还有多少天的一个好办法。
- Variables are used to store values. 这里首先要考虑的是,在构建应用程序时,我们欢迎参与者加入,并且我们会向用户反馈当前完成天数。我们将多次用到#90DaysOfDevOps。这是一个很好的例子将#90DaysOfDevOps作为我们程序中的一个变量。
- Like a little box with our saved information or values.
- We can then use this variable across the program which also benefits that if this challenge or variable changes then we only have to change this in one place. Meaning we could translate this to other challenges we have in the community by just changing that one variable value.
To declare this in our Go Program we define a value by using a **keyword** for variables. This will live within our `func main` block of code that you will see later. You can find more about [Keywords](https://go.dev/ref/spec#Keywords)here. - 变量用于存储值
- 类似一个带有信息或数值的盒子
- 后续我们可以在整个程序中使用这个变量,这将在变量发生改变时提供便利,我们只需要在一处改变它的值。这意味着我们可以只改变一个变量值,将其用于社区中的其他挑战中。
Remember to make sure that your variable names are descriptive. If you declare a variable you must use it or you will get an error, this is to avoid possible dead code, code that is never used. This is the same for packages not used. 为了在我们的Go程序中声明变量我们使用一个**关键字**(keyword)来定义一个值。它将存在于我们稍后出现的`func main`代码块中。你可以在[keywords](https://go.dev/ref/spec#Keywords)中查看更多相关信息。
请记住并确保你的变量名是被定义的。如果你声明一个变量,你必须使用它,否则你会得到一个错误。这是为了避免可能出现的无效代码(那些从未被使用的代码)。对于未使用的包也是如此。
``` ```
var challenge = "#90DaysOfDevOps" var challenge = "#90DaysOfDevOps"
``` ```
With the above set and used as we will see in the next code snippet you can see from the output below that we have used a variable.
通过上面的设置,我们会在后续代码片段中使用它,你可以通过下面的输出看到我们已使用这一变量。
``` ```
package main package main
@ -42,15 +44,16 @@ func main() {
fmt.Println("Welcome to", challenge "") fmt.Println("Welcome to", challenge "")
} }
``` ```
You can find the above code snippet in [day11_example1.go](Go/day11_example1.go)
You will then see from the below that we built our code with the above example and we got the output shown below. 你可以在[这里](../../Days/Go/day11_example1.go)找到上述代码片段。
![](Images/Day11_Go1.png) 你会看到我们构建了上述示例代码,并得到下列输出。
We also know that our challenge is 90 days at least for this challenge, but next, maybe it's 100 so we want to define a variable to help us here as well. However, for our program, we want to define this as a constant. Constants are like variables, except that their value cannot be changed within code (we can still create a new app later on down the line with this code and change this constant but this 90 will not change whilst we are running our application) ![](../../Days/Images/Day11_Go1.png)
Adding the `const` to our code and adding another line of code to print this. 我们知道我们的挑战期限是90天但接下来也可能是100天所以我们想去定义一个变量来帮助我们记录。但是在我们的程序中我们想要把它定义为一个常量。常量和变量类似除了它们的值不能被改变(我们会创建一个新的应用并改变这个常量其中90的数值不会改变)。
添加`const`到我们的代码中,并添加一行来打印它。
``` ```
package main package main
@ -65,15 +68,16 @@ func main() {
fmt.Println("This is a", daystotal, "challenge") fmt.Println("This is a", daystotal, "challenge")
} }
``` ```
You can find the above code snippet in [day11_example2.go](Go/day11_example2.go)
If we then go through that `go build` process again and run you will see below the outcome. 你可以在[这里](../../Days/Go/day11_example2.go)找到上述代码。
![](Images/Day11_Go2.png) 如果我们后续再次使用并运行`go build`,你会看到下列输出。
Finally, and this won't be the end of our program we will come back to this in [Day12](day12.md) to add more functionality. We now want to add another variable for the number of days we have completed the challenge. ![](../../Days/Images/Day11_Go2.png)
Below I added `dayscomplete` variable with the number of days completed. 最后,我们将在[第十二天](day12.md)的时候添加其他的功能。现在,我们想加入另外的变量来表示我们已完成这个挑战的天数。
我在下边添加了叫`dayscomplete`的变量,代表完成天数。
``` ```
package main package main
@ -90,17 +94,18 @@ func main() {
fmt.Println("Great work") fmt.Println("Great work")
} }
``` ```
You can find the above code snippet in [day11_example3.go](Go/day11_example3.go)
Let's run through that `go build` process again or you could just use `go run` 你可以在[这里](../../Days/Go/day11_example3.go)找到上述代码。
![](Images/Day11_Go3.png) 再次运行`go build`或者直接使用`go run`。
Here are some other examples that I have used to make the code easier to read and edit. We have up till now been using `Println` but we can simplify this by using `Printf` by using `%v` which means we define our variables in order at the end of the line of code. we also use `\n` for a line break. ![](../../Days/Images/Day11_Go3.png)
I am using `%v` as this uses a default value but there are other options that can be found here in the [fmt package documentation](https://pkg.go.dev/fmt) you can find the code example [day11_example4.go](Go/day11_example4.go) 下面是更容易解读的代码示例。到目前为止,我们用到了`Println`,我们也可以将`Printf`配合`%v`(代表在代码后边的变量)来使用。另外使用`\n`来换行。
Variables may also be defined in a simpler format in your code. Instead of defining that it is a `var` and the `type` you can code this as follows to get the same functionality but a nice cleaner and simpler look for your code. This will only work for variables though and not constants. [这里](../../Days/Go/day11_example4.go)我用到了`%v`来使用默认格式,同时在[fmt package documentation](https://pkg.go.dev/fmt)中也谈到了其他的一些用法。
变量也可以用更简单的方式来定义。除了在定义的时候使用`var`和你可以使用的`type`,下面的代码也可以达到相同的目的,并且能做到更加简洁。这种方法只适用于变量,而不适用于常量(const)。
``` ```
func main() { func main() {
@ -108,53 +113,55 @@ func main() {
const daystotal = 90 const daystotal = 90
``` ```
## Data Types ## 数据类型
In the above examples, we have not defined the type of variables, this is because we can give it a value here and Go is smart enough to know what that type is or at least can infer what it is based on the value you have stored. However, if we want a user to input this will require a specific type.
We have used Strings and Integers in our code so far. Integers for the number of days and strings are for the name of the challenge. 在上述例子中我们并没有定义变量的类型这是因为Go会根据我们输入的值来得知它是什么类型至少它对保存的值有效。然而如果我们想让用户输入特定的数据类型情况又会有所不同。
It is also important to note that each data type can do different things and behaves differently. For example, integers can multiply where strings do not. 我们已经在代码中用到了字符串和整型。整型用来表示天数,字符串用来表示这个挑战的名称。
There are four categories 这里需要关注的,不同的数据类型可以做不一样的事情。例如,整型可以做乘积,而字符串不可以。
- **Basic type**: Numbers, strings, and booleans come under this category. 下面列举了四种类型
- **Aggregate type**: Array and structs come under this category.
- **Reference type**: Pointers, slices, maps, functions, and channels come under this category.
- **Interface type**
The data type is an important concept in programming. Data type specifies the size and type of variable values. - **基础型(Basic type)**,数字(numbers)、字符串(strings)、布尔型(booleans)在这个类别下。
- **聚合型(Aggregate type)**,数组(array)和结构体(structs)在这个类别下。
- **引用型(Reference type)**,指针(pointer)、切片(slices)、集合(maps)、函数(functions)和通道(channels)在这个类别下。
- **接口型(Interface type)**
Go is statically typed, meaning that once a variable type is defined, it can only store data of that type. 数据类型是编程中的一个重要概念。数据类型明确了不同变量的大小和类型。
Go has three basic data types: Go是静态的,这意味着一旦变量的类型被定义了,它只能用于储存该类型的数据。
- **bool**: represents a boolean value and is either true or false Go有三种基本的数据类型
- **Numeric**: represents integer types, floating-point values, and complex types
- **string**: represents a string value
I found this resource super detailed on data types [Golang by example](https://golangbyexample.com/all-data-types-in-golang-with-examples/) - **bool**: 指一个布尔值true或false
- **Numeric**: 指整型、浮点型和复杂型
- **string**: 指一个字符值
I would also suggest [Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I&t=2023s) at this point covers in some detail a lot about the data types in Go. [Golang by example](https://golangbyexample.com/all-data-types-in-golang-with-examples/)给出了很详细的数据类型的例子。
If we need to define a type in our variable we can do this like so: 我也推荐[Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I&t=2023s)里边有谈到许多Go中数据类型的详细内容。
如果我们想去定义一个变量的类型,我们可以做以下操作:
``` ```
var TwitterHandle string var TwitterHandle string
var DaysCompleted uint var DaysCompleted uint
``` ```
Because Go implies variables where a value is given we can print out those values with the following: 因为Go给定了变量一个值我们可以直接打印出来
``` ```
fmt.Printf("challenge is %T, daystotal is %T, dayscomplete is %T\n", conference, daystotal, dayscomplete) fmt.Printf("challenge is %T, daystotal is %T, dayscomplete is %T\n", conference, daystotal, dayscomplete)
``` ```
There are many different types of integer and float types the links above will cover off these in detail.
- **int** = whole numbers 对于整型和浮点型还有许多不同的种类,你可以查看上面提到的链接,阅读更详细的内容。
- **unint** = positive whole numbers
- **floating point types** = numbers that contain a decimal component
## Resources - **int** = 整数
- **unint** = 正整数
- **floating point types** = 含有十进制分量的数
## 相关资料
- [StackOverflow 2021 Developer Survey](https://insights.stackoverflow.com/survey/2021) - [StackOverflow 2021 Developer Survey](https://insights.stackoverflow.com/survey/2021)
- [Why we are choosing Golang to learn](https://www.youtube.com/watch?v=7pLqIIAqZD4&t=9s) - [Why we are choosing Golang to learn](https://www.youtube.com/watch?v=7pLqIIAqZD4&t=9s)
@ -164,6 +171,6 @@ There are many different types of integer and float types the links above will c
- [FreeCodeCamp - Learn Go Programming - Golang Tutorial for Beginners](https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1025s) - [FreeCodeCamp - Learn Go Programming - Golang Tutorial for Beginners](https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1025s)
- [Hitesh Choudhary - Complete playlist](https://www.youtube.com/playlist?list=PLRAV69dS1uWSR89FRQGZ6q9BR2b44Tr9N) - [Hitesh Choudhary - Complete playlist](https://www.youtube.com/playlist?list=PLRAV69dS1uWSR89FRQGZ6q9BR2b44Tr9N)
Next up we are going to start adding some user input functionality to our program so that we are asking how many days have been completed. 接下来我们会开始添加一些用户输入的功能到程序中,我们可以查询已经完成了多少天的挑战。
See you on [Day 12](day12.md). [第十二天](day12.md)见。

View File

@ -1,136 +1,134 @@
--- ---
title: '#90DaysOfDevOps - The Big Picture: Git - Version Control - Day 35' title: '#90DaysOfDevOps - 概述: Git - 版本控制 - 第三十五天'
published: false published: false
description: 90DaysOfDevOps - The Big Picture Git - Version Control description: 90DaysOfDevOps - 概述: Git - 版本控制
tags: 'devops, 90daysofdevops, learning' tags: 'devops, 90daysofdevops, learning'
cover_image: null cover_image: null
canonical_url: null canonical_url: null
id: 1049041 id: 1049041
--- ---
## The Big Picture: Git - Version Control ## 概述: Git - 版本控制
Before we get into git, we need to understand what version control is and why? In this opener for Git, we will take a look at what version control is, the basics of git. 在我们开始git之前我们需要了解什么是版本控制为什么需要它在Git部分的开头我们将介绍版本控制以及git的基础。
### What is Version Control? ### 什么是版本控制?
Git is not the only version control system so here we want to cover what options and what methodologies are available around version control. Git不是唯一的版本控制系统,所以我们会讨论关于版本控制的工具和现成的方法。
The most obvious and a big benefit of Version Control is the ability to track a project's history. We can look back over this repository using `git log` and see that we have many commits and many comments and what has happened so far in the project. Don't worry we will get into the commands later. Now think if this was an actual software project full of source code and multiple people are committing to our software at different times, different authors and then reviewers all are logged here so that we know what has happened, when, by whom and who reviewed. 版本控制最显著、最大的好处是可以记录一个项目的历史。我们可以通过`git log`回看整个仓库,我们会看到有很多的提交(commits)和评论(comments),还有项目中发生了什么。不要急,我们会在后边谈到相关的命令(commands)。可以思考一下,如果这是一个实际的软件项目,有着大量的源代码,很多人在不同的时间对我们的软件进行提交,不同的作者、审核人都在这里进行记录。所以我们可以知道什么时候、发生了什么、谁发起的、谁审阅的。
![](Images/Day35_Git1.png) ![](../../Days/Images/Day35_Git1.png)
Version Control before it was cool, would have been something like manually creating a copy of your version before you made changes. It might be that you also comment out old useless code with the just in case mentality. 首先,版本控制是很妙的,它就像是在你进行修改之前对现有的版本进行了复制。也类似你为了以防万一,把没用的代码注释掉一样。
![](Images/Day35_Git2.png) ![](../../Days/Images/Day35_Git2.png)
I have started using version control over not just source code but pretty much anything, talks about projects like this (90DaysOfDevOps) because why would you not want that rollback and log of everything that has gone on. 我已经在很多地方使用到了版本控制,不单单是源代码方面,也包括一些分享项目(像这个 90DaysOfDevOps)。所以为什么不回滚和记录所有发生的事呢。
However, a big disclaimer **Version Control is not a Backup!** 然而,一个重要声明:**版本控制不是备份!**
Another benefit of Version Control is the ability to manage multiple versions of a project, Let's create an example, we have a free app that is available on all operating systems and then we have a paid-for app also available on all operating systems. The majority of the code is shared between both applications. We could copy and paste our code each commit to each app but that is going to be very messy especially as you scale your development to more than just one person, also mistakes will be made. 版本控制的另一个好处是可以管理一个项目的多个版本。这里举个例子,我们有一个免费的应用可以在所有系统上运行,还有一个付费的应用也可以在所偶系统上运行。主要的代码在两个应用中共享使用。我们可以复制黏贴我们的代码,每一次对不同应用的提交。但这样会导致混乱,特别是当你与多个人改变开发产品时,同时也可能发生一些错误。
The premium app is where we are going to have additional features, let's call them premium commits, the free edition will just contain the normal commits. 下面我们想对一个高级应用程序(premium app)添加新功能,先称之为高级提交(premium commits),其中的自由编辑(free edition)包含了常规的提交。
The way this is achieved in Version Control is through branching. 版本控制通过分支(branching)来实现这点。
![](Images/Day35_Git3.png) ![](../../Days/Images/Day35_Git3.png)
Branching allows for two code streams for the same app as we stated above. But we will still want new features that land in our source code free version to be in our premium and to achieve this we have something called merging. 分支(branching)允许一个应用程序有两个代码流(code streams),就像我们上面提到的。但我们仍然想让新的功能加入到我们的源代码中。合并(merging)就是用来做这件事的。
![](Images/Day35_Git4.png) ![](../../Days/Images/Day35_Git4.png)
Now, this same easy but merging can be complicated because you could have a team working on the free edition and you could have another team working on the premium paid for version and what if both change code that affects aspects of the overall code. Maybe a variable gets updated and breaks something. Then you have a conflict that breaks one of the features. Version Control cannot fix the conflicts that are down to you. But version control allows this to be easily managed. 这个看起来简单,但合并可以是复杂的,因为你可能在自由编辑(free edition)上与团队一起工作,同时你和另一个团队在付费版本(premium paid for version)上工作。如果同时更改代码,会对所有代码产生影响。可能一个变量被更新导致有些东西出错。然后你会得到一个冲突(conflict),它会破坏项目中的功能。版本控制不能修复冲突,但版本控制使它们被易于管理。
The primary reason if you have not picked up so far for version control, in general, is the ability to collaborate. The ability to share code amongst developers and when I say code as I said before more and more we are seeing much more use cases for other reasons to use source control, maybe its a joint presentation you are working on with a colleague or a 90DaysOfDevOps challenge where you have the community offering their corrections and updates throughout the project. 主要的原因是如果你还不太了解版本控制一般的它有协作的功能。与开发者共享代码的能力正如我之前提到的我们为了各种原因去寻找有用的案例、使用源代码。它可能是你与同事制作的演讲或是一个90DaysOfDevOps的挑战你拥有一个社区可以检查内容是否正确或是跟进整个项目。
Without version control how did teams of software developers even handle this? I find it hard enough when I am working on my projects to keep track of things. I expect they would split out the code into each functional module. Maybe a little part of the puzzle then was bringing the pieces together and then problems and issues before anything would get released. 如果没有版本控制,软件开发团队还如何处理这些?当我在一步步制作项目时,我发现这将是困难的。我希望它们能够被拆分进不同的功能模块中。在还没发布时,其中一点点的疑问可能导致各部分的问题一起出现。
With version control, we have a single source of truth. We might all still work on different modules but it enables us to collaborate better. 通过使用版本控制,我们只有一个目标。我们可能在不同模块上进行工作,但这能让大家更好地进行合作。
![](Images/Day35_Git5.png) ![](../../Days/Images/Day35_Git5.png)
Another thing to mention here is that it's not just developers that can benefit from Version Control, it's all members of the team to have visibility but also tools all having awareness or leverage, Project Management tools can be linked here, tracking the work. We might also have a build machine for example Jenkins which we will talk about in another module. A tool that Builds and Packages the system, automating the deployment tests and metrics. 还有一个需要提到的是,不单是开发者可以从版本控制中获益,团队中的所有成员都可以查看。但对工具应该有了解和利用,项目管理工具可以是类似的,用于追踪工作进程。我们可能会有一个构建平台(build machine)例如Jenkins 我们将会在另一个章节谈到。一个创建和打包的系统,自动化部署测试和评估。
### What is Git? ### 什么是Git
Git is a tool that tracks changes to source code or any file, or we could also say Git is an open-source distributed version control system. Git是一个追踪代码或文件改变的工具,或者我们也可以说它是一个开源的分布式版本控制系统。
There are many ways in which git can be used on our systems, most commonly or at least for me I have seen it in at the command line, but we also have graphical user interfaces and tools like Visual Studio Code that have git aware operations we can take advantage of. Git可以有多种方式在我们的系统中使用对于我来说最常见的是命令行。我们也有图像交互工具例如Visual Studio Code就有git操作我们可以使用它。
Now we are going to run through a high-level overview before we even get Git installed on our local machine. 在开始安装Git之前我们要看看宏观的概述。
Let's take the folder we created earlier. 我们之前创建了一个文件夹。
![](Images/Day35_Git2.png) ![](../../Days/Images/Day35_Git2.png)
To use this folder with version control we first need to initiate this directory using the `git init command. For now, just think that this command puts our directory as a repository in a database somewhere on our computer. 要将版本控制应用在这个文件夹中,我们第一步需要使用`git init`来初始化。可以想象这个命令将我们的目录变成了一个仓库(repository),并放入了电脑某处的数据库中。
![](Images/Day35_Git6.png) ![](../../Days/Images/Day35_Git6.png)
Now we can create some files and folders and our source code can begin or maybe it already has and we have something in here already. We can use the `git add .` command which puts all files and folders in our directory into a snapshot but we have not yet committed anything to that database. We are just saying all files with the `.` are ready to be added. 现在我们可以创建一些文件、文件夹和源代码,或者它们已经在这里了。我们使用`git add .`命令来将目录中所有文件和文件夹放入一个快照(snapshot),到此我们还没有提交(commit)任何东西到数据库中。我们只是将所有的文件使用`.`来进行了添加。
![](Images/Day35_Git7.png) ![](../../Days/Images/Day35_Git7.png)
Then we want to go ahead and commit our files, we do this with the `git commit -m "My First Commit"` command. We can give a reason for our commit and this is suggested so we know what has happened for each commit. 然后我们想进一步并提交文件,使用`git commit -m "My First Commit"`。我们可以描述提交的原因,这是一个好的行为,后续我们才能知道每次提交都发生了什么。
![](Images/Day35_Git8.png) ![](../../Days/Images/Day35_Git8.png)
We can now see what has happened within the history of the project. Using the `git log` command. 现在我们可以用`git log`来看看项目的历史记录里发生了什么。
![](Images/Day35_Git9.png) ![](../../Days/Images/Day35_Git9.png)
We can also check the status of our repository by using `git status` this shows we have nothing to commit and we can add a new file called samplecode.ps1. If we then run the same `git status you will see that we file to be committed. 如果我们在这里创建一个新的文件`samplecode.ps1`,那么状态提示将发生改变。我们也可以用`git status`来检查我们仓库的状态。它会提示我们没有提交任何的东西可以添加一个新的文件samplecode.ps1。如果我们后续输入相同的命令`git status`,你会看到将会被提交的文件。
![](Images/Day35_Git10.png) ![](../../Days/Images/Day35_Git10.png)
Add our new file using the `git add samplecode.ps1` command and then we can run `git status` again and see our file is ready to be committed. 使用`git add samplecode.ps1`来添加新的文件,然后再次输入`git status`。我们看到这个文件已经准备好被提交了。
![](Images/Day35_Git11.png) ![](../../Days/Images/Day35_Git11.png)
Then issue `git commit -m "My Second Commit"` command. 下面输入`git commit -m "My Second Commit"`。
![](Images/Day35_Git12.png) ![](../../Days/Images/Day35_Git12.png)
Another `git status` now shows everything is clean again. 这次的`git status`再次告诉我们所有东西都是最新的。
![](Images/Day35_Git13.png) ![](../../Days/Images/Day35_Git13.png)
We can then use the `git log` command which shows the latest changes and first commit. 后续我们可以使用`git log`来查看最新的和第一个提交。
![](Images/Day35_Git14.png) ![](../../Days/Images/Day35_Git14.png)
If we wanted to see the changes between our commits i.e what files have been added or modified we can use the `git diff b8f8 709a` 如果我们想每次提交中的改变内容,比如哪些文件被添加或修改了,我们可以使用`git diff b8f8 709a`。
![](Images/Day35_Git15.png) ![](../../Days/Images/Day35_Git15.png)
Which then displays what has changed in our case we added a new file. 这样会显示发生了什么改变。在我们的例子中,我们添加了一个新的文件。
![](Images/Day35_Git16.png) ![](../../Days/Images/Day35_Git16.png)
We can also and we will go deeper into this later on but we can jump around our commits i.e we can go time travelling! By using our commit number we can use the `git checkout 709a` command to jump back in time without losing our new file. 我们会在后续进一步学习相关用法,至此我们可以在提交中穿梭了,就像是时间旅行一样!通过利用我们的提交序号(commit number)`git checkout 709a`可以跳回到之前的版本并保留我们新建的文件。
![](Images/Day35_Git17.png) ![](../../Days/Images/Day35_Git17.png)
But then equally we will want to move forward as well and we can do this the same way with the commit number or you can see here we are using the `git switch -` command to undo our operation. 但同时,我们要继续前进。相同的,我们可以使用提交序号或直接使用`git switch -`来撤销我们的操作。
![](Images/Day35_Git18.png) ![](../../Days/Images/Day35_Git18.png)
The TLDR; 总结:
- Tracking a projects history - 追踪一个项目的历史记录
- Managing multiple versions of a project - 管理一个项目的多个版本
- Sharing code amongst developers and a wider scope of teams and tools - 与开发者、更广范围的团队和工具分享源代码
- Coordinating teamwork - 协调团队合作
- Oh and there is some time travel! - 和时间旅行的可能!
这似乎是跳跃的,但希望你能有所收获,了解版本控制的命令的功能和宏观思路。
后续我们会安装git并在你本地的机器上设置进一步了解有关的使用案例和命令。
This might have seemed a jump around but hopefully, you can see without really knowing the commands used the powers and the big picture behind Version Control. ## 相关资料
Next up we will be getting git installed and set up on your local machine and diving a little deeper into some other use cases and commands that we can achieve in Git.
## Resources
- [What is Version Control?](https://www.youtube.com/watch?v=Yc8sCSeMhi4) - [What is Version Control?](https://www.youtube.com/watch?v=Yc8sCSeMhi4)
- [Types of Version Control System](https://www.youtube.com/watch?v=kr62e_n6QuQ) - [Types of Version Control System](https://www.youtube.com/watch?v=kr62e_n6QuQ)
@ -139,5 +137,5 @@ Next up we will be getting git installed and set up on your local machine and di
- [Git and GitHub for Beginners - Crash Course](https://www.youtube.com/watch?v=RGOj5yH7evk&t=8s) - [Git and GitHub for Beginners - Crash Course](https://www.youtube.com/watch?v=RGOj5yH7evk&t=8s)
- [Complete Git and GitHub Tutorial](https://www.youtube.com/watch?v=apGV9Kg7ics) - [Complete Git and GitHub Tutorial](https://www.youtube.com/watch?v=apGV9Kg7ics)
See you on [Day 36](day36.md) [第三十六天](day36.md)见!

View File

@ -1,71 +1,71 @@
--- ---
title: '#90DaysOfDevOps - Installing & Configuring Git - Day 36' title: '#90DaysOfDevOps - 安装和配置 Git - 第三十六天'
published: false published: false
description: 90DaysOfDevOps - Installing & Configuring Git description: 90DaysOfDevOps - 安装和配置 Git
tags: "devops, 90daysofdevops, learning" tags: "devops, 90daysofdevops, learning"
cover_image: null cover_image: null
canonical_url: null canonical_url: null
id: 1048738 id: 1048738
--- ---
## Installing & Configuring Git ## 安装和配置 Git
Git is a open source, cross platform tool for version control. If I like me you are using Ubuntu or most Linux environments you might find that you already git installed but we are going to run through the install and configuration. Git是一个开源的、跨平台的版本控制工具。如果你也在使用Ubuntu或其他Linux环境你会发现你已经安装了git但我们还是会讲一下它的安装和设置。
Even if you already have git installed on your system it is also a good idea to make sure we are up to date. 即使你已经在你的系统上安装了git最好还是确定我们在用的是最新的。
### Installing Git ### 安装Git
As already mentioned Git is cross platform, we will be running through Windows and Linux but you can find macOS also listed [here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) 就像前面提到的Git是跨平台的。我们可以在Windows和Linux上运行并且macOS也在[安装教程](https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git)中。
For [Windows](https://git-scm.com/download/win) we can grab our installers from the official site. 对于[Windows版本](https://git-scm.com/download/win),我们可以从官网上下载安装包。
You could also use `winget` on your Windows machine, think of this as your Windows Application Package Manager. 你也可以在Windows上使用`winget`类似Windows上的应用程序包管理器。
Before we install anything lets see what version we have on our Windows Machine. Open a PowerShell window and run `git --version` 在开始安装之前我们可以看看已安装的版本信息。打开PowerShell并运行`git --version`
![](Images/Day36_Git1.png) ![](../../Days/Images/Day36_Git1.png)
We can also check our WSL Ubuntu version of Git as well. 也可以在查看WSL Ubuntu上的Git版本。
![](Images/Day36_Git2.png) ![](../../Days/Images/Day36_Git2.png)
At the time of writing the latest Windows release is `2.35.1` so we have some updating to do there which I will run through. I expect the same for Linux. 在编辑这个文档的时候最新的Windows版本是`2.35.1`因此我会在后续做更新在Linux上也类似的。
I went ahead and downloaded the latest installer and ran through the wizard and will document that here. The important thing to note is that git will uninstall previous versions before installing the latest. 我下载了最新的安装包跟着运行向导完成安装。值得注意的是git会在安装最新版之前将旧版本卸载掉。
Meaning that the process shown below is also the same process for the most part as if you were installing from no git. 这意味着下面的流程也将适用于没有安装过git的情形。
It is a very simple installation. Once downloaded double click and get started. Read through the GNU license agreement. But remember this is free and open source software. 安装的过程很简单。下载完成后双击安装包开始。阅读GNU许可。但记住这是免费的、开源的软件。
![](Images/Day36_Git3.png) ![](../../Days/Images/Day36_Git3.png)
Now we can choose additional components that we would like to also install but also associate with git. On Windows I always make sure I install Git Bash as this allows us to run bash scripts on Windows. 在安装的同时我们可以选择一些额外的与之相关选项。在Windows上我通常会安装Git Bash它可以运行bash脚本。
![](Images/Day36_Git4.png) ![](../../Days/Images/Day36_Git4.png)
We can then choose which SSH Executable we wish to use. IN leave this as the bundled OpenSSH that you might have seen in the Linux section. 后续我们可以在SSH上执行。相关的内容在Linux部分有提到OpenSSH。
![](Images/Day36_Git5.png) ![](../../Days/Images/Day36_Git5.png)
We then have experimental features that we may wish to enable, for me I don't need them so I don't enable, you can always come back in through the installation and enable these later on. 我们按需求勾选实验性的功能,而我并不需要这部分,你也可以随时回到这一步进行添加。
![](Images/Day36_Git6.png) ![](../../Days/Images/Day36_Git6.png)
Installation complete, we can now choose to open Git Bash and or the latest release notes. 安装完成了我们可以勾选打开Git Bash和查看更新文档。
![](Images/Day36_Git7.png) ![](../../Days/Images/Day36_Git7.png)
The final check is to take a look in our PowerShell window what version of git we have now. 最后一步要在PowerShell中检查已安装的git版本信息。
![](Images/Day36_Git8.png) ![](../../Days/Images/Day36_Git8.png)
Super simple stuff and now we are on the latest version. On our Linux machine we seemed to be a little behind so we can also walk through that update process. 很简单现在我们已经有了最新版。在Linux机器上我们相差了一些版本所以我们可以进行一下更新。
I simply run the `sudo apt-get install git` command. 我简单运行`sudo apt-get install git`命令。
![](Images/Day36_Git9.png) ![](../../Days/Images/Day36_Git9.png)
You could also run the following which will add the git repository for software installations. 你也可以使用下面的命令行它会把软件安装添加到git仓库中。
``` ```
sudo add-apt-repository ppa:git-core/ppa -y sudo add-apt-repository ppa:git-core/ppa -y
@ -73,74 +73,76 @@ sudo apt-get update
sudo apt-get install git -y sudo apt-get install git -y
git --version git --version
``` ```
### Configuring Git
When we first use git we have to define some settings, ### 配置Git
当我们第一次使用git时我们已经定义了一些设置
- Name - Name
- Email - Email
- Default Editor - Default Editor
- Line Ending - Line Ending
This can be done at three levels 这些可以在三个层次上完成
- System = All users - System = 全部用户
- Global = All repositories of the current user - Global = 当前用户的全部仓库
- Local = The current repository - Local = 当前仓库
Example: 例如:
`git config --global user.name "Michael Cade"` `git config --global user.name "Michael Cade"`
`git config --global user.email Michael.Cade@90DaysOfDevOPs.com"` `git config --global user.email Michael.Cade@90DaysOfDevOPs.com"`
Depending on your Operating System will determine the default text editor. In my Ubuntu machine without setting the next command is using nano. The below command will change this to visual studio code.
你的操作系统会有默认的文本编辑器。在我的Ubuntu机器上默认使用的是nano。下面的命令会把它更改为visual studio code。
`git config --global core.editor "code --wait"` `git config --global core.editor "code --wait"`
now if we want to be able to see all git configuration then we can use the following command. 现在如果我们想查看所有的git设置我们可以使用下面的命令。
`git config --global -e` `git config --global -e`
![](Images/Day36_Git10.png) ![](../../Days/Images/Day36_Git10.png)
On any machine this file will be named `.gitconfig` on my Windows machine you will find this in your user account directory. 在所有机器上,这个文件被命名为`.gitconfig`。在我的Windows电脑上它被放在了用户文件夹中。
![](Images/Day36_Git11.png) ![](../../Days/Images/Day36_Git11.png)
### Git Theory ### Git理论
I mentioned in the post yesterday that there were other version control types and we can split these down into two different types. One is Client Server and the other is Distributed. 在昨天的笔记中有提到,版本控制有不同的类型,我们可以把它们分为两类。一种是客户端服务器,另一种是分布式。
### Client-Server Version Control ### 客户端服务器的版本控制
Before git was around Client-Server was the defacto method for version control. An example of this would be [Apache Subversion](https://subversion.apache.org/) which is an open source version control system founded in 2000. 在git之前版本控制通常使用的客户端服务器。[Apache Subversion](https://subversion.apache.org/)就是一个例子他是一个2000年建立的开源版本控制系统。
In this model of Client-Server version control, the first step the developer downloads the source code, the actual files from the server. This doesnt remove the conflicts but it does remove the complexity of the conflicts and how to resolve them. 在这个客户端服务器的版本控制模型中,第一步是从服务器下载源代码。这不会消除冲突(conflicts),但它消除了冲突的复杂性,并给出解决方案。
![](Images/Day36_Git12.png) ![](../../Days/Images/Day36_Git12.png)
Now for example lets say we have two developers working on the same files and one wins the race and commits or uploads their file back to the server first with their new changes. When the second developer goes to update they have a conflict. 举个例子,我们有两个开发者在同一个文件上工作,一个更早地向服务器提交了新的代码。当另一个开发者想要提交时,就存在冲突(conflict)。
![](Images/Day36_Git13.png) ![](../../Days/Images/Day36_Git13.png)
So now the Dev needs to pull down the first devs code change next to theirs check and then commit once those conflicts have been settled. 因此,开发的过程需要获得第一次的代码的修改,并在冲突被处理后再进行提交。
![](Images/Day36_Git15.png) ![](../../Days/Images/Day36_Git15.png)
### Distributed Version Control ### 分布式的版本控制
Git is not the only distributed version control system. But it is very much the defacto. Git不是唯一的分布式版本控制系统。但它是较常见的一个。
Some of the major benefits of Git are: Git的主要优势有
- Fast - 快速
- Smart - 智能
- Flexible - 灵活
- Safe & Secure - 安全
Unlike the Client-Server version control model, each developer downloads the the source repository meaning everything. History of commits, all the branches etc. etc. 与客户端服务器版本控制不同的,每个开发者下载源仓库的全部内容。提交记录、所有分支...
![](Images/Day36_Git16.png) ![](../../Days/Images/Day36_Git16.png)
## Resources ## 相关资料
- [What is Version Control?](https://www.youtube.com/watch?v=Yc8sCSeMhi4) - [What is Version Control?](https://www.youtube.com/watch?v=Yc8sCSeMhi4)
- [Types of Version Control System](https://www.youtube.com/watch?v=kr62e_n6QuQ) - [Types of Version Control System](https://www.youtube.com/watch?v=kr62e_n6QuQ)
@ -149,4 +151,4 @@ Unlike the Client-Server version control model, each developer downloads the the
- [Git and GitHub for Beginners - Crash Course](https://www.youtube.com/watch?v=RGOj5yH7evk&t=8s) - [Git and GitHub for Beginners - Crash Course](https://www.youtube.com/watch?v=RGOj5yH7evk&t=8s)
- [Complete Git and GitHub Tutorial](https://www.youtube.com/watch?v=apGV9Kg7ics) - [Complete Git and GitHub Tutorial](https://www.youtube.com/watch?v=apGV9Kg7ics)
See you on [Day 37](day37.md) [第三十七天](day37.md)见