megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Rename a Git Branch

Using branches are one of the most powerful features of Git and becomes a part of the software development process.

Last night, I came across an issue where I created a new branch and committed my changes until the git tool rejected it because the branch was already created by someone else in the repository.

Luckily, I was able to resolve this issue by renaming my branch using git branch -m command.

Here's a short guide on how you can do that too!

1. Switch to the remote branch you want to rename

git checkout <your_old_branch>

2. Rename the current remote branch

git branch -m <your_new_branch>

Proceed to the next step, if you've pushed your old branch to the remote repository.

3. Push the renamed remote branch

git push origin -u <your_new_branch>

4. Delete the old remote branch

git push origin --delete <your_old_branch>

If you've come this far without any issues, you've successfully renamed your local and remote Git branch.

Hope you found this useful!

Import and Export MySQL database via Terminal

Sometimes, phpMyAdmin can be painful to use especially when you want to import/export a MySQL database.

If you're not afraid of using the Terminal, try these commands to save your time:

Import MySQL database

mysql -u username -p database_name < your_sql_file.sql

Before you run this command, please make sure that you've created the database_name schema in your database or else, you might get an error especially if the .sql file doesn't have a CREATE DATABASE statement.

Export MySQL database

mysqldump -u username -p database_name > your_sql_file.sql

This command will export your database with the file name your_sql_file.sql to your current path.

Hope this helps you out!

Concatenate multiple rows into one field

Say, you have a table named hobbies and wanted to display a list of hobbies based on user_id, you'd probably do something like this:

SELECT title FROM hobbies WHERE user_id = 8;

This would return a list of hobbies like this:

Boxing
Coding
Reading
Fishing

That's simple but what if you wanted to display them in one row? Like this:

Boxing, Coding, Reading, Fishing

You can make use of the GROUP_CONCAT method to achieve the same result by executing the following SQL query:

SELECT GROUP_CONCAT(title, SEPARATOR ', ') FROM hobbies WHERE user_id = 8;

Nice, what if you wanted to view a list of hobbies of all users? In most cases, a table like this might have a many-to-many relationship, so in order to avoid possible duplicates, you can try this:

SELECT user_id, GROUP_CONCAT(title, SEPARATOR ', ') FROM hobbies
GROUP BY user_id

Hope this tip helps you out!

Find your Public IP address using the Terminal

Previously, I used to determine my Public IP address on Google Search by typing "What is my IP?" and I was good with it.

Until, I thought of actually viewing it via the terminal itself. So, I wrote two lines of code in my ~/.bashrc file:

export myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
alias myip="echo $myip"

Alternatively, you could try this too:

export myip="$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com)"

Save the file and apply your new configuration by typing the following command:

source ~/.bashrc

That's it, now all you have to do is type myip in your terminal and it will display your Public IP address.

Hope you found this useful! ๐Ÿ˜€

Fix screen tearing in XFCE desktop environment

Yesterday, I decided to try XFCE desktop environment and boy, it's really faster than the GNOME desktop environment.

As soon as I started to play around with it, I noticed a good amount of screen tearing and to my surprise, I came to know that the XFCE environment is known to have such issues.

After a few minutes of research, I was able to fix it. Here are the steps:

Note: This is done on Ubuntu 18.04 Bionic Beaver

If you dont have the package inxi installed in your system, do it right now:

sudo apt install inxi

After you're done installing, type the following command to find out which graphics you're using:

inxi -G

If you're using an Intel Graphics Driver, you'll probably get something like this:

Graphics:  Card: Intel Device 5926
           Display Server: x11 (X.Org 1.19.6 ) driver: i915 Resolution: 1920x1080@60.00hz
           OpenGL: renderer: Mesa DRI Intel Iris Plus Graphics 640 (Kaby Lake GT3e) (KBL GT3)
           version: 4.6 Mesa 20.0.8

You can try go to Settings Manager->Window Manager Tweaks->Compositor" and enable **Synchronize drawing to the vertical blank. From what I've read, if you do that, it should stop but it didn't do anything for me.

If the above technique didn't work, go to /usr/shar/X11/xorg.conf.d/ and create a file for your graphics card named 10-intel.conf.

Copy-paste the following configuration into the file:

Section "Device"
  Identifier  "Intel Graphics"
  Driver      "intel"
  Option "TearFree" "true"
EndSection

Save the file, reboot your system and look for any screen tearing issues. If you didn't face any, that means it worked! ๐Ÿ˜

Clear a file directory using Filesystem in Laravel

I was working on a project that dealt with generating large .zip exports and as a result, the storage/exports directory, which was used to store all the .zip exports, ended up going all the way up to a whopping size of 10 gigabytes! ๐Ÿ˜ฎ

I resolved it by calling the Filesystem package in my controller:

<?php
use Illuminate\Filesystem\Filesystem;
?>

Then simply, create a new instance and define the directory you wanted to clear:

<?php
$folder = new Filesystem;
$folder->cleanDirectory('storage/exports');
?>

Hope this helps you out!

How to move a running process to the background?

Pause the current process using Ctrl + Z, which will send it to the background and then type bg to allow the process to run in the background.

Alternatively, you can add a & after the desired process you wanted to execute and it'll automatically execute it as a background process.

Hope this helps you out!

Get pagination data by page number

By default, Laravel's paginator checks the value of the page based on the query string and uses that to display the results and also, it generates links to previous and next pages as well.

The paginate() method takes the following parameters by default:

<?php
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
?>

So, if you want to fetch the pagination data of a specific page, then just write this:

<?php
$pageNumber = 5;
$data = ExampleModel::paginate(5, ['*'], 'page', $pageNumber);
?>

Hope you found this article useful!