Advanced Git Commands

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hey, there's Cybrarians,
00:00
>> and welcome back to the Linux plus course
00:00
>> here at Cybrary,
00:00
>> I'm your instructor Rob Goelz.
00:00
In today's lesson we're going to be
00:00
covering some advanced Git commands.
00:00
So upon completion of this lesson,
00:00
you are going to be able to understand
00:00
some advanced Git concepts,
00:00
as well as how git branches and merging work and
00:00
using the git log command to view commit history.
00:00
One thing you're going to counter
00:00
repeatedly is the concept of branches in git.
00:00
A branch is just an area of
00:00
a repository for a specific project section,
00:00
and a project can have multiple branches.
00:00
For example,
00:00
>> we might have a main branch for production,
00:00
>> a development branch named dev
00:00
and a testing branch named test.
00:00
Now a branch can be created
00:00
>> using the git branch command.
00:00
>> So for example, if we wanted to create
00:00
the development branch called dev,
00:00
we could do git branch dev.
00:00
Once we've done that, developers can
00:00
specify the branch that they want to work in
00:00
to protect files in another branch from being changed
00:00
>> by using the git checkout command.
00:00
>> For example, if we want to switch
00:00
>> from the main production branch into development
00:00
>> to work on a feature,
00:00
>> we could do git checkout dev
00:00
and switch into that development branch.
00:00
Now once we switched into the development branch,
00:00
developer work continues in that dev branch
00:00
>> and it uses the same git process we
00:00
>> saw in the previous lesson to add files
00:00
or stage changes and then commit those changes.
00:00
Now once the work in the dev branch is completed,
00:00
we can actually merge that entire branch
00:00
back into the main branch.
00:00
In our example,
00:00
>> that's the production branch, remember?
00:00
>> To merge branches,
00:00
>> we switch back to the main branch
00:00
>> by doing git checkout main,
00:00
>> and then we can merge in the change branch
00:00
by doing git merge dev.
00:00
Once we've merged things in,
00:00
we're notified of the changes to the main branch,
00:00
but we could also run git log
00:00
>> to see the commit history.
00:00
>> Let's take a look at all of this with some Demo Time.
00:00
Here we are back in our demo environment
00:00
>> and we are in the directory home/rob/bash-scripts.
00:00
>> So let's first of all take a look at the branches.
00:00
We can just do this by typing
00:00
the git branch command or we can also do git status.
00:00
You'll see that you're on branch main right here.
00:00
But let's go ahead and create a new branch.
00:00
We're going to create a branch called dev.
00:00
We'll do git branch dev,
00:00
and then we can change into the new branch
00:00
by doing git checkout dev.
00:00
Now we can see the branch by doing our git status,
00:00
we can see that we're on branch dev.
00:00
We could also do just git branch
00:00
>> without specifying anything else,
00:00
>> and we can see that we're on dev.
00:00
We see that we have an asterisks next to dev,
00:00
and it's also highlighted in green.
00:00
Let's go ahead and copy
00:00
>> our first test script into this directory.
00:00
>> We're going to copy home/rob/testscript.sh
00:00
into home/rob/bashscripting or bash scripts rather.
00:00
Then if we run git status again, git status, sorry,
00:00
>> we can see that we have untracked files.
00:00
>> So we can see that the file is here, testscript.sh,
00:00
>> but we need to add it and we need to track it.
00:00
>> What we'll do first is we'll do git add dot,
00:00
and then we'll add in testscript.sh.
00:00
We can see that again, we get status.
00:00
We can see that the file has been added,
00:00
but it needs to be committed.
00:00
So changes to be committed right here
00:00
is this new file testscript.sh,
00:00
and now what we can do is we can do a commit again.
00:00
So we do git commit -m
00:00
is for message and we're going to give it the message,
00:00
add additional script file,
00:00
and then we can go ahead and hit "Enter."
00:00
Now we've added that file.
00:00
Now let's move back to the main branch
00:00
and merge in the change.
00:00
What we can do is we can do a git checkout main
00:00
>> and we're in the main branch,
00:00
>> we can see that with git status again,
00:00
and we can see that we're on branch main
00:00
>> or just git branch.
00:00
>> We see that we're back in main.
00:00
Now let's do an ls.
00:00
>> What do we see?
00:00
>> Well, we don't see that file,
00:00
we just see bin and read me.
00:00
That's because we haven't merged
00:00
>> in the contents of dev.
00:00
>> We can do that by doing git merge dev,
00:00
and now we see that the file
00:00
has been changed or inserted.
00:00
If we do an ls, now we also see testscript.sh.
00:00
We can see the file merged if you do an ls,
00:00
we can see in our folder, but we can also run git log,
00:00
and now we can see the commit message in history here,
00:00
and see that we've added the additional script file
00:00
>> right here, today at this time.
00:00
>> With that, we've reached the end of this lesson
00:00
and in this lesson we covered advanced concepts in git
00:00
>> such as merging and branching
00:00
>> and we used the git commands,
00:00
>> git branch, git merge, and git log.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next