1
00:00:00,480 --> 00:00:02,940
In this lesson we're going to discuss git

2
00:00:02,940 --> 00:00:04,650
and its core commands.

3
00:00:04,650 --> 00:00:06,960
Now, most of the time when developers collaborate

4
00:00:06,960 --> 00:00:08,310
for a certain project,

5
00:00:08,310 --> 00:00:11,010
they need to have a distributed version control system,

6
00:00:11,010 --> 00:00:13,020
so they can easily manage the different versions

7
00:00:13,020 --> 00:00:15,680
of the code they're writing and minimize any conflicts,

8
00:00:15,680 --> 00:00:17,520
while being able to revert back

9
00:00:17,520 --> 00:00:19,080
to older versions of the code

10
00:00:19,080 --> 00:00:21,120
if they make any kind of mistake.

11
00:00:21,120 --> 00:00:22,860
In this case, you're going to be able

12
00:00:22,860 --> 00:00:24,960
to use something like Git.

13
00:00:24,960 --> 00:00:27,060
Now let's say you just arrived from your vacation,

14
00:00:27,060 --> 00:00:28,560
and you wanted to check on the progress

15
00:00:28,560 --> 00:00:30,210
that your team has made on a project

16
00:00:30,210 --> 00:00:31,500
that you were working on.

17
00:00:31,500 --> 00:00:33,960
Well, if you had a version control system,

18
00:00:33,960 --> 00:00:35,670
you could very easily do this

19
00:00:35,670 --> 00:00:37,110
by knowing what version it was,

20
00:00:37,110 --> 00:00:40,650
or what date it was last controlled or changed.

21
00:00:40,650 --> 00:00:41,490
Now by far,

22
00:00:41,490 --> 00:00:43,980
the most commonly used modern version control system

23
00:00:43,980 --> 00:00:47,670
in the world is known as Git, G I T.

24
00:00:47,670 --> 00:00:51,600
Now Git is a mature, actively maintained open-source project

25
00:00:51,600 --> 00:00:54,060
that was originally developed back in 2005

26
00:00:54,060 --> 00:00:57,540
by the same person who developed the Linux operating system.

27
00:00:57,540 --> 00:01:00,210
Now a staggering number of software projects rely

28
00:01:00,210 --> 00:01:02,010
on Git for version control,

29
00:01:02,010 --> 00:01:03,960
including a lot of commercial projects,

30
00:01:03,960 --> 00:01:06,540
as well as most of the open source community.

31
00:01:06,540 --> 00:01:08,280
Its core component of Git is known

32
00:01:08,280 --> 00:01:10,170
as the Git repository.

33
00:01:10,170 --> 00:01:12,840
This is a storage area where versions of code

34
00:01:12,840 --> 00:01:15,270
and related files are going to be put.

35
00:01:15,270 --> 00:01:16,740
To get started with Git,

36
00:01:16,740 --> 00:01:18,600
you can use an available package manager

37
00:01:18,600 --> 00:01:20,610
to install the Git package.

38
00:01:20,610 --> 00:01:24,090
To run the git command, simply type in git, the options,

39
00:01:24,090 --> 00:01:25,950
and the subcommand you want.

40
00:01:25,950 --> 00:01:28,140
Now some of the major subcommands that you're going to use

41
00:01:28,140 --> 00:01:31,170
with Git include things like the config subcommand.

42
00:01:31,170 --> 00:01:33,570
This is used to set up options for a repository

43
00:01:33,570 --> 00:01:37,290
or for your Git users, as well as other global options.

44
00:01:37,290 --> 00:01:40,320
The init subcommand is used to create a Git repository

45
00:01:40,320 --> 00:01:42,990
or re-initialize an existing one.

46
00:01:42,990 --> 00:01:45,720
The clone subcommand is used to create a working copy

47
00:01:45,720 --> 00:01:47,520
of an existing repository.

48
00:01:47,520 --> 00:01:50,070
And the add subcommand is used to add files

49
00:01:50,070 --> 00:01:52,410
to be tracked by the Git repository.

50
00:01:52,410 --> 00:01:54,390
The commit subcommand is going to be used

51
00:01:54,390 --> 00:01:56,760
to update the Git repository with your changes,

52
00:01:56,760 --> 00:01:58,710
and this essentially creates a snapshot

53
00:01:58,710 --> 00:02:01,680
of that repository, or a snapshot of the files

54
00:02:01,680 --> 00:02:04,050
at that particular moment in time.

55
00:02:04,050 --> 00:02:06,090
Now the status subcommand is going to be used

56
00:02:06,090 --> 00:02:08,520
to display the status of the repository.

57
00:02:08,520 --> 00:02:11,310
The branch subcommand is used to manage branches

58
00:02:11,310 --> 00:02:14,280
or pointers to specific repository snapshots

59
00:02:14,280 --> 00:02:16,260
after you've committed changes.

60
00:02:16,260 --> 00:02:19,230
The merge subcommand is going to be used to integrate changes

61
00:02:19,230 --> 00:02:21,690
from one branch into a master branch.

62
00:02:21,690 --> 00:02:24,930
The pull subcommand is used to acquire and merge changes

63
00:02:24,930 --> 00:02:27,150
that are made to other repositories and branches

64
00:02:27,150 --> 00:02:29,370
into the local working copy.

65
00:02:29,370 --> 00:02:32,730
The push subcommand is used to upload a local working copy

66
00:02:32,730 --> 00:02:35,370
of a repository to a remote repository,

67
00:02:35,370 --> 00:02:38,310
such as a centralized repository or database.

68
00:02:38,310 --> 00:02:41,220
The log subcommand is used to display the changes made

69
00:02:41,220 --> 00:02:42,840
to a local repository.

70
00:02:42,840 --> 00:02:45,090
And the checkout subcommand is used to switch

71
00:02:45,090 --> 00:02:48,030
to a specific branch that you want to work with.

72
00:02:48,030 --> 00:02:50,700
The tag subcommand is going to be used to add a tag

73
00:02:50,700 --> 00:02:51,960
to your Git repository,

74
00:02:51,960 --> 00:02:53,820
which acts as a specific reference point

75
00:02:53,820 --> 00:02:55,350
in your Git history.

76
00:02:55,350 --> 00:02:57,630
These git tags are used to capture the specific point

77
00:02:57,630 --> 00:02:59,760
in history that is further used to point

78
00:02:59,760 --> 00:03:01,350
to a released version.

79
00:03:01,350 --> 00:03:04,020
Now, this tag doesn't really change a branch,

80
00:03:04,020 --> 00:03:05,880
but instead it just adds a marker

81
00:03:05,880 --> 00:03:07,770
for us to be able to realize what happened

82
00:03:07,770 --> 00:03:09,570
at that given point in time.

83
00:03:09,570 --> 00:03:12,450
The rebase subcommand is one of two Git utilities

84
00:03:12,450 --> 00:03:14,160
that specializes in integrating changes

85
00:03:14,160 --> 00:03:15,840
from one branch into another.

86
00:03:15,840 --> 00:03:17,880
The other one is the merge command.

87
00:03:17,880 --> 00:03:19,530
Now, when we talk about the merge command,

88
00:03:19,530 --> 00:03:22,530
it's always about making a forward-moving change record

89
00:03:22,530 --> 00:03:24,570
that's going to merge both bases into one,

90
00:03:24,570 --> 00:03:25,590
and that's going to become the one

91
00:03:25,590 --> 00:03:27,030
we're going to use moving forward.

92
00:03:27,030 --> 00:03:29,700
But with rebase, we instead are going to use this

93
00:03:29,700 --> 00:03:32,580
to do a rewriting of our historical features.

94
00:03:32,580 --> 00:03:35,250
For example, when we rebase, this is really the process

95
00:03:35,250 --> 00:03:37,470
of moving or combining a sequence of commits

96
00:03:37,470 --> 00:03:39,150
to a new base commit.

97
00:03:39,150 --> 00:03:41,040
Now the primary reason for rebasing is

98
00:03:41,040 --> 00:03:43,260
to maintain a linear project history.

99
00:03:43,260 --> 00:03:44,880
And this allows you to re-baseline

100
00:03:44,880 --> 00:03:47,850
into a new baseline history moving forward.

101
00:03:47,850 --> 00:03:49,380
All right, let's say you need

102
00:03:49,380 --> 00:03:52,230
to create a local repository and commit some changes

103
00:03:52,230 --> 00:03:54,180
to it as you go through the process.

104
00:03:54,180 --> 00:03:56,880
Well, let's talk about the process flow for doing this.

105
00:03:56,880 --> 00:03:59,400
First, you're going to configure global settings,

106
00:03:59,400 --> 00:04:00,690
including the username,

107
00:04:00,690 --> 00:04:05,690
by typing git config dash dash global user.name

108
00:04:06,000 --> 00:04:08,100
quote User quote,

109
00:04:08,100 --> 00:04:10,590
where user's the name of the user you want to create.

110
00:04:10,590 --> 00:04:12,690
Second, you're going to create a directory

111
00:04:12,690 --> 00:04:14,520
where your project's going to reside.

112
00:04:14,520 --> 00:04:16,110
To do this, you're going to type in

113
00:04:16,110 --> 00:04:19,350
mkdir /dev-project,

114
00:04:19,350 --> 00:04:20,970
and then we're going to change ourself

115
00:04:20,970 --> 00:04:23,670
into that directory by using the CD command.

116
00:04:23,670 --> 00:04:26,730
Now, we want to initialize this directory using Git

117
00:04:26,730 --> 00:04:28,530
to make it a Git repository.

118
00:04:28,530 --> 00:04:29,760
So we'll do this by typing

119
00:04:29,760 --> 00:04:33,720
git init /dev-project.

120
00:04:33,720 --> 00:04:34,950
Now, at this point,

121
00:04:34,950 --> 00:04:38,100
we want to put some project files into this repository.

122
00:04:38,100 --> 00:04:39,180
These are going to be the files

123
00:04:39,180 --> 00:04:41,160
that make up the actual development project

124
00:04:41,160 --> 00:04:43,740
that we're storing and controlling, using Git.

125
00:04:43,740 --> 00:04:45,960
In Git, you create a working copy

126
00:04:45,960 --> 00:04:47,700
by using the clone subcommand.

127
00:04:47,700 --> 00:04:52,700
So, we're going to type in git clone /dev-project.

128
00:04:53,280 --> 00:04:57,090
Next we're going to type git add myfile.

129
00:04:57,090 --> 00:04:59,040
And this will add the project files

130
00:04:59,040 --> 00:05:02,970
into the Git tracking system, specifically, myfile.

131
00:05:02,970 --> 00:05:05,220
Next, we want to be able to commit the changes

132
00:05:05,220 --> 00:05:07,410
by taking a snapshot of our project.

133
00:05:07,410 --> 00:05:09,630
So at this stage, we're going to enter a message

134
00:05:09,630 --> 00:05:11,910
that summarizes what changes we made.

135
00:05:11,910 --> 00:05:16,277
For example, I might type in git commit -m,

136
00:05:16,277 --> 00:05:20,460
and then quote, initial commit or whatever message I want,

137
00:05:20,460 --> 00:05:21,660
end quote.

138
00:05:21,660 --> 00:05:23,970
Now at this point, if we want to see the status

139
00:05:23,970 --> 00:05:27,420
of our Git repository, we can type in git status,

140
00:05:27,420 --> 00:05:29,010
and this will retrieve the current status

141
00:05:29,010 --> 00:05:30,600
of the changed files.

142
00:05:30,600 --> 00:05:33,240
So if I had three files that were being worked on

143
00:05:33,240 --> 00:05:34,980
for a particular step in this project,

144
00:05:34,980 --> 00:05:37,320
but only two were ready to be committed at this time,

145
00:05:37,320 --> 00:05:40,920
those would show up here as added, but not yet committed.

146
00:05:40,920 --> 00:05:44,070
Another option here is to work with Git branches.

147
00:05:44,070 --> 00:05:45,330
Now branching is a feature

148
00:05:45,330 --> 00:05:48,000
in most modern version control systems.

149
00:05:48,000 --> 00:05:49,710
In Git, branches are part

150
00:05:49,710 --> 00:05:51,840
of your everyday development process,

151
00:05:51,840 --> 00:05:53,940
and Git branches are effectively a pointer

152
00:05:53,940 --> 00:05:56,970
to a snapshot of your different changes you've made.

153
00:05:56,970 --> 00:05:59,580
When you want to add a new feature or a fix or a bug,

154
00:05:59,580 --> 00:06:01,140
no matter how big or small,

155
00:06:01,140 --> 00:06:04,680
you're going to spawn a new branch to encapsulate your changes.

156
00:06:04,680 --> 00:06:07,290
By creating a branch in the master copy of the code,

157
00:06:07,290 --> 00:06:08,797
you're going to be able to then work on that

158
00:06:08,797 --> 00:06:11,640
without affecting all the other developers.

159
00:06:11,640 --> 00:06:14,430
To do this, you're going to type git branch,

160
00:06:14,430 --> 00:06:16,627
and then the name of the new branch that you want.

161
00:06:16,627 --> 00:06:18,630
Then whenever you're done, and you need

162
00:06:18,630 --> 00:06:21,360
to merge those changes back into the master branch,

163
00:06:21,360 --> 00:06:23,250
you're going to type git merge,

164
00:06:23,250 --> 00:06:25,020
and the name of that new branch.

165
00:06:25,020 --> 00:06:27,420
This will be able to make those changes and put them back

166
00:06:27,420 --> 00:06:29,810
into the master branch, and doing so allows you

167
00:06:29,810 --> 00:06:31,616
to create new and improved versions

168
00:06:31,616 --> 00:06:34,263
of the software and the master branch.

169
00:06:35,130 --> 00:06:37,830
Now, this is one of the reasons that Git is so powerful,

170
00:06:37,830 --> 00:06:40,440
is this ability to create branches and then put them back

171
00:06:40,440 --> 00:06:43,170
and commit them into the main master branch.

172
00:06:43,170 --> 00:06:45,510
This allows Git to be very collaborative,

173
00:06:45,510 --> 00:06:48,570
and it really becomes game-changing for software developers.

174
00:06:48,570 --> 00:06:50,610
So, so far we've been collaborating

175
00:06:50,610 --> 00:06:52,830
with our most important collaborator, right?

176
00:06:52,830 --> 00:06:54,780
We've been working with ourself,

177
00:06:54,780 --> 00:06:56,700
but Git allows us to collaborate

178
00:06:56,700 --> 00:07:00,000
with somebody else or with lots of other people, as well,

179
00:07:00,000 --> 00:07:02,190
as long as they're also going to have the same privileges

180
00:07:02,190 --> 00:07:04,020
to our Git repository.

181
00:07:04,020 --> 00:07:06,930
Normally, there's going to be some sort of a process flow

182
00:07:06,930 --> 00:07:09,357
when you're collaborating with other developers using Git,

183
00:07:09,357 --> 00:07:10,710
and you're going to work that out,

184
00:07:10,710 --> 00:07:13,080
based on your organizational requirements.

185
00:07:13,080 --> 00:07:15,181
For example, a standard way of doing this

186
00:07:15,181 --> 00:07:17,910
might be to first type git pull,

187
00:07:17,910 --> 00:07:19,710
and then pull in the other branch

188
00:07:19,710 --> 00:07:22,170
to pull the other developers' code and changes,

189
00:07:22,170 --> 00:07:24,510
and merge them into your local repository.

190
00:07:24,510 --> 00:07:28,470
Then you might type git push and the remote repository

191
00:07:28,470 --> 00:07:31,050
with your branch to push your own changes back

192
00:07:31,050 --> 00:07:32,730
to that remote repository.

193
00:07:32,730 --> 00:07:34,920
This allows developers all over the world

194
00:07:34,920 --> 00:07:36,450
to pull and push changes

195
00:07:36,450 --> 00:07:39,750
into the central repository so they can all work together.

196
00:07:39,750 --> 00:07:42,420
Now, any changes made locally are then going to be uploaded

197
00:07:42,420 --> 00:07:45,630
to that central repository using that push command.

198
00:07:45,630 --> 00:07:47,340
And you can see what changes were merged

199
00:07:47,340 --> 00:07:50,400
and what other actions were taken on that repository.

200
00:07:50,400 --> 00:07:51,720
Again, if you want to be able

201
00:07:51,720 --> 00:07:53,820
to see what's inside your project directory,

202
00:07:53,820 --> 00:07:56,400
you can type git log, and that's going to be able

203
00:07:56,400 --> 00:07:58,544
to tell you all the changes that have happened

204
00:07:58,544 --> 00:08:00,630
over a certain period of time.

205
00:08:00,630 --> 00:08:01,470
For example,

206
00:08:01,470 --> 00:08:05,880
if I enter git log dash dash since equals 10 dot days,

207
00:08:05,880 --> 00:08:07,770
I'm going to see all the commits that have happened

208
00:08:07,770 --> 00:08:09,600
in the last 10 days.

209
00:08:09,600 --> 00:08:12,330
I can use this information to troubleshoot any issues

210
00:08:12,330 --> 00:08:13,290
that may have come up

211
00:08:13,290 --> 00:08:16,170
in the last 10 days, or anything I might have missed

212
00:08:16,170 --> 00:08:18,750
if I was on vacation for the last 10 days.

213
00:08:18,750 --> 00:08:21,210
Another thing you can do is you can navigate and switch

214
00:08:21,210 --> 00:08:24,330
between branches of a project by using the checkout command

215
00:08:24,330 --> 00:08:25,620
inside of Git.

216
00:08:25,620 --> 00:08:28,140
To do this you'll type git checkout

217
00:08:28,140 --> 00:08:30,480
and the specific branch you want to check out.

218
00:08:30,480 --> 00:08:32,909
This enables developers to focus their attention

219
00:08:32,909 --> 00:08:35,309
on different branches of the same project,

220
00:08:35,309 --> 00:08:37,799
and really hone in on what they're working on.

221
00:08:37,799 --> 00:08:39,299
Now, another thing that we need to talk about

222
00:08:39,299 --> 00:08:42,390
is this file known as the git ignore file.

223
00:08:42,390 --> 00:08:45,060
This is written as dot gitignore,

224
00:08:45,060 --> 00:08:47,550
and it's a file that exists within the repository.

225
00:08:47,550 --> 00:08:50,100
The purpose of this file is to identify files

226
00:08:50,100 --> 00:08:52,666
that should be ignored during a commit action.

227
00:08:52,666 --> 00:08:56,190
For example, you might have a read me dot TXT file,

228
00:08:56,190 --> 00:08:58,560
or a project to do dot TXT list

229
00:08:58,560 --> 00:08:59,970
that's inside this directory,

230
00:08:59,970 --> 00:09:01,320
and you don't need that to be committed

231
00:09:01,320 --> 00:09:03,712
as part of the project, but it does reside

232
00:09:03,712 --> 00:09:07,020
inside your project directory just for your convenience.

233
00:09:07,020 --> 00:09:09,690
Well, if you're going to use those and you list them out

234
00:09:09,690 --> 00:09:12,480
inside the git ignore file, those will get ignored

235
00:09:12,480 --> 00:09:14,190
when you do a commit.

236
00:09:14,190 --> 00:09:17,970
Another thing that's important is the star dot git slash.

237
00:09:17,970 --> 00:09:20,370
This is a directory that contains all the files

238
00:09:20,370 --> 00:09:23,070
that Git is going to use to manage version control

239
00:09:23,070 --> 00:09:24,630
for your project.

240
00:09:24,630 --> 00:09:26,910
It's going to be a single location where Git stores

241
00:09:26,910 --> 00:09:28,380
all of its information.

242
00:09:28,380 --> 00:09:30,900
Think about it like almost like a flat database.

243
00:09:30,900 --> 00:09:33,630
The directory here resides in the project directory,

244
00:09:33,630 --> 00:09:35,520
and it's created with the git init command

245
00:09:35,520 --> 00:09:37,470
at the beginning of your project.

246
00:09:37,470 --> 00:09:40,920
So as you can see, there is a lot to know inside of Git,

247
00:09:40,920 --> 00:09:44,010
but the good news is, for Linux Plus, you're not going to need

248
00:09:44,010 --> 00:09:45,540
to know it fully in depth.

249
00:09:45,540 --> 00:09:48,360
Git is something used heavily by software developers,

250
00:09:48,360 --> 00:09:51,090
and not nearly as much by system administrators.

251
00:09:51,090 --> 00:09:52,500
It's something you should know about,

252
00:09:52,500 --> 00:09:53,700
but you're not going to have to be an expert

253
00:09:53,700 --> 00:09:54,903
on it for the exam.

