{"id":36562,"date":"2016-04-28T11:00:43","date_gmt":"2016-04-28T08:00:43","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=36562"},"modified":"2019-04-23T14:52:10","modified_gmt":"2019-04-23T11:52:10","slug":"git-tutorial-beginners","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/","title":{"rendered":"How does Git work? Git Tutorial for Beginners"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p><code>Version Control Systems<\/code> are one of the software tools that help developers manage the source code over time. They keep track of the modification, compared with different versions of the source code. As the number of lines increases, it could go up to thousands and thousands lines.<\/p>\n<p>In real life, it could be a disaster if we make some irrecoverable mistakes without any backups. However, this will not happen in code-writing for the developers, especially with the help of <code>Git<\/code>, which is one of the most popular source code management system for software development. <code>Git<\/code> has been widely used among programmers.<br \/>\n&nbsp;<\/p>\n<p>&nbsp;<br \/>\nCompared with other Version Control Systems, such as <code>SVN<\/code> (Apache Subversion) and <code>CVS<\/code> (Concurrent Version System), Git is more powerful by its distributed nature, fast operation and branch handling mechanism. The distributed system makes it easy and efficient for multiple developers work together towards the same project. <code>Git<\/code> works fast as its lightweight operations. Also every developer can work with the code as its own branch. Others can merge to different branches and this makes cooperation work quite easy. In addition, <code>Git<\/code> is free and open-source.<\/p>\n<p>Because of the efficient of <code>Git<\/code>, <code>Github<\/code>, which is the web-based repository hosting service platform, is also popularly used. You can find many useful materials\/codes in <code>Github<\/code> with the <a href=\"https:\/\/github.com\/\">link here<\/a>.<\/p>\n<p>In this article, we will go over the basic principles and operations of <code>Git<\/code>. All the examples below is shown in MacOS EI Capitan Version 10.11.3 and the Git version is 2.5.4. Note all the operations below is working on local machine, without connecting to Github.<\/p>\n<h2>2. Git installation<\/h2>\n<p>Before we dig into the principles and operations of <code>Git<\/code>, we need to have the tool and install <code>Git<\/code>. To install <code>Git<\/code>, two ways could be used:<br \/>\n(1) Use the command line: For different operating systems, different command line may be used in terminal. For example, for linux, <code>sudo yum install git-all<\/code> could install <code>Git<\/code> successfully. For more detailed installation methods on other system, you can follow the <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Getting-Started-Installing-Git\">tutorial here.<\/a><\/p>\n<p>(2) Use the package: Also, it&#8217;s easier if you could find the installation packages for <code>Git<\/code>. Then you can download it and easily install it by click the package. It also provide the GUI for <code>Git<\/code>, which makes it user-friendly. The <code>Git<\/code> community provides information for different systems, such as MacOS, Windows, Linux and Solaris. For detailed packages download, you can refer to the <a href=\"https:\/\/git-scm.com\/downloads\">link here<\/a>.<\/p>\n<h2>3. Basic concepts and principles<\/h2>\n<p>Firstly, we need to get used to the repository concepts. In <code>Git<\/code>, there will be three repositories or areas. If we consider the usage of <code>Github<\/code>, then we need to understand the remote repository. To make it clear, we&nbsp;use the following flow to demonstrate how the git works.<\/p>\n<p><figure id=\"attachment_36668\" aria-describedby=\"caption-attachment-36668\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/gitflow.jpg\"><img decoding=\"async\" class=\"wp-image-36668 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/gitflow.jpg\" alt=\"gitflow\" width=\"603\" height=\"524\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/gitflow.jpg 603w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/gitflow-300x261.jpg 300w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/a><figcaption id=\"caption-attachment-36668\" class=\"wp-caption-text\">Git working flow<\/figcaption><\/figure><\/p>\n<ul>\n<li><strong>Workspace<\/strong>: it\u2019s the place where you see in your computer system, or the directory where you check out your files. Files in the workspace could be added to the Git by using <code>git add<\/code> command. Basically it could be any folders in your computer.<\/li>\n<li><strong>Index<\/strong>: it\u2019s also called stating area. It\u2019s an invisible space where you can add files that you want to commit. To add commit, you can use <code>git commit<\/code> command.<\/li>\n<li><strong>Local repository<\/strong>: it\u2019s also an invisible repository. Actually it\u2019s stored in the .git folder, which is hidden in the folder you created.<\/li>\n<li><strong>Remote repository<\/strong>: this could be another computer, or it could be the server of others, such as Github, which we can consider it as a remote repository. To access to the remote repository, git push or <code>git pull<\/code> could be used.<\/li>\n<\/ul>\n<p>In addition, we also need to be familiar with some other common concepts in <code>Git<\/code>.<\/p>\n<ul>\n<li><strong>Branch<\/strong>: it&#8217;s used to create another line of code. Usually it&#8217;s for creating another new feature. Once the new feature has completed, it can be merged back the the master branch.<\/li>\n<li><strong>Master Branch<\/strong>: we can consider it as the main branch\/code to work on. You can add other branches if needed. But the whole project can only have one master branch.<\/li>\n<li><strong>Commit<\/strong>: it holds the current state of the repository. It can be considered as a node of a linked-list. Every commit has a pointer to the parent commit object. You can go back to the parent commit object by changing the pointer.<\/li>\n<li><strong>HEAD<\/strong>: it is the pointer to the most recent commit on the current branch. It\u2019s actually a hash value of current commit, which is calculated by SHA-1 hash on a file with a hash value of 160 bits that uniquely identifies the contents of the file.<\/li>\n<\/ul>\n<h2>4. Git operations<\/h2>\n<p>After we understand the repositories and basic concepts of <code>Git<\/code>, we will move to the operations of <code>Git<\/code>.<\/p>\n<p>Before we go to the detailed example, make sure git is installed on your computer\/laptop. Then open the terminal, try to run <code>git --version<\/code>, you will see the version of the Git you\u2019ve installed as below:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:~ WXM$ git --version\ngit version 2.6.4 (Apple Git-63)\n<\/pre>\n<p>To have a basic idea of what command you&#8217;re going to use, you can refer to the table below:<\/p>\n<table style=\"height: 555px;\" width=\"567\">\n<tbody>\n<tr>\n<td width=\"72\">git command<\/td>\n<td width=\"279\">functions<\/td>\n<\/tr>\n<tr>\n<td>git init<\/td>\n<td>create a new local repository<\/td>\n<\/tr>\n<tr>\n<td>git clone<\/td>\n<td>download a project and its entire version history<\/td>\n<\/tr>\n<tr>\n<td>git add<\/td>\n<td>add file to the stating area<\/td>\n<\/tr>\n<tr>\n<td>git commit<\/td>\n<td>add file from staging area to local repository<\/td>\n<\/tr>\n<tr>\n<td>git status<\/td>\n<td>list all new or modified files to be commited<\/td>\n<\/tr>\n<tr>\n<td>git diff<\/td>\n<td>show files differences to be staged<\/td>\n<\/tr>\n<tr>\n<td>git log<\/td>\n<td>list version history for current branch<\/td>\n<\/tr>\n<tr>\n<td>git rm<\/td>\n<td>delete file from the working directory<\/td>\n<\/tr>\n<tr>\n<td>git reset<\/td>\n<td>undo commits and change back to specified one<\/td>\n<\/tr>\n<tr>\n<td>git push<\/td>\n<td>upload all local branch commits to github<\/td>\n<\/tr>\n<tr>\n<td>git pull<\/td>\n<td>download bookmark history and incoorparates changes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>4.1 git init command<\/h3>\n<p>After checking the installation of <code>Git<\/code>, we need to create a folder in our computer. So we create a blank folder named GitTutorial. Then go into the folder and type command <code>git init<\/code>. This command will change this folder to be a repository which you can manage. Also, the output after you type <code>git init<\/code> shows that you\u2019ve successfully created and initialized an empty git repository. After running <code>ls -ah<\/code>, you will see there\u2019s a .git folder inside the GitTutorial folder. This folder is utilized to track the git folder. Make sure you won\u2019t change it, which may lead to errors happening to the whole git folder. Also note that .git folder is hidden by default, and you can use the previous <code>ls -ah<\/code> command to make it visible.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>For all these operations, you can check the result below:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:~ WXM$ cd Documents\/\nWXMs-MacBook-Pro:Documents WXM$ cd GitTutorial\/\nWXMs-MacBook-Pro:GitTutorial WXM$ ls\nWXMs-MacBook-Pro:GitTutorial WXM$ git init\nInitialized empty Git repository in \/Users\/WXM\/Documents\/GitTutorial\/.git\/\nWXMs-MacBook-Pro:GitTutorial WXM$ ls -ah\n.\t..\t.git\n<\/pre>\n<h3>4.2 git add command<\/h3>\n<p>Secondly, we can use a text file as an example in usage of Git. Inside the GitTutorial folder, create an empty txt file with name GitExample.txt. Then add any words in it with: This is a txt file for Git Example.<\/p>\n<p>Lastly, we need to add the GitExample file into the Git system. Then run <code>git add GitExample.txt<\/code>. This command will add the txt file to the staging area, which is a middle area between workspace and the local repository.<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ vi GitExample.txt\nWXMs-MacBook-Pro:GitTutorial WXM$ ls\nGitExample.txt\nWXMs-MacBook-Pro:GitTutorial WXM$ cat GitExample.txt \nThis is a txt file for Git Example.\nWXMs-MacBook-Pro:GitTutorial WXM$ git add GitExample.txt \n<\/pre>\n<h3>4.3 git commit command<\/h3>\n<p>Then, we need to add this file from staging area to local repository. To achieve this, we can run <code>git commit -m \"First time writing the txt file\"<\/code>. Note the string after -m is the comment that we make by ourselves. It can be anything you want to comment. Below shows the results of all these operations.<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git commit -m \"First time writing the txt file\"\n[master (root-commit) 9f3fca6] First time writing the txt file\n 1 file changed, 1 insertion(+)\n create mode 100644 GitExample.txt\n<\/pre>\n<h3>4.4 git status command<\/h3>\n<p>Afterward, we can check the status of the whole folder with command <code>git status<\/code>. Then it seems to be everything if fine until now.<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git status\nOn branch master\nnothing to commit, working directory clean\n<\/pre>\n<h3>4.5 git diff command<\/h3>\n<p>The main purpose for <code>git diff<\/code> command is to show changes of the file between commits, commit and working tree, etc.<\/p>\n<p>To make it work, we need to create some differences. As a start, we modify the content in the GitExample.txt file and add one more line with \u201cOne more line\u201d. The content in side the txt file now is:<\/p>\n<pre class=\"brush:bash\">This is a txt file for Git Example.\nOne more line.\n<\/pre>\n<p>After this, we can run command <code>git diff<\/code> to check the difference between the current version with the previous one, the output is:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git diff\ndiff --git a\/GitExample.txt b\/GitExample.txt\nindex 30e6813..8c7c8d2 100644\n--- a\/GitExample.txt\n+++ b\/GitExample.txt\n@@ -1 +1,2 @@\n This is a txt file for Git Example.\n+One more line.\n<\/pre>\n<p>Then we can see that the sentence with \u201c+\u201d shows that where we add some contents to the file. Also we can predict that &#8220;-&#8221; operation shows we delete some contents from the file.<\/p>\n<p>Then if we made another change to the the txt file and add another line with \u201cI love changing things.\u201d Until now we\u2019ve made three changes \u2013 adding three lines compared with the first version. The four commits are : Love making changes &lt;- Made changes again &lt;- Add bad words &lt;- First time writing readme file, where the arrow shows the order of the changes.<\/p>\n<p>Another useful git diff command is <code>git diff --cached<\/code>, it\u2019ll show the differences between the index and the most recent commit. After running this command in our example, it sows nothing. The reason for this, is that until now, there\u2019s no difference between the index and most recent commit.<br \/>\nThen if we want to check the difference between the working directory and he most recent commit, <code>git diff HEAD<\/code> could be used.<\/p>\n<p>Here, we check the status of our current system, with the help of <code>git status<\/code>:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git status\nOn branch master\nChanges not staged for commit:\n  (use \"git add ...\" to update what will be committed)\n  (use \"git checkout -- ...\" to discard changes in working directory)\n\n\tmodified:   GitExample.txt\n\nno changes added to commit (use \"git add\" and\/or \"git commit -a\")\n<\/pre>\n<p>If we add the txt file to the index with command <code>git add GitExample.txt<\/code>, then run the <code>git diff<\/code>, it shows nothing. That\u2019s because right now the content in the working directory and the index are the same. This time, we run the command <code>git diff --cached<\/code>, it shows the content below instead of showing nothing as previous.<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git diff --cached\ndiff --git a\/GitExample.txt b\/GitExample.txt\nindex 30e6813..8c7c8d2 100644\n--- a\/GitExample.txt\n+++ b\/GitExample.txt\n@@ -1 +1,2 @@\n This is a txt file for Git Example.\n+One more line.\n<\/pre>\n<p>To check the status, run command <code>git status<\/code>:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git status\nOn branch master\nChanges to be committed:\n  (use \"git reset HEAD ...\" to unstage)\n\n\tmodified:   GitExample.txt\n<\/pre>\n<p>Until now, there\u2019s still difference between the index and the most recently commit. It can be proved by running <code>git diff HEAD<\/code><\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git diff HEAD\ndiff --git a\/GitExample.txt b\/GitExample.txt\nindex 30e6813..8c7c8d2 100644\n--- a\/GitExample.txt\n+++ b\/GitExample.txt\n@@ -1 +1,2 @@\n This is a txt file for Git Example.\n+One more line.\n<\/pre>\n<p>However, after we commit the file and rerun the previous command again, it shows nothing.<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitTutorial WXM$ git commit -m \"Add one more line\"\n[master cb3db9a] Add one more line\n 1 file changed, 1 insertion(+)\n<\/pre>\n<p>Also, the status is clear right now, if we run the command <code>git status<\/code>, it shows everything is clear:<\/p>\n<pre class=\"brush:bash\">WXMs-MacBook-Pro:GitDiffExample WXM$ git status\nOn branch master\nnothing to commit, working directory clean\n<\/pre>\n<h3>4.6 git log command<\/h3>\n<p>In real life of development, thousands lines of code could be changed. We can not remember it exactly. So <code>git log<\/code> should be useful right now, and it\u2019ll show all the change history we have made. We can try it with our own example here after we made three commits in our previous part:<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git log\ncommit 67713e5af45b67aa2628b12a73d4b493a16de159\nAuthor: Jun \nDate:   Sun Apr 24 17:19:12 2016 -0500\n\n    Love making changes\n\ncommit 47640c18cf433c6f6c7ba26b34f886688d34a1d3\nAuthor: Jun \nDate:   Sun Apr 24 17:18:38 2016 -0500\n\n    Made changes again\n\ncommit 1f50e80eb551e3ed7981251378d4609e30248d2f\nAuthor: Jun \nDate:   Sun Apr 24 16:58:21 2016 -0500\n\n    Add bad words\n\ncommit d90715179d20e10c5d88fc8d8a58972605fb38c0\nAuthor: Jun \nDate:   Sun Apr 24 16:32:28 2016 -0500\n\n    First time writing readme file\n<\/pre>\n<p>The above file shows every change we\u2019ve made with specific date and time. You should notice the long sequence of strings after the commit. That\u2019s the commit id, which corresponds to specific commit. Why it\u2019s not in 1, 2, 3\u2026 order? That\u2019s because Git is a distributed system. If every commit id is in order, when multiple changes happen, there should be conflict.<br \/>\nSometimes, the changes are quite a lot and what we want is just to check the changes. Then <code>git log --pretty=oneline<\/code> could be utilized to show each commit in one line.[ulp id=&#8217;pzgfvmZhgslwSymm&#8217;]<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git log --pretty=oneline\n67713e5af45b67aa2628b12a73d4b493a16de159 Love making changes\n47640c18cf433c6f6c7ba26b34f886688d34a1d3 Made changes again\n1f50e80eb551e3ed7981251378d4609e30248d2f Add bad words\nd90715179d20e10c5d88fc8d8a58972605fb38c0 First time writing readme file\n<\/pre>\n<h3>4.7 Git undo operation<\/h3>\n<p>In some cases, we want to change our minds and revert the commit we have made, then <code>Git<\/code> makes this easy to operate on. This is called <code>git undo commit<\/code>.&nbsp;To make this happen, we add another line to the GitExample.txt, with \u201cBad words for others\u201d. Then we add the file to the stating area and to the local repository. The following is the process that we have done:<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git add GitExample.txt \npcp352933pcs:GitTutorial WXM$ git commit -m \"Add bad words\"\n[master 1f50e80] Add bad words\n 1 file changed, 1 insertion(+)\npcp352933pcs:GitTutorial WXM$ cat GitExample.txt \nThis is a txt file for Git Example.\nBad words for others.\n<\/pre>\n<p>If we want to go back to the previous version, git reset command could be used. What we use here is git reset &#8211;hard HEAD^. HEAD is means the commit of current branch. Symbol \u201c^\u201d means go back to the previous one version, \u201c^^\u201d means the previous two version and so on so forth. While it\u2019s hard to continue with multiple \u201c^\u201d symbols when there\u2019s hundreds and thousands versions.<\/p>\n<p>Then you can use the number directly. For example git reset &#8211;hard HEAD^ is the same as git reset &#8211;hard HEAD~1 and git reset &#8211;hard HEAD^^ is the same as git reset \u2013hard HEAD~2. Pay attention to the symbol \u201c~\u201d before the numbers and do not forget to add it.<br \/>\nFor our own example, right now the head is in 67713e5af45b67aa2628b12a73d4b493a16de159 Love making changes. If we want to go back to the previous version. Run the git reset &#8211;hard HEAD^ command and check the result.<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitToturialWXM$ cat GitExample.txt \nThis is a txt file for Git Example.\nBad words for others.\nI made changes again:)\nI love changing things.\npcp352933pcs:GitTutorial WXM$ git reset --hard HEAD^\nHEAD is now at 47640c1 Made changes again\npcp352933pcs:GitTutorial WXM$ cat GitExample.txt \nThis is a txt file for Git Example.\nBad words for others.\nI made changes again:)\n<\/pre>\n<p>We can see from the above, that after we run the <code>git reset<\/code> command, it goes back to the previous version Made changes again. The 47640c1 is exactly the shortcut commit id for the previous commit.<br \/>\nThen if we want to go back to the initial status, we can go back twice with command <code>git reset --hard HEAD~2<\/code><\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git reset --hard HEAD~2\nHEAD is now at d907151 First time writing readme file\npcp352933pcs:GitTutorial WXM$ cat GitExample.txt \nThis is a txt file for Git Example.\n<\/pre>\n<p>Then run <code>git log<\/code>, it shows only the first version.<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git log\ncommit d90715179d20e10c5d88fc8d8a58972605fb38c0\nAuthor: Jun &lt;wuxiaomin98@hotmail.com&gt;\nDate: Sun Apr 24 16:32:28 2016 -0500\n\nFirst time writing readme file\npcp352933pcs:GitTutorial WXM$ cat GitExample.txt\nThis is a txt file for Git Example\n<\/pre>\n<p>However, if one day we find that the lines we added are useful, could we go further and get it back? Yes, we can! If the terminal is not closed, we need to find the commit number which the version is. For example, the commit number for Love making changes version is 67713e5af45b67aa2628b12a73d4b493a16de159. Then we can use the <code>git reset --hard<\/code> command with the commit id following it. Note that we can use the first few numbers of the commit id in most case.<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git reset --hard 67713e5a\nHEAD is now at 67713e5 Love making changes\npcp352933pcs:GitTutorial WXM$ cat GitExample.txt \nThis is a txt file for Git Example\nBad words for others.\nI made changes again:)\nI love changing things.\n<\/pre>\n<p>You see, it\u2019s back. However, make sure you really want to go back and force. In some cases, if you don\u2019t know the commit id, you won\u2019t come back to the newest version.<br \/>\nIn some cases, that you really want to come back to the newest version. Then <code>git reflow<\/code> could be useful for you, which will track every command you\u2019ve made. Run the command and you\u2019ll get something magic.<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git reflog\nd907151 HEAD@{0}: reset: moving to HEAD^^^\n67713e5 HEAD@{1}: reset: moving to 67713e5a\nd907151 HEAD@{2}: reset: moving to HEAD~2\n47640c1 HEAD@{3}: reset: moving to HEAD^\n67713e5 HEAD@{4}: commit: Love making changes\n47640c1 HEAD@{5}: commit: Made changes again\n1f50e80 HEAD@{6}: commit: Add bad words\nd907151 HEAD@{7}: commit: First time writing readme file\n<\/pre>\n<p>Here we have all the commit number to each commit and each reset\/undo operation. So you don\u2019t need to worry about where you\u2019re now.<br \/>\nTo make it more clear to understand, we can use the following figure to show how it work. The HEAD works as a pointer to point to specific version. For example, if HEAD is pointer to the last version. Then after using the reset command, we can go back the initial version.<\/p>\n<p><figure id=\"attachment_35627\" aria-describedby=\"caption-attachment-35627\" style=\"width: 403px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo1.jpg\"><img decoding=\"async\" class=\"wp-image-35627 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo1.jpg\" alt=\"git undo1\" width=\"403\" height=\"304\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo1.jpg 403w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo1-300x226.jpg 300w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/a><figcaption id=\"caption-attachment-35627\" class=\"wp-caption-text\">Git undo to version 1<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_35628\" aria-describedby=\"caption-attachment-35628\" style=\"width: 403px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo.jpg\"><img decoding=\"async\" class=\"wp-image-35628 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo.jpg\" alt=\"git undo\" width=\"403\" height=\"304\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo.jpg 403w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/03\/git-undo-300x226.jpg 300w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/a><figcaption id=\"caption-attachment-35628\" class=\"wp-caption-text\">Git undo to version 4<\/figcaption><\/figure><\/p>\n<p>As we can see from the previous figure, it&#8217;s quite easy for us to change to different versions of our code. It&#8217;s just an operation of command.<\/p>\n<h3>4.8 git rm operation<\/h3>\n<p>Also, it could be possible for us to delete the code in our directory. For example, <code>git rm<\/code> with the file name following the command. This command will delete the file from the working directory and stages the deletion.<\/p>\n<p>If we use the <code>git rm --cached<\/code> with the file name following the command, it will remove the file from the version control but preserves the file locally.<\/p>\n<p>Notice that we do not recommend it to use this command often. The reason for this is that this deletion may be unrecoverable. A better approach could be make a different branch and work on that branch.<\/p>\n<h2>5. Github connection<\/h2>\n<p>Lastly, we introduce <code>Github<\/code> briefly. To connect with <code>Github<\/code>, we need to have a <code>Github<\/code> account firstly. You can go to github.com and register one for yourself.<\/p>\n<p>After this, you can follow <a href=\"https:\/\/help.github.com\/articles\/set-up-git\/\">the link<\/a> to connect to <code>Github<\/code>. Then you can upload\/push and download\/pull the changes to the <code>Github<\/code> server.<\/p>\n<p>After the previous operations of <code>git add, git commit<\/code>, use <code>git push<\/code> command, it will publish the local changes to the remote repository. For example, the following shows how it looks after we push our code to the server:<\/p>\n<pre class=\"brush:bash\">pcp352933pcs:GitTutorial WXM$ git push\nCounting objects: 3, done.\nDelta compression using up to 4 threads.\nCompressing objects: 100% (2\/2), done.\nWriting objects: 100% (3\/3), 309 bytes | 0 bytes\/s, done.\nTotal 3 (delta 1), reused 0 (delta 0)\nTo https:\/\/github.com\/******\/GitTutorial.git\n   5a5a9ea..d907151  master -&gt; master\n<\/pre>\n<p>Now, other developers can view the changes by performing pull\/clone operation or updating their local repository. This will download the code in server to local directory and work on it. It can be achieved by <code>git clone<\/code> with the URL that on the server.<\/p>\n<p>The following will show you how it works:<\/p>\n<pre class=\"brush:bash\">Cloning into 'test_repo'...\nremote: Counting objects: 3, done.\nremote: Total 3 (delta 0), reused 3 (delta 0)\nUnpacking objects: 100% (3\/3), done.\n<\/pre>\n<h2>6. Conclusion<\/h2>\n<p>In conclusion, <code>Git<\/code> is a very useful tool for software developers. One should understand the principle of it and use it often. Also, you can find many useful materials in <code>Github<\/code> and you may want to contribute to some open sources.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the modification, compared with different versions of the source code. As the number of lines increases, it could go up to thousands and thousands lines. In real life, it could be &hellip;<\/p>\n","protected":false},"author":73,"featured_media":27377,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1353],"tags":[],"class_list":["post-36562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How does Git work? Git Tutorial for Beginners - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How does Git work? Git Tutorial for Beginners - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-28T08:00:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-23T11:52:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jun Wu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jun Wu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\"},\"author\":{\"name\":\"Jun Wu\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678\"},\"headline\":\"How does Git work? Git Tutorial for Beginners\",\"datePublished\":\"2016-04-28T08:00:43+00:00\",\"dateModified\":\"2019-04-23T11:52:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\"},\"wordCount\":2602,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg\",\"articleSection\":[\"Git\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\",\"name\":\"How does Git work? Git Tutorial for Beginners - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg\",\"datePublished\":\"2016-04-28T08:00:43+00:00\",\"dateModified\":\"2019-04-23T11:52:10+00:00\",\"description\":\"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Git\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/git\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How does Git work? Git Tutorial for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678\",\"name\":\"Jun Wu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg\",\"caption\":\"Jun Wu\"},\"description\":\"Jun (Steven) Wu is a current Master student in Computer Science &amp; Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.\",\"sameAs\":[\"https:\/\/wuxiaomin98.wordpress.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/jun-wu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How does Git work? Git Tutorial for Beginners - Java Code Geeks","description":"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/","og_locale":"en_US","og_type":"article","og_title":"How does Git work? Git Tutorial for Beginners - Java Code Geeks","og_description":"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the","og_url":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-04-28T08:00:43+00:00","article_modified_time":"2019-04-23T11:52:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg","type":"image\/jpeg"}],"author":"Jun Wu","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jun Wu","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/"},"author":{"name":"Jun Wu","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678"},"headline":"How does Git work? Git Tutorial for Beginners","datePublished":"2016-04-28T08:00:43+00:00","dateModified":"2019-04-23T11:52:10+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/"},"wordCount":2602,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg","articleSection":["Git"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/","url":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/","name":"How does Git work? Git Tutorial for Beginners - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg","datePublished":"2016-04-28T08:00:43+00:00","dateModified":"2019-04-23T11:52:10+00:00","description":"1. Introduction Version Control Systems are one of the software tools that help developers manage the source code over time. They keep track of the","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/git-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/git\/git-tutorial-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/"},{"@type":"ListItem","position":3,"name":"Git","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/git\/"},{"@type":"ListItem","position":4,"name":"How does Git work? Git Tutorial for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/6593091dba631ca0f41c4ba4e7c97678","name":"Jun Wu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/11\/Jun-Wu-96x96.jpg","caption":"Jun Wu"},"description":"Jun (Steven) Wu is a current Master student in Computer Science &amp; Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.","sameAs":["https:\/\/wuxiaomin98.wordpress.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/jun-wu\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/73"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36562"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36562\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/27377"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=36562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}