In this video, I'll explain Git. So what is Git? Git is a distributed version control
system. It's not a part of Linux. From now on, we will be talking about items that are
not a direct part of Linux, but it is often used in Linux environment. And fun fact, Git
has been created by Linus Torvalds, who is also the creator of the Linux kernel. Linus
created Git because he needed something to work together in an efficient way with multiple
people. So in Git, developers can work on projects together where each file revision
is stored in the system. Old versions of files are always available and a log is maintained
of who made which change to which files. And Git is distributed because it allows the developers
to work offline after cloning a remote repository locally. Now, Git works with a repository
which can contain different development branches. And developers as well as users can easily
upload as well as download new files to and from the Git repository. And to do so, a Git
client is needed. And this Git client in Linux typically is the Git command, but other Git
clients are available as well. There are different Git platforms. GitHub is a very common platform.
GitLab is a common alternative. But private Git servers can also be hosted. No matter
how you host it, it always works in the same way. Now let's try to understand the Git workflow.
After cloning, the developer makes changes to the working tree. And all changes are made
to the local repository, the locally cloned repository. And that means that the developer
changes are not immediately visible to the entire community. Once a developer is happy,
the developer can push changes to the remote repository. And if the local repository is
network accessible, the owner of the repository can pull changes from it. Now in Git, different
trees are used to work with the state of the file. There are three of them. The trees are
stored as files in the .git directory. You don't normally see them, but it is nice to
understand how it works to understand the workflow in Git. So there is the working directory,
which holds the actual files. Then there is the index, which acts as a staging area. And
the head points to the last commit that was made. When you are working with Git, you start
with git add. Git add is used to add files to the index. To commit these files to the head,
you use git commit minus m commit message. And that is what the developer would normally do
when they are happy with the file that was created. Also, at some point, the local repository
needs to be connected to the remote repository. If that hasn't been done already, you need
git add origin HTTPS colon slash slash server slash repo name to connect to the remote repository.
So on the Git client, you always need to add the remote repository using git remote add origin
HTTPS colon slash slash server name slash repo name. And once you have done that, you can complete
the sequence and use git push origin master, where master is replaced with the actual branch
name that you want to push the changes to. Let me make a quick drawing to visualize this process.
Let's try to understand the Git workflow. It all starts with the Git server. And on the Git server,
you have your Git repository. So you need to be able that this Git repository is getting to the
Git client. Well, it depends a little bit on how you want to work. But one of the things that you
can do is a simple Git clone. And Git clone is initializing the data from the Git server to the
Git client. You can also do a Git init on the client if it's not yet known, and you need to
push data to the Git server. Now on the Git client, you'll start working. And in order to work,
you have your working directory. And in the working directory, nothing is synchronized
with Git yet. In order to synchronize data, you first need to put it into the index,
and then you need to put it into the head. And there are three different phases in your work
with Git that are relevant. The first thing is where you are going to use the git add command.
So git add is adding your changes from the working directory to the index,
and then you are going to use git commit to commit your changes to the head. And once the
changes are ready in the head, then you are going to use git push. And git push is going to push
the changes swap all the way to the Git repository. Now, it is possible that in the meantime,
somebody else has made changes as well. And if that is happening, well, you also need to have
git pull and git merge. We will talk about those in upcoming videos. Now, final thing I want to
tell you about is file states. So files in Git can be in different states. And the git status
command can be used to request the current file state. Three states. There is modified. Modified
means that the file in the working tree is different from the file in the repository.
There is staged, which means that the modified file was added to a list of changed files in the
index. And committed, that means that the modified file has been committed to the head
and is ready to be pushed. So the only thing remaining
is git push to make sure that the file is shared with the community.