Git/C2/Stashing-and-Cleaning/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Stashing and Cleaning

Author: Priya K

Keywords: Video tutorial, stashing, save stash, delete stash, save temporary changes, stash pop, stash drop, stash apply, stash clear


Visual cue
Narration
Slide 1:

Stashing and Cleaning in Git

Welcome to the spoken tutorial on stashing and cleaning in Git.
Slide 2:Learning Objectives


In this tutorial, we will learn about stashing.


We will learn how to

  • Create a stash
  • Apply a stash and
  • Clean a stash
Slide 3:

System requirement


To record 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 Git commands and branching in Git.
  • If not, for relevant tutorials, please visit our website.
Let us learn about stashing.
Slide 5:

Stashing

  • Stashing is used to save temporary changes of a branch.
  • It helps to pause the current work without committing it, when switching branches.
  • Stash of the temporary changes can be revoked at any time.
Recall that we already came across the term stash earlier in this tutorial series.


Now, let’s learn it in more detail.

Press Ctrl+Alt+T to open the terminal Let us begin by opening the terminal.
We will open our Git repository mywebpage which we created earlier.
Type cd mywebpage and press Enter 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.

From here onwards, please remember to press the Enter key after typing every command on the terminal.
Type git branch and press Enter First we will check the branch list by typing

git space branch

Highlight chapter-three I have already created a branch named chapter-three.

And I have done a commit inside it, for demonstration purpose.

Please make sure that you also create a new branch and make a commit inside it.

Type git branch chapter-three and press Enter We will go into the branch chapter-three by typing

git space checkout space chapter-three

Type git log --oneline and press Enter


Highlight c56658b Initial commit in chapter-three branch

Let’s check the Git log.


This is the commit which I made in the chapter-three branch for demonstration.

Type ls and press Enter


Highlight the html files

Let’s check the folder content by typing ls


If you are working in Windows operating system, use dir command in place of ls command.


Note that here we have three html files.


<<PAUSE>>

Highlight mypage.html


Type gedit mypage.html & and press Enter

Now, we’ll make some changes on the file mypage.html


Let us open the file mypage.html by typing

gedit space mypage.html space ampersand.

Copy and paste some lines I will copy and paste some lines into this file, from my Writer document, which I had saved earlier.
Save and close the file Then save and close the file.
Type git status and press Enter To check the Git status, type git space status
Highlight Changes not staged for commit We can understand that our changes are not staged yet.
When we work in a big project, we may need to switch branches frequently.
Type git checkout master and press Enter Now, let’s say, we want to go back to our master branch to work on something else.


Type git space checkout space master.

Highlight “commit your changes or stash them” This error shows that we can't switch back to other branches without committing the changes.
I don't want to commit the changes right now, as my work is just half-done.
Type git checkout --force master

Highlight --force

If we will forcefully exit this branch using the hyphen hyphen force flag, the changes will get discarded.
Highlight stash But what if I want to save the changes temporarily?


This will be done using Stashing.

Type git stash save “ Stashed mypage.html” We can save the changes temporarily by typing

git space stash space save space

within double quotes “Stashed mypage.html”

Highlight Stashed mypage.html and press Enter Here “Stashed mypage.html” is the stash name I have given.


You can name it as per your preference.

Highlight Stashed mypage.html and chapter-three On the terminal, the stash name and the branch name where the stash is created, is displayed.
Type git status and press Enter


Highlight “nothing to commit”

We will check the Git status by typing

git space status.


You can see the message “nothing to commit”.

So we can switch branches now.


<<PAUSE>>

Type git checkout master and press Enter Now let's try to go into master branch by typing

git space checkout space master.


Note that after stashing, we can switch to other branches.

Type git checkout chapter-three and press Enter Next, let us see another way of stashing.


For that, again I will go to chapter-three branch by typing

git space checkout space chapter-three.

Type gedit history.html & and press Enter Now I will edit the file history.html.


Type gedit space history.html space ampersand.

Copy and paste the content I will add some lines from my Writer document here.
Save and close the file Then save and close the file.
Type git status and press Enter Let's check the Git status by typing

git space status.

Highlight modified: history.html


Type git stash and press Enter

Say, for example, in stash I want to save these changes in another way.


Type git space stash.

Highlight the stash name Note that we didn't give the stash name here.


If we don't give the stash name, the stash will be saved in the name of the latest commit.

Type git log --oneline and press Enter Next, we will check whether the stash name and the latest commit are same.


Let's check the Git log first.

Type git stash list and press Enter To check the stash list, type git space stash space list.
Highlight the latest commit from git log

Highlight the latest stash from stash list

You can see that the latest commit and the latest stash name are same.
Highlight “stash@{0}: WIP on chapter-three: c56658b Initial commit in chapter-three branch ” Note that the latest stash is listed first.


Which means the stashes are listed in chronological order.

Highlight the stash@{0} This is the stash id which will be generated automatically.
I will create one more stash for demonstration purpose.
Type gedit story.html & and press Enter For that, I will edit the file story.html.


Type gedit space story.html space ampersand.

Add some lines I will add some lines in the file story.html.
Save and close the file Then save and close the file.
Type git stash save “ Stashed story.html” and press Enter Now I will save the changes in a stash.


Type git space stash space save space within double quotes “Stashed story.html”.

Type git stash list and press Enter Let's check the stash list by typing git space stash space list.
Highlight the stashes We can see that now we have three stashes in the chapter-three branch.


<<PAUSE>>

In certain situations, we may not remember what changes we have saved in the stashes.
Let us see how to check it.
Highlight stash@{0}


Type git diff stash@{0} and press Enter

Say, for example, I want to see the details of stash@{0}.


So, type git space diff space stash at the rate symbol within curly brackets zero

Highlight story.html changes We can see the changes of story.html.


That is what we saved in stash@{0}.


<<PAUSE>>

Next we will continue to work on the stashed files.


For that, first we have to apply the stashes.

Type git stash list and press Enter To check the stash list, type git space stash space list.
Highlight stash@{1} For example, now we will apply the stash@{1}.
Type git stash apply stash@{1} To do so, type git space stash space apply space stash @ (at the rate symbol) within curly brackets one.
Highlight stash@{1}

Highlight stash@{0}

and press Enter

If you don't mention the stash id, the latest stash (i.e) stash@{0} will be applied.
Highlight “modified file: history.html” You can see that our stash is applied successfully.
Type git stash list and press Enter Let's check the stash list by typing git space stash space list.
We can see the stash@{1} still in the list and this may lead to confusion in future.


So after applying a stash, it is better to delete it manually.

Highlight stash@{1}

Type git stash drop stash@{1} and press Enter

To delete the stash@{1}, type

git space stash space drop space stash@ (at the rate symbol) within curly brackets one.

Type git stash list and press Enter To check the stash list, type git space stash space list.
Highlight “stash@{1}: WIP on chapter-three: c56658b Initial commit in chapter-three branch”


Highlight “stash@{1}: On chapter-three: Stashed mypage.html”

We can see that our stash@{1} is removed.


And stash@{2} became stash@{1}.


<<PAUSE>>

Type git stash pop and press Enter Now, we will learn to apply a stash in another way.


Type git space stash space pop.

Highlight stash@{0}

Highlight “modified: story.html”

We can see that our stash@{0} is applied.
Highlight stash@{0} from stash list So, if we use stash pop command, the most recent stash (i.e) stash@{0} will be applied.
Type git stash list and press Enter Again we will check the stash list by typing

git space stash space list.

Highlight “stash@{0}: On chapter-three: Stashed story.html”

Highlight “stash@{0}: On chapter-three: Stashed mypage.html”

Now we can see that stash@{0} is removed.


And stash@{1} became stash@{0}.

Highlight “stash pop”

Highlight dropped stash@{0}

So the stash pop command will apply the stash@{0} and delete it automatically.


<<PAUSE>>

Next we will learn how to remove all the stashes at once.
Type git stash clear and press Enter To delete all the stashes from our repository, type

git space stash space clear.

Type git stash list and press Enter Again, we will check the stash list by typing

git space stash space list.


We can see that our stash list is empty now.

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

Summary


Let us summarize.

In this tutorial, we have learnt about Stashing.


We also learnt how to

  • Create a stash
  • Apply a stash and
  • Clean a stash
Slide 7:

Assignment


As an assignment
  • Create three stashes in your repository
  • Explore the command git stash show
  • Understand the difference between the commands git stash show and git stash show stash@{1}
  • Apply the latest stash

(Use – git stash pop)

  • And delete all the stashes from the repository.

(Hint – git stash clear)

Slide 8:

Acknowledgement


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


Please download and watch it.

Slide 9:

Spoken Tutorial Workshops


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:

Spoken Tutorial Project

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