Things you wish to know when using Git

ahhh… I wish to know all of this stuff a long time ago :(

This article was written as an individual review assignment for PPL CSUI 2021

For a software engineer, or everyone who needs version control in their works, using git as their version control is very recommended or maybe various people will think it’s a must to use it. So in this article, I will show you some of the tips and tricks that people mostly missed when using Git.

Hope this would help your work guys :)

Have you ever in kind of situation when you want to know who is the person to blaming when you pulling an update from a branch, and because of that your work doesn’t run as before, or in other words, “error”? well you can using git blame for getting that information, but, do you know how to use it? personally, I am not that fluent in using Git, so, GitLens will be there for helping you get that information. GitLens is an extension that you can install in your vscode. just search GitLens in your vscode extension then you can install it, after that you are ready to go using this extension. GitLens will automatically detect if your projects already initialized with git, if already initialized, your vscode will be a little bit different because every line of your code, you will see who is the person, the time, and the message commit from the person commit that line of code.

as you can see in the above snippet code on my PPL project, I am highlighting line 19, and it shows the information about that line of code commit. it shows that “You” which is me who is commit this line of code, 2 days ago with commit message “[RED] start implement registrations”. you can get more verbose information on this by clicking a special button in the top right of your vscode

after you click that button, you will get much more verbose information like this

another useful function that you can find from using GitLens is you can compare the changes of your file based on your commit history, and it’s very easy to use. To use it just go to the right top of your vscode, and click this button

as the result, you can compare your currently opening file with its previous version

2. Use more than one git remote

mostly of people who familiar with using git will familiar with “origin” word. but what word is origin exactly in your “git push origin master” command? the simplest explanation is, origin is an alias of your repository link, so instead of writing your repository link every pushing or pulling, why not just giving an alias for that link so it will be much easier for us pushing or pulling our projects. in Git, this alias is called remote and the standard name people usually use for their repository remote name is “origin”

okay, after a short explanation of what is remote and what is origin, I will show you an example use case of using more than just one remote and why it’s useful for us to know it. At the time this article written by me, I was unable to pushing and pulling my projects from my project repository, it’s because my college using their own hosted gitlab, and suddenly all of the servers in my college were down because of a terrible accident and of course, the college-owned gitlab also down. because of this accident, I and my team can’t work as usual because we can’t push and pull each other works. the solution for this is we created a mirror remote for our projects so even the main repository is down, we still able to work in our mirror repository. the workaround is simple, just create a new repo in other git services such as gitlab.com, github.com, etc, and from that, we will be adding the repo to our projects.

  • first of all, create a brand new repository
  • in your terminal, type this command

git remote add mirror https://gitlab.com/xxxxxxx/peradilanmu-admin.git

  • and the last step is pushing to your mirror repository
  • now check your mirror repository, you should be able to work in the mirror repository now :) just use your git as usual, but instead of using origin remote, use mirror remote

3. Git reset, your last resort

So, you already have a working solution for your project but you still think can make it better? okay, it’s normal and a very good attitude. but, have you ever in some condition that you are already trying to improve your code, but instead of working much better, it becomes prone to error and bug? and the condition getting worse when you already change many files in your project? well, you can do undo stuff in every file you already changed, but are you sure want to do that stuff for every file? even there will be more than 10 changed files you have to undo? well if you are sure you want to get back to the last version of your code and don’t worry loss all the changes, you can try this command

git reset — hard

or if you want to know more about this, you can check the official documentation from git-scm, https://git-scm.com/docs/git-reset, there is more than just — hard flag such as soft, mixed and other flags you can use.

4. “git add .” or “git add -u”, what's the difference?

Most of my friends are using “git add .” every adding commit to their works in git, and there is nothing wrong with that. but I was also get annoyed every time see my friends doing this stuff, why? because most of them don’t care what will be added to the commit, the important thing is,” I want all of my works saved in the repository”. when we are using “git add .” it’s means we want to add all files in our projects to our repository including all of the unwanted files and sensitive files such as credentials file, well if you are using .gitignore and already ignoring all of this files, it should be fine. but what if you are not yet ignoring all of this file? and you are pushing all of these files to the repository? it can be a disaster for you in the future. so for the prevention action, I always recommend using “git add -u” instead of “git add .”. but in special condition, “git add .” will be safe enough for us to use it, such as when you init a new project, instead of adding file one by one you can just add all file rights? but make sure all the junk and credentials files are already ignored.

well, that’s all for now, I planned to update this article when I have another time to adding more useful tips and tricks that I can share. but for now, it’s enough for me. Hope those tips and tricks could help you :)

References

https://git-scm.com/docs/git-reset

https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens

https://git-scm.com/docs/git-remote