Difference between revisions of "Git/C2/The-git-checkout-command/English"
Nancyvarkey (Talk | contribs) |
|||
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 | + | * 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 | + | | 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;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| 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 | + | | 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''' | + | | 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''' | + | | 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 | + | 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''' | + | | 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'''. | |
− | + | ||
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. | ||
− | * | + | * 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
|
|
Slide 1:
|
Welcome to the spoken tutorial on git checkout command. |
Slide 2:
|
In this tutorial, we will learn how to
|
Slide 3:
System requirement
|
For this tutorial, I am using
You can use any editor of your choice. |
Slide 4:
Pre-requisites
|
To follow this tutorial
<<PAUSE>> |
Press Ctrl+Alt+T to open the terminal | Now, let us see how to add multiple files to the Git repository.
|
Type cd mywebpage and press Enter | We will go into our Git repository mywebpage which we created earlier.
|
I will continue to use html files for demonstration.
| |
Type gedit mystory.html mynovel.html & and press Enter | We will create 2 html files now.
gedit space mystory.html space mynovel.html space ampersand
|
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.
|
Highlight the command git add . | The git add dot command will add all the untracked files 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.
|
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.
|
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”.
|
Type git commit -am “Added two files” | Let us now commit our work at this point.
git space commit space hyphen a space hyphen m space within double quotes “Added two files” and press Enter. |
Note that
| |
Highlight -a and -m flags | This is because here we have used -a and -m flags.
Switch back to our slides. |
Slide 5:
|
|
Switch back to our terminal
|
Switch back to the terminal.
|
Highlight the latest commit | You can see the list of commits.
Which means the commits are listed in chronological order.
|
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 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.
|
Type git status and press Enter | Now we will check whether the file has been removed from the Git repository.
|
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.
|
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 on your keyboard to exit.
|
Now suppose that we have deleted mypage.html by mistake.
And now we want to restore it back.
| |
Highlight the commit message “Added two files” | We can restore the deleted file from the previous commits.
“Added two files”. |
Copy the first five digits of the commit hash | Select the first five digits of the second commit hash.
|
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.
|
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.
| |
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.
|
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
|
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.
| |
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.
|
Type git status and press Enter
|
Check the Git status by typing git space status 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.
|
|
Now let us check the Git log by typing git space log and press Enter.
|
Press down arrow key
|
Press down arrow key to see more.
|
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.
|
How can we go to a previous revision of our work? | |
Highlight the commit list | Currently we have four commits in our repository.
|
Type, git checkout hash and press Enter | Say, we want to go back to the “Initial commit” stage.
|
Type ls and press Enter | List the files by typing ls and press Enter.
|
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.
|
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.
|
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.
|
Type git log and press Enter | Check the Git log by typing git space log and press Enter.
|
Type git checkout master and press Enter | Now let’s try to go back to the latest revision.
|
Highlight “Already on 'master'” | We are unable to go back to the latest revision.
Instead we get a message: “Already on 'master'”.
|
So, note, once we use the command git reset hyphen hyphen hard, we can't go back to the latest stage.
| |
With this, we come to the end of this tutorial. | |
Slide 6:
Summary
|
Let us summarize.
|
Slide 7:
Assignment
|
As an assignment
|
Slide 8:
Acknowledgement
|
The video at the following link summarises the Spoken Tutorial project.
|
Slide 9:
Acknowledgement |
The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass online tests.
|
Slide 10:
Acknowledgement
|
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
This is Priya from IIT Bombay. Thanks for joining. |