Hello Sunil
Essential Git commands - Basic to advance with examples

Essential Git commands : Basic to advance with examples

Let’s suppose you start work with “file1.txt” with one line. Then you get rid of this line and add a second and third line.

Added two more lines in file1.txt file

Now if you run the diff command it will show the differences between the committed version and the local changed version in the staging area.

git diff

Here’s how it will look:

git diff - git commands

In above command:

  • + sign indicates changes which are added to file
  • – sign indicates changes which are removed from file

git stash

Suppose you are implementing a new feature for your product. Your code is in progress and suddenly a client’s escalation comes. Because of this, you have to keep aside your new feature work for a few hours.

You can’t commit your partial code and also can’t throw away your changes. So you need some temporary space, where you can store your partial changes and later on commit it.

The “git stash” command can help you to (temporarily but safely) store your uncommitted local changes – and leave you with a clean working copy.

Let’s say you have modified “file1.txt” in your local machine:

Modified file1.txt with new line addition

Now check the status by following command:

git status -s

status -s  - git commands

In above output letter “M” appears before “file1.txt” which indicates that existing file is modified. Whereas “??” appears before “Link.txt” which indicates that this is a new file and Git is not aware about it hence such a file is called untracked file.

Now, you want to switch branch due to client’s escalation on an urgent bug fix, but you don’t want to commit what you have been working on yet. You shouldn’t just commit them, of course, because it’s unfinished work.

This is where “git stash” comes in handy. So you will stash the changes by typing the following command:

git stash

git stash - git commands

Now, your working directory is clean and all uncommitted local changes have been saved on this kind of “clipboard” that Git’s Stash represents. Let’s verify it with the “git status” command.

Check git status after using stash command

Now you can safely switch the branch and work elsewhere. You can also view a list of stashed changes by using the following command:

git stash list

git stash list - git commands

As already mentioned, Git’s Stash is meant as a temporary storage. When you are ready to continue where you left off, you can restore the saved state easily by typing the following command:

git stash pop

git stash pop - git commands

The “pop” flag will reapply the last saved state and, at the same time, delete its representation on the Stash (in other words: it does the clean-up for you).

Also read: Useful tricks you might not know about Git stash

git tag

The “git tag” command helps you to create tags for your specific commit in Git history. Often “tag” is used to mark release version of the product.

Creating a tag is really simple by typing the following command:

git tag -a <tag_name> -m <message_of_the_tag>

git tag -a  -m - git commands

Here you provides a tag name with “-a” option and a tag message with “-m” option. After it use the following command to push the tag into the remote repository.

git push origin tag <tag_name>

git push origin tag - git commands

If you want to view all the available tags on your repository then type the following command:

git tag

See the available tags on your repository by using git tag command

In order to see what’s in the tag, you can use “git show” command:

git show <tag_name>

git show - git commands

To delete tag from your local repository type the following command:

git tag -d <tag_name>

git tag -d - git commands

To delete tag from remote repository type the following command:

git push origin <:tag_name>

push origin   - git commands - delete remote repo

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.

Add comment

Stay Updated

Want to be notified when our article is published? Enter your email address below to be the first to know.

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.