Git 101 and its day to day usage: Part 1

This is a part one of a series of blog where I am planning to capture on how we provide technical apprenticeship to new college graduates that intern with us.

First thing that any new software developer should learn is Source Control Management (SCM) and we use Git.

We try to use as much as open source as we can in our applications and this exercise is no different. For git we use Git-it (Desktop App)

Once we are done with the basics, there are few commands that we use which are not covered in the app but are essential in day to day operation.

As the name suggest, this stashes the changes in a dirty working directory. This command is handy when you are working on something, and you have to switch your context to something else. While the same thing can be achieved if you create a branch but this is much faster and simpler (though the changes remain in your computer).

While has many options, most commonly that are used are:

Lets take an example to understand how these commands can be effectively used.

  • You are working on your develop branch and testing a fix for bug 137. Output of git diff is below
Output of git diff
  • While working on this, a higher priority bug 168 comes along.
  • You run . Output of is empty.
  • You start to work on fix for bug 168. Output of git diff is below
Output of git diff
  • You are really having a bad day and you decided on take on a new bug. You stash these changes as well
  • Output of will look like this
Git stash list output
  • You can look at the content of each stash with . Output of the above command will be
Output of git stash show
  • You can apply any of these stashed and resume your work by simply using or . Output of after running is below
Output of git diff after git stash pop

is branch independent by default. This means that if you run in one branch, move to another branch and then do , the code is popped in that branch. I find this as the easiest way to move my local code changes from one branch to another.

PS: You can always run into conflicts when popping a stash. You can read here on how to handle it.

Caution: Use this command only on commits which are local and not that have been shared with anyone
Consider that you have been working on a branch for few days with few files and you have decided to abandon this after validating the feasibility. Now you would like to go back to the initial state. You can use

  • to discard the changes in the working directory (dirty files which have not been committed) as well as files that have been committed.
  • to make all the changes come to your working directory.

Lets look at the example:

Suppose this is the output of your and there are no local changes that are not committed.

Output of git log

As you can see, there is a spelling mistake, in the last commit. I wanted to say changes and not chantes. You can reset the head to the first commit (4f804b) and rewrite the commit message with the following commands.

4f804b06ac9617501bad9432d51eda65d6d48c4d

With this, the git log and git diff looks like below images respectively.

Now I can correct my typing mistake and make the correct commit message with

However if you want to completely remove the changes in the second commit and permanently move to the previous commit, you can use option.

So if you have two commits when this exercise was started and if you run 4f804b06ac9617501bad9432d51eda65d6d48c4d

The output of will be the same as with the without example but the output of would be empty and you would permanently loose the changes that you have made in the file1

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store