Git/C2/The-git-checkout-command/English-timed
From Script | Spoken-Tutorial
Revision as of 10:52, 28 March 2017 by PoojaMoolya (Talk | contribs)
|
|
00:01 | Welcome to the spoken tutorial on git checkout command. |
00:06 | In this tutorial, we will learn how to: * add multiple files to Git repository |
00:12 | Remove a file from Git repository |
00:16 | Restore the removed file |
00:18 | Discard the changes made to a file and |
00:21 | Revert to an earlier revision. |
00:25 | For this tutorial, I am using: Ubuntu Linux 14.04, |
00:31 | Git 2.3.2 and gedit Text Editor. |
00:36 | You can use any editor of your choice. |
00:40 | To follow this tutorial, you must have knowledge of running Linux commands on Terminal. |
00:47 | If not, for relevant Linux tutorials, please visit our website. |
00:52 | Now, let us see how to add multiple files to the Git repository. |
00:58 | Press Ctrl+Alt+T to open the terminal. |
01:02 | We will go into our Git repository "mywebpage" which we created earlier. |
01:09 | Type: cd space mywebpage and press Enter. |
01:14 | I will continue to use html files for demonstration. |
01:19 | You may use any file type of your choice. |
01:23 | We will create 2 html files now. |
01:27 | So, type: gedit space mystory.html space mynovel.html space ampersand. |
01:37 | We use the & (ampersand) to free up the prompt. Press' Enter. |
01:43 | I will copy and paste some code into these files, from my Writer document, which I had saved earlier. |
01:50 | Let us save these fies. |
01:53 | In the terminal, first check the Git status by typing git space status and press Enter. |
02:03 | It shows two untracked files. |
02:06 | We will now add the untracked files for tracking. |
02:10 | Type: git space add space dot and press Enter. |
02:17 | The git add dot command will add all the untracked files to the staging area. |
02:23 | Hence, the two files "mystory.html" and "mynovel.html" are added to the staging area. |
02:32 | Let’s check the Git status once again by typing git space status and press Enter. |
02:40 | Now, we can see that both our files have been added to the staging area of the Git repository. |
02:47 | Let us switch back to our files mystory.html and mynovel.html. |
02:54 | Now, we will add a few more lines of code to both these files. |
03:00 | Like before, I will copy-paste from my Writer document. |
03:05 | Once again save and close the files. |
03:08 | Let’s check the Git status by typing git space status and press Enter. |
03:16 | It shows “Changes not staged for commit” and “modified: mynovel.html" and "mystory.html”. |
03:26 | This means that the changes we made, have not been added to the staging area. |
03:32 | Let us now commit our work at this point. |
03:36 | So, type: git space commit space hyphen a space hyphen m space within double quotes “Added two files” and press Enter. |
03:50 | Note that we didn't add the modified files to the staging area before committing them and |
03:57 | the editor also didn't open up for committing message as we saw in earlier tutorial. |
04:03 | This is because, here we have used hyphena and hyphen m flags. |
04:10 | So, what are these flags for? |
04:13 | Switch back to our slides. |
04:15 | Hyphen 'a' flag is used to add all the modified files to the staging area. |
04:21 | When we use hyphen a flag, we don’t need git add command separately to add the modified files to the staging area. |
04:30 | Hyphen m flag is used to give commit message in the command line itself. |
04:36 | We can use the flags hyphen a and hyphen m as hyphen am. |
04:42 | Switch back to the terminal. |
04:45 | Check the Git log by typing git space log and press Enter. |
04:52 | You can see the list of commits. |
04:54 | Note that the latest commits are listed first |
04:58 | which means the commits are listed in chronological order. |
05:03 | In case, you have added a wrong file to the Git repository, it can be easily removed. |
05:10 | Say, for example, I want to remove the file mypage.html. |
05:16 | Type: git space rm space hyphen hyphen cached space mypage dot html and press Enter. |
05:26 | This command will remove the file mypage.html from the staging area. |
05:32 | We will check the Git status now by typing git space status and press Enter. |
05:40 | It says that the file mypage.html is untracked. |
05:45 | Now, we can delete the file from file system by typing |
05:49 | rm space mypage dot html and press Enter. |
05:55 | This command would completely remove the file from mywebpage folder. |
06:00 | Now, we will check whether the file has been removed from the Git repository. |
06:06 | So, type: git space status and press Enter. |
06:12 | It shows the message “deleted: mypage.html”. |
06:16 | Now, list the files by typing "ls" and press Enter. |
06:21 | Here, we cannot see the file mypage.html anymore as it has been deleted. |
06:28 | At this point, let’s freeze our code. |
06:32 | To commit, type: git space commit space hyphen am space within double quotes “Deleted mypage.html” and press Enter. |
06:45 | Let’s see the Git log by typing git space log and press Enter. |
06:51 | Press q key on your keyboard to exit. |
06:55 | Here, we can find the latest commit by reading the commit message. |
06:59 | Now, suppose that we have deleted mypage.html by mistake and now we want to restore it back. |
07:08 | What can we do? We can restore the deleted file from the previous commits. |
07:13 | Let’s restore our file from the second commit, which has the commit message “Added two files”. |
07:20 | Select the first five digits of the second commit hash |
07:24 | and press Ctrl + Shift + C keys to copy them. |
07:28 | The first five digits are sufficient. |
07:31 | But you can copy more than five digits also, if you wish to. |
07:36 | Type: git space checkout space and press Ctrl + Shift + V keys to paste the commit hash. |
07:45 | Now, type the file name "mypage.html" and press Enter. |
07:51 | Check the Git status by typing git space status and press Enter. |
07:58 | Now, you can see the file mypage.html. |
08:02 | Let us commit our work at this point. |
08:05 | Note that it is very important to commit our work whenever we add or delete any file. |
08:12 | Type: git space commit space hyphen am space “Restored mypage.html” and press Enter. |
08:22 | Now, list the files by typing ls and press Enter. |
08:28 | We can see that our file mypage.html is restored. |
08:33 | Next, we will see how to discard the changes made to a file. |
08:38 | Open the files by typing gedit space mypage.html space mystory.html space ampersand and press Enter. |
08:50 | We will do some modifications in mypage.html and mystory.html. |
08:58 | Let us add and delete some lines in both the files. |
09:03 | Then save and close the files. |
09:06 | In certain situations, we may not want to continue with these changes. |
09:11 | That means, we want to go back to the previous stage of our work. |
09:16 | Let us learn how to do it. |
09:19 | First, we will check the Git status by typing git space status and press Enter. |
09:27 | It says that some files have been modified. |
09:30 | Now, type: git space checkout space dot and press Enter. |
09:37 | This command will delete the latest changes of our work. |
09:41 | Check the Git status by typing git space status and press Enter. |
09:48 | It says “nothing to commit”. |
09:51 | Let us check the files to see whether the changes are still there or not. |
09:57 | Type: gedit space mypage.html space mystory.html & and press Enter. |
10:07 | We can see that our modifications have been discarded. Close the files. |
10:13 | Now, let us check the Git log by typing git space log and press Enter. |
10:20 | It shows list of commits. |
10:23 | Press down arrow key to see more. |
10:26 | Press q key on your keyboard to exit. |
10:30 | If you want to see the commits list in one line, type: git space log space hyphen hyphen oneline and press Enter. |
10:42 | Here, you can see the commits list with their commit hash and commit messages in one line. |
10:48 | How can we go to a previous revision of our work? |
10:53 | Currently we have four commits in our repository |
10:56 | which means, we have four revisions of our work. |
11:01 | Say, we want to go back to the “Initial commit” stage. |
11:05 | So, type: git space checkout space, then copy and paste the commit hash of the Initial commit and press Enter. |
11:15 | List the files by typing ls and press Enter. |
11:19 | We can see only one file mypage.html here, because in this stage, we had only this file. |
11:28 | Now, check the Git log by typing git space log and press Enter. |
11:34 | We can see the first commit only i.e. the Initial commit. |
11:39 | To return back to the current revision, type: git space checkout space master and press Enter. |
11:48 | We will learn more about the term master in future tutorials. |
11:53 | Let’s check the Git log once again by typing |
11:57 | git space log space hyphen hyphen oneline and press Enter. |
12:03 | Now you can see all four commits. So, we are now at the latest stage. |
12:10 | In this manner, we can go back to any stage of our work. |
12:14 | There is another way to go to the old revision. |
12:18 | Type: git space reset space hyphen hyphen hard. |
12:23 | Then copy and paste the commit hash of the Initial commit and press Enter. |
12:29 | Check the Git log by typing git space log and press Enter. |
12:35 | It shows that we are now at the Initial commit stage. |
12:39 | Now, let’s try to go back to the latest revision. |
12:43 | Like before, type: git space checkout space master and press Enter. |
12:51 | We are unable to go back to the latest revision. |
12:55 | Instead, we get a message: “Already on 'master'”. |
12:58 | It means this is our latest revision. |
13:02 | So, note, once we use the command git reset hyphen hyphen hard, we can't go back to the latest stage. |
13:11 | So, we should be very careful with this command. |
13:15 | With this, we come to the end of this tutorial. |
13:18 | Let us summarize. In this tutorial, we have learnt how to: *Add multiple files to Git repository |
13:27 | Remove a file from Git repository. * Restore the removed file |
13:32 | Discard the changes made to a file and * Revert to an earlier revision. |
13:39 | As an assignment- Go to your Git repository which you created in the previous tutorial assignment. |
13:46 | Do some modifications in your text file. |
13:49 | Commit the changes. |
13:52 | Try to revert to your old revision. |
13:55 | Again do some modifications in your text file and try to discard the changes. |
14:02 | The video at the following link summarizes the Spoken Tutorial project; please download and watch it. |
14:11 | The Spoken Tutorial Project team conducts workshops and gives certificates to those who pass online tests. |
14:18 | For more details, please write to us. |
14:22 | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India. |
14:29 | More information on this mission is available at the following link. |
14:34 | This is Priya from IIT Bombay. Thanks for joining. |