Common git commands to use

Here are a common git commands that I end up using pretty frequently. Will keep on updating this list

  1. List all remote branches
  2. Simplest is git branch -a Shows all remote and local branches. git branch -r Will show all remote branches only.
    Another way is to use git remote feature like:
    git remote show origin This shows all branches on remote and how they corelate with local repo.
    git ls-remote Will meticulously list all branches and tags.

  3. Just view modified files between 2 branches (without diff)
  4. git diff --name-status master..branch Will list out only filenames git diff master..branch without –name-status will show whole diff.

  5. git remote -v Displays url of remote repo from where local git was pulled
  6. Alternatively use git remote show origin or git config --get remote.origin.url

  7. Viewing Unpushed Git Commits
  8. git log origin/master..HEAD
    To view diffs: git diff origin/master..HEAD
    Another way: git log @{u}..

Handy commands in Mac/Linux systems

Here’s a list of common commands I use in Linux. I will continue to update this list with more stuff.

  1. Checking fireball logs:
  2. [code lang=”bash” gutter=”false”]
    cat /var/log/appfirewall.log

    # You will see stuff like
    Jun 21 10:37:51 prakhars-MacBook-Pro Firewall[90]: postgres is listening from 0.0.0.0:5432 proto=6
    Jun 21 10:38:11 prakhars-MacBook-Pro Firewall[90]: mysqld is listening from ::ffff:0.0.0.0:3306 proto=6
    Jun 21 10:40:21 prakhars-MacBook-Pro Firewall[90]: Dropbox is listening from 0.0.0.0:17500 proto=6
    Jun 21 10:40:21 prakhars-MacBook-Pro Firewall[90]: java is listening from ::ffff:0.0.0.0:0 proto=6
    Jun 21 10:44:38 prakhars-MacBook-Pro Firewall[90]: GoogleTalkPlugin is listening from 127.0.0.1:49198 proto=6
    Jun 21 10:45:35 prakhars-MacBook-Pro Firewall[90]: python is listening from 127.0.0.1:8000 proto=6
    [/code]

  3. screen command: Use it to create parallel sessions in terminal. Useful for running long processes on remote servers even after signing out of server.Usage:
    [code lang=”bash”]
    screen
    # This creates a separate screen.

    screen -ls
    #lists all attached/detached screens IDs

    screen -x 13152
    # Enter (Attach) screen ID 13152 (just an example, can be any ID number)

    echo $STY
    # Displays current screen ID you are working in.
    [/code]
    Ctrl-a d Detach from current screen.

  4. dpkg --get-selections List all installed packages. (Only on debian systems like Ubuntu which support apt-get)
    Smartly grep to find package you are looking for eg- dpkg --get-selections | grep mysql

Display file permissions as octal number in ls command

Normally, in *nix systems like Linux, mac the file permissions are displayed as symbolic notations. So when doing a ls -l in a directory you’ll see drwxr-xr-x+ like symbolic permissions next to a directory. This is equivalent to 755 in octal permissions. I find octal permissions easier to grasp and understand. Unfortunately, there is no native switch in ls to show octal permissions. So here is a little custom command that will do the needful.

[code lang=”bash”]
ls -l | awk ‘{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}’
[/code]

This command will show octal file permissions before its symbolic representation.

If you find it impressive and would like to bookmark it, just add this line in your ~/.profile or ~/.bash_profile (depending on mac or linux) using your favorite editor (I’d use vim).

[code lang=”bash”]
alias l= "ls -la | awk ‘{k=0;for(i=0;i<=8;i++)k+=((substr(\$1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf(\" %0o \",k);print}’"
[/code]

We have just escaped $ and " with a \ in above line.

Some under-the-hood details: This command works by piping the output of ls -l to awk as its input. awk is a pattern directed scanning and processing language. For more info you can do man awk which shows:

Awk scans each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f progfile. With
each pattern there can be an associated action that will be performed when a line of a file matches the pattern. Each line is matched against the pattern
portion of every pattern-action statement; the associated action is performed for each matched pattern. The file name – means the standard input. Any file
of the form var=value is treated as an assignment, not a filename, and is executed at the time it would have been opened if it were a filename. The option
-v followed by var=value is an assignment to be done before prog is executed; any number of -v options may be present. The -F fs option defines the input
field separator to be the regular expression fs.

An input line is normally made up of fields separated by white space, or by regular expression FS. The fields are denoted $1, $2, …, while $0 refers to
the entire line. If FS is null, the input line is split into one field per character.

A pattern-action statement has the form

pattern { action }

A missing { action } means print the line; a missing pattern always matches. Pattern-action statements are separated by newlines or semicolons.