Common Git Commands

Cleaning Your Workspace

It is common to get rid of unwanted changes in your workspace. But cleaning up is not straight forward as it should be. We use clean command to remove untracked files/folders and checkout for unstaged changes on tracked file.

Remove Untracked Files and Directories

git clean -f   # removes files
git clean -d   # removes directories
git clean -fd  # short hand for the above two
git clean -n   # dry runs the command and shows list of removing files

It is not possible to get back files removed with git clean command the safer option is to use git stash –all

Undo Unstaged Changes in Tracked Files

git checkout       # undo a single file
git checkout .     # undo all file under current directory

another alternative to clean and checkout commands is stash and remove the stash

git stash               # git stash the uncommitted/untracked changes
git stash drop          # get rid of the stash

Resetting Changes

Resetting staged changes

$ git reset              # resets staged changes to unstaged
$ get reset --hard       # resets and deletes staged changes

Resetting the last commit

$ git rest HEAD^        # resets the last commit to unstaged

Resolving Common Git Issues

Refusing to merge unrelated histories

This may happen on first pull request after adding a remote git repo. Use this flag to resolve it.

--allow-unrelated-historie

Squashing Commits

It is highly recommended to get rid of noisy commit message and squash them into a single message. This is specially important when you are done working on your dev branch and ready to merge into master.

Squash last 5 commits and give them a single commit message

$ git reset --soft HEAD~5 && git commit # Squash last 5 commits into one

Cherry Picking

Sometimes you want to pick only a specific commits from another branch and add it to a current branch. This is specially useful when you have difficulty resolving conflict during merge or rebasing.

$ git cherry-pick <commit_hash>
$ git cherry-pick <commit_has_oldest>..<commit_has_newest> #pick range of commits

 

Search with grep

Search a string in files, recursively

$ grep -inHER "word" ./src/*.js
-i       case insensitive
-n       print line number
-H       print filename
-E       use regular expression in "word" (avoid for simple string search)
-R       search recursively

Search a string in files, print lines before/after the matched string

$ grep -inHER -A 2 -B 3 "word" ./src/*.js
-A print 3 additional lines after the match line
-B print 3 additional lines before the match line
-C  print  lines around the match

Search a string which is a complete wrod

$ grep -inw "word" ./src/*.js
-w       "word" must a complete word, not a part.

Recursive search, show NOT matching

$ grep -invHER "word" ./src/*.js
-v       show lines which don't have "word"

Recursive search, show match count

$ grep -icHER "word" ./src/*.js
-c       show match counts in each file

Recursive search, get number of files that have matching string

$ grep -icHER "word" ./src/*.js | wc -l
-icHER    gives match count in each file and filename
|         pipe
wc -l     wc is word count -l option counts the match files

Vim Configuration

I use The Ultimate Vim Configuration . This plugin installs the configuration in ~/.vim_runtime. It has spreads configurations across sever files and .vimrc sources those files as following.

set runtimepath+=~/.vim_runtime

source ~/.vim_runtime/vimrcs/basic.vim
source ~/.vim_runtime/vimrcs/filetypes.vim
source ~/.vim_runtime/vimrcs/plugins_config.vim
source ~/.vim_runtime/vimrcs/extended.vim

try
source ~/.vim_runtime/my_configs.vim
catch
endtry

The last file my_configs.vim is the place where you can put your own custom configurations which don’t come out of the box.


set pastetoggle=<F2> "Toggle paste mode on or off
set number "Display line numbers
set tabstop=2 softtabstop=2 expandtab "Tab width set to 2

view raw

my_configs.vim

hosted with ❤ by GitHub

TMUX – The Terminator of all Terminals

TMUX is the best terminal emulator out there. What I like about it is its ability to split a terminal window into several panes and save your session.

Start TMUX Session

$ tmux new -s <session_name>      #create a new session
$ tmux attach -t <session_name>   #attach an existing session
$ tmux list-session               #list all existing sessions

The above commands are fired from outside the tmux itself.

Windows

Windows are Tabs which cover the terminal screen. The first window is created when tmux session is started.  The following commands are fired inside tmux window.

$ tmux new-window                 # create a new window (prefix + c)
$ tmux rename-window <new_name>   # rename a window (prefix + ,)

Shortcuts for managing windows:

<prefix> 0-9                     # switch to a different window
<prefix> c                       # create window
<prefix> n                       # next window
<prefix> p                       # previous window
<prefix> w                       # list window
<prefix> &                       # kill window

Panes

A window can be split into several panes, vertical and horizontal.

<prefix> %                       # split vertically
<prefix> "                       # split horizontally
<prefix> o                       # jump panes clockwise
<prefix> { or }                  # swap panes
<prefix> x                       # kill pane

Resizing Panes

To resize panes, we need to give command : resize-pane followed by prefix.

<prefix> : resize-pane <D|U|L|R> <number>
-D 10           # resize current pane down by 10 blocks
-U 10           # resize current pane up by 10 blocks
-L 10           # resize current pane left by 10 blocks
-R 10           # resize current pane right by 10 blocks

JavaScript Modules

There are several ways of writing modules in JavaScript:

  • Module Pattern
  • AMD modules
  • CommonJS modules
  • ES6 modules

Module Pattern

Unlike other programing languages, JavaScript doesn’t have concept of module. Module Pattern helps us to create module in JavaScript using closure. Here is an example of Revealing Module Pattern. The  function returns an object that list all the functions available outside the module.

/* Revealing Module Pattern */
var counterModule = (function () {
  let counter = 0;
  let addOne = function () {
    counter += 1;
  }
  let print = function () {
    console.log('The value of counter is: ' + counter);
  }
  let increment = function () {
    addOne();
  }
  /* Public Interface */
  return {
    increment: increment,
    print: print
  }
}());

A module dependent on third party can get its dependency libraries is follows:

(function ($, _) {
  /* Module Code have access to jQuery and lodash */
}(jQuery, _));

AMD Module

Asynchronous Module Definition (AMD) is a specification for writing JavaScript modules. It defines an API that create modules and loads their dependencies asynchronously. AMD loader registers a module you created and it loads the module when needed. Asynchronous loading of modules on browser improve performance, debugging and cross-domain access issue.

Define and Require are the most APIs of AMD specification. Define is used to define a module and the other one is used to import a dependency of a module.

Define

define('counterModule', [jQuery, _], function ($, _) {
  let counter = 0; 
  let addOne = function () { 
    counter += 1; 
  } 
  let print = function () { 
    console.log('The value of counter is: ' + counter); 
  } 
  let increment = function () { 
    addOne(); 
  }
  var counterModule = {
    print: print,
    increment: increment
  };

  return counterModule;
}

Require

require (['counterModule'], function (counter) {
  counter.increment();
  counter.print();
});

CommonJS Module

CommonJS module specifies simple API for creating and using modules. Unlike AMD, which focuses on browser, CommonJS attempts to solve server-side concerns like IO, filesystem and promises.

CommonJS contains a object named exports and function named require. Exports object holds reference to all the object a module wants to expose to other modules. Require function imports the exports from other modules.

//counter.js
var counter = 0;
function addOne () {
 counter += 1;
}
function print () {
 console.log('The value of counter is: ' + counter);
}
function increment () {
 addOne();
}

exports.increment = increment;
exports.print = print;
//test.js
var ctr = require('./counter.js');
ctr.increment();
ctr.print();

ES6 Module

Similar to CommonJS ES6 Modules are written their own files. ES6 modules has declarative syntax for importing and exporting. They allow configuration and condition loading of modules.

Named Exports

A module can export several type of object by adding export keyword before them. These exports are identified by their names.

Default Exports

A module can export multiple objects and one of the export can be declared as default export. Default exports are easier to import. If a module has only one export we usually declare it default. Only one default export is allowed per module.

// library.js
export default function () { };

// main.js
import lib from 'library';
lib();

 

Conclusion

We reviewed various ways of defining modules in JavaScript and how to use them. Each way of defining modules have their own advantages and disadvantages.

GIT Configuration

Here are some handy GIT Configuration commands.

Global configuration

See current configuration

$ git config --list

Edit global git configuration

$ vim ~/.gitconfig

Configure system proxy

$ git config --global http.proxy http://user:password@143.115.170.2:80

Local config (for each repository)

When you need to change configurations for a specific repository , change .git/config file of the repository or use the following command:

$ git config --local -e

Use https instead of ssh

Sometimes networks block ssh connection and force use of use https instead. If git pull gives timeout error, ssh might not be working. We can test it using the following command:

$ ssh -T git@github.com

If this timeouts, ssh is blocked and we need to configure git to use https instead. change the repository url in .git/config

url = git@github.com:username/repo.git

to

url = https://github.com/username/repo.git