1 00:00:00,000 --> 00:00:13,200 In this video, I'll explain Git. So what is Git? Git is a distributed version control 2 00:00:13,200 --> 00:00:18,879 system. It's not a part of Linux. From now on, we will be talking about items that are 3 00:00:18,879 --> 00:00:25,360 not a direct part of Linux, but it is often used in Linux environment. And fun fact, Git 4 00:00:25,360 --> 00:00:30,480 has been created by Linus Torvalds, who is also the creator of the Linux kernel. Linus 5 00:00:30,480 --> 00:00:34,919 created Git because he needed something to work together in an efficient way with multiple 6 00:00:34,919 --> 00:00:40,720 people. So in Git, developers can work in products together where each file revision 7 00:00:40,720 --> 00:00:46,400 is stored in the system. Old versions of files are always available and a log is maintained 8 00:00:46,400 --> 00:00:53,720 of who made which change to which files. And Git is distributed because it allows the developers 9 00:00:53,720 --> 00:01:00,119 to work offline after cloning a remote repository locally. Now, Git works with a repository 10 00:01:00,119 --> 00:01:05,360 which can contain different development branches. And developers as well as users can easily 11 00:01:05,360 --> 00:01:11,440 upload as well as download new files to and from the Git repository. And to do so, a Git 12 00:01:11,440 --> 00:01:16,800 client is needed. And this Git client in Linux typically is the Git command, but other Git 13 00:01:16,800 --> 00:01:23,680 clients are available as well. There are different Git platforms. GitHub is a very common platform. 14 00:01:23,680 --> 00:01:29,639 GitLab is a common alternative. But private Git servers can also be hosted. No matter 15 00:01:29,639 --> 00:01:38,239 how you host it, it always works in the same way. Now let's try to understand the Git workflow. 16 00:01:38,239 --> 00:01:43,440 After cloning, the developer makes changes to the working tree. And all changes are made 17 00:01:43,440 --> 00:01:48,680 to the local repository, the locally cloned repository. And that means that the developer 18 00:01:48,680 --> 00:01:55,879 changes are not immediately visible to the entire community. Once a developer is happy, 19 00:01:55,879 --> 00:02:02,199 the developer can push changes to the remote repository. And if the local repository is 20 00:02:02,199 --> 00:02:10,160 network accessible, the owner of the repository can pull changes from it. Now in Git, different 21 00:02:10,160 --> 00:02:17,919 trees are used to work with the state of the file. There are three of them. The trees are 22 00:02:17,919 --> 00:02:22,800 stored as files in the .git directory. You don't normally see them, but it is nice to 23 00:02:22,800 --> 00:02:29,759 understand how it works to understand the workflow in Git. So there is the working directory, 24 00:02:29,759 --> 00:02:36,440 which holds the actual files. Then there is the index, which acts as a staging area. And 25 00:02:36,440 --> 00:02:41,880 the head points to the last commit that was made. When you are working with Git, you start 26 00:02:41,880 --> 00:02:48,839 with git add. Git add is used to add files to the index. To commit these files to the head, 27 00:02:48,839 --> 00:02:54,039 you use git commit minus m commit message. And that is what the developer would normally do 28 00:02:54,039 --> 00:03:01,160 when they are happy with the file that was created. Also, at some point, the local repository 29 00:03:01,160 --> 00:03:05,960 needs to be connected to the remote repository. If that hasn't been done already, you need 30 00:03:05,960 --> 00:03:12,759 git add origin HTTPS colon slash slash server slash repo name to connect to the remote repository. 31 00:03:13,479 --> 00:03:20,279 So on the Git client, you always need to add the remote repository using git remote add origin 32 00:03:20,279 --> 00:03:26,839 HTTPS colon slash slash server name slash repo name. And once you have done that, you can complete 33 00:03:26,839 --> 00:03:34,279 the sequence and use git push origin master, where master is replaced with the actual branch 34 00:03:34,279 --> 00:03:39,960 name that you want to push the changes to. Let me make a quick drawing to visualize this process. 35 00:03:41,000 --> 00:03:48,759 Let's try to understand the Git workflow. It all starts at the Git server. And on the Git server, 36 00:03:48,759 --> 00:03:55,479 you have your Git repository. So you need to be able that this Git repository is getting to the 37 00:03:55,479 --> 00:04:02,919 Git client. Well, it depends a little bit on how you want to work. But one of the things that you 38 00:04:02,919 --> 00:04:11,320 can do is a simple Git clone. And Git clone is initializing the data from the Git server to the 39 00:04:11,320 --> 00:04:17,239 Git client. You can also do a Git init on the client if it's not yet known, and you need to 40 00:04:17,239 --> 00:04:25,079 push data to the Git server. Now on the Git client, you'll start working. And in order to work, 41 00:04:26,200 --> 00:04:32,359 you have your working directory. And in the working directory, nothing is synchronized 42 00:04:32,359 --> 00:04:38,760 with Git yet. In order to synchronize data, you first need to put it into the index. 43 00:04:39,799 --> 00:04:46,920 And then you need to put it into the head. And there are three different phases in your work 44 00:04:46,920 --> 00:04:54,600 with Git that are relevant. The first thing is where you are going to use the git add command. 45 00:04:55,959 --> 00:05:01,880 So git add is adding your changes from the working directory to the index. And then you are going to 46 00:05:01,880 --> 00:05:09,959 use git commit to commit your changes to the head. And once the changes are ready in the head, 47 00:05:10,679 --> 00:05:17,239 then you are going to use git push. And git push is going to push the changes swap all the way 48 00:05:17,799 --> 00:05:24,040 to the Git repository. Now it is possible that in the meantime, somebody else has made changes as 49 00:05:24,040 --> 00:05:29,239 well. And if that is happening, well, you also need to have git pull and git merge. We will talk 50 00:05:29,239 --> 00:05:36,679 about those in upcoming videos. Now final thing I want to tell you about is file states. So files 51 00:05:36,679 --> 00:05:42,119 in Git can be in different states. And the git status command can be used to request the current 52 00:05:42,119 --> 00:05:48,760 file state. Three states. There is modified. Modified means that the file in the working 53 00:05:48,760 --> 00:05:54,679 tree is different from the file in the repository. There is staged, which means that the modified 54 00:05:54,679 --> 00:06:00,760 file was added to a list of changed files in the index. And committed, that means that the modified 55 00:06:00,760 --> 00:06:06,359 file has been committed to the head and is ready to be pushed. So the only thing remaining is git 56 00:06:06,359 --> 00:06:10,119 push to make sure that the file is shared with the community.