Difference between revisions of "Git/C2/The-git-checkout-command/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 24: Line 24:
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn how to
 
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn how to
 
  
 
* Add multiple files to '''Git repository'''
 
* Add multiple files to '''Git repository'''
Line 31: Line 30:
 
* Discard the changes made to a file and
 
* Discard the changes made to a file and
 
* Revert to an earlier revision
 
* Revert to an earlier revision
 
 
  
 
|-
 
|-
Line 42: Line 39:
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| For this tutorial, I am using
 
| style="border:1pt solid #000000;padding:0.097cm;"| For this tutorial, I am using
 
  
 
* '''Ubuntu Linux 14.04'''
 
* '''Ubuntu Linux 14.04'''
Line 58: Line 54:
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| To follow this tutorial
 
| style="border:1pt solid #000000;padding:0.097cm;"| To follow this tutorial
 
  
 
* You must have knowledge of running '''Linux''' commands on '''Terminal'''.
 
* You must have knowledge of running '''Linux''' commands on '''Terminal'''.
Line 197: Line 192:
  
 
* We didn't add the modified files to staging area before committing them and
 
* We didn't add the modified files to staging area before committing them and
* The editor also didn't open up for commit message as we saw in earlier tutorial.
+
* The editor also didn't open up for committing message as we saw in earlier tutorial.
 
+
 
+
  
 
|-
 
|-
Line 215: Line 208:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| * '''-a flag '''is used to add all the modified files to the''' staging area.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
 +
* '''-a flag '''is used to add all the modified files to the''' staging area.'''
 
* When we use '''-a''' flag, we don’t need '''git add''' command separately to add the modified files to the '''staging area. '''
 
* When we use '''-a''' flag, we don’t need '''git add''' command separately to add the modified files to the '''staging area. '''
 
* '''-m''' flag is used to give '''commit '''message in the '''command line '''itself
 
* '''-m''' flag is used to give '''commit '''message in the '''command line '''itself
 
* We can use the flags '''-a''' and '''-m''' as '''-am'''.
 
* We can use the flags '''-a''' and '''-m''' as '''-am'''.
 
 
  
 
|-
 
|-
Line 237: Line 229:
  
  
Note that the latest '''commits''' are listed first. Which means the commits are listed in chronological order.
+
Note that the latest '''commits''' are listed first.  
 +
 
 +
Which means the commits are listed in chronological order.
  
  
Line 244: Line 238:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| In case you have added a wrong file to the''' Git repository''',  
+
| style="border:1pt solid #000000;padding:0.097cm;"| In case you have added a wrong file to the''' Git repository''', it can be easily removed.
 
+
it can be easily removed.
+
  
 
|-
 
|-
Line 298: Line 290:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| At this point, let’s freeze our work.
+
| style="border:1pt solid #000000;padding:0.097cm;"| At this point, let’s freeze our code.
  
 
|-
 
|-
Line 318: Line 310:
  
 
Press '''q''' key  
 
Press '''q''' key  
| style="border:1pt solid #000000;padding:0.097cm;"| Here we can find the latest commit by reading the commit message.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Press '''q''' key on your keyboard to exit.
  
  
Press '''q''' key on your keyboard to exit.
+
Here we can find the latest commit by reading the commit message.
  
  
Line 328: Line 320:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| Now suppose that we have deleted '''mypage.html''' by mistake and now we want to restore it back.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now suppose that we have deleted '''mypage.html''' by mistake.
 +
 
 +
And now we want to restore it back.
  
  
Line 338: Line 332:
  
  
Let’s restore our file from the second '''commit, '''which has the '''commit '''message''' “Added two files”.'''
+
Let’s restore our file from the second '''commit, '''which has the '''commit '''message  
 +
 
 +
'''“Added two files”.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Copy the first five digits of the commit hash
 
| style="border:1pt solid #000000;padding:0.097cm;"| Copy the first five digits of the commit hash
| style="border:1pt solid #000000;padding:0.097cm;"| Select the first five digits of the second '''commit hash''' and press''' Ctrl + Shift + C '''keys to copy them.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Select the first five digits of the second '''commit hash'''.
 +
 
 +
 
 +
And press''' Ctrl + Shift + C '''keys to copy them.
  
  
Line 352: Line 351:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git checkout''' hash''' mypage.html''' and press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git checkout''' hash''' mypage.html''' and press''' Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git space checkout space''' and press '''Ctrl + Shift + V''' keys to paste the '''commit hash.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git space checkout space'''.
 +
 
 +
And press '''Ctrl + Shift + V''' keys to paste the '''commit hash.'''
  
  
Line 370: Line 371:
  
  
Note that it is very important to '''commit''' our work whenever we add or remove any file.
+
Note that it is very important to '''commit''' our work whenever we add or delete any file.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git commit -am “Restored mypage.html”''' and press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git commit -am “Restored mypage.html”''' and press''' Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git space commit space hyphen am space “Restored mypage.html”''' and press''' Enter'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git space commit space hyphen am space “Restored mypage.html”'''  
 +
 
 +
and press''' Enter'''.
  
 
|-
 
|-
Line 388: Line 391:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| Next we will see how to discard the changes made to a file.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Next, we will see how to discard the changes made to a file.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' gedit mypage.html mystory.html &''' and press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' gedit mypage.html mystory.html &''' and press''' Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Open the files by typing''' gedit space mypage.html space mystory.html space ampersand''' and press''' Enter'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Open the files by typing''' gedit space mypage.html space mystory.html space ampersand'''  
 +
 
 +
 
 +
And press''' Enter'''.
  
 
|-
 
|-
Line 416: Line 422:
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Let us learn how to do it.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Let us learn how to do it.
 
 
  
 
|-
 
|-
Line 458: Line 462:
  
  
 +
Type''' gedit space mypage.html space mystory.html &'''
  
 
+
And press''' Enter'''.
Type''' gedit space mypage.html space mystory.html &''' and press''' Enter'''.
+
  
  
Line 473: Line 477:
  
 
Type''' git log''' and press''' Enter'''
 
Type''' git log''' and press''' Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us check the''' Git log''' by typing''' git space log''' and press''' Enter'''.
 
+
 
+
Now let us check the''' Git log''' by typing''' git space log''' and press''' Enter'''.
+
  
  
Line 549: Line 550:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git log hyphen hyphen oneline''' and press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' git log hyphen hyphen oneline''' and press''' Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Let’s check the''' Git log''' once again by typing''' '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let’s check the''' Git log''' once again by typing
  
 
'''git space log space hyphen hyphen oneline''' and press''' Enter'''.
 
'''git space log space hyphen hyphen oneline''' and press''' Enter'''.
Line 557: Line 558:
  
  
In this manner, we can go back any stage of our work.
+
In this manner, we can go back to any stage of our work.
  
  
Line 627: Line 628:
 
* Discard the changes made to a file and
 
* Discard the changes made to a file and
 
* Revert to an earlier revision  
 
* Revert to an earlier revision  
 
 
  
 
|-
 
|-
Line 644: Line 643:
 
* Try to revert to your old revision.
 
* Try to revert to your old revision.
 
* Again do some modifications in your text file.
 
* Again do some modifications in your text file.
* Try to discard the changes.  
+
* And try to discard the changes.  
 
+
 
+
  
 
|-
 
|-

Latest revision as of 17:14, 12 June 2015

Title of script: The git checkout command

Author: Priya K

Keywords: Video tutorial, add multiple files, revert to older revisions, restore, discard changes, checkout


Visual cue
Narration
Slide 1:


Welcome to the spoken tutorial on git checkout command.
Slide 2:


In this tutorial, we will learn how to
  • Add multiple files to Git repository
  • Remove a file from Git repository
  • Restore the removed file
  • Discard the changes made to a file and
  • Revert to an earlier revision
Slide 3:

System requirement


For this tutorial, I am using
  • Ubuntu Linux 14.04
  • Git 2.3.2 and
  • gedit Text Editor

You can use any editor of your choice.

Slide 4:

Pre-requisites


To follow this tutorial
  • You must have knowledge of running Linux commands on Terminal.
  • If not, for relevant Linux tutorials, please visit our website.

<<PAUSE>>

Press Ctrl+Alt+T to open the terminal Now, let us see how to add multiple files to the Git repository.


Press Ctrl+Alt+T to open the terminal.

Type cd mywebpage and press Enter We will go into our Git repository mywebpage which we created earlier.


Type cd space mywebpage and press Enter.

I will continue to use html files for demonstration.


You may use any file type of your choice.

Type gedit mystory.html mynovel.html & and press Enter We will create 2 html files now.


So type,

gedit space mystory.html space mynovel.html space ampersand


We use the & (ampersand) to free up the prompt.


Press Enter.

Copy and paste the code into the file I will copy and paste some code into these files, from my Writer document, which I had saved earlier.
Save and close the files. Let us save these files.
Type git status and press Enter


In the terminal, first check the Git status by typing git space status and press Enter.
Highlight Untracked file, mynovel.html and mystory.html It shows two untracked files.
Type git add .

Press Enter

We will now add the untracked files for tracking.


Type git space add space dot and press Enter.

Highlight the command git add . The git add dot command will add all the untracked files to the staging area.


Hence, the two files mystory.html and mynovel.html are added to the staging area.

Type git status and press Enter


Let’s check the Git status once again by typing git space status and press Enter.
Highlight new files Now, we can see that both our files have been added to the staging area of the Git repository.


<<PAUSE>>

Switch back to our files mystory.html and mynovel.html Let us switch back to our files mystory.html and mynovel.html.
Add few lines Now we will add a few more lines of code to both these files.


Like before, I will copy-paste from my Writer file.

Save and close the files Once again save and close the files.
Type git status and press Enter


Let’s check the Git status by typing git space status and press Enter.
Highlight “Changes not staged for commit” and “modified: mynovel.html

mystory.html

It shows “Changes not staged for commit” and “modified: mynovel.html and mystory.html”.


This means that the changes we made, have not been added to the staging area.

Type git commit -am “Added two files” Let us now commit our work at this point.


So type

git space commit space hyphen a space hyphen m space within double quotes “Added two files”

and press Enter.

Note that
  • We didn't add the modified files to staging area before committing them and
  • The editor also didn't open up for committing message as we saw in earlier tutorial.
Highlight -a and -m flags This is because here we have used -a and -m flags.


So, what are these flags for?

Switch back to our slides.

Slide 5:


  • -a flag is used to add all the modified files to the staging area.
  • When we use -a flag, we don’t need git add command separately to add the modified files to the staging area.
  • -m flag is used to give commit message in the command line itself
  • We can use the flags -a and -m as -am.
Switch back to our terminal


Type git log and press Enter

Switch back to the terminal.


Check the Git log by typing git space log and press Enter.

Highlight the latest commit You can see the list of commits.


Note that the latest commits are listed first.

Which means the commits are listed in chronological order.


<<PAUSE>>

In case you have added a wrong file to the Git repository, it can be easily removed.
Type git rm --cached mypage.html and press Enter Say, for example, I want to remove the file mypage.html.


Type git space rm space hyphen hyphen cached space mypage dot html and press Enter.


This command will remove the file mypage.html from the staging area.

Type git status and press Enter We will check the Git status now by typing git space status and press Enter.
Highlight the message “Untracked files mypage.html It says that the file mypage.html is untracked.
Type rm mypage.html and press Enter Now we can delete the file from file system by typing

rm space mypage dot html and press Enter.


This command would completely remove the file from mywebpage folder.

Type git status and press Enter Now we will check whether the file has been removed from the Git repository.


So, type git space status and press Enter.

Highlight the message “deleted: mypage.html It shows the message “deleted: mypage.html”.
Type ls and press Enter Now list the files by typing ls and press Enter.


Here we cannot see the file mypage.html anymore as it has been deleted.


<PAUSE>

At this point, let’s freeze our code.
Type git commit -am “Deleted mypage.html” and press Enter To commit, type

git space commit space hyphen am space within double quotes “Deleted mypage.html” and press Enter.

Type git log and press Enter


Let’s see the Git log by typing git space log and press Enter.
Highlight “deleted mypage.html”


Press q key

Press q key on your keyboard to exit.


Here we can find the latest commit by reading the commit message.


<<PAUSE>>

Now suppose that we have deleted mypage.html by mistake.

And now we want to restore it back.


What can we do?

Highlight the commit message “Added two files” We can restore the deleted file from the previous commits.


Let’s restore our file from the second commit, which has the commit message

“Added two files”.

Copy the first five digits of the commit hash Select the first five digits of the second commit hash.


And press Ctrl + Shift + C keys to copy them.


The first five digits are sufficient.


But you can copy more than five digits also, if you wish to.

Type git checkout hash mypage.html and press Enter Type git space checkout space.

And press Ctrl + Shift + V keys to paste the commit hash.


Now type the file name mypage.html and press Enter.

Type git status and press Enter Check the Git status by typing git space status and press Enter.
Highlight new file: mypage.html Now you can see the file mypage.html.
Let us commit our work at this point.


Note that it is very important to commit our work whenever we add or delete any file.

Type git commit -am “Restored mypage.html” and press Enter Type git space commit space hyphen am space “Restored mypage.html”

and press Enter.

Type ls and press Enter Now list the files by typing ls and press Enter.


We can see that our file mypage.html is restored.


<<PAUSE>>

Next, we will see how to discard the changes made to a file.
Type gedit mypage.html mystory.html & and press Enter Open the files by typing gedit space mypage.html space mystory.html space ampersand


And press Enter.

We will do some modifications in mypage.html and mystory.html.
Add and delete lines in both the files Let us add and delete some lines in both the files.
Save and close the files Then save and close the files.
In certain situations, we may not want to continue with these changes.


That means, we want to go back to the previous stage of our work.

Let us learn how to do it.
Type git status and press Enter First we will check the Git status by typing git space status and press Enter.
Highlight modified: mypage.html

modified: mystory.html

It says that some files have been modified.
Type git checkout . and press Enter


Now type git space checkout space dot and press Enter.


This command will delete the latest changes of our work.

Type git status and press Enter


Highlight “nothing to commit

Check the Git status by typing git space status and press Enter.


It says “nothing to commit”.


Type gedit mypage.html mystory.html & and press Enter

Let us check the files to see whether the changes are still there or not.


Type gedit space mypage.html space mystory.html &

And press Enter.


We can see that our modifications have been discarded. Close the files.


<<PAUSE>>


Type git log and press Enter

Now let us check the Git log by typing git space log and press Enter.


It shows list of commits.

Press down arrow key


Press q key

Press down arrow key to see more.


Press q key on your keyboard to exit.

Type git log --oneline and press Enter If you want to see the commits list in one line, type

git space log space hyphen hyphen oneline and press Enter.

Highlight the list Here you can see the commits list with their commit hash and commit messages in one line.


<<PAUSE>>

How can we go to a previous revision of our work?
Highlight the commit list Currently we have four commits in our repository.


Which means, we have four revisions of our work.

Type, git checkout hash and press Enter Say, we want to go back to the “Initial commit” stage.


So, type git space checkout space, then copy and paste the commit hash of the Initial commit and press Enter.

Type ls and press Enter List the files by typing ls and press Enter.


We can see only one file mypage.html here because in this stage we had only this file.

Type git log and press Enter


Now check the Git log by typing git space log and press Enter.
Highlight Initial commit We can see the first commit only i.e. the Initial commit.
Type git checkout master and press Enter To return back to the current revision, type git space checkout space master and press Enter.


We will learn more about the term master in future tutorials.

Type git log hyphen hyphen oneline and press Enter Let’s check the Git log once again by typing

git space log space hyphen hyphen oneline and press Enter.


Now you can see all four commits. So we are now at the latest stage.


In this manner, we can go back to any stage of our work.


<<PAUSE>>

There is another way to go to the old revision.
Type git reset --hard hash and press Enter Type git space reset space hyphen hyphen hard.


Then copy and paste the commit hash of the Initial commit and press Enter.

Type git log and press Enter Check the Git log by typing git space log and press Enter.


It shows that we are now at the Initial commit stage.

Type git checkout master and press Enter Now let’s try to go back to the latest revision.


Like before, type git space checkout space master and press Enter.

Highlight “Already on 'master' We are unable to go back to the latest revision.

Instead we get a message: “Already on 'master'”.


It means this is our latest revision.

So, note, once we use the command git reset hyphen hyphen hard, we can't go back to the latest stage.


So we should be very careful with this command.


<<PAUSE>>

With this, we come to the end of this tutorial.
Slide 6:

Summary


Let us summarize.


In this tutorial, we have learnt how to

  • Add multiple files to Git repository
  • Remove a file from Git repository
  • Restore the removed file
  • Discard the changes made to a file and
  • Revert to an earlier revision
Slide 7:

Assignment


As an assignment


  • Go to your Git repository which you created in the previous tutorial assignment.
  • Do some modifications in your text file.
  • Commit the changes.
  • Try to revert to your old revision.
  • Again do some modifications in your text file.
  • And try to discard the changes.
Slide 8:

Acknowledgement


The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide 9:

Acknowledgement

The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass online tests.


For more details, please write to us.

Slide 10:

Acknowledgement


Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this Mission is available at the following link.

This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst