Introduction_to_Git_and_GitHub/module2.ipynb
2025-03-07 14:21:00 +03:00

2287 lines
83 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": {},
"source": [
"# Skipping the Staging Area\n",
"\n",
"This reading contains the code used in the instructional videos from [**Skipping the Staging Area**<svg aria-labelledby=\"cds-react-aria6078214324-:r1pi:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r1pi:\"><title id=\"cds-react-aria6078214324-:r1pi:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/uAFPm/skipping-the-staging-area).\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 scripts\n",
"\n",
"atom all\\_checks.py\n",
"```\n",
"\n",
"## File with code\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import os\n",
"\n",
"import sys\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",
"    if check\\_reboot():\n",
"\n",
"        print(\"Pending Reboot.\")\n",
"\n",
"        sys.exit(1)\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git commit \\-a \\-m \"Call check\\_reboot from main, exit with 1 on error\"\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 033f27a\\] Call check\\_reboot from main, exit with 1 on error\n",
"\n",
" 1 file changed, 4 insertions(+), 1 deletion(-)\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 033f27a8196987d61c4fd42930f2148b23434a03 (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 14:39:18 2019 +0200\n",
"\n",
"    Call check\\_reboot from main, exit with 1 on error\n",
"\n",
"commit cc1acbf10fdea6cc07ebf827697666b6a35b0f36\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": [
"# Getting more information from the user\n",
"\n",
"This reading contains the code used in the instructional videos from [**Getting More Information About Our Changes**<svg aria-labelledby=\"cds-react-aria6078214324-:r224:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r224:\"><title id=\"cds-react-aria6078214324-:r224:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/KLQkB/getting-more-information-about-our-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",
"```bash\n",
"git log \\-p\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 033f27a8196987d61c4fd42930f2148b23434a03 (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 14:39:18 2019 +0200\n",
"\n",
"    Call check\\_reboot from main, exit with 1 on error\n",
"\n",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index 340f1f7..710266a 100644\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -1,12 +1,15 @@\n",
"\n",
" #!/usr/bin/env python3\n",
"\n",
" import os\n",
"\n",
"+import sys\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",
"(...)\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 033f27a8196987d61c4fd42930f2148b23434a03 (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 14:39:18 2019 +0200\n",
"\n",
"    Call check\\_reboot from main, exit with 1 on error\n",
"\n",
"commit **cc1acbf10fdea6cc07ebf827697666b6a35b0f36**\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",
"(...)\n",
"\n",
"user@ubuntu:~/scripts$ git show cc1acbf10fdea6cc07ebf827697666b6a35b0f36\n",
"\n",
"commit cc1acbf10fdea6cc07ebf827697666b6a35b0f36\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",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index c0d03b3..340f1f7 100644\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -1,5 +1,11 @@\n",
"\n",
" #!/usr/bin/env python3\n",
"\n",
"+import os\n",
"\n",
"+\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",
"+\n",
"\n",
" def main():\n",
"\n",
"     Pass\n",
"\n",
"```bash\n",
"git log \\--stat\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 033f27a8196987d61c4fd42930f2148b23434a03 (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 14:39:18 2019 +0200\n",
"\n",
"    Call check\\_reboot from main, exit with 1 on error\n",
"\n",
" all\\_checks.py | 5 ++++-\n",
"\n",
" 1 file changed, 4 insertions(+), 1 deletion(-)\n",
"\n",
"(...)\n",
"\n",
"```bash\n",
"atom  all\\_checks.py\n",
"```\n",
"\n",
"## File in video\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import os\n",
"\n",
"import sys\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",
"    if check\\_reboot():\n",
"\n",
"        print(\"Pending Reboot.\")\n",
"\n",
"        sys.exit(1)\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"    sys.exit(0)\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git diff\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index 710266a..fdc4476 100644\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -12,4 +12,7 @@ def main():\n",
"\n",
"         print(\"Pending Reboot.\")\n",
"\n",
"         sys.exit(1)\n",
"\n",
"+    print(\"Everything ok.\")\n",
"\n",
"+    sys.exit(0)\n",
"\n",
"+\n",
"\n",
" main()\n",
"\n",
"```bash\n",
"git  add \\-p\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index 710266a..fdc4476 100644\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -12,4 +12,7 @@ def main():\n",
"\n",
"         print(\"Pending Reboot.\")\n",
"\n",
"         sys.exit(1)\n",
"\n",
"+    print(\"Everything ok.\")\n",
"\n",
"+    sys.exit(0)\n",
"\n",
"+\n",
"\n",
" main()\n",
"\n",
"Stage this hunk \\[y,n,q,a,d,e,?\\]? y\n",
"\n",
"user@ubuntu:~/scripts$ \n",
"\n",
"```bash\n",
"git diff\n",
"\n",
"git diff \\--staged\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index 710266a..fdc4476 100644\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -12,4 +12,7 @@ def main():\n",
"\n",
"         print(\"Pending Reboot.\")\n",
"\n",
"         sys.exit(1)\n",
"\n",
"+    print(\"Everything ok.\")\n",
"\n",
"+    sys.exit(0)\n",
"\n",
"+\n",
"\n",
" main()\n",
"\n",
"```bash\n",
"git commit \\-m 'Add a message when everything is ok'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 49d610b\\] Add a message when everything is ok\n",
"\n",
" 1 file changed, 3 insertions(+)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Deleting and Renaming Files\n",
"\n",
"This reading contains the code used in the instructional videos from [**Deleting and Renaming FIles**<svg aria-labelledby=\"cds-react-aria6078214324-:r2br:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2br:\"><title id=\"cds-react-aria6078214324-:r2br:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/3OT51/deleting-and-renaming-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",
"**Code output:**\n",
"\n",
"total 8\n",
"\n",
"\\-rw-rw-r-- 1 user user 659 Jul  9 19:28 disk\\_usage.py\n",
"\n",
"\\-rw-rw-r-- 1 user user 659 Jul 15 21:43 processes.py\n",
"\n",
"```bash\n",
"git rm process.py\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"rm '[processes.py<svg aria-labelledby=\"cds-react-aria6078214324-:r2c7:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2c7:\"><title id=\"cds-react-aria6078214324-:r2c7:-title\">Opens in a new tab</title></svg>](http://processes.py/)'\n",
"\n",
"```bash\n",
"ls \\-l \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 4\n",
"\n",
"\\-rw-rw-r-- 1 user user 659 Jul  9 19:28 disk\\_usage.py\n",
"\n",
"```bash\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",
"        deleted:    [processes.py<svg aria-labelledby=\"cds-react-aria6078214324-:r2cj:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2cj:\"><title id=\"cds-react-aria6078214324-:r2cj:-title\">Opens in a new tab</title></svg>](http://processes.py/)\n",
"\n",
"```bash\n",
"git commit \\-m 'Delete unneeded processes file'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 9939311\\] Delete unneeded processes file\n",
"\n",
" 1 file changed, 24 deletions(-)\n",
"\n",
" delete mode 100644 processes.py\n",
"\n",
"```bash\n",
"git mv disk\\_usage.py check\\_free\\_space.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",
"        renamed:    disk\\_usage.py -> check\\_free\\_space.py\n",
"\n",
"```bash\n",
"git commit \\-m 'New name for disk\\_usage.py'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 7d7167b\\] New name for disk\\_usage.py\n",
"\n",
" 1 file changed, 0 insertions(+), 0 deletions(-)\n",
"\n",
"```bash\n",
"echo .DS\\_STORE > gitignore\n",
"\n",
"ls \\-la\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 20\n",
"\n",
"drwxrwxr-x  3 user user 4096 Jul 15 22:15 .\n",
"\n",
"drwxr-xr-x 19 user user 4096 Jul 15 16:37 ..\n",
"\n",
"\\-rw-rw-r--  1 user user  659 Jul  9 19:28 check\\_free\\_space.py\n",
"\n",
"drwxrwxr-x  8 user user 4096 Jul 15 21:52 .git\n",
"\n",
"\\-rw-rw-r--  1 user user   10 Jul 15 22:15 .gitignore\n",
"\n",
"```bash\n",
"git add .gitignore \n",
"\n",
"git commit \\-m 'Add a gitignore file, ignoring .DS\\_STORE files'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master abb0632\\] Add a gitignore file, ignoring .DS\\_STORE files\n",
"\n",
" 1 file changed, 1 insertion(+)\n",
"\n",
" create mode 100644 .gitignore"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Study Guide: Advanced Git\n",
"\n",
"| Command | Explanation & Link |\n",
"| --- | --- |\n",
"| git commit -a | [$ git commit -a<svg aria-labelledby=\"cds-react-aria6078214324-:r2lc:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lc:\"><title id=\"cds-react-aria6078214324-:r2lc:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---all) automatically stages the files that have been locally modified. New files which have not been published yet are not affected. |\n",
"| git log -p | [$ git log -p<svg aria-labelledby=\"cds-react-aria6078214324-:r2le:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2le:\"><title id=\"cds-react-aria6078214324-:r2le:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-log#generate_patch_text_with_p) produces patch text that displays the lines of code that were changed in each commit in the current repo. |\n",
"| git show | [$ git show<svg aria-labelledby=\"cds-react-aria6078214324-:r2lg:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lg:\"><title id=\"cds-react-aria6078214324-:r2lg:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-show) shows you one or more object(s) such as blobs, trees, tags, and commits. |\n",
"| git diff | [$ git diff<svg aria-labelledby=\"cds-react-aria6078214324-:r2li:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2li:\"><title id=\"cds-react-aria6078214324-:r2li:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-diff) is similar to the Linux \\`diff\\` command, and can show the changes between commits, changes between the working tree and index, changes between two trees, changes from a merge, and so on. |\n",
"| git diff --staged | [$ git diff --staged<svg aria-labelledby=\"cds-react-aria6078214324-:r2lk:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lk:\"><title id=\"cds-react-aria6078214324-:r2lk:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-diff) is an alias of $ git diff --cached, which  shows all staged files compared to the named commit. |\n",
"| git add -p | [$ git add -p<svg aria-labelledby=\"cds-react-aria6078214324-:r2lm:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lm:\"><title id=\"cds-react-aria6078214324-:r2lm:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-add) allows a user to interactively review patches before adding to the current commit. |\n",
"| git mv | [$ git mv<svg aria-labelledby=\"cds-react-aria6078214324-:r2lo:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lo:\"><title id=\"cds-react-aria6078214324-:r2lo:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-mv) is similar to the Linux \\`mv\\` command. This command can move or rename a file, directory, or symlink. |\n",
"| git rm | [$ git rm <svg aria-labelledby=\"cds-react-aria6078214324-:r2lq:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lq:\"><title id=\"cds-react-aria6078214324-:r2lq:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-rm)is similar to the Linux \\`rm\\` command. This command deletes or removes a file from the working tree. |\n",
"\n",
"There are many useful git command summaries online as well. Please take some time to research and study a few, such as [this one<svg aria-labelledby=\"cds-react-aria6078214324-:r2ls:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2ls:\"><title id=\"cds-react-aria6078214324-:r2ls:-title\">Opens in a new tab</title></svg>](https://education.github.com/git-cheat-sheet-education.pdf).\n",
"\n",
"## .gitignore files\n",
"\n",
".gitignore files are used to tell the git tool to intentionally ignore some files in a given Git repository. For example, this can be useful for configuration files or metadata files that a user may not want to check into the master branch. \n",
"\n",
"When writing a .gitignore file, there are some specific formats which help tell Git how to read the text in the file. For example, a line starting with # is a comment; a slash / is a directory separator. Visit [https://git-scm.com/docs/gitignore<svg aria-labelledby=\"cds-react-aria6078214324-:r2lu:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2lu:\"><title id=\"cds-react-aria6078214324-:r2lu:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/gitignore) to see more examples.\n",
"\n",
"[This GitHub repository<svg aria-labelledby=\"cds-react-aria6078214324-:r2m0:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria6078214324-:r2m0:\"><title id=\"cds-react-aria6078214324-:r2m0:-title\">Opens in a new tab</title></svg>](https://gist.github.com/octocat/9257657) offers some examples of configurations which are often included in a .gitignore file. These examples include: compiled sources, packages, logs, databases, and OS generated files."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Undoing Changes Before Committing\n",
"\n",
"This reading contains the code used in the instructional videos from [**Undoing changes before committing**<svg aria-labelledby=\"cds-react-aria2659598955-:r97:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r97:\"><title id=\"cds-react-aria2659598955-:r97:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/LPJn7/undoing-changes-before-committing)\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 scripts\n",
"\n",
"atom all\\_checks.py\n",
"```\n",
"\n",
"## File in video\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import os\n",
"\n",
"import sys\n",
"\n",
"def main():\n",
"\n",
"    if check\\_reboot():\n",
"\n",
"        print(\"Pending Reboot.\")\n",
"\n",
"        sys.exit(1)\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"    sys.exit(0)\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"./all\\_checks.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Traceback (most recent call last):\n",
"\n",
"  File \"all\\_checks.py\", line 14, in <module>\n",
"\n",
"    main()\n",
"\n",
"  File \"all\\_checks.py\", line 7, in main\n",
"\n",
"    if check\\_reboot():\n",
"\n",
"NameError: name 'check\\_reboot' is not defined\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\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",
"```bash\n",
"git checkout all\\_checks.py\n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"nothing to commit, working tree clean\n",
"\n",
"user@ubuntu:~/scripts$ ./all\\_checks.py \n",
"\n",
"Everything ok.\n",
"\n",
"```bash\n",
"./all\\_checks.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok.\n",
"\n",
"```bash\n",
"./all\\_checks.py > output.txt\n",
"\n",
"git add \\*\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",
"        new file:   output.txt\n",
"\n",
"```bash\n",
"git reset HEAD output.txt\n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch master\n",
"\n",
"Untracked files:\n",
"\n",
"  (use \"git add <file>...\" to include in what will be committed)\n",
"\n",
"        Output.txt\n",
"\n",
"```bash\n",
"git commit \\-m \"it should be os.path.exists\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Amending commits\n",
"\n",
"This reading contains the code used in the instructional videos from [**Amending commits**<svg aria-labelledby=\"cds-react-aria2659598955-:rkc:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:rkc:\"><title id=\"cds-react-aria2659598955-:rkc:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/nT1S2/amending-commits)\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 scripts/\n",
"\n",
"touch auto\\-update.py\n",
"\n",
"touch gather\\-information.sh\n",
"\n",
"ls \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 8\n",
"\n",
"\\-rwxrwxr-x 1 user user 319 Jul 16 17:56 all\\_checks.py\n",
"\n",
"\\-rw-rw-r-- 1 user user   0 Jul 16 20:19 auto-update.py\n",
"\n",
"\\-rw-rw-r-- 1 user user   0 Jul 16 20:19 gather-information.sh\n",
"\n",
"\\-rw-rw-r-- 1 user user  15 Jul 16 18:03 output.txt\n",
"\n",
"user@ubuntu:~/scripts$ git add auto-update.py \n",
"\n",
"user@ubuntu:~/scripts$ git commit -m 'Add two new scripts'\n",
"\n",
"\\[master 9c78761\\] Add two new scripts\n",
"\n",
" 1 file changed, 0 insertions(+), 0 deletions(-)\n",
"\n",
" create mode 100644 [auto-update.py<svg aria-labelledby=\"cds-react-aria2659598955-:rkj:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:rkj:\"><title id=\"cds-react-aria2659598955-:rkj:-title\">Opens in a new tab</title></svg>](http://auto-update.py/)\n",
"\n",
"```bash\n",
"git add auto\\-update.py\n",
"\n",
"git commit \\-m 'Add two new scripts'\n",
"```\n",
"\n",
"**Code output:**\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",
"\\# Date:      Tue Jul 16 20:20:24 2019 +0200\n",
"\n",
"\\# On branch master\n",
"\n",
"\\# Changes to be committed:\n",
"\n",
"\\#       new file:   auto-update.py\n",
"\n",
"\\#       new file:   gather-information.sh\n",
"\n",
"\\# Untracked files:\n",
"\n",
"\\#       output.txt\n",
"\n",
"```bash\n",
"git add gather\\-information.sh\n",
"\n",
"git commit \\--amend\n",
"```\n",
"\n",
"## File in video\n",
"\n",
"```bash\n",
"Add two new scripts.\n",
"\n",
"# Please enter the commit message for your changes. Line starting\n",
"\n",
"#with '#' will be ignored, and an empty message aborts the commit.\n",
"\n",
"#\n",
"\n",
"#Date: Mon Jan 6 08:28:17 2020 -0800\n",
"\n",
"#\n",
"\n",
"# On branch master\n",
"\n",
"# Changes to be committed:\n",
"\n",
"#   new file: auto-update.py\n",
"\n",
"#   new file: gather-information.sh\n",
"\n",
"#\n",
"\n",
"# Untracked files:\n",
"\n",
"#   output.txt\n",
"```\n",
"\n",
"## File in video\n",
"\n",
"```bash\n",
"Add two new scripts.\n",
"\n",
"gather\\-information.sh will collect information in case of errors.\n",
"\n",
"auto\\-update.py will run daily to update computers automatically.\n",
"\n",
"# Please enter the commit message for your changes. Line starting\n",
"\n",
"#with '#' will be ignored, and an empty message aborts the commit.\n",
"\n",
"#\n",
"\n",
"#Date: Mon Jan 6 08:28:17 2020 -0800\n",
"\n",
"#\n",
"\n",
"# On branch master\n",
"\n",
"# Changes to be committed:\n",
"\n",
"#   new file: auto-update.py\n",
"\n",
"#   new file: gather-information.sh\n",
"\n",
"#\n",
"\n",
"# Untracked files:\n",
"\n",
"#   output.txt\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rollbacks\n",
"\n",
"This reading contains the code used in the instructional videos from [**Rollbacks**<svg aria-labelledby=\"cds-react-aria2659598955-:rt9:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:rt9:\"><title id=\"cds-react-aria2659598955-:rt9:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/21kjM/rollbacks)\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 scripts\n",
"\n",
"atom all\\_checks.py\n",
"```\n",
"\n",
"## File in video\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import os\n",
"\n",
"import sys\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",
"    if check\\_reboot():\n",
"\n",
"        print(\"Pending Reboot.\")\n",
"\n",
"        sys.exit(1)\n",
"\n",
"    if disk\\_full():\n",
"\n",
"        print(\"Disk Full.\")\n",
"\n",
"        sys.exit(1)\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"    sys.exit(0)\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git commit \\-a \\-m 'Add call to disk\\_full function'\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master ec61497\\] Add call to disk\\_full function\n",
"\n",
" 1 file changed, 4 insertions(+)\n",
"\n",
"```bash\n",
"./all\\_checks.py \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Traceback (most recent call last):\n",
"\n",
"  File \"./all\\_checks.py\", line 22, in <module>\n",
"\n",
"    main()\n",
"\n",
"  File \"./all\\_checks.py\", line 15, in main\n",
"\n",
"    if disk\\_full():\n",
"\n",
"NameError: name 'disk\\_full' is not defined\n",
"\n",
"```bash\n",
"git revert HEAD\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Revert \"Add call to disk\\_full function\"\n",
"\n",
"Reason for rollback: The disk\\_full function is undefined.\n",
"\n",
"This reverts commit ec614976e1665b40134d2c01921f9b0fbf89d1e2.\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",
"\\#       modified:   all\\_checks.py\n",
"\n",
"\\# Untracked files:\n",
"\n",
"\\#       output.txt\n",
"\n",
"```bash\n",
"git revert HEAD\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 91c4968\\] Revert \"Add call to disk\\_full function\"\n",
"\n",
" 1 file changed, 4 deletions(-)\n",
"\n",
"```bash\n",
"git log \\-p \\-2\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 91c4968ebd80de900d71b9bc3f332f53149ac57d (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Tue Jul 16 21:43:18 2019 +0200\n",
"\n",
"    Revert \"Add call to disk\\_full function\"\n",
"\n",
"    Reason for rollback: The disk\\_full function is undefined.\n",
"\n",
"    This reverts commit ec614976e1665b40134d2c01921f9b0fbf89d1e2.\n",
"\n",
"diff --git a/all\\_checks.py b/all\\_checks.py\n",
"\n",
"index 21da366..fdc4476 100755\n",
"\n",
"\\--- a/all\\_checks.py\n",
"\n",
"+++ b/all\\_checks.py\n",
"\n",
"@@ -12,10 +12,6 @@ def main():\n",
"\n",
"         print(\"Pending Reboot.\")\n",
"\n",
"         sys.exit(1)\n",
"\n",
"\\-    if disk\\_full():\n",
"\n",
"\\-        print(\"Disk Full.\")\n",
"\n",
"\\-        sys.exit(1)\n",
"\n",
"\\-\n",
"\n",
"     print(\"Everything ok.\")\n",
"\n",
"     sys.exit(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Identifying a Commit\n",
"\n",
"This reading contains the code used in the instructional videos from [**Identifying a commit**<svg aria-labelledby=\"cds-react-aria2659598955-:r16k:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r16k:\"><title id=\"cds-react-aria2659598955-:r16k:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/WqddF/identifying-a-commit)**.** \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",
"git log \\-1\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit abb063210c1f011b0d6470a4c5f1d8f672edd3ef (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 22:20:45 2019 +0200\n",
"\n",
"    Add a gitignore file, ignoring .DS\\_STORE files\n",
"\n",
"```bash\n",
"git log \\-2\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit abb063210c1f011b0d6470a4c5f1d8f672edd3ef (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 22:20:45 2019 +0200\n",
"\n",
"    Add a gitignore file, ignoring .DS\\_STORE files\n",
"\n",
"commit 7d7167b2db44abf8cf014230f9b9708786e41c2a\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 21:52:59 2019 +0200\n",
"\n",
"    New name for disk\\_usage.py\n",
"\n",
"```bash\n",
"git show 30e70712882267ca2dd749acfa02ea3aacfd0b24\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 21:52:59 2019 +0200\n",
"\n",
"    New name for disk\\_usage.py\n",
"\n",
"diff --git a/disk\\_usage.py b/check\\_free\\_space.py\n",
"\n",
"similarity index 100%\n",
"\n",
"rename from disk\\_usage.py\n",
"\n",
"rename to check\\_free\\_space.py\n",
"\n",
"```bash\n",
"git show 30\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"fatal: ambiguous argument '7d': unknown revision or path not in the working tree.\n",
"\n",
"Use '--' to separate paths from revisions, like this:\n",
"\n",
"'git <command> \\[<revision>...\\] -- \\[<file>...\\]'\n",
"\n",
"```bash\n",
"git show 30e7\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 7d7167b2db44abf8cf014230f9b9708786e41c2a\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 21:52:59 2019 +0200\n",
"\n",
"    New name for disk\\_usage.py\n",
"\n",
"diff --git a/disk\\_usage.py b/check\\_free\\_space.py\n",
"\n",
"similarity index 100%\n",
"\n",
"rename from disk\\_usage.py\n",
"\n",
"rename to check\\_free\\_space.py\n",
"\n",
"```bash\n",
"git revert 30e7\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Revert \"New name for disk\\_usage.py\"\n",
"\n",
"Rollback reason: the previous name was actually better.\n",
"\n",
"This reverts commit 7d7167b2db44abf8cf014230f9b9708786e41c2a.\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",
"\\#       renamed:    check\\_free\\_space.py -> disk\\_usage.py\n",
"\n",
"```bash\n",
"git revert 7d71\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\[master 80b2dac\\] Revert \"New name for disk\\_usage.py\"\n",
"\n",
" 1 file changed, 0 insertions(+), 0 deletions(-)\n",
"\n",
" rename check\\_free\\_space.py => disk\\_usage.py (100%)\n",
"\n",
"```bash\n",
"git show 7d1de19\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"commit 80b2dacef4b567196e61651064f03089c5e70b5e (HEAD -> master)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Wed Jul 17 00:02:39 2019 +0200\n",
"\n",
"    Revert \"New name for disk\\_usage.py\"\n",
"\n",
"    Rollback reason: the previous name was actually better.\n",
"\n",
"    This reverts commit 7d7167b2db44abf8cf014230f9b9708786e41c2a.\n",
"\n",
"diff --git a/check\\_free\\_space.py b/disk\\_usage.py\n",
"\n",
"similarity index 100%\n",
"\n",
"rename from check\\_free\\_space.py\n",
"\n",
"rename to disk\\_usage.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Study guide: Git Revert\n",
"\n",
"When writing and committing code, making mistakes is a common occurrence. Thankfully, there are multiple ways for you to revert or undo your mistakes. Take a look at the helpful commands below.\n",
"\n",
"[git checkout<svg aria-labelledby=\"cds-react-aria2659598955-:r1gc:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1gc:\"><title id=\"cds-react-aria2659598955-:r1gc:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-checkout) is used to switch branches. For example, you might want to pull from your main branch. In this case, you would use the command git checkout main. This will switch to your main branch, allowing you to pull. Then you could switch to another branch by using the command  git checkout <branch>.\n",
"\n",
"[git reset<svg aria-labelledby=\"cds-react-aria2659598955-:r1ge:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1ge:\"><title id=\"cds-react-aria2659598955-:r1ge:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-reset#_examples)  can be somewhat difficult to understand. Say you have just used the command git add. to stage all of your changes, but then you decide that you are not ready to stage those files. You could use the command git reset to undo the staging of your files.\n",
"\n",
"There are some other useful articles online, which discuss more aggressive approaches to [resetting the repo<svg aria-labelledby=\"cds-react-aria2659598955-:r1gg:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1gg:\"><title id=\"cds-react-aria2659598955-:r1gg:-title\">Opens in a new tab</title></svg>](https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html) (Git repository). As discussed in this article, doing a hard reset can be extremely dangerous. With a hard reset, you run the risk of losing your local changes. There are safer ways to achieve the same effect. For example, you could run git stash, which will temporarily shelve or stash your current changes. This way, your current changes are kept safe, and you can come back to them if needed.\n",
"\n",
"[git commit --amend<svg aria-labelledby=\"cds-react-aria2659598955-:r1gi:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1gi:\"><title id=\"cds-react-aria2659598955-:r1gi:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---amend) is used to make changes to your most recent commit after-the-fact, which can be useful for making notes about or adding files to your most recent commit. Be aware that this git --amend command rewrites and replaces your previous commit, so it is best not to use this command on a published commit.\n",
"\n",
"[git revert<svg aria-labelledby=\"cds-react-aria2659598955-:r1gk:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1gk:\"><title id=\"cds-react-aria2659598955-:r1gk:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-revert) makes a new commit which effectively rolls back a previous commit. Unlike the git reset command which rewrites your commit history, the git revert command creates a new commit which undoes the changes in a specific commit. Therefore, a revert command is generally safer than a reset command.\n",
"\n",
"For more information on these and other methods to undo something in Git, checkout this [Git Basics - Undoing Things<svg aria-labelledby=\"cds-react-aria2659598955-:r1gm:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1gm:\"><title id=\"cds-react-aria2659598955-:r1gm:-title\">Opens in a new tab</title></svg>](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things) article.\n",
"\n",
"Additionally, there are some interesting considerations about how git object data is stored, such as the usage of SHA-1.\n",
"\n",
"SHA-1 is whats known as a *hash function*, a cryptographic function that generates a digital fingerprint of a file. Theoretically, its impossible for two different files to have the same SHA-1 hash, which means that SHA-1 can be used for two things:\n",
"\n",
"- Confirming that the contents of a file have not changed (digital signature).\n",
"- Serving as an identifier for the file itself (a token or fingerprint).\n",
"\n",
"Git calculates a hash for every commit. Those hashes are displayed by commands like git log or in various pages on Github. For commands like git revert, you can then use the hash to refer to a specific commit.\n",
"\n",
"Feel free to read more here:\n",
"\n",
"- [SHA-1 collision detection on GitHub.com<svg aria-labelledby=\"cds-react-aria2659598955-:r1go:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2659598955-:r1go:\"><title id=\"cds-react-aria2659598955-:r1go:-title\">Opens in a new tab</title></svg>](https://github.blog/2017-03-20-sha-1-collision-detection-on-github-com/)\n",
"\n",
"Even the most accomplished developers make mistakes in Git. It happens to everyone, so dont stress about it. You have these and other methods to help you revert or undo your mistakes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating new branches\n",
"\n",
"This reading contains the code used in the instructional videos from [**Creating new branches**<svg aria-labelledby=\"cds-react-aria3604314262-:ret:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:ret:\"><title id=\"cds-react-aria3604314262-:ret:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/FAmM2/creating-new-branches)\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",
"git branch\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\* master\n",
"\n",
"```bash\n",
"git branch new\\-feature\n",
"\n",
"git branch\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\* master\n",
"\n",
"  New-feature\n",
"\n",
"```bash\n",
"git checkout new\\-feature\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Switched to branch 'new-feature'\n",
"\n",
"```bash\n",
"git checkout \\-b even\\-better-feature\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Switched to a new branch 'even-better-feature'\n",
"\n",
"```bash\n",
"atom free\\_memory.py \n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with branches\n",
"\n",
"This reading contains the code used in the instructional videos from [**Working with branches**<svg aria-labelledby=\"cds-react-aria3604314262-:ro2:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:ro2:\"><title id=\"cds-react-aria3604314262-:ro2:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/UHBEa/working-with-branches)\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",
"git status\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"On branch even-better-feature\n",
"\n",
"nothing to commit, working tree clean\n",
"\n",
"```bash\n",
"ls \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 8\n",
"\n",
"\\-rw-rw-r-- 1 user user 659 Jul 17 00:02 disk\\_usage.py\n",
"\n",
"\\-rw-rw-r-- 1 user user  53 Jul 17 15:33 free\\_memory.py\n",
"\n",
"```bash\n",
"git checkout master\n",
"\n",
"git log \\-2\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Switched to branch 'master'\n",
"\n",
"user@ubuntu:~/checks$ git log -2\n",
"\n",
"commit 80b2dacef4b567196e61651064f03089c5e70b5e (HEAD -> master, new-feature)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Wed Jul 17 00:02:39 2019 +0200\n",
"\n",
"    Revert \"New name for disk\\_usage.py\"\n",
"\n",
"    Rollback reason: the previous name was actually better.\n",
"\n",
"    This reverts commit 7d7167b2db44abf8cf014230f9b9708786e41c2a.\n",
"\n",
"commit abb063210c1f011b0d6470a4c5f1d8f672edd3ef\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jul 15 22:20:45 2019 +0200\n",
"\n",
"    Add a gitignore file, ignoring .DS\\_STORE files\n",
"\n",
"```bash\n",
"ls \\-l\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"total 4\n",
"\n",
"\\-rw-rw-r-- 1 user user 659 Jul 17 00:02 disk\\_usage.py\n",
"\n",
"```bash\n",
"git branch\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"  even-better-feature\n",
"\n",
"\\* master\n",
"\n",
"  new-feature\n",
"\n",
"```bash\n",
"git branch \\-d new\\-feature \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Deleted branch new-feature (was 80b2dac).\n",
"\n",
"```bash\n",
"git branch\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"  even-better-feature\n",
"\n",
"\\* master\n",
"\n",
"```bash\n",
"git branch \\-d even\\-better-feature \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"error: The branch 'even-better-feature' is not fully merged.\n",
"\n",
"If you are sure you want to delete it, run 'git branch -D even-better-feature'."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Merging\n",
"\n",
"This reading contains the code used in the instructional videos from [**Merging**<svg aria-labelledby=\"cds-react-aria3604314262-:r11o:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r11o:\"><title id=\"cds-react-aria3604314262-:r11o:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/InLJQ/merging)\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 branch\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"   Even-better-feature\n",
"\n",
"\\* master\n",
"\n",
"```bash\n",
"git merge even\\-better-feature \n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"Updating 7d1de19..4361880\n",
"\n",
"Fast-forward\n",
"\n",
"  free-memory.py | 6 ++++++\n",
"\n",
"  1 file changed, 6 insertions (+)\n",
"\n",
"  Create mode 100644 free\\_memory.py\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"Switched to branch 'master'\n",
"\n",
"commit 436188012f633b773eb6034fc02051e34da05134 (HEAD -> master, even-better-feature)\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jan 6 09:47:07 2020 -0800 \n",
"\n",
"     Add an empty free\\_memory.py\n",
"\n",
"commit 7d7167b2db44abf8cf014230f9b9708786e41c2a\n",
"\n",
"Author: My name <me@example.com>\n",
"\n",
"Date:   Mon Jan 6 09:15:58 2020 -0800 \n",
"\n",
"    Revert \"New name for disk\\_usage.py\"\n",
"\n",
"    Rollback reason: the previous name was actually better :)\n",
"\n",
"    This reverts commit 7d7167b2db44abf8cf014230f9b9708786e41c2a.\n",
"\n",
"(...)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Merge conflicts\n",
"\n",
"This reading contains the code used in the instructional videos from [**Merge Conflicts**<svg aria-labelledby=\"cds-react-aria3604314262-:r1af:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1af:\"><title id=\"cds-react-aria3604314262-:r1af:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/96VdS/merge-conflicts)\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",
"atom free\\_memory.py\n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"def main():\n",
"\n",
"    \"\"\"Checks if there's enough free memory in the computer.\"\"\"\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git commit \\-a \\-m 'Add comment to main()'\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"\\[master fe2fc5b\\] Add comment to main()\n",
"\n",
"  1 file changed, 2 insertions(+), 2 deletions(-)\n",
"\n",
"```bash\n",
"git checkout even\\-better-feature \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"Switched to branch 'even-better-feature'\n",
"\n",
"```bash\n",
"atom free\\_memory.py \n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"def main():\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git commit \\-a \\-m 'Print everything ok'\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"\\[even-better-feature 6a6de99\\] Print everything ok\n",
"\n",
"1 file changed, 2 insertions(+), 2 deletions(-)\n",
"\n",
"```bash\n",
"git checkout master\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"Switched to branch 'master'\n",
"\n",
"```bash\n",
"git merge even\\-better-feature \n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"Auto-merging free\\_memory.py\n",
"\n",
"CONFLICT (content): Merge conflict in free\\_memory.py\n",
"\n",
"Automatic merge failed; fix conflicts and then commit the result.\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"On branch master\n",
"\n",
"You have unmerged paths.\n",
"\n",
"  (fix conflicts and run \"git commit\")\n",
"\n",
"  (use \"git merge --abort\" to abort the merge)\n",
"\n",
"Unmerged paths:\n",
"\n",
"  (use \"git add <file>...\" to mark resolution)\n",
"\n",
"        both modified:   free\\_memory.py\n",
"\n",
"no changes added to commit (use \"git add\" and/or \"git commit -a\")\n",
"\n",
"```bash\n",
"atom free\\_memory.py \n",
"\n",
"#!/usr/bin/env python3\n",
"\n",
"def main():\n",
"\n",
"<<<<<<< HEAD\n",
"\n",
"    \"\"\"Checks if there's enough free memory in the computer.\"\"\"\n",
"\n",
"\\=======\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"\\>>>>>>> even\\-better-feature\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"def main():\n",
"\n",
"    \"\"\"Checks if there's enough free memory in the computer.\"\"\"\n",
"\n",
"    print(\"Everything ok.\")\n",
"\n",
"main()\n",
"```\n",
"\n",
"```bash\n",
"git add free\\_memory.py\n",
"\n",
"git status\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"On branch master\n",
"\n",
"All conflicts fixed but you are still merging.\n",
"\n",
"  (use \"git commit\" to conclude merge)\n",
"\n",
"Changes to be committed:\n",
"\n",
"        modified:   free\\_memory.py\n",
"\n",
"```bash\n",
"git commit\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"Merge branch 'even-better-feature'\n",
"\n",
"Kept lines from both branches\n",
"\n",
"\\# Conflicts:\n",
"\n",
"\\#       free\\_memory.py\n",
"\n",
"\\#\n",
"\n",
"\\# It looks like you may be committing a merge.\n",
"\n",
"\\# If this is not correct, please remove the file\n",
"\n",
"\\#       .git/MERGE\\_HEAD\n",
"\n",
"\\# and try again.\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",
"\\# All conflicts fixed but you are still merging.\n",
"\n",
"\\#\n",
"\n",
"\\# Changes to be committed:\n",
"\n",
"\\#       modified:   free\\_memory.py\n",
"\n",
"```bash\n",
"git log \\--graph \\--oneline\n",
"```\n",
"\n",
"**Code output:** \n",
"\n",
"\\*   8cb5e62 (HEAD -> master) Merge branch 'even-better-feature'\n",
"\n",
"|\\\\  \n",
"\n",
"| \\* ca6de99 (even-better-feature) Print everything ok\n",
"\n",
"\\* | fe2fc5b Add comment to main()\n",
"\n",
"|/  \n",
"\n",
"\\* 4361880 Add an empty free\\_memory.py\n",
"\n",
"\\* 7d1de19 Revert \"New name for disk\\_usage.py\"\n",
"\n",
"\\* bb9bd78 Add a gitignore file, ignoring .DS\\_STORE files\n",
"\n",
"\\* 30e7071 New name for disk\\_usage.py\n",
"\n",
"\\* 0d5a271 Delete unneeded processes file\n",
"\n",
"\\* 5aada26 Adding file to delete it later\n",
"\n",
"\\* cfb2b8e Add periods to the end of sentences.\n",
"\n",
"\\* 21e6a1a Add new disk\\_usage check."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Study guide: Git branches and merging\n",
"\n",
"Now that youve learned about branches and merging, use this study guide as an easy reference for Git branching. This study guide gives a brief explanation of these useful commands along with a link to the Git documentation for each command. Keeping study guides like this one easily accessible can help you code more efficiently.\n",
"\n",
"| Command | Explanation & link |\n",
"| --- | --- |\n",
"| git branch | [$ git branch<svg aria-labelledby=\"cds-react-aria3604314262-:r1ko:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1ko:\"><title id=\"cds-react-aria3604314262-:r1ko:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-branch) can be used to list, create, or delete branches. |\n",
"| git branch <name> | [$ git branch <name><svg aria-labelledby=\"cds-react-aria3604314262-:r1kq:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1kq:\"><title id=\"cds-react-aria3604314262-:r1kq:-title\">Opens in a new tab</title></svg>](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging) can be used to create a new branch in your repository. |\n",
"| git branch -d <name> | [$ git branch -d <name><svg aria-labelledby=\"cds-react-aria3604314262-:r1ks:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1ks:\"><title id=\"cds-react-aria3604314262-:r1ks:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt--D) can be used to delete a branch from your repository. |\n",
"| git branch -D <name> | [$ git branch -D <branch><svg aria-labelledby=\"cds-react-aria3604314262-:r1ku:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1ku:\"><title id=\"cds-react-aria3604314262-:r1ku:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt--D) forces a branch to be deleted. |\n",
"| git checkout <branch> | [$ git checkout <branch><svg aria-labelledby=\"cds-react-aria3604314262-:r1l0:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1l0:\"><title id=\"cds-react-aria3604314262-:r1l0:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-checkout) switches your current working branch. |\n",
"| git checkout -b <new-branch> | [$ git checkout -b <new-branch><svg aria-labelledby=\"cds-react-aria3604314262-:r1l2:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1l2:\"><title id=\"cds-react-aria3604314262-:r1l2:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt--bltnewbranchgt) creates a new branch and makes it your current working branch. |\n",
"| git merge <branch> | [$ git merge <branch><svg aria-labelledby=\"cds-react-aria3604314262-:r1l4:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1l4:\"><title id=\"cds-react-aria3604314262-:r1l4:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-merge) joins changes from one branch into another branch. |\n",
"| git merge --abort | [$ git merge --abort<svg aria-labelledby=\"cds-react-aria3604314262-:r1l6:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1l6:\"><title id=\"cds-react-aria3604314262-:r1l6:-title\">Opens in a new tab</title></svg>](https://git-scm.com/docs/git-merge) can only be used after merge conflicts. This command will abort the merge and try to go back to the pre-merge state. |\n",
"| git log --graph | [$ git log --graph <svg aria-labelledby=\"cds-react-aria3604314262-:r1l8:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1l8:\"><title id=\"cds-react-aria3604314262-:r1l8:-title\">Opens in a new tab</title></svg>](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)prints an ASCII graph of the commit and merge history. |\n",
"| git log --oneline | [$ git log --oneline<svg aria-labelledby=\"cds-react-aria3604314262-:r1la:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria3604314262-:r1la:\"><title id=\"cds-react-aria3604314262-:r1la:-title\">Opens in a new tab</title></svg>](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History) prints each commit on a single line. |\n",
"\n",
"Keep this table handy while you are getting comfortable using Git branches and merging. Now, its time to put your newfound knowledge of Git branches and merging to use!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Glossary terms from module 2\n",
"\n",
"## **Terms and definitions from Module 2**\n",
"\n",
"**Branch:** A pointer to a particular commit, representing an independent line of development in a project\n",
"\n",
"**Commit ID:** An identifier next to the word commit in the log\n",
"\n",
"**Fast-forward merge:** A merge when all the commits in the checked out branch are also in the branch that's being merged\n",
"\n",
"**Head:** This points to the top of the branch that is being used\n",
"\n",
"**Master:** The default branch that Git creates for when a new repository initialized, commonly used to place the approved pieces of a project\n",
"\n",
"**Merge conflict:** This occurs when the changes are made on the same part of the same file, and Git won't know how to merge those changes\n",
"\n",
"**Rollback:** The act of reverting changes made to software to a previous state \n",
"\n",
"**Three-way merge:** A merge when the snapshots at the two branch tips with the most recent common ancestor, the commit before the divergence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exemplar: Merging Branches in Git\n",
"\n",
"## Introduction\n",
"\n",
"In the previous lab, you used your knowledge of Git and Git commit history to check out an existing repo and make some changes to it. You also tested what you learned about rolling back commits after bad changes in order to fix a script in the repo and run it to produce the correct output.\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",
"## Explore repository\n",
"\n",
"There is a Git repository named food-scripts consisting of a couple of food-related Python scripts.\n",
"\n",
"Navigate to the repository using the following command:\n",
"\n",
"```bash\n",
"cd ~/food\\-scripts\n",
"```\n",
"\n",
"Now, list the files using the ls command. There are three files named **favorite\\_foods.log**, **food\\_count.py**, and **food\\_question.py**.\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"favorite\\_foods.log  food\\_count.py  food\\_question.py\n",
"```\n",
"\n",
"Let's explore each file. Use the cat command to view each file.\n",
"\n",
"1\\. **favorite\\_foods.log**: This file consists of a list of food items. You can view it using the following command:\n",
"\n",
"```bash\n",
"cat favorite\\_foods.log\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"pie\n",
"\n",
"burgers\n",
"\n",
"pizza\n",
"\n",
"pie\n",
"\n",
"tacos\n",
"\n",
"fried chicken\n",
"\n",
"spaghetti\n",
"\n",
"rice\n",
"\n",
"cake\n",
"\n",
"broccoli\n",
"\n",
"cake\n",
"\n",
"cereal\n",
"\n",
"salad\n",
"\n",
"avocados\n",
"\n",
"burgers\n",
"```\n",
"\n",
"2\\. **food\\_count.py**: This script returns a list of each food and the number of times the food appeared in the favorite\\_foods.log file.\n",
"\n",
"Let's execute the script food\\_count.py:\n",
"\n",
"```bash\n",
"./food\\_count.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"rice, 12\n",
"\n",
"burgers, 10\n",
"\n",
"fried chicken, 9\n",
"\n",
"pie, 8\n",
"\n",
"pizza, 7\n",
"\n",
"salad, 7\n",
"\n",
"tacos, 6\n",
"\n",
"avocados, 6\n",
"\n",
"spaghetti, 5\n",
"\n",
"broccoli, 5\n",
"\n",
"ice cream, 5\n",
"\n",
"bananas, 5\n",
"\n",
"fish, 4\n",
"\n",
"cake, 3\n",
"\n",
"cereal, 3\n",
"\n",
"strawberries, 3\n",
"\n",
"watermelon, 2\n",
"```\n",
"\n",
"3\\. **food\\_question.py**: This prints a list of foods and prompts the user to enter one of those foods as their favorite. It then returns an answer of how many others in the list like that same food.\n",
"\n",
"Run the following command to see the output of food\\_question.py script:\n",
"\n",
"```bash\n",
"./food\\_question.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Traceback (most recent call last):\n",
"\n",
"  File \"./food\\_question.py\", line 10, in <module>\n",
"\n",
"    if item not in counter:\n",
"\n",
"NameError: name 'item' is not defined\n",
"```\n",
"\n",
"Uh-oh, this gives us an error. One of your colleagues reports that this script was working fine until the most recent commit. We'll be fixing this error later during the lab.\n",
"\n",
"## Understanding the repository\n",
"\n",
"Let's use the following Git operations to understand the workflow of the repository:\n",
"\n",
"- git status\n",
"- git log\n",
"- git branch\n",
"\n",
"**Git status:** This displays paths that have differences between the index file and the current HEAD commit; paths that have differences between the working tree and the index file; and paths in the working tree that are not tracked by Git. You can view the status of the working tree using the command: **\\[git status\\]**\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"You can now view the status of the working tree.\n",
"\n",
"**Git log:** This lists the commits done in the repository in reverse chronological order; that is, the most recent commits show up first. This command lists each commit with its SHA-1 checksum, the author's name and email, date, and the commit message.\n",
"\n",
"You can see logs by using the following command:\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"commit 21cf376832fa6eace35c0bf9e4bae4a3400452e9 (HEAD -> master)\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:09:39 2020 +0530\n",
"\n",
"    Rename item variable to food\\_item.\n",
"\n",
"commit b8d00e33237b24ea1480c363edd972cf4b49eedf\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:08:35 2020 +0530\n",
"\n",
"    Added file food\\_question.py that returns how many others in the list like that same food.\n",
"\n",
"commit 8d5a3189b88d273ef08286e5074b5e38d616da21\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:07:15 2020 +0530\n",
"\n",
"    Added file food\\_count.py that returns a list of each food and the number of times each food appears in favorite\\_foods.log file.\n",
"\n",
"commit 0a202e03e0356d2b5c323e51905d3d06e5e2d0ed\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:06:21 2020 +0530\n",
"\n",
"    Added file favorite\\_foods.log that contains list of foods.\n",
"```\n",
"\n",
"**Git branch:** Branches are a part of the everyday development process on the master branch. Git branches effectively function as a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, no matter how big or small, you spawn a new branch to encapsulate your changes. This makes it difficult for unstable code to get merged into the main codebase.\n",
"\n",
"### Configure Git\n",
"\n",
"Before we move forward with the lab, let's configure Git. Git uses a username to associate commits with an identity. It does this by using the **git config** command. Set the Git username with the following command:\n",
"\n",
"```bash\n",
"git config 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 even use **git config** to 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 them with your Git commits.\n",
"\n",
"```bash\n",
"git config 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 also use **git config** to change the user email associated with your Git commits.\n",
"\n",
"## Add a new feature\n",
"\n",
"In this section, we'll be modifying the repository to add a new feature, without affecting the current iteration. This new feature is designed to improve the food count (from the file **food\\_count.py**) output. So, create a branch named **improve-output** using the following command:\n",
"\n",
"```bash\n",
"git branch improve\\-output\n",
"```\n",
"\n",
"Move to the improve-output branch from the master branch.\n",
"\n",
"```bash\n",
"git checkout improve\\-output\n",
"```\n",
"\n",
"Here, you can modify the script file without disturbing the existing code. Once modified and tested, you can update the master branch with a working code.\n",
"\n",
"Now, open food\\_count.py in the nano editor using the following command:\n",
"\n",
"```bash\n",
"nano food\\_count.py\n",
"```\n",
"\n",
"Add the line below before **printing for loop** in the food\\_count.py script:\n",
"\n",
"```bash\n",
"print(\"Favourite foods, from most popular to least popular\")\n",
"```\n",
"\n",
"Save the file by pressing **Ctrl-o**, the **Enter** key, and **Ctrl-x**.\n",
"\n",
"Then run the script food\\_count.py again to see the output:\n",
"\n",
"```bash\n",
"./food\\_count.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Favourite foods, from most popular to least popular\n",
"\n",
"rice, 12\n",
"\n",
"burgers, 10\n",
"\n",
"fried chicken, 9\n",
"\n",
"pie, 8\n",
"\n",
"pizza, 7\n",
"\n",
"salad, 7\n",
"\n",
"tacos, 6\n",
"\n",
"avocados, 6\n",
"\n",
"spaghetti, 5\n",
"\n",
"broccoli, 5\n",
"\n",
"ice cream, 5\n",
"\n",
"bananas, 5\n",
"\n",
"fish, 4\n",
"\n",
"cake, 3\n",
"\n",
"cereal, 3\n",
"\n",
"strawberries, 3\n",
"\n",
"watermelon, 2\n",
"```\n",
"\n",
"After running the food\\_count.py script successfully, commit the changes from the improve-output branch by adding this script to the staging area using the following command:\n",
"\n",
"```bash\n",
"git add food\\_count.py\n",
"```\n",
"\n",
"Now, commit the changes you've done in the improve-output branch.\n",
"\n",
"```bash\n",
"git commit \\-m \"Adding a line in the output describing the utility of food\\_count.py script\"\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"\\[improve\\-output 09b9ce4\\] Adding a line in the output describing the utility of food\\_count.py script\n",
"\n",
" 1 file changed, 1 insertion(+), 1 deletion(-)\n",
"```\n",
"\n",
"## Fix the script\n",
"\n",
"In this section, we'll fix the script food\\_question.py, which displayed an error when executing it. You can run the file again to view the error.\n",
"\n",
"```bash\n",
"./food\\_question.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Traceback (most recent call last):\n",
"\n",
"  File \"./food\\_question.py\", line 10, in <module>\n",
"\n",
"    if item not in counter:\n",
"\n",
"NameError: name 'item' is not defined\n",
"```\n",
"\n",
"This script gives us the error: **\"NameError: name 'item' is not defined\"** but your colleague says that the file was running fine before the most recent commit they did.\n",
"\n",
"In this case, we'll revert back the previous commit.\n",
"\n",
"For this, check the git log history so that you can revert back to the commit where it was working fine.\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"commit 09b9ce441654a361477405d3eea785b3be5f8e8f (HEAD -> improve\\-output)\n",
"\n",
"Author: Name <user@example.com>\n",
"\n",
"Date:   Thu Oct 12 10:48:08 2023 +0000\n",
"\n",
"    Adding a line in the output describing the utility of food\\_count.py script\n",
"\n",
"commit 21cf376832fa6eace35c0bf9e4bae4a3400452e9 (master)\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:09:39 2020 +0530\n",
"\n",
"    Rename item variable to food\\_item.\n",
"\n",
"commit b8d00e33237b24ea1480c363edd972cf4b49eedf\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:08:35 2020 +0530\n",
"\n",
"    Added file food\\_question.py that returns how many others in the list like that same food.\n",
"```\n",
"\n",
"Here, you'll see the commits in reverse chronological order and find the commit having **\"Rename item variable to food\\_item\"** as a commit message. Make sure to note the commit ID for this particular commit.\n",
"\n",
"To revert, use the following command:\n",
"\n",
"```bash\n",
"git revert \\[commit\\-ID\\]\n",
"```\n",
"\n",
"Replace \\[commit-ID\\] with the commit ID you noted earlier.\n",
"\n",
"This creates a new commit again. You can continue with the default commit message on the screen or add your own commit message.\n",
"\n",
"Then continue by clicking **Ctrl-o**, the **Enter** key, and **Ctrl-x**.\n",
"\n",
"Now, run food\\_question.py again and verify that it's working as intended.\n",
"\n",
"```bash\n",
"./food\\_question.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Select your favorite food below:\n",
"\n",
"pie\n",
"\n",
"burgers\n",
"\n",
"pizza\n",
"\n",
"tacos\n",
"\n",
"fried chicken\n",
"\n",
"spaghetti\n",
"\n",
"rice\n",
"\n",
"cake\n",
"\n",
"broccoli\n",
"\n",
"cereal\n",
"\n",
"salad\n",
"\n",
"avocados\n",
"\n",
"ice cream\n",
"\n",
"fish\n",
"\n",
"strawberries\n",
"\n",
"bananas\n",
"\n",
"watermelon\n",
"\n",
"Which of the foods above is your favorite? rice\n",
"\n",
"12 of your friends like rice as well!\n",
"```\n",
"\n",
"## **Merge operation**\n",
"\n",
"Before merging the branch improve-output, switch to the master branch from the current branch improve-output branch using the command below:\n",
"\n",
"```bash\n",
"git checkout master\n",
"```\n",
"\n",
"Merge the branch improve-output into the master branch.\n",
"\n",
"```bash\n",
"git merge improve\\-output\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Updating 21cf376..70bd534\n",
"\n",
"Fast\\-forward\n",
"\n",
" food\\_count.py    | 2 +-\n",
"\n",
" food\\_question.py | 2 +-\n",
"\n",
" 2 files changed, 2 insertions(+), 2 deletions(-)\n",
"```\n",
"\n",
"Now, all your changes made in the improve-output branch are on the master branch.\n",
"\n",
"```bash\n",
"./food\\_question.py\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"Select your favorite food below:\n",
"\n",
"pie\n",
"\n",
"burgers\n",
"\n",
"pizza\n",
"\n",
"tacos\n",
"\n",
"fried chicken\n",
"\n",
"spaghetti\n",
"\n",
"rice\n",
"\n",
"cake\n",
"\n",
"broccoli\n",
"\n",
"cereal\n",
"\n",
"salad\n",
"\n",
"avocados\n",
"\n",
"ice cream\n",
"\n",
"fish\n",
"\n",
"strawberries\n",
"\n",
"bananas\n",
"\n",
"watermelon\n",
"\n",
"Which of the foods above is your favorite? burgers\n",
"\n",
"10 of your friends like burgers as well!\n",
"```\n",
"\n",
"To get the status from the master branch, use the command below:\n",
"\n",
"```bash\n",
"git status\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"On branch master\n",
"\n",
"nothing to commit, working tree clean\n",
"```\n",
"\n",
"To track the git commit logs, use the following command:\n",
"\n",
"```bash\n",
"git log\n",
"```\n",
"\n",
"**Output:**\n",
"\n",
"```bash\n",
"commit 57c54e10c808118898bb0f15625bda2c0f55fec3 (HEAD -> master, improve\\-output)\n",
"\n",
"Author: Name <user@example.com>\n",
"\n",
"Date:   Tue Oct 31 08:57:01 2023 +0000\n",
"\n",
"    Revert \"Rename item variable to food\\_item.\"\n",
"\n",
"    This reverts commit 21cf376832fa6eace35c0bf9e4bae4a3400452e9.\n",
"\n",
"commit 80b47ced952bffe110e2f9f00769cd43ed047ac1\n",
"\n",
"Author: Name <user@example.com>\n",
"\n",
"Date:   Tue Oct 31 08:55:24 2023 +0000\n",
"\n",
"    Adding a line in the output describing the utility of food\\_count.py script\n",
"\n",
"commit 21cf376832fa6eace35c0bf9e4bae4a3400452e9\n",
"\n",
"Author: Alex Cooper <alex\\_cooper@gmail.com>\n",
"\n",
"Date:   Wed Jan 8 14:09:39 2020 +0530\n",
"\n",
"    Rename item variable to food\\_item.\n",
"```\n",
"\n",
"## **Congratulations!**\n",
"\n",
"In this lab, you successfully created a branch from the master branch to add a new feature. You also rolled back a commit to where the script worked fine, and then merged it to the master branch. This will help as you work with colleagues who are simultaneously on the same repository."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}