Friday, April 8, 2016

Learn Git_Project_Poems Fiasco

road-not-taken.txt
==========================================================================
I Took All the Roads, by Robert Forks

Two roads went exactly the same way
And I was happy to travel them both
And be one traveler, I figured it out quick
blah blah blah
blah blah blah blah

blah blah blah blah
Both had terrible claims
Because it was smooth and boring
blah blah blah blah
This poem is terrible, Rob.

blah blah blah blah
In leaves no step had trodden blah
Oh, blah blah blah BLAAAAHHH
WEEEEEEEE!! This road is awesome.
I'm definitely coming back here!

Hahahahahahahhaha
Two roads diverged in a hahahahaha
HAHAHAHAHAHAHHAHAHAHAH! Gotcha.
==========================================================================
1.The file road-not-taken.txt doesn’t look right at all! Perhaps a rival poet snuck in and changed it while you were getting coffee. Good thing you’ve been committing often.
Checkout the HEAD version of road-not-taken.txt to discard changes made to the working directory.
$ git checkout HEAD road-not-taken.txt  
2.Now, finish the poem by adding a line. Here's a suggestion:
==========================================================================
And that has made all the difference.
==========================================================================
Then  Save.
3.Take a look at oven-bird.txt to see if it has also been tampered with.
Indeed it has! We’ll want to discard changes in the working directory again.
There’s a commonly used shortcut for this command:
$ git checkout -- filename
It does the same exact thing that git checkout HEAD filename does. Try it with oven-bird.txt.
$ git checkout -- oven-bird.txt
4.Now, finish "Oven Bird" by adding a line. Here's a suggestion:
==========================================================================
Is what to make of a diminished thing.
==========================================================================
Then  Save.
5.Click on fire-and-ice.txt.
This file has not been altered, but just to be sure, check the diff for this file.
Then, add these last two lines to the poem:
==========================================================================
Is also okay And would suffice.
==========================================================================
$ git diff  
6.Now that you’ve restored and completed road-not-taken.txt and oven-bird.txt and added a line to fire-and-ice.txt, add all three of the files to the staging area with a single command.
$ git add road-not-taken.txt oven-bird.txt fire-and-ice.txt
7.fire-and-ice.txt could be better. Before you commit, remove this file from the staging area. 
$ git reset HEAD fire-and-ice.txt 
8.Now that you've removed fire-and-ice.txt, make a commit.
$ git commit -m "Ying Yu Commit"
9.You get the crazy idea to change your poems in a big way.
Make some drastic changes to each of the three poems. Remember to click Save after each file change.
10.Now add all three files to the staging area.
$ git add road-not-taken.txt oven-bird.txt fire-and-ice.txt 
11.Make a commit
$ git commit -m "Becky 2nd Commit" 
12.A little later you take a look at the current state of your poems and regret your last commit.
Reset your Git project to the commit before you made those drastic changes.
$ git log
$ git reset d7722af 
13.There’s a problem: you reset HEAD to a previous commit, but the changes you want to get rid of are still in the working directory.
What Git backtracking command that you already know can discard changes to the working directory, restoring the files to the way they look in the HEAD commit?
$ git checkout HEAD road-not-taken.txt oven-bird.txt fire-and-ice.txt

Thursday, April 7, 2016

Learn Git_Lesson_How to Backtrack in Git

scene-5.txt
===========================================================================
Hamlet: Act 1, Scene 5

Enter Ghost and Hamlet

Hamlet:
Where wilt thou lead me? speak; I'll go no further

Ghost:
Mark me.

Hamlet:
I will.
=========================================================================== 
When working on a Git project, sometimes we make changes that we want to get rid of. Git offers a few eraser-like features that allow us to undo mistakes during project creation.
1.Back-tracing intro

1.1.You are in a Git project titled hamlet-prince-of-denmark. In the code editor, you'll be working on scene-5.txt. Here, Hamlet encounters the ghost of his father. Add this text to the file:
=========================================================================== 

Ghost: My hour is almost come, When I to sulphurous and tormenting flames Must render up myself
===========================================================================

1.2.From the terminal, add scene-5.txt to the staging area.
  $ git add scene-5.txt

1.3.Commit the changes to the repository with a good commit message.

$ git commit -m "First Commit"     
[master 600568f] First Commit      
 1 file changed, 5 insertions(+)
 2.head commit
In Git, the commit you are currently on is known as the HEAD commit. In many cases, the most recently made commit is the HEAD commit.
To see the HEAD commit, enter:  

$ git show HEAD
The output of this command will display everything the git log command displays for the HEAD commit, plus all the file changes that were committed.
Enter the command to show the HEAD commit. Notice the output. The ghost's most recently added line is in green text.

$ git show HEAD                                                                                  
commit 600568fb9f8f610aadb580a5789a1a1c0aa704b1                                                  
Author: codecademy <ccuser@codecademy.com>                                                       
Date:   Thu Apr 7 23:16:48 2016 -0400                                                                                                                                                            
    First Commit                                                                                                                                                                                 
diff --git a/scene-5.txt b/scene-5.txt                                                           
index b12dd97..71863dd 100644                                                                    
--- a/scene-5.txt                                                                                
+++ b/scene-5.txt                                                                                
@@ -11,4 +11,9 @@ Mark me.                                                                       
 Hamlet:                                                                                         
 I will.                                                                                                                                                                                        
+Ghost:                                                                                          
+My hour is almost come,                                                                         
+When I to sulphurous and tormenting flames                                                      
+Must render up myself.                                                                          
+                   3.git checkoutWhat if you decide to change the ghost's line in the working directory, but then decide you wanted to discard that change?
You could rewrite the line how it was originally, but what if you forgot the exact wording? The command
$ git checkout HEAD filename
will restore the file in your working directory to look exactly as it did when you last made a commit.

3.1.Change the ghost's words in some way. Here's a fun suggestion:
===========================================================================  


Ghost: My hour is almost come, When I to sulphurous and tormenting balloons Must render up myself.
 
===========================================================================  
3.2.From the terminal, use git diff to see the difference between scene-5.txt as it appears in the working directory vs. how it appears in your last commit.
You may need to press q on your keyboard to restore the terminal.
$ git diff                                     
diff --git a/scene-5.txt b/scene-5.txt         
index 71863dd..0ab1e92 100644                  
--- a/scene-5.txt                              
+++ b/scene-5.txt                              
@@ -13,7 +13,7 @@ I will.                                                                
 Ghost:                                        
 My hour is almost come,                       
-When I to sulphurous and tormenting flames    
+When I to sulphurous and tormenting balloons  
 Must render up myself.
3.3.Use the new Git command to restore the file in your working directory to look as it did when you last made a commit.
Notice that the changes you made to the ghost's line have been discarded.
$ git checkout HEAD scene-5.txt 
4.more git add
scene-3.txt
===========================================================================
Hamlet: Act 1, Scene 3

A room in Polonius' house.

Enter LARRY and OPHELIA

LARRY
My necessaries are embark'd: farewell:
And, sister, as the winds give benefit
And convoy is assistant, do not sleep,
But let me hear from you.

OPHELIA
Do you doubt that?

LARRY
For Hamlet and the trifling of his favour,
Hold it a fashion and a toy in blood,
A violet in the youth of primy nature,
Forward, not permanent, sweet, not lasting,
The perfume and suppliance of a minute; No more.

OPHELIA
No more but so?
=========================================================================== scene-7.txt
===========================================================================
Hamlet: Act 4, Scene 7

Another room in the castle.

Enter KING CLAUDIUS and LARRY

KING CLAUDIUS
Now must your conscience my acquaintance seal,
And you must put me in your heart for friend,
Sith you have heard, and with a knowing ear,
That he which hath your noble father slain
Pursued my life.

LARRY
It well appears: but tell me
Why you proceeded not against these feats,
So crimeful and so capital in nature,
As by your safety, wisdom, all things else,
You mainly were stirr'd up.

KING CLAUDIUS
O, for two special reasons;
Which may to you, perhaps, seem much unsinew'd,
But yet to me they are strong. The queen his mother
Lives almost by his looks; and for myself--
My virtue or my plague, be it either which--
She's so conjunctive to my life and soul,
That, as the star moves not but in his sphere,
I could not but by her. The other motive,
Why to a public count I might not go,
Is the great love the general gender bear him;
Who, dipping all his faults in their affection,
Would, like the spring that turneth wood to stone,
Convert his gyves to graces; so that my arrows,
Too slightly timber'd for so loud a wind,
Would have reverted to my bow again,
And not where I had aim'd them.

LARRY
And so have I a noble father lost;
A sister driven into desperate terms,
Whose worth, if praises may go back again,
Stood challenger on mount of all the age
For her perfections: but my revenge will come.
===========================================================================
The hamlet we are working on contains five files. In Git, it's common to change many files, add those files to the staging area, and commit them to Git in a single commit.
For example, say you want to change the character "LARRY" to "LAERTES" in the script. The name currently appears in two files. After you change the name in both files, you could add the changed files to the staging area with:
$ git add filename_1 filename_2
Note the word filename above refers to the name of the file you are adding to the staging area, such as scene-3.txt.
4.1.The code editor is open to scene-3.txt and scene-7.txt. In scene-3.txt, everywhere you see the name "LARRY" change it to "LAERTES."
4.2.Now change all instances of "LARRY" to "LAERTES" in scene-7.txt. 
4.3.Add the files to the staging area together using a single git command.
$ git add scene-3.txt scene-7.txt   
5.git reset I
scene-2.txt
===========================================================================
Hamlet: Act 1, Scene 2

A room of state in the castle.

Enter KING CLAUDIUS, QUEEN GERTRUDE, HAMLET, POLONIUS, LAERTES, VOLTIMAND,
CORNELIUS, Lords, and Attendants

KING CLAUDIUS
Though yet of Hamlet our dear brother's death
The memory be green, and that it us befitted
To bear our hearts in grief and our whole kingdom
To be contracted in one brow of woe,
Yet so far hath discretion fought with nature
That we with wisest sorrow think on him,
Together with remembrance of ourselves.
Therefore our sometime sister, now our queen,
The imperial jointress to this warlike state,
Have we, as 'twere with a defeated joy,--
With an auspicious and a dropping eye,
With mirth in funeral and with dirge in marriage,
In equal scale weighing delight and dole,--
Taken to wife: nor have we herein barr'd
Your better wisdoms, which have freely gone
With this affair along. For all, our thanks.
Now follows, that you know, young Fortinbras,
Holding a weak supposal of our worth,
Or thinking by our late dear brother's death
Our state to be disjoint and out of frame,
Colleagued with the dream of his advantage,
He hath not fail'd to pester us with message,
Importing the surrender of those lands
Lost by his father, with all bonds of law,
To our most valiant brother. So much for him.
Now for ourself and for this time of meeting:
Thus much the business is: we have here writ
To Norway, uncle of young Fortinbras,--
Who, impotent and bed-rid, scarcely hears
Of this his nephew's purpose,--to suppress
His further gait herein; in that the levies,
The lists and full proportions, are all made
Out of his subject: and we here dispatch
You, good Cornelius, and you, Voltimand,
For bearers of this greeting to old Norway;
Giving to you no further personal power
To business with the king, more than the scope
Of these delated articles allow.
===========================================================================
What if, before you commit, you accidentally delete an important line from scene-2.txt? Unthinkingly, you add scene-2.txt to the staging area. The file change is unrelated to the Larry/Laertes swap and you don't want to include it in the commit.
We can unstage that file from the staging area using
git reset HEAD filename
This command resets the file in the staging area to be the same as the HEAD commit. It does not discard file changes from the working directory, it just removes them from the staging area. 
5.1.To try out the new command, let's make a mistake on purpose!
The code editor is open to scene-2.txt. Delete any line from the file.
5.2.From the terminal, add scene-2.txt to the Git staging area. 
$ git add scene-2.txt  

5.3.Now check the status of the Git project.
In the output, notice scene-2.txt under “Changes to be committed”.
$ git status                                   
On branch master                               
Changes to be committed:                       
  (use "git reset HEAD <file>..." to unstage)                            
        modified:   scene-2.txt                
        modified:   scene-3.txt                
        modified:   scene-7.txt  5.4.Use the new Git command to unstage scene-2.txt from the staging area.
Notice in the output, "Unstaged changes after reset":

M scene-2.txt
  • M is short for "modification"
$ git reset HEAD scene-2.txt                   
Unstaged changes after reset:                  
M       scene-2.txt    
5.5.Now that changes made to scene-2.txt have been booted out of the staging area, you're ready to commit. From the terminal, make a commit to save the Larry/Laertes name swap in hamlet. 
$ git commit -m "Second Commit"                
[master deb7c2b] Second Commit                 
 2 files changed, 6 insertions(+), 6 deletions(-) 
6.git reset II
Creating a project is like hiking in a dark forest. Sometimes you take a wrong turn, then another wrong turn. Before you know it, you're surrounded by bears.
Git enables you to rewind to the part before you made the wrong turn and create a new destiny for the project. You can do this with:
$ git reset SHA
This command works by using the first 7 characters of the SHA of a previous commit. For example, if the SHA of the previous commit is 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:
$ git reset 5d69206
6.1.From the terminal, print out your Git commit log.
Note: If your cursor gets stuck in "git log" mode in the terminal, press "q" on your keyboard to escape.
$ git log                                                                                        
commit deb7c2bb1f7722f422f196466c51a24774de2038                                                  
Author: codecademy <ccuser@codecademy.com>                                                       
Date:   Fri Apr 8 02:16:52 2016 -0400                                                                                                                                                            
    Second Commit                                                                                                                                                                                
commit d640dc3b22b3ee1d0a6e87477a35fd5e75c0712e                                                  
Author: codecademy <ccuser@codecademy.com>                                                       
Date:   Thu Nov 5 13:19:17 2015 -0500                                                                                                                                                            
    Complete ghost line of dialogue                                                                                                                                                             
commit 7be7ec672af73cf31ef72c92e3374fd4e29c675a                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:15:05 2015 -0500                                                                                                                                                          
    Add first page of scene-7.txt                                                                                                                                                               
commit 83f7b3591f4ab7aedb3160388b59e65ee1cd94a2                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:14:48 2015 -0500                                                                                                                                                            
    Add first page to scene-5.txt                                                                                                                                                                
commit 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:14:30 2015 -0500   
Add first page to scene-3.txt
6.2.From the terminal, enter the command to reset to a previous commit, using the first 7 characters of one of the past commit SHAs in your Git log.
Next, print the Git commit log again.
Notice anything interesting? The commits that came after the one you reset to are gone. The HEAD commit has been reassigned. You just changed history.
$ git reset 7be7ec6
$ git log
commit 7be7ec672af73cf31ef72c92e3374fd4e29c675a                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:15:05 2015 -0500                                                                                                                                                          
    Add first page of scene-7.txt                                                                                                                                                               
commit 83f7b3591f4ab7aedb3160388b59e65ee1cd94a2                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:14:48 2015 -0500                                                                                                                                                            
    Add first page to scene-5.txt                                                                                                                                                                
commit 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba                                                  
Author: danasselin <johndoe@example.com>                                                         
Date:   Tue Nov 3 17:14:30 2015 -0500   Add first page to scene-3.txt 
7.git reset review
To better understand git reset commit_SHA, notice the diagram on the right. Each circle represents a commit.
Before reset:
  • HEAD is at the most recent commit
After resetting:
  • HEAD goes to a previously made commit of your choice
  • The gray commits are no longer part of your project
  • You have in essence rewinded the project's history

8.generalizations
  • git checkout HEAD filename: Discards changes in the working directory.
  • git reset HEAD filename: Unstages file changes in the staging area.
  • git reset SHA: Can be used to reset to a previous commit in your commit history.
Additionally, you learned a way to add multiple files to the staging area with a single command:
$ git add filename_1 filename_2

Learn Git_Project_Snap-Fit Robots Inc.

disclaimer.txt
==========================================================================
SNAPFIT ROBOTS PRODUCT DISCLAIMER

Your Snapfit Robot Model i075 is provided with guarantee of limited 1-year warranty only. Outside of maintenance and malfunction descriptions covered in the warranty, customer shall make no claims of any kind, either express or implied, including but not limited to warranties of sellability, fitness for specific usage, of title, or of noninfringement of third and fourth party rights, and/or rights to attend robot parties. Use of the product by uninformed user is at the users risk.
==========================================================================
1.Initialize a new Git project. 
$ git init                                     
Initialized empty Git repository in /home/ccuser/workspace/snapfit-robots/.git/    
2.Check the status of the Git project.
You will see multiple files listed in the output as “Untracked”.       
$ git status                                   
On branch master                                                                             
Initial commit                                                                              
Untracked files:                               
  (use "git add <file>..." to include in what will be committed)                                                                          
        disclaimer.txt                         
        instructions.txt                       
        warranty.txt                                                                         
nothing added to commit but untracked files present (use "git add" to track) 
3.Add each file to the Git staging area.             
$ git add disclaimer.txt                       
$ git add instructions.txt                     
$ git add warranty.txt   
4.Check the status of the Git project again.                  
$ git status                                   
On branch master                                                                            
Initial commit                                                                              
Changes to be committed:                       
  (use "git rm --cached <file>..." to unstage)                                             
        new file:   disclaimer.txt             
        new file:   instructions.txt           
        new file:   warranty.txt    
5.Make a commit.                                                       
$ git commit -m "First Commit"   
[master (root-commit) 44104f4] First Commit    
 3 files changed, 70 insertions(+)             
 create mode 100644 disclaimer.txt             
 create mode 100644 instructions.txt           
 create mode 100644 warranty.txt      
6.View your Git commit log.If your cursor is stuck in Git log mode, press "q" on your keyboard to escape.
$ git log                                      
commit 44104f45807f12aec6bf742b9ce33f8ab0e3723c
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:35:47 2016 -0400 
             First Commit     
7.Include this line in disclaimer.txt:
=========================================================================
Warning: For best battery life, do not leave robot battery charging overnight
=========================================================================
8.Add the file to the staging area. 
$ git add disclaimer.txt  
9.Now make a commit. 
$ git commit -m "Second Commit"                
[master b42ec23] Second Commit                 
 1 file changed, 2 insertions(+), 2 deletions(-)                                              
10.View your Git commit log again to identify your commit. 
$ git log                                      
commit b42ec23be3890be52b086e0a1237f1636127dcf5
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:42:26 2016 -0400                                                       
    Second Commit                                                                          
commit 44104f45807f12aec6bf742b9ce33f8ab0e3723c
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:35:47 2016 -0400                                                        
    First Commit      
11.Revise each file in whatever ways you'd like. Then add your changes to the staging area and make another commit. 
$ git add disclaimer.txt 
$ git commit -m "Third Commit"                 
[master a503e44] Third Commit                  
 1 file changed, 2 insertions(+)               
$ git log                                      
commit a503e44a442628c7555cf09371cdaaefff196648
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:55:19 2016 -0400                                                      
    Third Commit                                                                            
commit b42ec23be3890be52b086e0a1237f1636127dcf5
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:42:26 2016 -0400                                                       
    Second Commit                                                                            
commit 44104f45807f12aec6bf742b9ce33f8ab0e3723c
Author: codecademy <ccuser@codecademy.com>     
Date:   Thu Apr 7 22:35:47 2016 -0400                                                        
    First Commit