Git/C2/The-git-checkout-command/English

From Script | Spoken-Tutorial
Revision as of 12:59, 12 June 2015 by Priyacst (Talk | contribs)

Jump to: navigation, search

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 commit 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 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”


Press q key

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


Press q key on your keyboard to exit.


<<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 remove 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 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.
  • 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