Generic Engineering Tidbits

Abhishek Jain
Better Engineer
Published in
7 min readJan 19, 2020
Photo by Alejandro Escamilla on Unsplash

This is a collection of tricks and tips I use on daily basis. See if you like something.

This is the table of contents which I have to manually create because medium doesn’t have that feature (yet).

  • vi/vim/gvim
  • unix
  • git
  • perl
  • cron jobs
  • Microsoft Outlook

Git

xkcd

Useful commands

Create a new git repo

go to the directory where you want your git repo to be

cd my/home/california/git init --bare .

Now you have a git repo

Now cd to the area where you want to clone it

cd my/another/home/timbuktugit clone my/home/california/ .

Now you have a clone here

test with one file

echo "hello world" > hellogit add hellogit ci -m "first commit" hellogit push --set-upstream origin master

if it asks you to run “git branch — unset-upstream” do it

Now you have successfully pushed your first file

make another test clone somewhere else and see everything is working fine

The very first time, other users won’t be able to push right away or whoever pushes first, will become owner, so you might need to run these commands

cd my/home/california/objects/chmod -R 774 .

Then everyone in your group should be able to push

Switch between branches

git checkout my_branch

Create and push a tag

git tag -a my_tag -m "golden copy for life"
git push origin my_tag

Shows all the tags so far

git tag -l -n9

Create and push a branch

git checkout -b my_branch
git push origin my_branch

To push into the branch for the first time

git push --set-upstream origin my_branch

Shows all the branches so far

git branch -a

Set meld as git diff tool

git difftool -t meld --no-prompt my_file

*alias it for easy use

alias gitd 'git difftool -t meld --no-prompt'

To replace master with your_branch

git checkout my_branch
git merge -s ours master
git checkout master
git merge my_branch

To merge master’s changes in your_branch

do all the fixes in master and push

git checkout my_branchgit merge master

Add files to gitignore to they dont show up when you do git status

Add those to this file: .git/info/exclude

Find the gitignore or exclude file which is ignoring your file

git check-ignore -v filename

Replace one file from previous version

git checkout c5f567 -- file1/to/restore

Fixing Error after Merge

If you run into conflicts after git pull and resolve conflict manually, when trying to commit the file, you can run into error:

fatal: cannot do a partial commit during a mergeAdd "-i" option to commit command to fix this issue.git commit -i -m "message" file.name

Diff between two commits of a file

git diff 2432523534 43543543534 my_file

Useful Links

How a git work flow should be

https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag

https://www.atlassian.com/git/tutorials/using-branches/git-checkout

https://stackoverflow.com/questions/4470523/create-a-branch-in-git-from-another-b

PERL

source : xkcd

Print time taken by your script

#!/usr/bin/perl
use strict;
use warnings;
my $start = time;
#my code
my $duration = time - $start;
printf("Execution time: %0.0f min %0.0f sec \n", $duration/60 ,$duration%60 );

Find used and free space in a disk

This is equivalent of df -h

#!/usr/bin/perl
use strict;
use warnings;
use Filesys::Df;

my $dir = "your path";
my $ref = df($dir, 1024*1024*1024); # Default output is 1K blocks

if(defined($ref)) {
print "$dir: ";
printf("| disk size: %0.0fG",$ref->{blocks});
printf("| free: %0.0fG",$ref->{bfree});
printf("| used: %0.0fG", $ref->{used});
printf("| percent full: %0.0f\n",$ref->{per});
}

Do some profiling

perl -d:NYTProf your_script.pl

This will generate a report: nytprof.out

To generate a human readable report, just run this command

nytprofhtml

Then open the html

firefox nytprof/index.html

Cron jobs

If you want to run a script periodically at a specific interval, you can set up a cron job for that. There are a lot of applications of cron jobs, you can poll something, refresh your data every hour, refresh your reports every day, run regression tests every night and many such things.

The cron job runs from a particular machine until you kill it or if that server turn off or loses power. If you have server farm like we do at Intel, you should note down the host machine id, otherwise that job will keep running forever and you wouldn’t be able to go back and kill it.

You need to run this command to add a new job or edit existing ones.

crontab -e

This will mostly open a vi editor. If you are setting it up for the first time, the file will be empty.

There you can add the full path of your script like this

0 * * * * /my/path/to/the/script

Note the spaces between the *.

Save and close the file (:wq)

Now your script will run every 0th minute. That means once every hour.

This below line will make it run at every 15th minute of the hour. And it also shows the meaning of those * *

15 * * * * /my/path/to/the/script* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

This is good enough to get started.

You can play around with those combinations on the website below:

This goes in more details:

MS Outlook

If your company still uses Microsoft Outlook for emails, these tricks can make you more productive:

Ignore Emails

Stuck in an email chain you don’t care about anymore? Right click and ignore the conversation and it will directly to in “Deleted Items”

https://support.office.com/en-us/article/ignore-all-email-messages-in-a-conversation-5ec0c3c2-3440-484e-9aca-0588234d44e9

Learn to use email rules to unclutter your inbox

https://support.office.com/en-us/article/manage-email-messages-by-using-rules-c24f5dea-9465-4df4-ad17-a50704d66c59

Insert code snippets in your emails with highlighting and indents

Copy the snippet here : https://tohtml.com/

Select language. Select theme if you want. Copy paste into email.

Vacation Calendar Invite

To send a non-invasive calendar invite :

  1. Click on new meeting in your calendar
  2. Click on “Response Options” and uncheck “Request Response”
  3. Click on “Show As” and change “Busy” to “Free”
  4. Click on Reminder, select “None”
  5. Select start time and end time and check “All Day Event”
  6. Add a meaningful subject line
  7. Add your coverage details and other information.
  8. Add all the recipients and send.
  9. Plan your vacation
  10. Stress out about the trip and work
  11. Realize you are supposed to enjoy this trip
  12. Enjoy stress-fully

Official Microsoft guidelines:

I will add more things to list periodically.

Please share your productivity hacks at work!

--

--