{"id":855,"date":"2020-02-01T11:26:50","date_gmt":"2020-02-01T11:26:50","guid":{"rendered":"http:\/\/blog.davcloud.top\/?p=855"},"modified":"2020-07-01T16:04:46","modified_gmt":"2020-07-01T08:04:46","slug":"how-to-use-git-and-github","status":"publish","type":"post","link":"https:\/\/blog.davcloud.top\/?p=855","title":{"rendered":"How to use Git and Github?"},"content":{"rendered":"\n<h2>Abstract<\/h2>\n\n\n\n<p>This article aims at introducing how to use Git and Github to manage different version and branch of a project. An example of creating a project with Git will be given and explain how it can be managed by Git and push to Github. We will take Linux system as example, for Windows can use Git Bash instead.<\/p>\n\n\n\n<h2>Part 1. Use Git<\/h2>\n\n\n\n<h3>What is Git?<\/h3>\n\n\n\n<p>Git is an open source distributed version control system that handles project versioning from small to very large projects efficiently and quickly. With git, a project can be easily developed by more developers with different branches, which is helpful to improve development of a large project. Besides, it allows developer to manage different versions of a project, helping developer to keep track of the process and recover to a stable version in the situation of unexpected events.<\/p>\n\n\n\n<h3>First time configuration<\/h3>\n\n\n\n<h4>User name and Email<\/h4>\n\n\n\n<p>Configure user name and email, which will be used in each commit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git config --global user.name \"username\"\n$ git config --global user.email example@email.com<\/code><\/pre>\n\n\n\n<h3>Create a new Git project<\/h3>\n\n\n\n<p>Assume that a project will be named as &#8220;hello_world&#8221; and placed in the home path ~\/.<\/p>\n\n\n\n<p>First, create a new folder called &#8220;hello_world&#8221;, which will be used as project workspace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~$ mkdir hello_world<\/code><\/pre>\n\n\n\n<p>Then, go to the workspace and initialize the repository with following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~$ cd hello_world\n~\/hello_world$ git init<\/code><\/pre>\n\n\n\n<p>Create a README.md file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ touch README.md<\/code><\/pre>\n\n\n\n<h3>Workflow<\/h3>\n\n\n\n<p>The repository consists of three trees. The first is Working dir, which holds the actual files. The second is Index, which act as a staging area. The third is HEAD, which points to the latest commit.<\/p>\n\n\n\n<p>Once the files in Working dir changes, the changed parts are required to add to stage area before commit. When you commit, all the changes in stage area will be submit to the HEAD, and the stage area will become cleaned.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/rogerdudler.github.io\/git-guide\/img\/trees.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<h3>Add and Commit<\/h3>\n\n\n\n<p>Add changes to Index by the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git add &lt;filename><\/code><\/pre>\n\n\n\n<p>For convenience, we can add all the changes in the workspace with command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git add .<\/code><\/pre>\n\n\n\n<p>To commit all changes from Index to HEAD, run command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git commit -m \"Commit message\"<\/code><\/pre>\n\n\n\n<p>Now the latest version of project has been commit to HEAD.<\/p>\n\n\n\n<h2>Part 2. Use Github<\/h2>\n\n\n\n<h3>What is Github?<\/h3>\n\n\n\n<p>Github is a global company that provides&nbsp;hosting&nbsp;for&nbsp;software development version control using Git. With Github, the code can be pushed online and developed by more developer. Besides, it is also a famous open source code platform.<\/p>\n\n\n\n<h3>Sign up on Github<\/h3>\n\n\n\n<p>Go to the official website of Github to sign up: <a href=\"https:\/\/github.com\/\">https:\/\/github.com\/<\/a><\/p>\n\n\n\n<h3>Create a new repository on Github<\/h3>\n\n\n\n<h4>Step 1. Choose in the menu<\/h4>\n\n\n\n<p>Choose &#8220;New Repository&#8221; option in the add menu:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"168\" height=\"235\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-8.png\" alt=\"\" class=\"wp-image-917\"\/><\/figure><\/div>\n\n\n\n<h4>Step 2. Complete repository options<\/h4>\n\n\n\n<p>Complete the repository name, description and privacy option:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"727\" height=\"649\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-7.png\" alt=\"\" class=\"wp-image-915\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-7.png 727w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-7-300x268.png 300w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-text-color has-vivid-green-cyan-color\">Note: For free account, you can only create public repository. However, if you are a student, you can apply student account and create private repository free.<\/p>\n\n\n\n<p>If you want to push a exist local repository to this repository later, do not choose &#8220;Initialize this repository with a README&#8221;.<\/p>\n\n\n\n<h3>Allow your computer to access your Github account<\/h3>\n\n\n\n<h4>Step 1. Edit settings of your account<\/h4>\n\n\n\n<p>Choose &#8220;Settings&#8221; in the menu of the account:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"200\" height=\"458\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-9.png\" alt=\"\" class=\"wp-image-919\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-9.png 200w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-9-131x300.png 131w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n\n<h4>Step 2. Click &#8220;SSH and GPG keys&#8221; option<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"229\" height=\"594\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-5.png\" alt=\"\" class=\"wp-image-908\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-5.png 229w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-5-116x300.png 116w\" sizes=\"(max-width: 229px) 100vw, 229px\" \/><\/figure><\/div>\n\n\n\n<h4>Step 3. Click &#8220;New SSH key&#8221; option<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"744\" height=\"51\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-6.png\" alt=\"\" class=\"wp-image-911\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-6.png 744w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-6-300x21.png 300w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/figure><\/div>\n\n\n\n<h4>Step 4. Generate SSH key of your computer<\/h4>\n\n\n\n<p>Generate SSH key of your computer in terminal(mac and linux) or git bash(Windows) with command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~$ ssh-keygen -t rsa -C \"example@email.com\"<\/code><\/pre>\n\n\n\n<p>The email address is the email which you used to sign up for Github.<\/p>\n\n\n\n<p>Then, a file &#8220;id_rsa.pub&#8221; will be generated in the path that feedback in the terminal, usually is ~\/.ssh.<\/p>\n\n\n\n<h4>Step 5. Copy all text in id_rsa.pub to your Github keys list<\/h4>\n\n\n\n<p>Add a title for this key and copy the SSH key(all text in id_rsa.pub) to the page below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"769\" height=\"421\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-10.png\" alt=\"\" class=\"wp-image-924\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-10.png 769w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-10-300x164.png 300w\" sizes=\"(max-width: 769px) 100vw, 769px\" \/><\/figure><\/div>\n\n\n\n<p>The SSH key usual start with &#8220;ssh-rsa&#8221; and end with your email.<\/p>\n\n\n\n<p>Then you can see the added SSH key in the list.<\/p>\n\n\n\n<h3>Push a local repository to github repository<\/h3>\n\n\n\n<h4>Add remote repository for local repository<\/h4>\n\n\n\n<p>Add the repository on Github as remote repository of local repository:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git remote add origin git@github.com:&lt;username>\/hello_world.git<\/code><\/pre>\n\n\n\n<p>The address can also be found on your github repository:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"906\" height=\"38\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-11.png\" alt=\"\" class=\"wp-image-934\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-11.png 906w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-11-300x13.png 300w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2020\/01\/image-11-768x32.png 768w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><\/figure><\/div>\n\n\n\n<h4>Push the local repository to remote repository<\/h4>\n\n\n\n<p>Push with command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git push -u origin master<\/code><\/pre>\n\n\n\n<h3>Branch<\/h3>\n\n\n\n<p>Branches are used to develop features isolated from each other. The default is &#8220;master&#8221;, which is created when you create the repository. You can create a new branch and develop new features in it. When developed, you can merge it to the main branch &#8220;master&#8221;.<\/p>\n\n\n\n<h4>Create a new branch<\/h4>\n\n\n\n<p>Create a new branch and switch to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git checkout -b &lt;branch_name><\/code><\/pre>\n\n\n\n<h4>List all branches<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git branch<\/code><\/pre>\n\n\n\n<p>All the branches will be list, and the branch with a &#8220;*&#8221; before its name is the current branch.<\/p>\n\n\n\n<h4>Switch to a branch<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git checkout &lt;branch_name><\/code><\/pre>\n\n\n\n<h4>Delete a branch<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git branch -d &lt;branch_name><\/code><\/pre>\n\n\n\n<h4>Push a branch to Github repository<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git push -u origin &lt;branch_name><\/code><\/pre>\n\n\n\n<h4>Update local repository from Github repository<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git pull<\/code><\/pre>\n\n\n\n<h4>Merge<\/h4>\n\n\n\n<p>Different branches can be merged with each other. The command below can merge a branch into current branch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git merge &lt;branch_name><\/code><\/pre>\n\n\n\n<p>If you want to merge a branch to the master branch, please switch to the master branch first and then merge:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/hello_world$ git checkout master\n~\/hello_world$ git merge &lt;branch_name><\/code><\/pre>\n\n\n\n<h2>References<\/h2>\n\n\n\n<ol><li><a href=\"http:\/\/rogerdudler.github.io\/git-guide\/index.html\">http:\/\/rogerdudler.github.io\/git-guide\/index.html<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Abstract This article aims at introducing how to use Git and Github to manage different version and branch of a project. An example of creating a project with Git will be given and explain how it can be managed by Git and push to Github. We will take Linux system as example, for Windows can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[467],"tags":[471,472],"_links":{"self":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/855"}],"collection":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=855"}],"version-history":[{"count":84,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/855\/revisions"}],"predecessor-version":[{"id":1527,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/855\/revisions\/1527"}],"wp:attachment":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}