5 git commands that will save your life

polmonroig

Pol Monroig Company

Posted on June 17, 2020

5 git commands that will save your life

1. Amend mistakes

We've all been there, your in a rush for your next deadline and you have to deliver your next application, as always you have git to back you up, literally it backs up and versions your data. Unfortunately you've been taking git lightly and so you have committed something horrible, something that was not supposed to be saved, but what will you do? You don't have spare time to ask a question on Stackoverflow... Suddenly you remember a great command, a command that can discard all your changes.
git reset --hard <Commit>
With this command you remove your commit and move to a selected one. But beware! I will also delete any uncommited changes.
So what happens if you don't want to lose any changes? Well git comes to the rescue again with
git reset --soft HEAD~1

2. Find where you are

Have you ever been working on something but have so many files that it's impossible to distinguish between what is useful and what must be deleted? Have you ever wondered where you are? Or who are you? It turns to git can answer to the first two questions. One of the most useful and simple command you'll ever use is
git status
This command will help you visualize what are your staged files, your uncommited files, and and idea of what have you done in the current commit.

3. Go back in time

I have always wanted to go back in time, or at least only if I can come back later to the present. A lot of people don't know it, but git is a time machine, not one of those fancy time machines that can send you to the Jurassic period, but a time machine that can go back inside your code.
git checkout <Commit>
When you checkout a commit you are essentially travelly to a specific point in time where you wrote that code. You can open any files and navigate through the folders as if you where there. Awesome right? If you change the commit for a branch you can change between branches, but that isn't as fun.
Alt Text

4. Finding differences

Traveling in time is amazing, but sometimes is a little too much, perhaps you just want to see a specific file or wish to compare the same file between different commits.
git diff <OldCommit>...<NewCommit>
git diff <OldCommit>...<NewCommit> -- <file>
This command does precisely that, it takes two commits and compares their differences. You can only compare a specific file if that is what you want.

5. How to read your log?

Having a log is very important, it contains information on any changes you have done, why you have done them, who has done them and when. That is why git has its own log, although learning how to write your log correctly is as important as to how to read it.
Most people use git log, but it is so simple, so vague, let's add some decoration...
With git log -n <integer> you can view only a specified number of commits, really helpful if you don't want to see all.
git log --since <Commit> --until <Commit>
works by filtering any commits between two dates. Finally a very interesting alternative is
git log --grep=<Keyword>
This commands filters any commands that contain a specific keyword.

Conclusion

Git is an immensely powerful tool, it has amazing commands and I have just shown you some. Hope you learned at least one command this day.

πŸ’– πŸ’ͺ πŸ™… 🚩
polmonroig
Pol Monroig Company

Posted on June 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related