1
00:00:00,000 --> 00:00:04,425
[MUSIC]

2
00:00:04,425 --> 00:00:09,324
Let us now explore some basic Git
commands that are very useful for

3
00:00:09,324 --> 00:00:11,474
us in this specialization.

4
00:00:11,474 --> 00:00:15,055
There is lot more to Git than
what we cover in this exercise.

5
00:00:18,130 --> 00:00:23,231
To get started, go to your convenient
location on your computer,

6
00:00:23,231 --> 00:00:26,523
and then create a folder named git-test.

7
00:00:30,140 --> 00:00:34,408
Then open this folder in
your favorite editor.

8
00:00:37,895 --> 00:00:42,585
Here I have the git-test
folder that we just created

9
00:00:42,585 --> 00:00:45,390
open in my Visual Studio Code.

10
00:00:45,390 --> 00:00:51,915
Let me add a file to this
folder named index.html.

11
00:00:54,122 --> 00:01:00,430
Now you can see that I have added in some
HTML code into this index.html file.

12
00:01:00,430 --> 00:01:06,507
Let's save the changes,
now let's switch to our command line.

13
00:01:06,507 --> 00:01:10,903
At the command line,
go to the git-test folder, and

14
00:01:10,903 --> 00:01:15,860
let's initialize this folder
to be a Git repository.

15
00:01:15,860 --> 00:01:19,800
So to do that, add the command line,
type, git init.

16
00:01:21,620 --> 00:01:27,710
Now this folder has been initialized
to be a Git repository, so

17
00:01:27,710 --> 00:01:32,650
this is our first Git command
that we have learned, git init.

18
00:01:32,650 --> 00:01:36,320
This initializes the current
folder as a Git repository, and

19
00:01:36,320 --> 00:01:42,380
when it initializes the folder,
it will mark that folder as a master.

20
00:01:42,380 --> 00:01:47,770
This is the master branch for my Git,
now let's not worry about branches and

21
00:01:47,770 --> 00:01:51,000
so on,
we will not deal with that in this course.

22
00:01:51,000 --> 00:01:55,738
We will only be working with the master
branch in this particular specialization,

23
00:01:55,738 --> 00:01:58,500
so this would be marked as the master.

24
00:01:58,500 --> 00:02:02,060
Now, this is the initial
point of our repository,

25
00:02:03,770 --> 00:02:08,410
the next command that we're
going to look at is git status.

26
00:02:08,410 --> 00:02:12,600
If you type git status in the command
line, it'll tell you the current status of

27
00:02:12,600 --> 00:02:17,460
the folder, so let's do that at
the command line and see what it shows.

28
00:02:19,470 --> 00:02:23,395
At the command line, type git status, and

29
00:02:23,395 --> 00:02:28,945
read the information that is
typed out on the command window.

30
00:02:28,945 --> 00:02:33,943
You see that it says, On branch master, so
that is the master branch that we are on,

31
00:02:33,943 --> 00:02:40,690
and it says, untracked files,
and then shows index.html in red.

32
00:02:40,690 --> 00:02:44,458
On your specific computer,
it may be using different colors or

33
00:02:44,458 --> 00:02:49,040
represent this differently, but
this is what it shows on my Mac.

34
00:02:49,040 --> 00:02:54,040
So now this index.html file that we
have just created in this folder

35
00:02:54,040 --> 00:02:58,140
is now not been added
to our Git repository.

36
00:02:58,140 --> 00:03:01,335
So let's go ahead and
add that file to the Git repository.

37
00:03:01,335 --> 00:03:07,510
So to do that, we say, git add,
and you can simply say dot,

38
00:03:07,510 --> 00:03:12,350
which means that all the files in
the current directory will be added to

39
00:03:12,350 --> 00:03:17,680
what is called as the staging
area of my Git repository.

40
00:03:17,680 --> 00:03:22,880
So now if I again type,
git status, you will see

41
00:03:22,880 --> 00:03:27,910
that the file index.html
is marked in green, and

42
00:03:27,910 --> 00:03:35,130
it says changes to be committed there,
and then shows the file name.

43
00:03:35,130 --> 00:03:35,672
And then,

44
00:03:35,672 --> 00:03:39,601
so that means that this file is now ready
to be committed to my Git repository.

45
00:03:41,438 --> 00:03:46,856
So the next command that we saw
was git add, by using git add,

46
00:03:46,856 --> 00:03:51,190
you can add file or
folders to the staging area.

47
00:03:51,190 --> 00:03:57,250
So once you add it to the staging area,
then you can commit that snapshot

48
00:03:57,250 --> 00:04:03,100
of our folder status
to our Git repository.

49
00:04:03,100 --> 00:04:06,460
So that means that when
you do the git commit,

50
00:04:08,130 --> 00:04:12,610
what this command will
do is commit the current

51
00:04:12,610 --> 00:04:17,200
state of our folders
into our Git repository.

52
00:04:17,200 --> 00:04:21,780
So all the files, as they exist at
the moment, once they have been staged

53
00:04:21,780 --> 00:04:25,560
using the git add, then they will be
committed through a Git repository,

54
00:04:25,560 --> 00:04:29,510
then we execute the git command folder.

55
00:04:29,510 --> 00:04:33,580
So, at this point,
when we execute the git command,

56
00:04:33,580 --> 00:04:38,373
then our initial state will now
be changed to the first commit to

57
00:04:38,373 --> 00:04:42,369
the Git repository, so
let's go ahead and do that.

58
00:04:45,208 --> 00:04:50,261
Back at the command prompt,
let's type git commit,

59
00:04:50,261 --> 00:04:54,880
and then we can even add
a message to our commit.

60
00:04:54,880 --> 00:04:59,275
So I'm going to say git
commit -m "first commit",

61
00:04:59,275 --> 00:05:03,879
because this is our first
commit to our Git repository.

62
00:05:03,879 --> 00:05:10,106
So when I do that, it says, okay, one file
has been added to the Git repository and

63
00:05:10,106 --> 00:05:15,300
some other information will be
typed out onto the command window.

64
00:05:15,300 --> 00:05:20,440
So let's now check, again, git status,

65
00:05:20,440 --> 00:05:26,220
and now you see that it says, nothing
to commit, working directory is clean.

66
00:05:26,220 --> 00:05:31,230
So what that means is that the current
state of my working directory or

67
00:05:31,230 --> 00:05:35,860
working folder has been
committed to the Git repository.

68
00:05:35,860 --> 00:05:41,050
So a snapshot has been committed to my
Git repository, now I can type the next

69
00:05:41,050 --> 00:05:47,200
command, called git log --oneline.

70
00:05:47,200 --> 00:05:52,156
And see that it shows a number there,
an eight digit number there, and

71
00:05:52,156 --> 00:05:57,545
it also shows the message that we put
in our commit saying "first commit".

72
00:05:57,545 --> 00:06:03,340
So that is the log of all the commits that
have been put into my Git repository.

73
00:06:05,120 --> 00:06:08,900
So going back to our next git command,

74
00:06:08,900 --> 00:06:14,094
we saw that git log --oneline

75
00:06:14,094 --> 00:06:18,540
will show us a brief
log of all the commits.

76
00:06:18,540 --> 00:06:20,442
If you simply type git log,

77
00:06:20,442 --> 00:06:25,658
it'll display a lot more detailed
information about all the commits, but

78
00:06:25,658 --> 00:06:30,652
this is sufficient enough for
obtaining information that we require.

79
00:06:33,013 --> 00:06:40,820
Let's now come back to our editor
here in Visual Studio Code.

80
00:06:40,820 --> 00:06:45,379
So I'm going to add more changes to
my index.html file, so I will add,

81
00:06:54,307 --> 00:06:57,790
So I have changed my index.html file.

82
00:06:57,790 --> 00:07:02,453
Now let me add another folder
under the git-test folder, so

83
00:07:02,453 --> 00:07:06,890
I will create a subfolder
here named templates.

84
00:07:06,890 --> 00:07:09,344
And inside this templates folder,

85
00:07:09,344 --> 00:07:13,039
I'm going to create another
file named test.html.

86
00:07:13,039 --> 00:07:17,798
This is just to show you how
Git can commit entire folder

87
00:07:17,798 --> 00:07:21,120
hierarchy into its repository.

88
00:07:21,120 --> 00:07:24,734
So with test.html now there,

89
00:07:24,734 --> 00:07:29,321
I'm just going to copy everything from

90
00:07:29,321 --> 00:07:34,047
my index.html into my test.html, and

91
00:07:34,047 --> 00:07:41,422
save the changes Going
back to the command line,

92
00:07:41,422 --> 00:07:46,187
let's now check out the status of
our Git repository and this folder.

93
00:07:46,187 --> 00:07:51,079
So typing git status shows that
the index.html file that we have

94
00:07:51,079 --> 00:07:55,971
already added earlier to
the repository has now been modified,

95
00:07:55,971 --> 00:08:00,570
so there is a newer version
of the index.html file.

96
00:08:00,570 --> 00:08:03,220
Also, it shows that
there are some untracked

97
00:08:03,220 --> 00:08:05,530
files in this folder called templates.

98
00:08:06,620 --> 00:08:11,720
Let's add all these changes to our
repository, to the staging area.

99
00:08:11,720 --> 00:08:16,350
So again, type git add., and

100
00:08:16,350 --> 00:08:21,620
then all these files will be
added to the staging area.

101
00:08:21,620 --> 00:08:27,820
Again, checking out the status, you now
see that the changes that have been

102
00:08:27,820 --> 00:08:32,850
added to the staging area, so all these
files have been added to the staging area.

103
00:08:32,850 --> 00:08:37,735
Let's do one more commit, so
I would say git commit -m,

104
00:08:41,526 --> 00:08:46,595
"Second commit", and then,
let's check out the log.

105
00:08:50,794 --> 00:08:54,179
If you check out the log,
you'll now see that there

106
00:08:54,179 --> 00:08:58,930
are two commits in my Git repository,
the first and the second commit.

107
00:08:58,930 --> 00:09:02,826
And note that each one of them is
given a different number there.

108
00:09:02,826 --> 00:09:09,700
If you want to see the full details of
the log, you can type simply, git log.

109
00:09:09,700 --> 00:09:12,510
And then you will see
more details in there

110
00:09:12,510 --> 00:09:15,095
than what you would be interested in.

111
00:09:15,095 --> 00:09:19,115
So notice that the one line commit

112
00:09:19,115 --> 00:09:24,405
only gives the first few characters
of my commit number there.

113
00:09:24,405 --> 00:09:26,674
That is sufficient enough for
us to operate with.

114
00:09:29,000 --> 00:09:34,006
Let me now go back again to my
Visual Studio, and add one more,

115
00:09:43,821 --> 00:09:45,890
One more line to my index.html file.

116
00:09:47,140 --> 00:09:52,666
So now my index.html file has been
modified, and let's save the changes.

117
00:09:55,066 --> 00:09:57,813
Going back to the command line,

118
00:09:57,813 --> 00:10:04,490
doing git status shows that
the index.html file has been modified.

119
00:10:04,490 --> 00:10:08,640
So let's add this to the staging area, and

120
00:10:08,640 --> 00:10:14,382
then do a third commit,
so let's say git add .,

121
00:10:14,382 --> 00:10:18,720
git status.

122
00:10:18,720 --> 00:10:22,700
Now you'll see that the index.html,
the modified version, has been added.

123
00:10:22,700 --> 00:10:27,305
Now, we can say git commit,

124
00:10:31,164 --> 00:10:37,144
"Third commit", And do git log --oneline,

125
00:10:37,144 --> 00:10:43,818
and you'll see that there are three
commits in our repository.

126
00:10:43,818 --> 00:10:47,625
So now our repository contains
snapshots of three different points,

127
00:10:47,625 --> 00:10:51,234
at the end of the first commit,
at the end of the second commit, and

128
00:10:51,234 --> 00:10:52,929
at the end of the third commit.

129
00:10:52,929 --> 00:10:56,297
Now we also can roll back changes,

130
00:10:56,297 --> 00:11:01,417
we can revert the repository
to a previous version.

131
00:11:01,417 --> 00:11:04,366
We can pull out a file
from an older commit and

132
00:11:04,366 --> 00:11:09,180
then replace the existing file in
our directory from the older commit.

133
00:11:09,180 --> 00:11:13,604
So let's see how we can operate
with these things by learning

134
00:11:13,604 --> 00:11:15,608
a couple of more commands.

135
00:11:15,608 --> 00:11:21,759
At this stage, our index.html
file is in the current state,

136
00:11:21,759 --> 00:11:26,530
so you can notice that it has an h1 and
two p's.

137
00:11:26,530 --> 00:11:29,365
Let's now look at the next Git command.

138
00:11:30,815 --> 00:11:36,185
The next Git command that we're going
to learn about is git checkout.

139
00:11:36,185 --> 00:11:40,300
So this checkout command
allows us to check out a file

140
00:11:40,300 --> 00:11:44,040
from a previous commit
in our Git repository.

141
00:11:44,040 --> 00:11:47,709
So if we don't like the current
file that we have in our folder,

142
00:11:47,709 --> 00:11:50,907
and we want to go back to
a previous version of the file.

143
00:11:50,907 --> 00:11:54,782
We can always check out the file
from a previous commit or

144
00:11:54,782 --> 00:12:00,230
from the current commit, and
then continue to work with that file.

145
00:12:00,230 --> 00:12:07,190
So let's make use of this and see some
further changes to our Git repository.

146
00:12:09,350 --> 00:12:14,705
Going back to our command line,
we remember that,

147
00:12:14,705 --> 00:12:20,235
between the second and the third commit,
I made changes to my index.html file.

148
00:12:20,235 --> 00:12:25,477
Suppose I want to revert back to
the index.html file from my second commit.

149
00:12:25,477 --> 00:12:31,244
So then I can simply say,
git checkout 900cfcf,

150
00:12:31,244 --> 00:12:35,045
so that is the commit identifier,

151
00:12:35,045 --> 00:12:40,980
the number that identifies
that particular commit.

152
00:12:40,980 --> 00:12:46,117
And then I can say index.html,
and what you would notice

153
00:12:46,117 --> 00:12:53,190
is that all the file will now be checked
out into my current working directory.

154
00:12:55,721 --> 00:12:57,901
Going to my Visual Studio Code,

155
00:12:57,901 --> 00:13:03,340
you now notice that my index.html file
has reverted to the previous version.

156
00:13:03,340 --> 00:13:07,928
So the change that I made before
the third commit is now gone.

157
00:13:07,928 --> 00:13:16,482
So my index.html file has been restored to
its state at the end of the second commit.

158
00:13:18,607 --> 00:13:22,936
Now at the command line,
if I type git status,

159
00:13:22,936 --> 00:13:26,711
you notice that this index.html file,

160
00:13:26,711 --> 00:13:33,520
which has reverted to what it was
at the end of the second commit.

161
00:13:33,520 --> 00:13:37,710
It has now already been staged,
so using this git checkout

162
00:13:38,720 --> 00:13:43,670
will pull out an older version of the file
and then replace what is in the current

163
00:13:43,670 --> 00:13:48,400
directory and then it will also
check it into the staging area.

164
00:13:48,400 --> 00:13:53,400
So now, if I do that and
then I realize that this is what I want,

165
00:13:53,400 --> 00:13:56,450
I can simply do another
commit at this point.

166
00:13:56,450 --> 00:14:02,249
And then that file can be
committed as the fourth commit,

167
00:14:02,249 --> 00:14:05,200
but suppose I don't like this.

168
00:14:05,200 --> 00:14:10,553
I want to revert back to
the index.html file at

169
00:14:10,553 --> 00:14:17,704
the end of the third commit,
then all I can do is say git reset,

170
00:14:20,178 --> 00:14:24,866
HEAD and index.html.

171
00:14:28,084 --> 00:14:32,404
So at this point,
what happens is that the index.html,

172
00:14:32,404 --> 00:14:37,264
the modified version that I have
checked out is still there, but

173
00:14:37,264 --> 00:14:41,137
this file has been unstaged
from the staging area.

174
00:14:43,376 --> 00:14:48,440
If you go back and
look at the index.html in your,

175
00:14:50,991 --> 00:14:56,094
Editor, it will still show the state
at the end of the second commit

176
00:14:56,094 --> 00:15:01,255
because we had pulled out that
file using checkout for that.

177
00:15:01,255 --> 00:15:06,900
Now if we want to revert it back to what
it was at the end of the third commit,

178
00:15:06,900 --> 00:15:09,470
then we do one more checkout
from the third commit.

179
00:15:10,540 --> 00:15:16,431
Going to our command window,
type git status, and

180
00:15:16,431 --> 00:15:23,558
you would notice that the index.html
is marked as modified.

181
00:15:23,558 --> 00:15:29,661
But it also shows this particular
statement here, it says git checkout --,

182
00:15:29,661 --> 00:15:35,029
and the file name, to discard
the changes in the working directory.

183
00:15:35,029 --> 00:15:38,334
So that's one way you can
discard the changes that

184
00:15:38,334 --> 00:15:43,095
are made to a particular file
corresponding to the previous commit.

185
00:15:43,095 --> 00:15:46,565
So let me restore this
index.html back to what it was

186
00:15:46,565 --> 00:15:47,653
at the end of the third commit.

187
00:15:47,653 --> 00:15:54,315
So to do that,
I will simply say git checkout --

188
00:15:54,315 --> 00:15:58,712
index.html, and then if I do,

189
00:16:01,503 --> 00:16:05,815
Git status, it shows that my
directory is clean, and basically my

190
00:16:05,815 --> 00:16:10,520
directory has been restored to the state
of the end of the third comment.

191
00:16:12,370 --> 00:16:16,909
Going to the file in my Visual Studio
Code, I see that the file has been

192
00:16:16,909 --> 00:16:20,767
restored back to what it was at
the end of the third commit.

193
00:16:20,767 --> 00:16:25,251
So if you have made changes to a file
after a commit and you want to just

194
00:16:25,251 --> 00:16:30,632
discard those changes, you can simply
checkout the file from the last commit.

195
00:16:30,632 --> 00:16:35,338
And then all your changes that you've done
after the last commit will be discarded at

196
00:16:35,338 --> 00:16:36,144
this part..

197
00:16:36,144 --> 00:16:40,304
So these are some basic commands
that are very useful for

198
00:16:40,304 --> 00:16:42,958
you as you go through the courses and

199
00:16:42,958 --> 00:16:48,821
the specialization because you may want
to commit at the end of each exercise.

200
00:16:48,821 --> 00:16:51,617
And as you proceed forward,

201
00:16:51,617 --> 00:16:56,744
you would still have a committed
version of the state

202
00:16:56,744 --> 00:17:01,790
of your folder at the end
of the previous exercise.

203
00:17:01,790 --> 00:17:06,249
So that way if you are carrying out a new
exercise and you discover that you've

204
00:17:06,249 --> 00:17:10,032
made mistakes and you want to
revert back to the previous commit.

205
00:17:10,032 --> 00:17:14,832
You'll always have a way of
doing that using the commands

206
00:17:14,832 --> 00:17:18,010
that we have just learned.

207
00:17:18,010 --> 00:17:24,430
So with this basic understanding
of these few Git commands,

208
00:17:24,430 --> 00:17:29,427
we would be able to proceed
forward with understanding and

209
00:17:29,427 --> 00:17:33,570
using Git in the courses
of this specialization.

210
00:17:35,560 --> 00:17:41,434
So now, we have reviewed the git reset for
a specific file or git reset in general.

211
00:17:41,434 --> 00:17:47,970
If you simply type get reset,
it'll restore you back to the last commit.

212
00:17:47,970 --> 00:17:51,360
So it will reset the staging
area to the last commit

213
00:17:51,360 --> 00:17:55,270
without disturbing the changes that
you've done to your working directory.

214
00:17:55,270 --> 00:17:59,270
So once you reset,
then you can checkout the previous

215
00:17:59,270 --> 00:18:02,140
version of the file that you have
committed in the previous commit.

216
00:18:02,140 --> 00:18:05,940
So this way you can restore your
folder back to where you were

217
00:18:07,440 --> 00:18:10,450
at the starting point
of the previous commit.

218
00:18:10,450 --> 00:18:16,037
So sometimes when you are going through an
exercise and you realize you made mistake,

219
00:18:16,037 --> 00:18:20,082
you always have a way of reverting
back to a previous version.

220
00:18:20,082 --> 00:18:25,081
So with these commands I think
you're all set to go ahead to use

221
00:18:25,081 --> 00:18:28,651
Git in the courses of this specialization.

222
00:18:30,865 --> 00:18:33,694
So at the end of this exercise,
did you Git it?

223
00:18:33,694 --> 00:18:37,459
[MUSIC]