Welcome to this Masterclass of Learning Git With Sarthak!
Git and its associated services need no introduction. You must have used Git on Day 1 of your work. In fact, the amazing thing about recent years is that students are taking massive interest in self-learning and are learning Git right from their school and college days.
So if you have never touched Git before, or are struggling to learn its intricacies, fear not - this Git masterclass will teach you everything you need to know to use Git in your daily life.
Let's Git going 🚀
How to change branch in Git?
Well that's easy. Just use git checkout branch_name
.
This is not wrong. But consider this scenario - you are working on a file, adding code, and suddenly your manager asks you to get on a call and discuss the code in some other branch.
What do you do? If you type git checkout branch_name
in the terminal, then you get the following error -
What does this mean?
This error comes up because you tried to change your branch BEFORE committing or "saving" your ongoing work. And Git is smart enough to know and track unsaved changes across your entire file directory.
This is a fool-proof system built into Git. Git was programmed this way to handle exactly this scenario. It warns the developer about unsaved commits and will not allow you to change branches before committing them.
So, the answer? Just commit these changes before changing branches!
NOPE.
Consider this again - What if you frequently need to change branches throughout the day in between your ongoing work?
Will you commit your unfinished work every single time? Multiple useless commit messages are not good practices. Even if you just commit and push them all later, they will still be logged in Git's commit history.
This is where the command git stash
comes into play.
What is git stash?
As the word states, stash
means to simply "put" something in a place until you come back to retrieve it later.
In the context of Git, we use git stash
to temporarily save the ongoing work/changes and come back to it later without having to worry about unnecessary commit messages.
Let's type this in the terminal - git stash
Now, you should be able to see a message similar to the one below -
Next, type in git checkout branch_name
- in this case I'm using a branch called test
-
Successfully switched to the desired branch!
And if you notice in the working branch, there are no pending commits or unsaved changes anymore. How did this happen?
Git used the stash
command to record the current state of the working directory and index.
Now you can do your work in the changed branch, and later come back to the earlier working environment.
How to get back to the stashed changes?
Once you are done with your other work, you can revert to your earlier working directory with this command - git checkout earlier_branch
.
And once you are checked back into the older working branch, you need to run this command - git stash apply
As seen above, your uncommitted changes are back on track and you can pick up from where you left off.
That is it for Day 1!
If you liked this blog post, and want to see the tutorial in action, head over to the video part here - Git Masterclass | Day 1
Don't forget to subscribe to my weekly newsletter where I share insights and tips about software development, technology in general, and much more ⚡️
Stay tuned for more Git tutorials coming your way 💪
Top comments (0)