===========================================================================
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
To see the
Enter the command to show the
You could rewrite the line how it was originally, but what if you forgot the exact wording? The command
3.1.Change the ghost's words in some way. Here's a fun suggestion:
===========================================================================
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 addscene-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?
$ git checkout HEAD scene-5.txt
4.more git addscene-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.
===========================================================================
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:
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
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.2.From the terminal, add scene-2.txt to the Git staging area.
$ git add scene-2.txt
5.3.Now check the
In the output, notice scene-2.txt under “Changes to be committed”.
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:
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
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
Next, print the Git commit log again.
Notice anything interesting? The commits that came after the one you reset to are gone. The
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
Before reset:
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
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.
$ git add filename_1 filename_2
No comments :
Post a Comment