Hello, and welcome back to the wonderful world of Git.
We've already seen a little bit of Git, and we've downloaded our repository.
We downloaded that onto our host system, but we might want to
download it again onto our virtual machine.
But we've seen how we can deliver then standardized versions of our scripts.
In our case, we looked at a Vagrantfile to allow then for that consistency.
We can extend that by taking a look at branches.
In this simple demonstration,
we're going to be taking a look at how we can use branches to access
different versions of the same file, and that's really what branches allow
us to do. Later, we'll see how we can start creating our own branches, but
we'll look at just downloading branches.
So long as we're inside of the directory somewhere,
we're going to be able to use git status.
Git status is going to check the Git database, and that will
then show us have we made any changes that then need to be
stored back into our repository.
It will also show the branch that we are connected to.
The default branch is going to be main in our setup.
If I look at git branch, it will list the local branches.
Now we'll only have main locally at the moment, but we'll see then the
branches that are available had we downloaded more branches.
If we take a look at what branches offer us,
we can go through and look at the current version of our hello script, and
we'll see it's a very basic Hello World Bash script.
So long as we're working again within our comptia‑automation directory,
I can use git branch ‑a, so look at all.
This will then show me remote branches as well, branches that we haven't
downloaded and are stored in our central repository.
Looking at the list, if we want to then checkout, that's really then
work with another branch, we can then check out our script arg. And we
go through and cat out our script now, we'll see that our script is now
working with arguments. So we haven't changed the file ourselves, but
we're storing within the repository different versions of exactly the
same file.
If we want to move back to our main branch, then we can go
and do this with git checkout and main.
Now this gives us an introduction to branches, and we're going to
move directly out through to our command line now to look at how we
can switch between these different branches. So we can view our
local branches, viewing the remote branches, and switch between
different branch versions.
Come on.
Let's dig out that command line.
So hello and welcome to your true learning environment.
Yes, this is where your learning begins. Now we're working currently on
our virtual machine, not on our host machine. When we downloaded the Git
repository before, it was on our host machine, not our virtual machine,
so, of course, listing our current home directory, we can see we don't
have anything here.
However,
it's going to be very easily to clone our repository again. Let
me just get back to the command. So we can run exactly the same
git clone command as we did before, and, of course, then that
brings our repository down.
Now just because we have the repository on our system now doesn't, of
course, mean that we're working in that repository.
So if I try something like git status, we can see then that the
command fails because we can't find the .git directory in our
current directory or any of its parents.
Whereas, of course, if I move into then the downloaded directory, I
can run the same command, git status, because this is a Git
repository, and all of the version control information is contained
within then the .git directory structure.
So this really is my repository, but we're able to access our working
information by going through and taking a look at, let's say, one of our
scripts. If I look into shellscripting and the hello script, we can see
then the current version that we have.
If I look at my git status command again, of course, this is now going to
work, and it tells me some really useful information. Become more useful
when we're working and editing our file, but it tells me the branch that
I'm working with, it's telling me that I haven't made any changes, so we
are up to date, and I don't need to save anything or commit anything back
to the repository.
If I use the command git branch,
we can look at available branches, and currently, the only branch I've
downloaded is main. Now some repositories use main,
some repositories use master. It's just terminology that we're using. But this
is then the branch that's going to be downloaded if we don't specify the
branch to use. Branches represent branching of code,
would you believe?
So we can have different versions. Often, we'll start a new branch when
we're developing our code. So we can easily trash that branch if it goes
wrong, or once it's successfully created,
we can merge it. But let's clear our screen. If I want to
look at the branches that are available,
I can do a git branch ‑a. So we're looking at all, and we can then point back to
the remote. So that is then our Git server, and we can see there that we've got
main, scriptarg, and scriptread. The head is really our cursor position showing
me where I am working at the moment.
If I want to work with another branch,
then I can go through and do a git checkout. With this then, we can
go through and specify the name of the branch,
so we're going to use then say script and arg. If I now go through and
take a look at the git branch command by itself,
we can see locally I've got main and scriptarg, the asterisk
showing me my current branch that I'm working with.
If I repeat the cat command again,
we can go through and see now that we've got a script
argument $1 involved in this Bash script.
If I want to move to another branch, we can go into,
let's say, scriptread,
go back and cat it out, and we can see we've got yet another version.
So we've only got the one file, but we can store different versions of
the same file. And, of course, it could be many different files that
we would be working with. For demonstration purposes, we're just using
the one file to give us the idea of what a version control system
gives to you,
literally allowing us to switch between different
versions of the same file or files.
When we're ready,
we can get back to our main branch, so just go through and check this out.
And of course, once we're back in main, we haven't made any changes to our file,
so as we go back, we now read our very simple Hello, world,
but we can have these separate development branches being kept completely isolated from our code, and we can choose to merge it when we want