It was a another great week of learning new tools as well as keep on practicing/building on what we already learned in the past three weeks.

For our weekly presentations - this week we were all given one Git command. I was given Git Stash command.

Git Stash:

So what is Git Stash?

The dictionary definition of Stash is to store something safely in a hidden place. So in software development/Git context - let's say you have made some changes to your code but you're not quite ready to commit and push it to Git yet. Or for some reason you need to switch branches to work on something else, or you want to revert back temporarily to where you started and you don't know what to do with you changes. In any cases Git allows you to stash on those changes and store it safely, so you can come back to those changes later on.

So how does it works?

So let's say we have a file called Calc.py which has a following code in it.

Now let's create a branch called 'add' because we want to work on add function.

$ git branch add

To make sure that the brach was successfully created. We can check it by:

$ git checkout add

If the the branch was successfully created then we will see the following message:

Now let's go back to our Calc.py file and make some changes to our add function.

Save the changes in our Calc.py

To view changes we made:

$ git diff

So now for a whatever reason we need to switch to our master branch or we need to see what our Calc.py file looked like before we started making all the changes to our file, we can stash our changes and go back to where we were.

To stash our changes:

$ git stash save "added return value"

Now if we type:

$ git diff

$ git status

Now let's reopen Calc.py. You'll will notice that our code is what it was before we made all the changes.

Don't worry we haven't lost all the changes we made. It is stashed in a safe place. You can bring back those changes anytime you want.

Now go to file make changes in all the remaining functions save those changes.

$ git stash save "calc functions"

Now just to make sure that it worked, let's type:

$ git stash list

Each stash has a unique id numbers. Numbers 0 and 1 in stash@{0} and stash{1} are stash's id numbers.

Very important point to remember. When we have a more than one stashes, by default the last (or the most recent) stash becomes stash@{0}, and it'll push the previous stash down, so it'll become stash@{1}.

Now let's say we made a few changes in your file and we want to apply those change to our file. There are a couple of ways we can apply those changes:

$ git stash apply stash{0}

The above command will apply all the changes we made, but it'll still keep the stash.

$ git stash pop stash{0}

The above command will also apply all the changes we made, but it'll drop the stash after it applied all the changes to the file.

How to delete the stashes/branches?

If we are in add stash branch then we will not be able to delete the stash. We will get an error message:

So first we need to change to our master branch.

$ git checkout master

And now we can delete the 'add' branch.

$ git branch -d add

If the above command gives you an error - The branch 'add' is not fully merged. If you are sure you want to delete it and still you want to delete it, then you can force delete it using -D instead of -d, as:

$ git branch -D add

To delete 'add' branch from remote as well, execute:

$ git push origin --delete add

To find out more about stash, type:

$ man git-stash

Python

We also started learning Python this week. We are using a book called "Learn Python the Hard Way" by Zed A. Shaw to learn Python.

We were given a few quizzes and our task was to write functions and push our code to GitHub.

So here's one of the quizzes. Write a function in Python which will give the following output.

Following is a code:

That's all for this week.

Thank you for reading.