In this lesson,
I'm going to demonstrate how you can use source control
like Git to keep track of your files
when you're working as a team.
Now, Git is pretty much the defacto standard these days
for code repositories.
And so, as we go through this,
I'm going to show you a couple of simple things
you can do with Git,
but, really, the point is not for you
to learn how to do this and be hands-on,
I just want to demonstrate the capability of a tool like Git,
but you are not going to be tested on specific things
of how to use Git on the exam.
Instead, you need to understand why you would use Git.
And for the most part,
you're going to use Git to be able to do version control
and source control of your code for your templates,
run books, infrastructure as code,
and other scripts and files.
All right, let's get started.
Now the first thing we're going to do
when we start working with Git
is we want to make sure we're setting it up
with our username and email
so people know who's making changes
when we're interacting with a Git repository.
To do this, we're going to change
something known as a global variable inside of Git,
and you just by typing git config --global user.name,
which is the variable that I want to change,
in this case, my username,
and then I want to put in my username.
So I'm going to go ahead and put in 'Jason Dion'.
Next, we want to type in git config --global user.mail,
and we're going to put in our email address,
which in my case is support@diontraining.com.
Once you hit Enter,
you'll see we're back at the command prompt.
Now if you got an error,
that means something went wrong and we need to fix it,
but with Git, most of the time,
you're not going to get a response
except to be returned back to the command prompt,
which means you can assume it was successful.
The next thing I want to do is make a directory
for us to play inside of.
So I'm just going to create a directory called TEST-PROJECT.
Now that I've done that, if I type in ls,
you will see there is one directory there
called TEST-PROJECT.
Now for me to be able to use this,
I need to initialize it as a Git repository.
So I'm going to type in git init, for init,
and in space and the name of the directory
that we just created and hit Enter.
Once we've done that,
we now can see that the Git repository
has been initialized as empty
inside of jason's Download folder
underneath the PROJECT directory,
underneath the TEST-PROJECT directory,
and then under the .git directory, or .git directory.
And this is a directory that's going to hold all of the files
for that Git repository,
and then any of the files we want to control,
we're going to place inside of TEST-PROJECT
and then add them into the Git repository
by creating a symbolic link.
Now to look inside this,
we're going to do a cd
and then we're going to do TEST-PROJECT and hit Enter.
And then we're going to type in ls -la,
and this will list out all the things
inside the TEST-PROJECT directory.
Notice we have three things, ., .., and .git.
Now if you think back to your earlier studies,
you should know that in a Linux system,
a . means the present working directory
or the directory we're currently in,
which is users/jason/Downloads/PROJECT/TEST-PROJECT.
The .. is one directory higher.
In this case, that would be the PROJECT directory
'cause that is one directory up
from our current directory of the TEST-PROJECT.
That's going to happen on every window system,
every Unix system, every Linux system,
and every Mac system, like the one I'm using here.
The third one we have is the .git,
and the .git is going to hold all of those files for us
inside of it.
We're going to just go in there by doing a cd command,
and then we're going to do ls -la again.
And here you can see what we're going to have.
In here, we have a couple of files like the head file,
the config file, and the description file,
and then we have several directories,
which is the branches, hooks, info, objects and ref.
And all these are things that are created and they are empty
because we don't have anything in this Git repository yet,
except the structure,
but every Git repository will have this structure.
All right, let me go back up one directory,
back into the TEST-PROJECT directory
and then I'm going to go ahead and clear my screen.
Now for us to be able to use Git,
we have to have some files to add to our Git repository.
So let's go ahead and create a text file by typing in vim,
which is a text editor inside of Unix, Linux,
and Mac-type systems,
and I'm just going to give it a file name of test.txt.
Once I hit Enter, I'm now in the Vim editor
and I'll press I to enter insert mode,
and now I can write whatever I want.
For example, I could say, "This is a script
that Jason has written."
Now that we've done typing what we want,
we'll press the Escape key, :, and then wq,
which stands for write and quit, and hit Enter.
Once we've done that, we're back in our directory.
And now we want to see if it actually did anything
inside of that file.
So I'm going to type in the word cat and then test.txt,
which will print the contents to the screen,
which should say, "This is a script written by Jason."
And now you can see that we do have the test.txt file
inside of this directory,
which is called the TEST-PROJECT directory.
Now from here, what we want to do is check our Git repository
and see does it realize any changes have occurred.
So we'll type in git status,
and this will always tell us the latest status
of our Git repository.
From here, you can see that I'm on the branch master,
I have no commits yet,
and at this point, it notices there is one file
called test.txt, but it's under the untracked files
because we never told Git
we want it to think about this file
and to track any changes that happens.
So all it's saying is, "Hey, there's a new file here,
I'm not really tracking it, but there is a new file,
you might want to look at it."
And so in this case, it says nothing was added to the commit
but there was an untracked file present.
Now I know what that file is 'cause I just created it,
and I want to add that to my Git repository.
So if we make changes,
it'll notify us of those changes in the future.
To do that, we're just going to type in the word git,
and then add, and then the name of the file,
which is test.txt.
From there, we're now going to do a git status once more,
and we should get a similar error,
but you're going to see that it's added
but the changes haven't been done.
So here you can see that we have a new file in green
and it's ready to be committed.
To commit those changes to the Git repository,
we'll type in git and then the word commit,
and then I'm going to use -m, which stands for message,
and I can put whatever comment or message I want here.
So I'm just going to say this is the initial commit
with one file called text.txt, and I'll hit Enter.
And there we go.
We have now committed the one file called text.txt.
One file was changed, one file was inserted,
and it was in create mode for test.txt.
Now at this point,
if we go ahead and do a git status,
we should see that everything looks clean and clear.
And you can see we are on the branch master,
there's nothing to commit, and is a working tree clean,
which means all the files are in sync
and nobody's modified a file since we did the last commit,
which is true because we just did the commit
and haven't done anything else.
So we're going to go back into Vim to add a new line to this
by going vim test.txt,
and then we're going to hit the I for insert.
We'll arrow over to the end of the line.
And then from here, I'm just going to put . and hit Enter,
and I'll say, "Let's pretend I added a new line
to a Python script!"
Okay, and then we're going to hit Escape,
:wq, for write and quit, and there we go.
Now from here, if I do the cat test.txt,
we should see the change we made.
You can see there's now two lines to script instead of one.
The next thing we want to do is we want to do a git status.
Now will there be any changes to this file
that Git's going to recognize?
There should be 'cause we added a whole new line.
And so you'll see what happens
is that we are on the branch master,
changes not staged for commit.
And it shows in red, test.txt was modified.
Now it's just telling us that the file was changed.
If we went in further,
we could actually log every single change and know it.
But right now,
we're just worried about the fact that this changed,
and that's good enough for now.
And so we know there are no changes added to the commit.
If we want to add this file back to the commit
so we can put it into the repo,
we can do that by again typing in git add test.txt,
which says I'm happy with my changes in this file,
I want it to go to the master branch.
Now that I've done that,
I'm going to go ahead and type in git commit -m
"Revision 1 - Added new line to the python script".
And then hit Enter.
And you can see there, we've added the new line
to the Python script with Revision 1,
one file was changed,
and there was two insertions and one deletion.
So now if we go ahead and do a git status again,
we should expect that we have a clean branch,
and we do because we committed all of our changes.
So at this point, we now know how to put files in and out.
And if you notice here, we still have our test.txt file
and all the changes have been synced.
The last thing I want to show you
is that you can use Git and look at the Git log
by typing in git log.
And here it will show you everything that's ever happened
inside of your Git repository.
Now note, it will always show you the latest thing first
and the oldest thing last.
So if you're reading from top to bottom,
you will see that this is the head or master branch.
And in that head or master branch,
the last commit we did was 244,
and you could see that we added a new line
to the Python script.
Before that, we had our initial commit,
and you'll see that on the bottom half.
And that's the way Git works at a very basic level.
By adding files, removing files, committing files,
checking your status, and checking your logs,
you can know what files are in your Git repository,
you can have them tracked,
and any changes will be notified to you
and anybody else who's working on this project
and using the same Git repository.