DEV Community

Nandan Kumar
Nandan Kumar

Posted on • Originally published at blog.nandankumar.info on

Understanding version control and mastering git - Tag, log, Stash, and more..!

Git version control image

After a short one-week break, I am back with more on git commands. In the previous blogs in this series, we learned about version control and some basic git commands. We also learned about branching and merging (two very important concepts in git).

You can follow the complete series (or any of the previous blogs in this series) by clicking here.

In this blog, I will cover more on git commands like Tag, Log, Stash, etc, which come in handy in day-to-day development. Let's get started.

git tag: Identify a significant commit

This Git tag command can be used to mark a significant changeset, such as a release.

  • Create a new tag
git tag <tagname> 
git tag 1.0.0
Enter fullscreen mode Exit fullscreen mode
  • Tag a specific commit
git tag <tagname> <commitID> 
git tag 1.0.0 a3d3dwe323de3r2e
Enter fullscreen mode Exit fullscreen mode
  • List out all the tags
git tag
Enter fullscreen mode Exit fullscreen mode
  • Push all tags to the remote repository
git push --tags origin
Enter fullscreen mode Exit fullscreen mode
  • Delete a tag
git tag -d <tagname>
Enter fullscreen mode Exit fullscreen mode
  • Checking Out Tags
git checkout <tagname>
Enter fullscreen mode Exit fullscreen mode

git log: List a commit history

This Git log command can be used to see the logs of commits like an overview of all the changes made in a specific branch.

  • list the version history for the current branch.
git log
Enter fullscreen mode Exit fullscreen mode
  • list the version history of a specific file
git log -follow <file name>
Enter fullscreen mode Exit fullscreen mode
  • shows the metadata and content changes of the specified commit
git show <commitID>
Enter fullscreen mode Exit fullscreen mode

git reset: For when things may go wrong

IF sometimes a commit or a file add goes wrong and you want to undo the changes, the reset command comes to the rescue

  • This command unstages the file, but it preserves the file contents.
git reset <file Name>
Enter fullscreen mode Exit fullscreen mode
  • This command undoes all the commits after the specified commit and preserves the changes locally.
git reset <commitID>
Enter fullscreen mode Exit fullscreen mode
  • This command discards all history and goes back to the specified commit.
git reset -hard <commitID>
Enter fullscreen mode Exit fullscreen mode
  • If you mess up, you can replace the changes in your working tree with the last content in the head: Changes already added to the index, as well as new files, will be kept.
git checkout -- <filename>
Enter fullscreen mode Exit fullscreen mode
  • To drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it.
git fetch origin
git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

git stash: For when you want to get a quick tour of another branch.

There will be instances when you are working on a branch and you have to switch to another branch due to some work.

In such cases, you have to commit your existing changes. But that is not always desirable; you would ideally want to keep your commit history clean and would not want to commit a half-baked work. In such cases, you can stash your work by using the git stash command.

  • This command temporarily stores all the modified tracked files.
git stash
Enter fullscreen mode Exit fullscreen mode
  • This command temporarily stores all the modified tracked files with a message
git stash save "<stash message>"
Enter fullscreen mode Exit fullscreen mode
  • This command lists out all the stashed changesets
git stash list
Enter fullscreen mode Exit fullscreen mode
  • This command restores the most recently stashed files.
git stash pop
Enter fullscreen mode Exit fullscreen mode
  • This command discards the most recently stashed changeset.
git stash drop
Enter fullscreen mode Exit fullscreen mode

Pro Tip: You can use git stash to pass your code from one branch to another branch in case of conflicts.

That's all for now! We will cover conflicts in the next blog. I know it's the most painful task of all. And not just conflicts, we will also cover more on git and its commands. We will dig deeper and learn how to ignore certain files in a repository and so on.

If you have any questions on git, please feel free to shoot out a mail to nandan.1345@gmail.com and I will try and get back to you.

Stay tuned & follow me on my social media channels. Do make sure to subscribe to my newsletter to get regular updates on my upcoming posts.

Discussion (0)