Introduction_to_Git_and_GitHub/module1.ipynb
2025-03-04 18:25:40 +03:00

1516 lines
54 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"jupyter": {
"source_hidden": true
}
},
"source": [
"# Diffing Files\n",
"\n",
"This reading contains the code used in the instructional videos from [**Diffing Files**<svg aria-labelledby=\"cds-react-aria402160580-:rim:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria402160580-:rim:\"><title id=\"cds-react-aria402160580-:rim:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/tnlzg/diffing-files) \n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"```bash\n",
"\n",
"cat rearrange1.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"import re\n",
"\n",
"def rearrange\\_name(name):\n",
"\n",
"    result = re.search(r\"^(\\[\\\\w .\\]\\*), (\\[\\\\w .\\]\\*)$\", name)\n",
"\n",
"    if result == None:\n",
"\n",
"        return name\n",
"\n",
"    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n",
"\n",
"user@ubuntu:~$ cat rearrange2.py \n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"import re\n",
"\n",
"def rearrange\\_name(name):\n",
"\n",
"    result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n",
"\n",
"    if result == None:\n",
"\n",
"        return name\n",
"\n",
"    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n",
"\n",
"1\n",
"\n",
"```bash\n",
"cat rearrange2.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"import re\n",
"\n",
"def rearrange\\_name(name):\n",
"\n",
"    result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n",
"\n",
"    if result == None:\n",
"\n",
"        return name\n",
"\n",
"    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n",
"\n",
"\n",
"```bash\n",
"diff rearrange1.py rearrange2.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"6c6\n",
"\n",
"<     result = re.search(r\"^(\\[\\\\w .\\]\\*), (\\[\\\\w .\\]\\*)$\", name)\n",
"\n",
"\\---\n",
"\n",
"\\>     result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n",
"\n",
"\n",
"```bash\n",
"diff validations1.py validations2.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"5c5,6\n",
"\n",
"< assert (type(username) == str), \"username must be a string\"\n",
"\n",
"\\--\n",
"\n",
"\\> if type(username != str: \n",
"\n",
"\\>     raise TypeError(\"username must be a string\"\n",
"\n",
"11a13,15\n",
"\n",
"\\>     return False\n",
"\n",
"\\> # Usernames can't begin with a number\n",
"\n",
"\\> if username\\[0\\].isnumeric():\n",
"\n",
"\n",
"```bash\n",
"diff \\-u validations1.py validations2.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\--- validations1.py 2019-06-06 14:28:49.639209499 +0200\n",
"\n",
"+++ validations2.py 2019-06-06 14:30:48.019360890 +0200\n",
"\n",
"@@ -2,7 +2,8 @@\n",
"\n",
" def validate\\_user(username, minlen):\n",
"\n",
"\\-    assert type(username) == str, \"username must be a string\"\n",
"\n",
"+    if type(username) != str:\n",
"\n",
"+        raise TypeError(\"username must be a string\")\n",
"\n",
"     if minlen < 1:\n",
"\n",
"         raise ValueError(\"minlen must be at least 1\")\n",
"\n",
"@@ -10,5 +11,8 @@\n",
"\n",
"         return False\n",
"\n",
"     if not username.isalnum():\n",
"\n",
"         return False\n",
"\n",
"+    # Usernames can't begin with a number\n",
"\n",
"+    if username\\[0\\].isnumeric():\n",
"\n",
"+        return False\n",
"\n",
"     return True"
]
},
{
"cell_type": "markdown",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"source": [
"# Applying Changes\n",
"\n",
"This reading contains the code used in the instructional videos from [**Applying Changes** <svg aria-labelledby=\"cds-react-aria2448342354-:r9g:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2448342354-:r9g:\"><title id=\"cds-react-aria2448342354-:r9g:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/qIzdp/applying-changes).\n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"\n",
"```\n",
"cat cpu\\_usage.py \n",
"```\n",
"\n",
"\n",
"cat cpu\\_usage.py \n",
"\n",
"**Code output:**\n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"import psutil\n",
"\n",
"def check\\_cpu\\_usage(percent):\n",
"\n",
"    usage = psutil.cpu\\_percent()\n",
"\n",
"    return usage < percent\n",
"\n",
"if not check\\_cpu\\_usage(75):\n",
"\n",
"    print(\"ERROR! CPU is overloaded\")\n",
"\n",
"else:\n",
"\n",
"    print(\"Everything ok\")\n",
"\n",
"\n",
"```\n",
"cat cpu\\_usage.diff \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"\\--- cpu\\_usage.py 2019-06-23 08:16:04.666457429 -0700\n",
"\n",
"+++ cpu\\_usage\\_fixed.py 2019-06-23 08:15:37.534370071 -0700\n",
"\n",
"@@ -2,7 +2,8 @@\n",
"\n",
" import psutil\n",
"\n",
" def check\\_cpu\\_usage(percent):\n",
"\n",
"\\-    usage = psutil.cpu\\_percent()\n",
"\n",
"+    usage = psutil.cpu\\_percent(1)\n",
"\n",
"+    print(\"DEBUG: usage: {}\".format(usage))\n",
"\n",
"     return usage < percent\n",
"\n",
" if not check\\_cpu\\_usage(75):\n",
"\n",
"\n",
"```\n",
"patch cpu\\_usage.py < cpu\\_usage.diff \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"patching file cpu\\_usage.py\n",
"\n",
"\n",
"```\n",
"cat cpu\\_usage.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"import psutil\n",
"\n",
"def check\\_cpu\\_usage(percent):\n",
"\n",
"    usage = psutil.cpu\\_percent(1)\n",
"\n",
"    print(\"DEBUG: usage: {}\".format(usage))\n",
"\n",
"    return usage < percent\n",
"\n",
"if not check\\_cpu\\_usage(75):\n",
"\n",
"    print(\"ERROR! CPU is overloaded\")\n",
"\n",
"else:\n",
"\n",
"    print(\"Everything ok\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practical Application of diff and patch\n",
"\n",
"This reading contains the code used in the instructional videos from [**Practical application of diff and patch**<svg aria-labelledby=\"cds-react-aria8760104591-:r2f:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:r2f:\"><title id=\"cds-react-aria8760104591-:r2f:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/IkuqC/practical-application-of-diff-and-patch). \n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"\n",
"```bash\n",
"cp disk\\_usage.py disk\\_usage\\_original.py \n",
"\n",
"cp disk\\_usage.py disk\\_usage\\_fixed.py \n",
"```\n",
"\n",
"\n",
"## **File with code**\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    return 1\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"return 0\n",
"\n",
"./disk\\_usage\\_fixed.py \n",
"\n",
"#this throws an error\n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"File \"./disk\\_usage\\_fixed.py\", line 19\n",
"\n",
"    return 1\n",
"\n",
"    ^\n",
"\n",
"SyntaxError: 'return' outside function\n",
"\n",
"## File with code\n",
"\n",
"The instructor adds import sys at the beginning, then change return 1 to sys.exit(1) and return 0 to sys.exit(0).\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"import sys\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    sys.exit(1)\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"sys.exit(0)\n",
"\n",
"./disk\\_usage\\_fixed.py\n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"ERROR: Not enough disk space\n",
"\n",
"## File with code\n",
"\n",
"Instructor changed change check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10) in previous file to check\\_disk\\_usage(\"/\", 2, 10).\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"import sys\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    sys.exit(1)\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"sys.exit(0)\n",
"\n",
"./disk\\_usage\\_fixed.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok\n",
"\n",
"```bash\n",
"diff \\-u disk\\_usage\\_original.py disk\\_usage\\_fixed.py > disk\\_usage.diff\n",
"\n",
"cat disk\\_usage.diff \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\--- disk\\_usage\\_original.py 2019-06-22 15:13:38.591579963 -0700\n",
"\n",
"+++ disk\\_usage\\_fixed.py 2019-06-22 15:41:35.013023839 -0700\n",
"\n",
"@@ -1,6 +1,7 @@\n",
"\n",
" #!/usr/bin/env python3\n",
"\n",
" import shutil\n",
"\n",
"+import sys\n",
"\n",
" def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"     \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"@@ -14,9 +15,9 @@\n",
"\n",
"     return True\n",
"\n",
" # Check for at least 2 GB and 10% free\n",
"\n",
"\\-if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"+if not check\\_disk\\_usage(\"/\", 2, 10):\n",
"\n",
"     print(\"ERROR: Not enough disk space\")\n",
"\n",
"\\-    return 1\n",
"\n",
"+    sys.exit(1)\n",
"\n",
" print(\"Everything ok\")\n",
"\n",
"\\-return 0\n",
"\n",
"+sys.exit(0)\n",
"\n",
"```bash\n",
"patch disk\\_usage.py < disk\\_usage.diff \n",
"\n",
"patching file disk\\_usage.py\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"patching file disk\\_usage.py\n",
"```bash\n",
"./disk\\_usage.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Study guide: diff and patch\n",
"\n",
"## The diff command\n",
"\n",
"You use the diff command to find the differences between two files. On its own, its a bit hard to use; instead, use diff -u to find lines that differ in two files:\n",
"\n",
"## Using diff -u\n",
"\n",
"You use the diff -u command to compare two files, line by line, and have the differing lines compared side-by-side in the same output. For an example of what this looks like, see below:\n",
"\n",
"```bash\n",
"~$ cat menu1.txt \n",
"\n",
"Menu1:\n",
"\n",
"Apples\n",
"\n",
"Bananas\n",
"\n",
"Oranges\n",
"\n",
"Pears\n",
"\n",
"~$ cat menu2.txt \n",
"\n",
"Menu:\n",
"\n",
"Apples\n",
"\n",
"Bananas\n",
"\n",
"Grapes\n",
"\n",
"Strawberries\n",
"\n",
"~$ diff \\-u menu1.txt menu2.txt \n",
"\n",
"\\--- menu1.txt   2019\\-12-16 18:46:13.794879924 +0900\n",
"\n",
"+++ menu2.txt   2019\\-12-16 18:46:42.090995670 +0900\n",
"\n",
"@@ \\-1,6 +1,6 @@\n",
"\n",
"\\-Menu1:\n",
"\n",
"+Menu:\n",
"\n",
" Apples\n",
"\n",
" Bananas\n",
"\n",
"\\-Oranges\n",
"\n",
"\\-Pears\n",
"\n",
"+Grapes\n",
"\n",
"+Strawberries\n",
"```\n",
"\n",
"## The patch command\n",
"\n",
"The patch command is useful for applying file differences. See the example below, which compares two files. The comparison is saved as a .diff file, which is then patched to the original file!\n",
"\n",
"```bash\n",
"~$ cat hello\\_world.txt \n",
"\n",
"Hello World\n",
"\n",
"~$ cat hello\\_world\\_long.txt \n",
"\n",
"Hello World\n",
"\n",
"It's a wonderful day!\n",
"\n",
"~$ diff -u hello\\_world.txt hello\\_world\\_long.txt \n",
"\n",
"\\--- hello\\_world.txt     2019-12-16 19:24:12.556102821 +0900\n",
"\n",
"+++ hello\\_world\\_long.txt        2019-12-16 19:24:38.944207773 +0900\n",
"\n",
"@@ -1 +1,3 @@\n",
"\n",
" Hello World\n",
"\n",
"+\n",
"\n",
"+It's a wonderful day!\n",
"\n",
"~$ diff \\-u hello\\_world.txt hello\\_world\\_long.txt > hello\\_world.diff\n",
"\n",
"~$ patch hello\\_world.txt < hello\\_world.diff \n",
"\n",
"patching file hello\\_world.txt\n",
"\n",
"~$ cat hello\\_world.txt \n",
"\n",
"Hello World\n",
"\n",
"It's a wonderful day!\n",
"```\n",
"\n",
"## Resources for more information\n",
"\n",
"There are other interesting patch and diff commands such as patch -p1 and diff -r . For more information on these commands, check out the following resources:\n",
"\n",
"- [http://man7.org/linux/man-pages/man1/diff.1.html<svg aria-labelledby=\"cds-react-aria8760104591-:rgs:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:rgs:\"><title id=\"cds-react-aria8760104591-:rgs:-title\">Opens in a new tab</title></svg>](http://man7.org/linux/man-pages/man1/diff.1.html)\n",
"- [http://man7.org/linux/man-pages/man1/patch.1.html<svg aria-labelledby=\"cds-react-aria8760104591-:rgu:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:rgu:\"><title id=\"cds-react-aria8760104591-:rgu:-title\">Opens in a new tab</title></svg>](http://man7.org/linux/man-pages/man1/patch.1.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More Information About Git\n",
"\n",
"Check out the following links for more information:\n",
"\n",
"- [https://git-scm.com/doc<svg aria-labelledby=\"cds-react-aria8737541281-:rq0:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:rq0:\"><title id=\"cds-react-aria8737541281-:rq0:-title\">Opens in a new tab</title></svg>](https://git-scm.com/doc)\n",
"- [https://www.mercurial-scm.org/<svg aria-labelledby=\"cds-react-aria8737541281-:rq2:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:rq2:\"><title id=\"cds-react-aria8737541281-:rq2:-title\">Opens in a new tab</title></svg>](https://www.mercurial-scm.org/)\n",
"- [https://subversion.apache.org/<svg aria-labelledby=\"cds-react-aria8737541281-:rq4:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:rq4:\"><title id=\"cds-react-aria8737541281-:rq4:-title\">Opens in a new tab</title></svg>](https://subversion.apache.org/)\n",
"- [https://en.wikipedia.org/wiki/Version\\_control<svg aria-labelledby=\"cds-react-aria8737541281-:rq6:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:rq6:\"><title id=\"cds-react-aria8737541281-:rq6:-title\">Opens in a new tab</title></svg>](https://en.wikipedia.org/wiki/Version_control)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# First steps with Git\n",
"\n",
"This reading contains the code used in the instructional videos from [**First steps with Git**<svg aria-labelledby=\"cds-react-aria8737541281-:r1jh:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:r1jh:\"><title id=\"cds-react-aria8737541281-:r1jh:-title\">Opens in a new tab</title><path d=\"M4.5 17c-.412 0-.766-.147-1.06-.44A1.445 1.445 0 013 15.5v-11c0-.412.147-.766.44-1.06.294-.293.648-.44 1.06-.44h4.75c.213 0 .39.071.534.214a.72.72 0 01.216.532c0 .21-.072.39-.216.535a.72.72 0 01-.534.219H4.5v11h11v-4.75c0-.213.072-.39.214-.534a.72.72 0 01.532-.216c.21 0 .39.072.535.216a.72.72 0 01.219.534v4.75c0 .412-.147.766-.44 1.06-.294.293-.647.44-1.06.44h-11zm11-11.438L8.583 12.48a.681.681 0 01-.52.219.758.758 0 01-.521-.24.729.729 0 010-1.062L14.438 4.5H12.75a.728.728 0 01-.534-.214.72.72 0 01-.216-.532c0-.21.072-.39.216-.535A.72.72 0 0112.75 3h3.5c.212 0 .39.072.534.216A.726.726 0 0117 3.75v3.5c0 .213-.072.39-.214.534a.72.72 0 01-.532.216.734.734 0 01-.535-.216.72.72 0 01-.219-.534V5.562z\" fill=\"currentColor\" data-darkreader-inline-fill=\"\" style=\"--darkreader-inline-fill: currentColor;\"></path></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/2xrXl/first-steps-with-git)**.** \n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"```bash\n",
"git config \\--global user.email \"me@example.com\"\n",
"\n",
"git config \\--global user.name \"My name\"\n",
"```\n",
"\n",
"```bash\n",
"mkdir checks\n",
"\n",
"cd checks\n",
"\n",
"git init\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Initialized empty Git repository in /home/user/checks/.git/\n",
"\n",
"```bash\n",
"ls \\-la\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 12\n",
"\n",
"drwxrwxr-x  3 user user 4096 Jul  9 18:16 .\n",
"\n",
"drwxr-xr-x 18 user user 4096 Jul  9 18:16 ..\n",
"\n",
"drwxrwxr-x  7 user user 4096 Jul  9 18:16 .git\n",
"\n",
"user@ubuntu:~/checks$ ls -l .git/\n",
"\n",
"total 32\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 branches\n",
"\n",
"\\-rw-rw-r-- 1 user user   92 Jul  9 18:16 config\n",
"\n",
"\\-rw-rw-r-- 1 user user   73 Jul  9 18:16 description\n",
"\n",
"\\-rw-rw-r-- 1 user user   23 Jul  9 18:16 HEAD\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 hooks\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 info\n",
"\n",
"drwxrwxr-x 4 user user 4096 Jul  9 18:16 objects\n",
"\n",
"drwxrwxr-x 4 user user 4096 Jul  9 18:16 refs\n",
"\n",
"```bash\n",
"ls \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 4\n",
"\n",
"\\-rw-rw-r-- 1 user user 657 Jul  9 18:26 disk\\_usage.py\n",
"\n",
"1\n",
"\n",
"ls \\-l .git/\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"total 32\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 branches\n",
"\n",
"\\-rw-rw-r-- 1 user user   92 Jul  9 18:16 config\n",
"\n",
"\\-rw-rw-r-- 1 user user   73 Jul  9 18:16 description\n",
"\n",
"\\-rw-rw-r-- 1 user user   23 Jul  9 18:16 HEAD\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 hooks\n",
"\n",
"drwxrwxr-x 2 user user 4096 Jul  9 18:16 info\n",
"\n",
"drwxrwxr-x 4 user user 4096 Jul  9 18:16 objects\n",
"\n",
"drwxrwxr-x 4 user user 4096 Jul  9 18:16 refs\n",
"\n",
"```bash\n",
"cp ../disk\\_usage.py .\n",
"\n",
"ls \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 4\n",
"\n",
"\\-rw-rw-r-- 1 user user 657 Jul  9 18:26 disk\\_usage.py\n",
"\n",
"```bash\n",
"git add disk\\_usage.py \n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"No commits yet\n",
"\n",
"Changes to be committed:\n",
"\n",
"  (use \"git rm --cached <file>...\" to unstage)\n",
"\n",
"new file:   disk\\_usage.py\n",
"\n",
"```bash\n",
"git commit\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
" GNU nano 3.2         /home/user/checks/.git/COMMIT\\_EDITMSG                    \n",
"\n",
"\\# Please enter the commit message for your changes. Lines starting\n",
"\n",
"\\# with '#' will be ignored, and an empty message aborts the commit.\n",
"\n",
"\\# On branch master\n",
"\n",
"\\# Initial commit\n",
"\n",
"\\# Changes to be committed:\n",
"\n",
"\\#       new file:   disk\\_usage.py\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tracking Files\n",
"\n",
"This reading contains the code used in the instructional videos from [**Tracking Files**<svg aria-labelledby=\"cds-react-aria8737541281-:r1t1:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8737541281-:r1t1:\"><title id=\"cds-react-aria8737541281-:r1t1:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/CO6OR/tracking-files)**.**\n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"```bash\n",
"cd checks\n",
"\n",
"ls \\-l\n",
"```\n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"total 4\n",
"\n",
"\\-rw-r--r-- 1 user user 657 Jul  9 12:52 disk\\_usage.py\n",
"\n",
"user@ubuntu:~/checks$ git status\n",
"\n",
"On branch master\n",
"\n",
"nothing to commit, working tree clean\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"nothing to commit, working on clean tree\n",
"\n",
"```bash\n",
"atom disk\\_usage.py \n",
"\n",
"git status\n",
"```\n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"Changes to be committed:\n",
"\n",
"  (use \"git restore --staged <file>...\" to unstage)\n",
"\n",
"modified:   disk\\_usage.py\n",
"\n",
"```bash\n",
"git commit \\-m 'Add periods to the end of sentences.'\n",
"```\n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master ae8d19c\\] Add periods to the end of sentences.\n",
"\n",
" 1 file changed, 2 insertions(+), 2 deletions(-)\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"nothing to commit, working tree clean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Basic Git Workflow\n",
"\n",
"This reading contains the code used in the instructional videos from [**The basic Git Workflow**<svg aria-labelledby=\"cds-react-aria1810281246-:r2r:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria1810281246-:r2r:\"><title id=\"cds-react-aria1810281246-:r2r:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/p5zYm/the-basic-git-workflow)**.**\n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"```bash\n",
"mkdir scripts\n",
"\n",
"cd scripts\n",
"\n",
"git init\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Initialized empty Git repository in /home/user/scripts/.git/\n",
"\n",
"```bash\n",
"git config \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"user.email=me@example.com\n",
"\n",
"user.name=My name\n",
"\n",
"core.repositoryformatversion=0\n",
"\n",
"core.filemode=true\n",
"\n",
"core.bare=false\n",
"\n",
"core.logallrefupdates=true\n",
"\n",
"## File with code\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"def main():\n",
"\n",
"    pass\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"chmod +x all\\_checks.py\n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"No commits yet\n",
"\n",
"Untracked files:\n",
"\n",
"  (use \"git add <file>...\" to include in what will be committed)\n",
"\n",
"all\\_checks.py\n",
"\n",
"nothing added to commit but untracked files present (use \"git add\" to track)\n",
"\n",
"```bash\n",
"git add all\\_checks.py\n",
"\n",
"git commit\n",
"```\n",
"\n",
"## File with code\n",
"\n",
"```bash\n",
"Create an empty all\\_checks.\n",
"\n",
"# Please enter the commit message for your changes. Lines starting\n",
"\n",
"# with '#' will be ignored, and an empty message aborts the commit.\n",
"\n",
"#\n",
"\n",
"# On branch master\n",
"\n",
"#\n",
"\n",
"# Initial commit\n",
"\n",
"#\n",
"\n",
"# Changes to be committed:\n",
"\n",
"#       new file:   all\\_checks.py\n",
"\n",
"#\n",
"```\n",
"\n",
"## File with code\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import os\n",
"\n",
"def check\\_reboot():\n",
"\n",
"    \"\"\"Returns True if the computer has a pending reboot.\"\"\"\n",
"\n",
"    return os.path.exists(\"/run/reboot-required\")   \n",
"\n",
"def main():\n",
"\n",
"    pass\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"On branch master\n",
"\n",
"Changes not staged for commit:\n",
"\n",
"  (use \"git add <file>...\" to update what will be committed)\n",
"\n",
"  (use \"git checkout -- <file>...\" to discard changes in working directory)\n",
"\n",
"modified:   all\\_checks.py\n",
"\n",
"no changes added to commit (use \"git add\" and/or \"git commit -a\")\n",
"\n",
"```bash\n",
"git add all\\_checks.py \n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"Changes to be committed:\n",
"\n",
"  (use \"git reset HEAD <file>...\" to unstage)\n",
"\n",
"modified:   all\\_checks.py\n",
"\n",
"```bash\n",
"git commit \\-m 'Add a check\\_reboot function'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master d8e139c\\] Add a check\\_reboot function\n",
"\n",
" 1 file changed, 6 insertions(+)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Anatomy of a Commit Message\n",
"\n",
"This reading contains the code used in the instructional videos from [**Anatomy of a Commit Message**<svg aria-labelledby=\"cds-react-aria6078214324-:rct:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:rct:\"><title id=\"cds-react-aria6078214324-:rct:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/mhymc/anatomy-of-a-commit-message).\n",
"\n",
"## Introduction\n",
"\n",
"This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n",
"\n",
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n",
"\n",
"```bash\n",
"cat example\\_commit.txt \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Provide a good commit message example\n",
"\n",
"The purpose of this commit is to provide an example of a hand-crafted,\n",
"\n",
"artisanal commit message. The first line is a short, approximately 50-character\n",
"\n",
"summary, followed by an empty line. The subsequent paragraphs are jam-packed\n",
"\n",
"with descriptive information about the change, but each line is kept under 72\n",
"\n",
"characters in length.\n",
"\n",
"If even more information is needed to explain the change, more paragraphs can\n",
"\n",
"be added after blank lines, with links to issues, tickets, or bugs. Remember\n",
"\n",
"that future you will thank current you for your thoughtfulness and foresight!\n",
"\n",
"\\# Please enter the commit message for your changes. Lines starting\n",
"\n",
"\\# with '#' will be ignored, and an empty message aborts the commit.\n",
"\n",
"\\# On branch master\n",
"\n",
"\\# Changes to be committed:\n",
"\n",
"\\# new file:   super\\_script.py\n",
"\n",
"\\# new file:   cool\\_config.txt\n",
"\n",
"\n",
"```bash\n",
"cd scripts\n",
"\n",
"git log\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit d8e139cc4f7dcd13b75cff67cfb68527e24c59c5 (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Thu Jul 11 17:19:32 2019 +0200\n",
"\n",
"    Add a check\\_reboot function\n",
"\n",
"commit 6cfc29966acda8213fcd8ac2735b31f3fdbc6c53\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Thu Jul 11 12:08:46 2019 +0200\n",
"\n",
"    Create and empty all\\_checks.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Git\n",
"\n",
"In any Git project, there are three sections: the Git directory, the working tree, and the staging area. This study guide provides some basic concepts and commands that can help you get started with Git as well as guidelines to help you write an effective commit message.\n",
"\n",
"## Git config command\n",
"\n",
"The Git config command is used to set the values to identify who made changes to Git repositories. To set the values of user.email and user.name to your email and name, type: \n",
"\n",
": ~$ git config  - -global user.email “[me@example.com<svg aria-labelledby=\"cds-react-aria6078214324-:rld:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:rld:\"><title id=\"cds-react-aria6078214324-:rld:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/supplement/WaYsJ/)”\n",
"\n",
": ~$ git config  - -global user.name “My name”\n",
"\n",
"## Git init command\n",
"\n",
": ~/checks$ git init\n",
"\n",
"The Git init command can create a new empty repository in a current directory or re-initialize an existing one. \n",
"\n",
"## Git ls -la command\n",
"\n",
": ~/checks$ ls -la\n",
"\n",
"The Git ls - la command checks that an identified directory exists.\n",
"\n",
": ~/checks$ ls -l .git/\n",
"\n",
"The ls-l.git command checks inside the directory to see the different things that it contains. This is called the Git directory. The Git directory is a database for your Git project that stores the changes and the change history.\n",
"\n",
"## Git add command\n",
"\n",
":~/checks$ git add disk\\_usage.py\n",
"\n",
"Using the Git add command allows Git to track your file and uses the selected file as a parameter when adding it to the staging area. The staging area is a file maintained by Git that contains all the information about what files and changes are going to go into your next commit.\n",
"\n",
"## Git status command\n",
"\n",
":~/checks$ git status\n",
"\n",
"The Git status command is used to get some information about the current working tree and pending changes.\n",
"\n",
"## Git commit command\n",
"\n",
":~/checks$ git commit\n",
"\n",
"The .git commit command is run to remove changes made from the staging area to the .git directory. When this command is run, it tells Git to save changes. A text editor is opened that allows a commit message to be entered.\n",
"\n",
"## Guidelines for writing commit messages\n",
"\n",
"A commit message is generally broken into two sections: a short summary and a description of the changes. When the git commit command is run, Git will open a text editor to write your commit message. A good commit message includes the following:\n",
"\n",
"**Summary:** The first line contains the summary, formatted as a header, containing 50 characters or less. \n",
"\n",
"**Description:** The description is usually kept under 72 characters and provides detailed information about the change. It can include references to bugs or issues that will be fixed with the change. It also can include links to more information when relevant. \n",
"\n",
"Click the link to review an example of a commit message: [https://commit.style/<svg aria-labelledby=\"cds-react-aria6078214324-:rlf:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:rlf:\"><title id=\"cds-react-aria6078214324-:rlf:-title\">Opens in a new tab</title></svg>](https://commit.style/) \n",
"\n",
"## Key takeaways\n",
"\n",
"Knowing basic Git commands and guidelines for writing better messages can help you get started with Git as well as better communicate with others."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Glossary terms from module 1\n",
"\n",
"## **Terms and definitions from Course 3, Module 1**\n",
"\n",
"**Commit:** A command to make edits to multiple files and treat that collection of edits as a single change\n",
"\n",
"**Commit files:** A stage where the changes made to files are safely stored in a snapshot in the Git directory\n",
"\n",
"**Commit message:** A summary and description with contextual information on the parts of the code or configuration of the commit change\n",
"\n",
"**Diff:** A command to find the differences between two files\n",
"\n",
"**DNS zone file:** A configuration file that specifies the mappings between IP addresses and host names in your network\n",
"\n",
"**Git:** A free open source version control system available for installation on Unix based platforms, Windows and macOS\n",
"\n",
"**Git directory:** A database for a Git project that stores the changes and the change history\n",
"\n",
"**Git log:** A log that displays commit messages \n",
"\n",
"**Git staging area:** A file maintained by Git that contains all the information about what files and changes are going to go into the next commit\n",
"\n",
"**Modified files:** A stage where changes have been made to a file, but the have not been stored or committed \n",
"\n",
"**Patch:** A command that can detect that there were changes made to the file and will do its best to apply the changes\n",
"\n",
"**Repository:** An organization system of files that contain separate software projects \n",
"\n",
"**Source Control Management (SCM):** A tool similar to VCS to store source code\n",
"\n",
"**Stage files:** A stage where the changes to files are ready to be committed \n",
"\n",
"**Tracked:** A files changes are recorded\n",
"\n",
"**Untracked:** A files changes are not recorded\n",
"\n",
"**Version control systems (VCS):** A tool to safely test code before releasing it, allow multiple people collaborate on the same coding projects together, and stores the history of that code and configuration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exemplar: Introduction to Git\n",
"\n",
"## Introduction\n",
"\n",
"In the previous lab, you were a project lead in an IT company. You and your team were working on a huge project, which consisted of multiple functionalities and modules. This project is evolving over time and so your team is expecting a lot of code revisions. In this lab, you learned how to use a distributed version control system called Git. You'll also discovered how to connect to a VM instance, install Git, and configure your Git user information. Then, you created a local Git repository, added a file to the repository, and did some basic operations like adding a file, editing files, and making commits.\n",
"\n",
"This exemplar is a walkthrough of the previous Qwiklab activity, including detailed instructions and solutions. You may use this exemplar if you were unable to complete the lab and/or you need extra guidance in competing lab tasks. You may also refer to this exemplar to prepare for the graded quiz in this module.\n",
"\n",
"## Install Git\n",
"\n",
"Before you install **Git** on your Linux VM, you need to first make sure that you have a fresh index of the packages available to you. To do that, run:\n",
"\n",
"```bash\n",
"sudo apt update\n",
"```\n",
"\n",
"Now, you can install **Git** on your Linux host using **apt** by running the following command:\n",
"\n",
"```bash\n",
"sudo apt install git\n",
"```\n",
"\n",
"For any prompts, continue by clicking **Y**.\n",
"\n",
"Check the installed version of **git** by using the command below:\n",
"\n",
"```bash\n",
"git \\--version\n",
"```\n",
"\n",
"## **Initialize a new repository**\n",
"\n",
"Create a **directory** to store your project in. To do this, use the following command:\n",
"\n",
"```bash\n",
"mkdir my\\-git-repo\n",
"```\n",
"\n",
"Now navigate to the **directory** you created.\n",
"\n",
"```bash\n",
"cd my\\-git-repo\n",
"```\n",
"\n",
"Next, initialize a new repository by using the following command:\n",
"\n",
"```bash\n",
"git init\n",
"```\n",
"\n",
"The **git init** command creates a new Git repository. In our case, it transformed the current directory into a Git repository. It can also be used to convert an existing, unversioned project to a Git repository or to initialize a new, empty repository.\n",
"\n",
"Executing **git init** creates a **.git** subdirectory in the current working directory, which contains all of the necessary Git metadata for the new repository. This metadata includes subdirectories for objects, refs, and template files. A HEAD file is also created which points to the currently checked out commit.\n",
"\n",
"If you've already run **git init** on a project directory containing a **.git** subdirectory, you can safely run **git init** again on the same project directory. The operation is what we call *idempotent*; running it again doesn't override an existing **.git** configuration.\n",
"\n",
"## **Configure Git**\n",
"\n",
"Git uses a username to associate commits with an identity. It does this by using the **git config** command. To set **Git username** use the following command:\n",
"\n",
"```bash\n",
"git config \\--global user.name \"Name\"\n",
"```\n",
"\n",
"Replace **Name** with your name. Any future commits you push to GitHub from the command line will now be represented by this name. You can use **git config** to even change the name associated with your Git commits. This will only affect future commits and won't change the name used for past commits.\n",
"\n",
"Let's set your **email address** to associate it with your **Git commits**.\n",
"\n",
"```bash\n",
"git config \\--global user.email \"user@example.com\"\n",
"```\n",
"\n",
"Replace **user@example.com** with your **email-id**. Any future commits you now push to GitHub will be associated with this email address. You can even use **git config** to change the **user email** associated with your **Git commits**.\n",
"\n",
"## **Git Operations**\n",
"\n",
"Let's now create a text file named **README**. We will be using the nano editor for this.\n",
"\n",
"```bash\n",
"nano README\n",
"```\n",
"\n",
"Type any text within the file, or you can use the following text:\n",
"\n",
"```bash\n",
"This is my first repository.\n",
"```\n",
"\n",
"Save the file by pressing **Ctrl-o**, **Enter** key, and **Ctrl-x**.\n",
"\n",
"Git is now aware of the files in the project. We can check the **status** using the following command:\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"This command displays the **status** of the working tree. It also shows changes that have been staged, changes that haven't been staged, and files that aren't tracked by Git.\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"On branch master\n",
"\n",
"No commits yet\n",
"\n",
"Untracked files:\n",
"\n",
"  (use \"git add <file>...\" to include in what will be committed)\n",
"\n",
"    README\n",
"\n",
"nothing added to commit but untracked files present (use \"git add\" to track)\n",
"```\n",
"\n",
"You can now see the file you created, **README**, under the section **Untracked files**. Git isn't tracking the files yet. To track the files, we have to commit these files by adding them to the staging area.\n",
"\n",
"Now let's **add** the file to the staging area using the following command:\n",
"\n",
"```bash\n",
"git add README\n",
"```\n",
"\n",
"This command adds changes from the working tree to the staging area i.e., it gathers and prepares files for Git before committing them. In other words, it updates the index with the current content found in the working tree to prepare the content that's staged for the next commit.\n",
"\n",
"You can now view the status of the working tree using the command: **git status**. This now shows the file **README** in **green** i.e., the file is now in the staging area and yet to be committed.\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"On branch master\n",
"\n",
"No commits yet\n",
"\n",
"Changes to be committed:\n",
"\n",
"  (use \"git rm --cached <file>...\" to unstage)\n",
"\n",
"    new file:   README\n",
"```\n",
"\n",
"However, **git add** doesn't affect the repository in any serious way because changes are not actually recorded until you commit them.\n",
"\n",
"Let's now commit the changes. A **Git commit** is equivalent to the term **Save**.\n",
"\n",
"Commit the changes using the following command:\n",
"\n",
"```bash\n",
"git commit\n",
"```\n",
"\n",
"This now opens an editor, asking you to type a commit message. Every commit has an associated commit message. A commit message is a log message from the user describing the changes.\n",
"\n",
"Enter the **commit** message of your choice or you can use the following text:\n",
"\n",
"```bash\n",
"This is my first commit!\n",
"```\n",
"\n",
"Once you have entered the commit message, save it by pressing **Ctrl-o** and **Enter** key. To exit click **Ctrl-x**.\n",
"\n",
"The **git commit** command captures a snapshot of the project's currently staged changes i.e., it stores the current contents of the index in a new commit along with the commit message.\n",
"\n",
"You have successfully committed your file!\n",
"\n",
"Let's now re-edit the file again to understand the process better. Open the file **README** using nano editor.\n",
"\n",
"```bash\n",
"nano README\n",
"```\n",
"\n",
"Now add another line of description for your repository below the earlier entered line. Add the description of your choice or you can use the following text:\n",
"\n",
"```bash\n",
"A repository is a location where all the files of a particular project are stored.\n",
"```\n",
"\n",
"Save and exit the editor by pressing **Ctrl-o**, **Enter** key, and **Ctrl-x**.\n",
"\n",
"Now, let's repeat the previous process. As mentioned earlier, you can always check the status of your repository by using:\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"To understand the difference, compare with the earlier scenario where you added the new file to the repository.\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"On branch master\n",
"\n",
"Changes not staged for commit:\n",
"\n",
"  (use \"git add <file>...\" to update what will be committed)\n",
"\n",
"  (use \"git restore <file>...\" to discard changes in working directory)\n",
"\n",
"    modified:   README\n",
"\n",
"no changes added to commit (use \"git add\" and/or \"git commit -a\")\n",
"```\n",
"\n",
"Git tracks the changes and displays that the file has been modified. You can view the changes made to file using the following command:\n",
"\n",
"```bash\n",
"git diff README\n",
"```\n",
"\n",
"You can see the differences between the older file and the new file. New additions are denoted by **green-colored** text and a **+** sign at the start of the line. Any replacements/removal are denoted by text in **red-colored** text and a **\\-** sign at the start of the line.\n",
"\n",
"Now, we will add these changes to the staging area.\n",
"\n",
"```bash\n",
"git add README\n",
"```\n",
"\n",
"View the **status** of the repository using the following command:\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"Git now shows the same file in **green-colored** text. This means the changes are staged and ready to be committed.\n",
"\n",
"Let's commit the file now by entering the commit message with the command itself, unlike the previous commit.\n",
"\n",
"```bash\n",
"git commit \\-m \"This is my second commit.\"\n",
"```\n",
"\n",
"The command **git commit** with **\\-m** flag takes the commit message, too. This is different to the command without flag, where you had to type the commit message within the editor. If multiple **\\-m** flags are given to the command, it concatenates the values as separate paragraphs.\n",
"\n",
"To view all the commits use the following command:\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Git log** command shows the commit history of the repository. It shows all the commits on the repository represented by a unique **commit ID** at the top of each commit. It also shows the author, date, and time and the commit message associated with the commits.\n",
"\n",
"You also have various options to limit the output of this command. The output can be filtered based on the last number of commits, author, commit message, etc.\n",
"\n",
"## **Congratulations!**\n",
"\n",
"Congrats! You've successfully installed the Git, initialized a repository, and performed basic Git operations. Now that you know how to do this, it will be easier for you and your team to work on a huge project with multiple functionalities and modules."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}