Git/C2/The-git-checkout-command/English
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:
|
* -a flag is used to add all the modified files to the staging area.
|
Switch back to our terminal
|
Switch back to the terminal.
|
Highlight the latest commit | You can see the list of commits.
|
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 work. | |
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”
|
Here we can find the latest commit by reading the commit message.
|
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.
|
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.
|
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 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.
| |
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.
|
|
|
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. |