Friday, December 08, 2017

Social Security Basics

I recently attended a few workshops sponsored by GM and hosted by Fidelity.  Here's what I learned:

Important Terms:
FRA - Full Retirement Age - 66 or 67, depending on your birthday.

AIME - Average Indexed Monthly Earnings - This is the average monthly amount you earned in your highest-earning 35 years.  It is indexed for inflation.  AIME is used to calculate PIA.  If you don't have 35 years of earnings, zeros will be averaged in.

PIA - Primary Insurance Amount - amount of Social Security payments you will receive at full retirement age (FRA).

Estimating your benefits - your PIA is based on the average of 35 highest earning years, so try to work 35 years if possible.  Details are  here.

If you were a homemaker and didn't work many years, you can claim 50% of your spouse's PIA if that is more than yours.  It does not affect your spouse's amount.

When to claim your SS benefits?
If you wait until FRA, you get 100%.  If you claim earlier you get less, claim later you get more, until age 70 when the amount does not go up.  There's no magic to claiming at your FRA.  You can claim earlier or later. It's not "right" to claim at FRA, it's just one option.

From www.Fidelity.com/whentoclaimSS for birthdate in 1958
Age% of PIA
62 70
63 80
64 87
65 93
66 100
67 108
68 116
69 124
70 127

For each year you wait you get about 8% increase in payments.
Better to wait if you have other resources, but it depends on your expected life expectancy.
The break even age is 81 or 82.  Will you be alive then?  If not, take SS early.

The average amount of SS paid is $1,300 per month, or $15,600 per year.
$2,788 is maximum you can receive per month.  So if you paid in buckets of money during your career you may reach this level at age 64, so you might as well start taking then, because it won't hurt you.

Survivor benefits can start when the survivor is 60 yrs old.

Working before FRA may reduce your PIA.  Details
here.  Some states tax SS income.

Will SS go bankrupt?  In the year 2034 trust fund will go dry, but SS can still fund 77% of obligations from taxes, we just won't have the trust fund.


One scary tidbit was that health care costs for a couple over 65, with Medicare, was $902 a month.

Friday, December 01, 2017

Git Access Error on Windows 7: unable to access ... Received HTTP code 407 from proxy after CONNECT

Occasionally I get this error connecting to Git a github:


$ git pull
fatal: unable to access 'https://github.com/fincher42/demo.git/': Received HTTP code 407 from proxy after CONNECT


The proxy is complaining that the password is out of date by giving us a  407 error,  which is an authentication error.  Then I remember I just changed my system password.

The solution is easy, just tell Git your new password:

git config --global http.proxy http://myUserId:myPassword@myProxy.myDomain.com:80
git config --global https.proxy https://myUserId:myPassword@myProxy.myDomain.com:80

Friday, November 24, 2017

Saving Money by Moving to an OOma phone

Being the frugal person I am (having the ginger and Scottish genes), I found it hard to pay $20 a month to my cable provider for an internet land line.

(I know, I know, the 70s called and wants their phone back)

I replaced my internet provider's $20/month phone with an $80 one time purchase Ooma phone.

Transferring our old number to the new Ooma phone cost a one time fee of $40.
The Ooma phone has to charge taxes on their service of $3-$4 a month.
At a one-time charge of $120 with the monthly tax , our family should start saving money in only seven months.
We should be saving $17 a month after that.

And one more thing:  Removing your internet phone from your internet provider makes switching to lower cost internet providers easier.  No messy phone number to switch every time you go with a lower cost provider.







Wednesday, November 22, 2017

JavaScript Notes

Objects and Prototypes

Notes from Jim Cooper's excellent PluraSight course on JavaScript Objects and Prototypes and Kyle Simpson's Advanced JavaScript class, and my random scribblings.

Use jsbin.com to play with JavaScript.

  1. Six Ways to Create Objects:
    1. Creating objects with Object Literals
      'use strict';
      var cat = {name: 'Fluffy', color: 'White'}
      console.log(cat.name); //Fluffy
      cat.age = 3; //we can create properties on the fly
      cat.speak = function() {console.log("Meeooow") }//add functions directly
      
    2. Creating objects with constructors:

      Calling "new" makes the function a constructor and does four magic things:

      1. Creates new object
      2. Sets the 'this' to its context
      3. Calls the function
      4. Returns a pointer to the new object

      Here's Jim's example:

      function Cat(name, color) {
        this.name = name
        this.color = color
      }
      var cat = new Cat('Fluffy','White');
      console.log(cat);
      
      This writes:
      [object Object] {
        color: "White",
        name: "Fluffy"
      }
      
    3. Creating objects with Object.create syntax Object.create()

      The "create()" method exists at the top level object cleverly named, "Object".

      Object.create(proto[, propertiesObject])
      

      "Object.create()" creates a new object and sets its prototype link to be the first parameter passed in. It also copies the properties from propertiesObject to the new object.

      The "new" keyword is just syntactic sugar, we could do the previous example like this:

      var cat = Object.create(Object.prototype,
      { 
      name: {
      value: 'Fluffy',
      enumerable:true,
      writable:true,
      configurable:true
      },
      color: {
      value: 'White',
      enumerable:true,
      writable:true,
      configurable:true
      }
      });
                             
      console.log(cat);
      
      o = {};
      //is shorthand for 
      o = Object.create(Object.prototype);
      
    4. A Fallback Polyfill for Older Browsers

      Older browsers may not have the "create()" method, so you can use a polyfill from developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

      if (typeof Object.create !== "function") {
          Object.create = function (proto, propertiesObject) {
              if (!(proto === null || typeof proto === "object" || typeof proto === "function")) {
                  throw TypeError('Argument must be an object, or null');
              }
              var temp = new Object();
              temp.__proto__ = proto;
              Object.defineProperties(temp, propertiesObject);
              return temp;
          };
      }
      
    5. Creating objects with the new ECMAScript 6

      ES6 has new built in support for creating classes:

      'use strict';
      class Cat {
         constructor(name, color) {
            this.name= name
            this.color = color
         }
         speak() {
         console.log('Meeooow')
         }
      }
      
  2. Properties
    1. Bracket Notation

      Besides the dot notation, 'this.color = "blue"' you can use the bracket notation. An advantage of the bracket notation is that you can use property names with embedded spaces:

      cat['color'] = "blue";
      cat['Eye Color'] = "green";
      
    2. Descriptors

      JavaScript properties have 4 descriptors: value, writable, enumerable, and configurable.

      1. Value

        This is simply the actual "value" of the property.

      2. writable

        "writable" is a boolean that determines if the value can be written over.

        var cat = {name: 'Fluffy', color: 'White'}
        console.log(Object.getOwnPropertyDescriptor(cat, 'name')
        Object {
          value: Fluffy
          writable: true
          enumerable: true
          configurable: true
        }
        

        To set a property of field use defineProperty

        Object.defineProperty(cat, 'name', {writable: false})
        var cat = {
           name: {first: 'Fluffy', last: 'LaBeouf'}, color: 'White'
        }
        

        To prevent writing of an object:

        Object.freeze(cat.name)
        
      3. enumerable

        If "enumerable" set to false, the property will not show up in any list

        Object.defineProperty(cat, 'name', {enumerable: false})
        
        console.log(Object.keys(cat))
        
        console.log(JSON.stringify(cat))//will not show enumerable-false items
        

        How to Loop Over Enumerables:

        for(var propertyName in cat) {
          console.log(propertyName+':'+ cat[propertyName])
        }
        
      4. configurable

        If configurable is set to true you cannot change enumerable attr or configurable attr, or delete it, but can change writable attr.

        Object.defineProperty(cat, 'name', {configurable: false}
        
    3. Getters and Setters

      Let's create a new property and add a get() and set() methods.

      var cat = {
         name: {first: 'Fluffy', last: 'LaBeouf'}, color: 'White'
      }
      Object.defineProperty(cat, 'fullName',
      {
      get: function() {
        return this.name.first + ' ' + this.name.last
      },
      set: function(value) {
        var nameParts = value.split(' ')
        this.name.first = nameParts[0]
        this.name.last = nameParts[1]
      }
      });
      console.log(cat.fullName); //"Fluffy LaBeouf"
      cat.fullName = 'Top Cat'; //"Top Cat"
      console.log(cat.fullName);
      
  3. Prototypes and Inheritance
    1. We can add methods to individual objects. Here we create a 'last' method to an object, arr.

      'use strict';
      var arr = ['red','blue','green']
      Object.defineProperty(arr, 'last', {get: function() {
         return this[this.length-1];}});
      console.log(arr.last);//"green"
      

      But wouldn't it be nice to add a method to all arrays. We can by adding a method to 'Array's prototype which is shared by all the Array objects. If an individual object doesn't contain a function, it looks to its "prototype" to see if it has that function.

      'use strict';
      var arr = ['red','blue','green']
      Object.defineProperty(Array.prototype, 'last', {get: function() {
         return this[this.length-1];}});
      console.log(arr.last); //"green"
      console.log(['sour','sweet','bitter'].last); //"bitter"
      

      All functions have a built-in property that points to another object called "prototype".

      var myFunc = function() { }
      console.log(myFunc.prototype); //[object Object] { ... }
      

      Objects do not have a prototype property but they have a __proto__

    2. function Cat(name, color) {
        this.name = name;
        this.color = color;
      }
      
      var fluffy = new Cat('Fluffy','White');
      console.log(Cat.prototype);//[object Object] { ... }
      console.log(fluffy.__proto__);//[object Object] { ... }
      console.log(Cat.prototype === fluffy.__proto__);//true
      Cat.prototype.age = 4;
      console.log(fluffy.__proto__.age);//4
      

      Important: A function's prototype is the object instance that will become the default prototype for all objects created using this function.

      Jim has a great example of linking two classes, "Animal" and "Cat" into an inheritance chain. It's kinda tedious and error prone. Be very careful. This is why Kyle Simpson prefers OLOO. (more on that later).

      'use strict';
      function Animal(voice) {
        this.voice = voice || 'grunt';
      }
      Animal.prototype.speak = function() {
        console.log(this.voice)
      }
      
      function Cat(name, color) {
        Animal.call(this, 'Meow');
        this.name = name;
        this.color = color;
      }
      Cat.prototype = Object.create(Animal.prototype);
      Cat.prototype.constructor = Cat;
      var fluffy = new Cat('Fluffy','White');
      console.log(fluffy.__proto__.__proto__);
      

      This prints:

      [object Object] {
        speak: function () {
        window.runnerWindow.proxyConsole.log(this.voice)
      }
      }
      

      The above code is a little cleaner with the new ES6 "class" structure:

      'use strict'
      class Animal {
        constructor(voice) {
        this.voice = voice || 'grunt'
        }
        speak() {
          console.log(this.voice)
        }
      }
      
      class Cat extends Animal {
      constructor(name, color) {
        super('Meow');
        this.name = name;
        this.color = color;
        }
      }
      
      var fluffy = new Cat('Fluffy', 'White');
      fluffy.speak();//"Meow"
      

JavaScript Scope

My notes from Plurasight's Advanced JavaScript by Kyle Simpson. His series of online books is available on github at github.com/getify/You-Dont-Know-JS.

A good reference site for JavaScript is the Mozilla Developer Network MDM at https://developer.mozilla.org/en-US/docs/JavaScript

A good place to learn styles of programming JS is

https://github.com/rwldrn/idiomatic.js.

The actual definitive spec is at http://www.ecma-international.org/ecma-262/5.1

  1. Details of Scope

    Scope: where to look for things

    JavaScript is not interpreted. Bash is interpreted. The JS compiler goes through all the code before starting execution. It finds declaration of variables and puts them into appropriate scope slots. Finds "var" and "function"s.

    JS has function scope only (kinda).

    lhs - left hand side (target)

    rhs - right hand side (source)

    "undefined" really means uninitialized

    Kyle has a good way of explaining scope: When executing a function "foo" and encountering a variable "foo", JavaScript asks the question: "Hey, scope of 'foo' do you have a lhs reference to variable x?"

    For a function declaration, the first thing in the statement is "function name()".

    function bar() { ... }
    

    function expressions should have names

    var foo = function bar() { ... }
    

    Why use named functions and not anonymous?

    1. Can refer to itself for recurse
    2. Can find it in minified stack trace
    3. Is self-documenting
  2. eval

    "eval" cheats lexical scope. putting an "eval" in your code, forces the engine to run slower in strict mode, code is more optimizable. Don't use eval unless absolutely necessary.

    For example, using setTimeout with a string of code is bad practive since it invokes "eval()"; better to use with a function:

    setTimeout("console.log('hi')", 1000); 
    setTimeout(function () {console.log('bye');}, 2000); //ok with func pointer
    
  3. "with" is "more evil than eval"
    var obj = { a: 2, b: 3, c: 4 };
    with(obj) {//implies an obj.
    a = b + c;
    }
    

    "with" effects the scope and in strick mode, you can't use "with".

Immediately-invoked-function-expression (IIFE) pattern

This is a widespread pattern in JavaScript with a history here: benalman.com/news/2010/11/immediately-invoked-function-expression. It forces a function to run immediately - hense the catchy name.

var foo = "outside";
(function() {
  var foo = "inside";
  console.log("foo:"+foo)//prints "inside"
})();

"let" (ES6) and block scope

"let" will set the scope of the variable to the "for" loop (or outer braces), not the function scope as "var" would have done. creates an implicit block on the "for".

function foo() {
  for(let i=0; i<var.length; i++) {
    ...
  }
}

warnings: "let" is not hoisted. If in the middle of an if statement block, the variable is only available after the "let". The area above the "let" declaration where the variable is not defined is called the "Temporal Dead Zone".

How to get a new scope? create a function, catch, or a brace with the "let" in ES6

"undefined" is a value, meaning a variable doesn't currently have a value.

"undeclared" never been declared. reference error.

hoisting

during compile phase, functions and then declarations are "hoisted" to the top.

var a=6;
function foo() { ... }

The compiler rearrages and hoists declarations to top

function foo() { ... }
var a;
a=6;

Why hoisting? mutual recursion would be impossible. like c header files.

The "this" pointer

Four Rules for how the 'this' keyword get bound.

Every function, while executing, has a reference to its current execution context called "this".

The value of the "this" pointer depends on the call site i.e., where the method gets invoked.

BTW, "this" is not like "this" in any other language.

  1. new

    "new" turns a function into a constructor call and sets the this pointer

    The "new" constructor does four things:

    1. A brand new empty object will be created
    2. The new object gets linked to another object
    3. The new object gets bound as the "this" keyword for the function call
    4. If the function doesn't return something, it will return "this"
  2. Explicit binding Rule

    .call() or .apply() take as their first arg "this"

    example of hardcoding

    function foo() { console.log(this.bar); }
    var obj = { bar: "bar" };
    var orig = foo;
    foo = function() { orig.call(obj); } //forces this context on foo
    foo.call(obj)
    

    utility to do hardbinding:

    function bind(fn,o) {return function() { fn.call(o); };}
    function foo() { console.log(this.bar); }
    var obj = { bar: "bar" };
    foo = bind(foo,obj);
    foo();//bar
    

    put bind on Function prototype

    if(!Function.prototype.bind2) {
      Function.prototype.bind2 =
        function(o) {
          var fn = this; //the original function
          return function() {
            return fn.apply(o,arguments);
          };
        };
    }
    var obj = { bar: "bar" };
    foo = foo.bind2(obj);
    foo("baz");
    

    ES5 has a builtin "bind"

    hardbind makes "this" be predictable
  3. Implicit binding Rule

    - object calling function becomes the "this"

  4. Default Binding Rule

    in strict mode, default "this" is set to the undefined value

    not in strict, "this" defaults to global, which in browser is window

Summary: Four Questions to determine what is the "this" object
  1. Was the function called with the "new" keyword? use that object.
  2. Was it called with .call() or .apply(). Use explicit object
  3. Was the function called with an owning object? use that.
  4. Default: global object (except in strict mode)

Details in his second book "You Don't Know JS.com" - chapter 2

Closure

Closure comes from Lambda calculus. Closure is when a function "remembers" its lexical scope even when the function is executed outside that lexical scope.

function foo() {
  var bar = "bar";
  function baz() {
     console.log(bar);
  }
  bam(baz);
}

function bam(baz) { baz(); }
foo();

//calling a method that returns a method that retains lexical scope (Closure)
function foo() {
  var bar = "bar";
  
  return function() { 
  console.log(bar);
  };
}

function bam() {
    foo()();  //call the returned method
}

bam();

The Classic Closure Example

for(var i=1; i<=5; i++) {
  setTimeout(function() {
    console.log("i: "+i);//writes 6 since i references outer scope
  },i*1000)
}


for(var i=1; i<=5; i++) {
  (function(i) {//use an IIFE
  setTimeout(function() {
    console.log("i: "+i);//writes 1,2,3,4,5
  },i*1000);
  })(i);
}

Classic Module Pattern

Two Characterics of Classic Module Pattern

1. Must have outer function wrapper

2. One or more functions that are returned that have closure over inner scope to access variables

example of module pattern

var foo = (function() {
  var o = {bar: "bar" };
  return {
    bar: function() {
      console.log(o.bar);
    }
  };
})();
foo.bar();
 
//example of modified module pattern
var foo = (function() {
  var publicAPI = {
    bar: function() {
      publicAPI.baz();
    },
    baz: function() {
      console.log("baz");
    }
  };
return publicAPI;
})();

foo.bar()

Object Orienting

OO Design Patterns

  1. Prototype

    Every single "object" is built by a constructor function.

    Each time a constructor is called, a new object is created.

    A constructor makes an object linked to its own prototype. (In class-based system, new objects are instantiations of the class)

    An object has a link called ".prototype" pointing to its prototype object. The prototype object has a link back to the object called ".constructor"

    A object has a private link called "[[Prototype]]" which points to its prototype object.

    Three ways to get a "new"'d object's prototype.

    1. A object in all browsers except IE has a public link called "__prototype__" (called DunderProto) which points to its prototype object.(in ES6, adopted in IE11)

    2. The standard way to get an object's prototype is "Object.getPrototypeOf(o)" (in IE9, ES5)

    3. For IE8 (ES3): o.constructor.prototype (but .constructor and .prototype are writtable)

    A function has a [[Prototype]] link (__proto__ in ES6 and all browsers except IE below 10) to the object "Function"'s prototype which contains "call()", "apply()", and "bind()".

  2. Inheritance

    Classical inheritance copys the class to make object. Instead use "Behavior Delegation".

    OLOO - Objects Linked to Other Objects

    function Foo(who) {
    	 this.me = who;
    }
    Foo.prototype.identify = function() {
      return "I am " + this.me;
    };
    function Bar(who) {
    	 Foo.call(this,who);
    }
    Bar.prototype = Object.create(Foo.prototype);
    Bar.prototype.speak = function() {
        alert("Hello, " + this.identify() + ".");
    };
    var b1 = new Bar("b1");
    b1.speak(); //alerts: "Hello, I am b1."
    
    //a slight improvement
    var b1 = Object.create(Bar.prototype);
    Bar.call(b1,"b1");
    b1.speak();
    
    //moving forward
    bar b1 = Object.create(Bar);
    b1.init("b1");
    b1.speak();
    

Monday, November 13, 2017

Building Beautiful RESTful APIs in .Net Core - Adnug November Meeting

Nate Barbettini talked to 26 developers at the Austin .Net Users Group.



Here's my notes from his talk:
RPC (Remote Procedure Call) vs. REST (REpresentational State Transfer)
RPC endpoints are verbs (api.example.io/getUrser), in REST endpoints are resources (/User)

HATEOAS - hypermmedia as the engine of application state
Data exchange should be treated as if the client were a web browser
No documentation or out-of-band info needed

Use RPC when dealing with a simple object, like the Slack API
Use REST when dealing with lots of different objects with CRUD operations possible on all of them
GraphQL is useful when trying to traverse a graph of REST calls

REST + JSON is popular but hard because no schema is available for the data.
HAL and JSONSchema try to solve that.  Also Ion trys to give structure to JSON data.
The Ion content type is application/ion+json
Ion data is embedded in JSON with fields, like "href","rel", and "value".

ASP.NET Core is a full rewrite of .Net to be portable, faster and cleaner.
Dependency Injection is now in the framework, don't have to download StructureMap

Nate has a free eBook about starting with .Net Core at https://www.recaffeinate.co/
Demo to create new .Net Core API with Web API template.  No diff between MVC or API controllers.

Nate used package Entityframework inmemory to do development of demo.  inmemory just gives an inmemory

ASP.NET Core is friendly to async/await pattern.  async first philosophy.

@nbarbettini
 https://www.recaffeinate.co/

Nate has a Lynda.com course on this.
https://github.com/nbarbettini/BeautifulRestApi is the result of his 4 hour Lynda course.

Nate has done a great job of learning .NET Core and sharing his knowledge with the community free of charge.



Wednesday, November 01, 2017

Just Switched Electrical Providers to Our Energy LLC Using GeekYourRate.com

In Texas we can choose our electrical provider.  PowerToChoose.org is a website to help consumers pick a provider, but it's very difficult to find which provider is the cheapest for me, since providers have a complex rate structure.  I did a previous blog post on choosing an electric provider.

I've found a great website, GeekYourRate.com, which cuts through all the weird pricing structures and shows the best rate for your usage.  The service costs $10, but it saved me $150 for the next year.
GeekYourRate.com said my cheapest option was Our Energy LLC, so I switched.   I had not heard of them before,  but will let you know how they work out.

Be careful not to just continue with your "current" provider when your time is up.  I think they are tempted to offer you a decent plan, but not the best, since it's such a hassle to change.  When I called my provider, and said that I had found a better rate, they offered me a better rate plan than what was mailed to me to renew. 

Here's my results from GeekYourRate.com:

 

Saturday, October 28, 2017

Investing 101 for Beginners - How to Make a Fortune in the Stock Market


The Basics of Investing


Preface:

You can work hard all your life, put lots of money into your savings, but can still be poor in retirement if you don't invest well. 

You will learn three things from this article: how much to invest, how often, and where.

How much money do you need to retire?

The Four Percent Rule. Let's start at the end.  How much money do you need to retire?  The rule of thumb is that you can take out 4% of your money every year for your retirement. So if you need $40,000 dollars a year from your savings (in addition to Social Security), you will have to save one million dollars by retirement age. That's a lot of money and it doesn't just happen.

How much to save?

As a general rule you should try to save 10-15% of your salary for retirement. This 10-15% includes what your employer contributes. The following table shows how far along you should be at a particular age to be on track.

AgeWhat multiple of your salary should you have already saved
301x
403x
506x
608x
6710x

Basics of Investing - Risk

You get paid for taking risks. Since inflation historically runs about 2% a year, if you put your money in the bank in a risk-free FDIC insured account at 0.05% interest, you are losing 1.95% every year.  You need to take more risk to earn more money.

What is the stock market?

When you own a stock, you own a little piece of the company.  When the company makes a profit for the year they will pay you a tiny part of that profit which is called a dividend.  If a company has a million stock shares, and you own one stock, you will get one-millionth of their profits for the year.  (Although some companies don't pay dividends, but let their stock price rise instead.  Either way you can make money).

Stocks are risky.

If the company does not do well, your stock value can go down. If the company goes bankrupt you can lose all the value of your stock. But with greater risk comes great reward. The US stock market averages about 8% returns a year.

What are stock funds?

Stock funds are composed of other stocks.  It's like a basket of stocks; when you buy a stock fund, you buy a small piece of multiple stocks.




Managed and Index Funds

Two types of funds exist, managed and indexed. Managed funds buy stocks based on the opinion of a highly paid stock picker and have a high fees (0.5-2%). Index funds buy stocks based on an list, like the 500 biggest companies in the stock market (the S&P 500 list) and have a low fee (0.03%).

Index funds will almost always beat managed funds because managed funds have a higher expense. Over the decades this tiny difference can amount to tens of thousand of dollars.

Don't invest in individual stocks, buy index stock funds.

Individual stocks are fun to play with, but they're too risky to invest serious money.

You can buy stocks and bonds online at a company like Vanguard or Schwab. You create an online account and transfer them money. They will buy and hold the stocks and bonds for you. It's easy and you can do it all online.

What are bonds?

Companies borrow money from investors by selling bonds. They are like government savings bonds - you buy them now and can redeem them later with interest and sometimes with quarterly payments. While bonds earn interest and are safer than stocks, they don't earn as much potential profit.
When a company goes bankrupt the bond holders get their money before stock owners, less risk so less reward.  Government bonds are the safest investment, but only pay about 2-3% returns.
The biggest risk with bonds is that inflation will rise, making your bonds worth less.

How to Mitigate Risk: Diversify

You should own a mix of stock index funds and bonds in case the stock market goes way down.
What percentage in stocks? One rule of thumb is to have the percentage of stocks the same as 120-your age.  So at age 50 you should have 70% of your savings in stocks, both domestic and foreign.  When you are 20 you should be 100% in stocks, since you have a lot of time to ride out the ups and downs in the market.
You should also diversify over countries.  Have some stock and bonds from other countries.
 John Bogle, founder of the Vanguard Group which champions low cost index funds, recommends this proportion of funds:
40% in the Vanguard Total U.S. Stock Market Index Fund
20% in the Vanguard Total International Stock Market Index Fund
40% in the Vanguard Total Bond Market Index Fund

Dollar cost averaging and beating the market.

Trying to invest money in the stock market when you think it is low, and selling when you think it is high almost never works. Instead, invest a little every month and ride out the storms.
The S&P 500 in 2008 was around 1,500 and dropped to 700.   Many of my friends panicked and sold their stocks and lost half their investments.  If they would have been patient, they would have made all that money back and then some since the market has recovered to 2,700.

Real Estate.

You can invest in real estate, but it takes time to manage.  Your own home is typically good investment.  Another way to invent in real estate is a Real Estate Investment Trust (REIT), which is like a stock, but it invests in real estate like apartments, shopping malls, office buildings, and homes.  This is the simplest way to own real estate.

Invest in ways that minimize your taxes.

Your work will typically have a Retirement 401(K) plan that will allow you to invest money without it first being taxed.  They will typically match a part of your investment, like the first 4% of your salary.  You get to deduct the amount you invest from your income so you don't have to pay taxes on it, yet.  When you retire you will pay taxes when you pull money out, but the money has grown for 30 years without paying taxes.
If your company doesn't offer a 401K, you can invest in an IRA, or a ROTH account. If you invest in an IRA, it is untaxed going in, allowing for compounding interest on the full amount you put in, but taxed when you withdraw money from the account. If you invest in a ROTH account, money is taxed going in, but considered non-taxable income when withdrawn.

In Summary

Save 10-15% of your salary for retirement every month.  Use stock index funds and bonds. Join your company's retirement plan. Your 65 year old self will thank you. 

Miscellaneous Tips:


  1. Don't buy toys on credit.
  2. Credit cards carry a huge interest rate and you will be losing money every time you have a balance at the end of the month.  Pay your credit cards off at the end of the month.  Buy clothes at Goodwill, eat frugally, take local vacations, drive an old car, use a Windows computer - do what you must, but never carry a balance on your credit cards.
    Your home and cars are the only things you should normally buy on credit.  Save up your money so you don't have to buy cars on credit.
  3. Watch your expenses.

  4. Do you really need a $5 cup of coffee every morning, a $10 lunch and a $20 dinner?

  5. Use a Credit Union

  6. Banks are made to provide profits for their owners, the stockholders.  Banks try to wring every last cent from you.  Credit Unions belong to the depositors and hence try to serve you, the depositor.  That being said, sometimes banks can provide services like ATMs everywhere that make them worth a second look, but remember banks live to make other people money.
    Bankers circling to figure out ways to charge you more fees

  7. Beware your broker and financial advisor

  8. Your stockbroker or financial advisor (unless they are fiduciaries) may not have your best interest at heart. They may steer you towards funds that are not as good as other funds, but pay a commission to your advisor.
    Don't let your broker run up trading fees that make her money, but cost you money.  This shouldn't be a problem since you are invest in index funds.  Right?
After saving 15% of you income every year for 40 years in an Index stock fund, you will have a fortune in the stock market.

Friday, October 20, 2017

Agile Austin Lunch Session - How Microsoft VSTS went from 18 mo Releases to Three Week Sprints

Clementino de Mendonça presented to 20 Agile Austiners on the topic "Agile at Scale SIG - Agile Transformation at Microsoft TFS/VSTS team".

Microsoft uses 3 week sprints with releases to different test environments named  Ring 0 to Ring 6.
Releases every 3 months, could be more frequent, but customers don't want it more frequently.

Microsoft's internal release notes for VS are available at https://www.visualstudio.com/team-services/release-notes/

How did they go from 18 month release cycle to daily?

In the old days it took 3 months of planning to organised details for the next release.  Developers coded in two week sprints, with stand ups.
The lower levels were Agile, but the upper level planning was not.

Before and After Microsoft Transformations:
4-6 month milestones to 3 week sprints
Personal offices to Team rooms
Long planning cycles to Continual Planning and Learning
Feature Branches to everyone in master branch with tags
Secret road map to publicly shared road map on the web
100 page specs to specs in PPT
Private repos to open source publicly available
Deep hierarchy to flat hierarchy

"Culture eats strategy for breakfast", Peter Drucker

Microsoft at one time was cutting edge on Agile (Ken Schwaber studied them for the creation of Scrum), but then lost it.

Microsoft looked at Pink's book "Drive". People want Autonomy, Master, and Purpose.
Youtube video summary here,

Alignment and Autonomy balance.  Too much alignment and the autonomy falters.

MS changed Roles.  In the old days developer testers were considered lower quality.
Used to have Program Management, Dev, and Testing.  Now only PM and Engineering.  Everybody writes tests - no separate testers.

Full list of roles:  UX / User Experience / Program Management / Engineering / Service Delivery.

PM is responsible for What and Why building.  Engineering is responsible for How.

Teams are cross discipline, 10-12 people, self managing, clear goals, lasts for 12-18 months, work in physical team rooms.
The team owns the features in production and own deployment.  So if problems occur, that team fixes the problem.

Instead of doing horizontal layers: teams doing UI other team doing API, others doing business logic.
Now, one team does all of those things in a vertical slice.

Autonomy - let teams choose what they want to work on.  They must train successors.  aka.selfformingteams

800 people working on VS.  Teams stay in contact with each other using "Sprint Mails" detailing what they did, and what they plan to do next sprint.  This is instead of big meetings.


Lightweight Planning: Sprints in 3 weeks, Plan in 3 sprints, Season is 6 months, and Strategy in 12 months.

Strategy / Features / Stories / Tasks.  Strategy comes from top down.

Backlog is taken from UserVoice, community requests and voted on.

Stabilization sprint used to happen.  Devs would push bug fixes to stabilization sprint instead of fixing immediately.

Bug Cap is total number of bugs to have, it's five times number of devs.

Takeways
1. Get good a science, but don't be overly prescriptive.
2. Stop celebrating activity, start celebrating results.
3. Embrace the new normal.  Delivery continuously.
4. You can't cheat shipping.  Putting into production.
5. Build the culture you want ... and you'll get the behavior you're after.





Wednesday, October 11, 2017

Agile Austin, Oct 10, 2017 - Rage, Disorder, and Your Agile Team

At Agile Austin this month Matt McBride from Genesis10 talked about Agile team culture at Kasasa.com headquarters.
He wrote Leadership Patterns for Software and Technology Professionals.


My random notes:
* Rage, Stress, and Its Impact
Rage is one specific behavior that our teams are exposed to in their daily lives.
Rage and anger are types of stress.  Rage and anger are specific instantiations of the "abstract class" named stress.

What is impact of stress?: Health, Behavior, and relationships with our team members.
Stress can be a good thing for short periods.

Cortisol is the chemical produced during stress.  Long-term Cortisol can shrink your brain.
Sources of Anger: disappointment, frustration, judgement, rejection fear.

Anger make people indiscriminately punitive.

* Threat of Architectural Disorder
complexity grows at an exponential pace
Nonfunctional features (security, testing ...) tend to get lost or ignored
Grady Booch on characteristics of successful projects: strong architectural vision and well-managed iterative cycle (scrum).

Major Classes of Program Features to  Think about At the Beginning
1. Significant features for business/customer
   We look for functional features with a high business risk
2. Architecturally significant features
    We look for functional and non-functional features with high technical risk.

From Agile we need to embrace these:  commitment, courage, focus, openness and respect.

Missing in college education is leadership skills.  How to educate non-technical customers.


Leadership:
1. Leaders are avid learners.
Leaders try, fail, learn, and grow.
2. Leadership is a choice.
We have to change the way we think about ourselves, careers, and work.

* Tactics for reducing anger, rage, and stress
Have everyone take one deep breadth
Then followup with a specific goal

Partnership Pattern - moving forward from newbie to Trusted Partner
1. Performance - establish record of success
2. Credibility - be trusted based on performance
3. Trust -
4. Become a Trusted Partner

* The resolute pattern
Recast problems as assumptions to be questioned

Invest in your team and in development of leadership and interaction skills.




Monday, October 09, 2017

Continuous Integration and Delivery with Visual Studio and Azure - Austin .Net Users Group October 9, 2017

Shawn Weisfeld (Shawn is also founder of http://UserGroup.tv) created and deployed a project from scratch at the Austin .Net Users Group. VisualStudio.com gives us a free repository. You can create a project stored in the cloud. Team Services is free for teams under 5 people. Visual Studio online and VS local easily share a repository by default. ms.portal.azure.com will host. You chose how many machines and the size of the machines. Azure will create a load-balancer for the cluster. Azure will automatically collect telemetry. Changing the environmental variables on production is easy - you don't have to create multiple versions of the web.config for each environment. Shawn showed how it really is insanely easy to deploy an app in Azure to cluster using VisualStudio.com, Azure.com and a local copy of Visual Studio 2017.

Saturday, September 23, 2017

How to Embed a JavaScript Script in a Handlebars Template

While working on the next generation of ThePresidentsOftheUnitedStates.com, a site I'm working on with my 10yr old daughter, we needed to add a little bit of JavaScript in the template to add the ordinal suffix to a number (e.g., "3rd President").

It's easy enough to do by registering a Handlebars helper and calling it with "{{{nth number}}}". Here's the code in context:

<header>
<h1>The Presidents Of The United States</h1>
</header>
<script>
Handlebars.registerHelper('nth', function(n) {
  return new Handlebars.SafeString(
    ["st","nd","rd"][((n+90)%100-10)%10-1]||"th"
  );
});
</script>
<script id="entry-template" type="text/x-handlebars-template">
{{#each presidents}}
<div class="row" itemscope itemtype="http://schema.org/Person">
  <div class="col-sm-4 centered" > <!-- left panel -->
  <h2 class="name" itemprop="name">{{fname}} {{mname}} {{lname}}</h2>
  <p class="ordinal">{{number}}{{{nth number}}} President of the United States</p>
Special thanks to Tomas Langkaas from stackoverflow for the "nth" algorithm in JS.

Tuesday, September 12, 2017

Git Examples and Notes


Every programmer has a git cheatsheet. This is mine.

What is Git?

Git is a distributed version control system written by Junio Hamano and Linus Torvalds. Linus said the name came from British slang for an argumentative, pig-headed person.

Where to Get Git?

You can download Git from git-scm.com/downloads

Tools

You can download a visual GUI for Git at www.atlassian.com/software/sourcetree.
"gitk" is a handy GUI tool installed with some systems.
"git citool" will open a GUI tool.

Documentation

Scott Chacon's and Ben Straub's book, ProGit is downloadable for free at git-scm.com/book/en/v2
Atlassian has a great tutorial at www.atlassian.com/git/tutorials
An interesting interactive visual demo of Git is at https://onlywei.github.io/explain-git-with-d3/#push
Git from the Bottom Up: https://jwiegley.github.io/git-from-the-bottom-up/
A great interactive cheatsheet: http://ndpsoftware.com/git-cheatsheet.html

Editors note: I will use <hashid> and <commit> interchangably. In some contexts it's clearer to use <hashid>. Both refer to a 40 character SHA-1 number, like 74c195b84d7ec5fcf2bc9d59b83b7b538c8ec60d.

Getting Started By Downloading Repository

git init
creates .git directory and machinery to create a git repository
git clone https://github.com/jquery/jquery.git
clones (copies) repository and creates a local repository with all the commits, trees, blobs, branches, and tags, but not the remotes. For that use the "-mirror" switch.
git clone <repository> <new-repository>
Goes to the directory above your working directory and then issues this command to create a copy of your current repository.

Saving Your Basic Information For Future Sessions

"git config" allows you to put information into the "~/.gitconfig" file for future sessions. This tells "git" things like your name and favorite editor.
git config --global user.name "Mitch Fincher"
git config --global user.email "me@myhost.org"
git config --global core.editor vim
git config --global help.autocorrect 1
git config --global color.ui auto
git config --global --list

Saving Aliases into ~/.gitconfig

You can create aliases to reduce typing. This next command will create a shorthand for "cma" which will add a line to your ~/.gitconfig file.
$ git config --global alias.cma "commit --all -m"
$ git cma "adding twelve.txt"
You can also edit your ~/.gitconfig file directly and create multi-line scripts that take parameters. The alias below takes a branch name, checks it out, and then merges in the previous branch. You would invoke it like this: "git qm master".
#quick merge
qm = "!f() { branchName=$1; git checkout ${branchName} && git merge  @{-1}; }; f"

Background: Git has Four Object Types

Git Database has four types: Blobs, Trees, Commits, and Annotated Tags
  1. Blob A "blob" is a compressed file with no meta-data. All the meta-data, like name and time-stamp, are stored in a tree. The blob, may, and probably is, referenced by many trees.
    A blob has a header record consisting of it's type, "blob", a space, the number of bytes, followed by a null byte. Git prepends this to the contents of the actual file and then calculates it's SHA-1 hash value. Git then compresses the contents using zlib and writes it to the file system using the hash value as the name into the .git/objects/directory.
    To create a hash from a string or file use the "git hash-object" command.
    To play with this online visit https://www.movable-type.co.uk/scripts/sha1.html
  2. Tree A "tree" contains references to blobs and other trees. Using "ls-tree" we can see the tree inside a commit.
    $ git ls-tree master
    100644 blob 1f0d9c362b635efe4f321a92d6715f00530ed2b2    greetings.txt
    100644 blob 586d7ab779c9fcd2d171ca012a6186144beea379    one.txt
    040000 tree 584d1a5650d8b2f5d7c71f1373c884569cad0cbf    spanish
    100644 blob 4adbf7c8f4d643d31800f4b3780c602ed455f993    three.txt
    
    Each line contains a mode, hashid, and filename.
    Git uses three modes for blobs, 100644 is a normal file, 100755 is an executable file, and 120000 is a symbolic link.
    We can also use "ls-tree" to look directly at a tree, like the one above,
    $ git ls-tree 584d1a5650d8b2f5d7c71f1373c884569cad0cbf
    100644 blob c5027f551f410b6a2438b285fd0cebff4c05cef4    greeting.txt
    
    The argument to "ls-tree" is a "tree-ish" object - either the hashid of a tree, or an object that can easily have a tree extracted from it, like a commit.
  3. Commit A "commit" is a snapshot of the working tree. It contains a tree hash, usually a parent, an author and committer info, date info, a blank line, and finally a commit message.
    git cat-file commit 107b22915830203ff40d0d8dfbf8e950961bc490
    tree 21e1ce7f2888871854a6a54e7f37853626331eb9
    parent cedfd274bc5fc6ed23baf7eb93d81a5f0e8416e8
    author Mitchell Fincher  1507926633 -0500
    committer Mitchell Fincher  1507926633 -0500
    
    adding spanish
    
    You can try this yourself, use "git log" to get a recent hash.
    Commits only reference parents, never children.
  4. Annotated Tags Unlike a lightweight tag, which is merely a reference to a commit, an annotated tag is an object. To create one, use the "-a" option.
    git tag -a rel1.1 cedfd274bc5fc6ed23baf7eb93d81a5f0e8416e8
    
    git will present you with the option to add a description, like "release 1.1".
    To see the new object reference to rel1.1, cat the file contents:
    $ cat .git\refs\tags\rel1.1
    ad9fea1c206def283ba24549ddf1979703f176a2
    
    Use cat-file to inspect the comments:
    git cat-file -p ad9fea1c206def283ba24549ddf1979703f176a2
    object cedfd274bc5fc6ed23baf7eb93d81a5f0e8416e8
    type commit
    tag rel1.1
    tagger Mitchell Fincher  1508168418 -0500
    
    release 1.1
    

Deep Magic Commands

Git has "porcelain" commands, pretty things to be seen and used by everyone like these: "git add", "git commit", "git push", "git pull".
Git also has many "plumbing" commands to be used by experts and the Morlocks who keep the Eloi happy, like "git cat-file", "git hash-object", "git count-objects". The porcelain command under the covers and made up of the plumbing commands.
A little background, git uses SHA-1 which reads any file or string and returns 20 bytes which is 40 hex digits. You can convert a string to its SHA1 hex value using hash-object.
echo "The fault, dear Brutus, is not in our stars" | git hash-object --stdin

2e72face651312aa9a4232ba28c1c1871cee403f
You can write a string into git's database by using the "-w" option.
 echo "The fault, dear Brutus, is not in our stars" | git hash-object --stdin -w 
 2e72face651312aa9a4232ba28c1c1871cee403f

$ ls .git\objects\2e\72face*
.git\objects\2e\72face651312aa9a4232ba28c1c1871cee403f
Git will write objects to its ".git/objects" directory. This is why git is called a "content-addressable file system".
Use "git hash-object <filename>" to show the SHA-1 hash created by the contents of <filename>.
A great course on the internals is https://app.pluralsight.com/library/courses/how-git-works/table-of-contents

Plumbing commands:

git cat-file -t <object>
returns the type of the object: "blob","commit","tree", or "tag"
git cat-file <type> <object>
shows the contents of the file associated with the object.
git cat-file blob 5f831d630dd069aca58b3a164ff526b53c142456
'Hello, world!'
git cat-file commit <commit>
shows the contents of a commit e.g., "git cat-file commit a7cc8785c5bd3594c9659b779ed56487097281b4":
tree a822836950b935d16f8ee572429dd1d754a85a73
parent 640f3af9f9e883a5ed0102154e453170f04267b7
author Mitchell Fincher <mitchf@fincher.org> 1505507251 -0500
committer Mitchell Fincher <mitchf@fincher.org> 1505507251 -0500

texas greeting
  
The object does not have to be the "type" requested, if the "type" can be easily deferenced from the object, like asking for a commit from a "tag".
$ git cat-file commit v4.0
tree 71dea1ed6141af0acfc61e8e145612255924723d
parent 888c1884b879ef34da6685ac0158c0e3f830d040
parent 872c5443db65734a507201b514b3ae80c6c32d2c
The pretty-print, "-p" option, will do its best to determine the type of object you want. Here we look at an annotated tag:
$ git cat-file -p v4.0
object d4c9f8f2fe56221002113dcfcb7082913d7e9921
type commit
tag v4.0
tagger Mitchell Fincher  1509128823 -0500

    added four.txt
Here we use the magic "-p" option to look at a tree:
$ git cat-file -p 9a9e2826ac8ed12d43ca0a23fbe5c26af4e64bab
100644 blob b5244467192a2cb0e94b6cee014d007a1b439b68    README.md
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    eight.txt
100644 blob 5bace8b1cedb799bfac944e5a65fd301de784700    eleven.txt
git ls-tree <tree-ish>
shows all the objects in the "tree-ish" object. A tree-ish object can be a tree or an object that contains a tree.
$ git ls-tree master
100644 blob 5f831d630dd069aca58b3a164ff526b53c142456    greeting
100644 blob b6d99afd00cffebbd33e6a99c946e4bf54319ec4    texas
   
git ls-files
shows info about the files on the stage (index) and the working directory. It does not distinguish between the two.
git rev-parse HEAD
HEAD is usually a reference to a branch which contains a pointer to a commit. "rev-parse" derefences the branch and shows the hash id of the commit without having to go through the intermediate branch.
$ git rev-parse HEAD
3a9aae03b78bb617e9ec7822289e4a991ea7c2d8

$ cat .git/HEAD
ref: refs/heads/master

$ cat refs/heads/master
3a9aae03b78bb617e9ec7822289e4a991ea7c2d8
git write-tree
takes the contents of the index (staging area) and creates a tree.
echo "My comment" | git commit-tree <hashid>
commit-tree takes a tree hashid and creates a commit from it. By suppling the comment, git can do its work without asking you for it.
git update-ref refs/heads/master <hashid>
writes the hashid into the file refs/heads/master, making that commit hashid the current one for the branch master. This could be done manually:
$ echo <hashid> > .git/refs/heads/master
You can also use update-ref to create a branch manually. To create a new branch pointing to a hash you enter:
git update-ref refs/heads/rel2 eafd8ea4563a802f6ad9dd65bf1222689785af26
git symbolic-ref HEAD
shows the value of the HEAD reference
$ git symbolic-ref HEAD
 refs/heads/develop
git symbolic-ref HEAD refs/heads/master
sets the value in .git/HEAD to be "refs/heads/master". You can try to do it manually with "echo", but it may put a space at the end of the line. To do this manually you should edit in your fav editor.
echo ref: refs/heads/branch1> .git\HEAD
C:\Users\mfincher\gittest (master ) #notice space at the end

What's in Them Files?

When you create a new repository with "git init", git will create a new directory, ".git" which is kind of a parallel dimension storing all the info that git needs. If you are into "Stranger Things", the .git directory is the "upside down" world. ".git" contains these directories and files:
$ ls -F
HEAD         description  info\        refs\
config       hooks\       objects\
HEAD
This file typically contains one line of textshowing the branch currently checked out. Usually this a symbolic link like "ref: refs/heads/develop", but can point directly to a commit hash.
description
you can edit this file to describe your repo
config
contains project specific settings and the URLs for your remotes, e.g., where does "origin" point.
info
keeps a list of file patterns to ignore.
C:\Users\mitchf\gitdemo2\.git (master)
$ cd info

C:\Users\mitchf\gitdemo2\.git\info (master)
$ ls
exclude

C:\Users\mitchf\gitdemo2\.git\info (master)
$ cat exclude
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
hooks
contains hook scripts
index
a single file which is a mini-file system containing all the staged changes

objects
stores all the contents of your repository objects in "blobs". The files do not have names. Their SHA-1 hash id is their name. The file's real name is stored elsewhere in the tree. So, if two files have the identical contents, the contents will be stored only once. The two trees will point to the exact same file because Git is a content-addressable file system.
Inside the objects directory is the top layer of subdirectories named the first two characters of the SHA-1 hash. Underneath that directory are files named with the remaining 38 character hash.
$ ls objects
1f    48    5e    76    81    93    ab    ce    f3    pack
3f    4a    64    7a    8a    99    bb    d6    f4
47    58    6f    7c    8e    a6    cd    ea    info

$ ls objects/lf
025359eec40ca200dc800f72ff78f6153a502a  82622d17e916d82b3badc38fbe8e025cd4a7d9
1621638abb2fbf888eef4e5a9314e74e055e99  8d7e0431e16b7f03856a5a9ac874d1bea13bcb
1af4d5ff8194432203d4bfd7b78f07426d8362  905c34c72391fa21571b54dd1a8d2711186975
refs
stores reference pointers to objects like heads, remotes, stashes and tags.

Tags

Tags come in two flavors: annotated and lightweight.

Lightweight tags

A lightweight tag is an immutable reference to a commit, it simply points to that commit. It gives the commit a warm, friendly name.
  #create a lightweight tag
$  git tag v6.0
  #look at the file contents created, it points to a commit
$ cat .git\refs\tags\v6.0
  058af196673b01337e8da091fc9da7acf703043d
  # see that it is the same hash as the master
$ cat .git\refs\heads\master
  058af196673b01337e8da091fc9da7acf703043d
  # later you can come back to this commit
$ git reset --hard v6.0
You can manually create a tag using update-ref
  git update-ref refs/tags/rel1.0 107b22915830203ff40d0d8dfbf8e950961bc490
git tag
shows all tags
$ git tag
rel1.0
v6.0
Annotated tags are real objects and are described here.
Ok, enough of the theory, let's move on to some useful commands.

checkout

git checkout <branch-name>
sets the current branch to <branch-name>. This updates the index, the files in the working tree, and sets the HEAD pointer (.git/HEAD) to the branch. Modified files in the working tree are not updated.
git checkout -b <new-branch>
creates a new branch from the current branch. It is equal to doing a "git branch <new-branch>" and then a "git checkout <new-branch>"
git checkout -- <file-name>
checkout a copy of <file-name> from the current branch and overwrite the working copy.
git checkout -- .
remove all the unstaged changed files and replace with original.
git checkout <commit>
detachs the HEAD and sets directly to this commit, "git checkout 661d5fd32".
git checkout -
checkout previous branch used
git checkout <branch-name> -- <path-to-file>
command to checkout a specific file from another branch
git checkout -- <mydeleteddirectory>
if you delete a directory and want git to fetch it back, "git pull" will not do it. It just transfers commits between remote and local. You need to do this instead.

What's Going On?

git status
shows your current branch and any interesting files
git status --short
shorter list with first two cols significant
git config --list
shows the combined settings from four files: .git/config file, ~/.gitconfig, /etc/gitconfig, and $XDG_CONFIG_HOME/git/config.
git log
see the changes
git log --oneline
to show quick one line summary
git log -2
see last two changes
git log --pretty=oneline
will not show commits that are reset over
git log --pretty="%h | %d %s (%cr) [%an]"
You can create your own log format using items like "%s" which is the "subject". Complete list of codes is at http://git-scm.com/docs/pretty-formats.
git log --pretty="%Cblue%h%Creset | %Cred%d%Creset %s (%cr) %Cgreen[%an]%Creset"
you can spice it up with color
git log --oneline --graph --decorate --all
pretty graph
git shortlog -sn
shows how often people are committing
git show head
shows last commit
git show HEAD~1
shows previous commit
git show-ref master
shows all commits for branches with master in name

Branch



git branch <new-branch>
creates new branch named <new-branch>. Typically you would then enter "git checkout <new-branch>" right afterwards. Or you can do both in one command with "git checkout -b <new-branch>".
git branch
shows all your local branches
git branch -r
shows all remote branches
git branch -a
shows all remote and local branches
git branch -vv
shows a verbose listing of the last commit on each branch
git branch --merged
shows all branches that have been merged into HEAD
git branch --merged master
lists branches merged into master
git branch --no-merged <commit>
lists branches that have not been merged i.e., list branches whose tips are not reachable from the commit (HEAD is used if commit not specified)

Misc

list of files needed to be merged
git diff --name-only --diff-filter=U
git remote update origin --prune
removes from your local list the branches that have been deleted in origin
git ls-files
shows all the files in the repo
git ls-file "**/<file-name>"
gets the full path to file
git fsck --full
checks for a corrupt git database. It will find things like a tag reference to an object that has been deleted.
git whatchanged
shows what has changed
remove all files from the stage:
git reset HEAD -- .
unset user password:
git config --global --unset user.password
remove permantly file from git and the file system:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch news/2021/2021-04-02-IMG_4423.jpg_original' --prune-empty --tag-name-filter cat -- --all
see what files you are about to push:
git diff --stat --cached origin/master
unstage files:
git reset

Help Me

git help
gets general help
git help config
gets help on the "config" command

Adds and Commits

git add <filename>
adds file to the stage
git add <file1> <file2>
adds multiple files to the stage
git add '*.txt'
adds using wildcards, but must have single quotes
git add --all
adds all new, deleted or modified files
git add -A
stages All files, same as "--all"
git add .
stages new and modified, without deleted
git add -u
stages modified and deleted, but not new files
git commit -m "my commit message"
commits staged files to repo

Deleting and Renaming Files

git rm <filename>
deletes the file and stages the deletion
git rm --cached <filename>
removes the file from version control, but keeps it on the filesystem.
git rm '*.txt'
stages the removal of all the txt files
git mv <filename> <new-filename>
renames a file

Remoting

git remote
returns the name of the current remote site
$ git remote
origin
git remote -v
use the verbose option to see the URLs for reading and writing
$ git remote -v
origin  https://mysite.com/myproject (fetch)
origin  https://mysite.com/myproject (push)
git remote show origin
shows the URL for "origin" and remote branches.
git remote set-url origin git@github.com:User/UserRepo.git
changes a remote location's value
git push -u origin master
pushes your code to the master branch of the repository defined by "origin"
git remote rename <old-name> <new-name>
renames the remote
git remote remove <branch-name>
removes branch-name from your list of remotes
git remote add <remote-name> <URL>
adds a new remote reference. This will add a section in your "config" file to map the name to the URL. For example after adding "demo" with "git remote add demo https://github.com/fincher42/demo.git", "config" contains:
[remote "origin"]
        url = https://github.com/fincher42/demo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
The value of "fetch" is called the "refspec". It has an optional "+" (directing git to update the reference even if it's not a fast-forward) followed by <src>:<dist>. "src" is a reference to where the data is on the remote site and "dist" is where to put the files locally.

What's Changed - git diff

git diff
shows only unstaged changes, defaults to HEAD
git diff --staged
shows only staged changes (those in the index)
git diff --cached
"cached" is an alias for "staged"
git diff HEAD
shows staged and unstaged changes
git diff <branch-name>
see changes between current branch and <branch-name>
git diff origin/master
see the difference with the origin master branch
git diff <commit>
see changes from <commit>
git diff HEAD~1..HEAD
see changes from previous commit
git diff --name-only <branch-name>
show only the names of the files that have changed

Reset

"reset" is confusing because it can edit the references, the index (stage), and trees. Note: "--mixed" is the default.
What do the reset options affect?
reset optionworking treeindexHEAD
--softX
--mixedXX
--hardXXX
git reset HEAD
remove changes in the index ("the stage") and resets the HEAD pointer. (default option is "--mixed")
git reset -- <filename>
removes <filename> from the staging area, but preserves its contents in the working directory
git reset --soft HEAD^
moves HEAD to its parent, leaving the index and working directory files untouched.
git reset HEAD files...
unstages the <files>
git reset --mixed HEAD^
moves HEAD to its parent, and resets the index, but leaves the working directory files untouched.
git reset --hard HEAD^
moves HEAD to its parent, and resets the index and the working files. (You should do a "git stash" before doing this reset)
git reset --hard eb70244
sets HEAD to specific commit and erases all changes in current working tree. Use "git checkout eb70244" to preserve changes.
git reset --hard eb70244
sets HEAD to specific commit and erases all changes in current working tree. Use "git checkout eb70244" to preserve changes.
git update-ref HEAD HEAD^
same as "git reset --soft HEAD^"
git reset HEAD^
undoes last commit and leaves files ready to be staged
git reset origin/master
restores to origin's version
If you need to rollback the origin's HEAD to a previous version (e.g., 12cdeee in master), you can do it this way:
git checkout master
git reset --hard 12cdeee
git push -f origin master

Revert

Revert will reverse the effects of previous commits.
git revert <commit>
removes the changes created by <commit>
git revert HEAD~5
removes the changes from the fifth last commit and creates a new commit to reflect that.

Merge

git merge <feature>
merges changes from the <feature> branch into current branch. The current branch is updated, but the target branch, "<feature>", is unaffected. Here is a typical flow to merge changes from a feature branch into master with no conflicts:
$ git checkout master
$ git checkout -b feature1
  # make edits and commits on feature1
$ git checkout master
$ git merge feature1
  #cleanup by deleting feature1 branch
$ git branch -d feature1 

Deleting a Branch

git branch -d <branch-name>
delete branch <branch-name>
git branch -d branch1 branch2 branch3 ...
multiple branches can be specified
git branch -D <branch-name>
delete branch <branch-name> even if unsaved changes will be lost. (-D is short for "--delete --force")
git push origin --delete <branch-name>
delete branch <branch-name> on remote
git push origin :feature4
delete branch feature4 on remote (prefixing with ":" does the same as "--delete" in this case)

Clean

"git clean" will remove untracked files and directories.
git clean -n
show which files will be deleted by a "clean" command
git clean -f
delete untracked file "-f" is needed to force deletion
git clean -df
delete untracked files and directories; useful for getting rid of build products.
git clean --dry-run
Shows what files would be deleted.

Push

"push" copies local refs to the remote and sends the objects necessary to complete the refs.
git push
usually pushes the current branch to origin unless config has a value for "branch.<name>.remote".
git push <repository>
pushes the current branch to <repository>, which can be a URL or the name of a remote
git push origin
pushes local changes to origin. With no further arguments, git looks in the ".git/config" file for the '[remote "origin"] merge' value.
git push -f
forces local changes to origin overwriting others changes. Not recommended except in extreme cases.
git push origin :<branch-name>
pushes an empty branch over "<branch-name>" causing it to be deleted.
git push origin <local-branch-name>:<remote-branch-name>
push a branch to origin with a different name

Fetch

Fetch does several things:
1. Copies the commit values from the remote branches to the .git/refs/remote/<repository>/<branch> files.
For example, assume the remote "master" branch is ahead by one commit at "cf2c3d9ccb6fb349543ee788ea27b48b2b63cf7a", and the local pointer to the remote "master" branch is at "a5d236166a2607841549bbbe1ff50ac13a3305ca" (meaning the contents of "gitdemo/.git/refs/remotes/origin/master" is "a5d236166a2607841549bbbe1ff50ac13a3305ca").
If we do a
  "$ git fetch origin master"

then the contents of "gitdemo/.git/refs/remotes/origin/master" is now updated to the value at the remote, "cf2c3d9ccb6fb349543ee788ea27b48b2b63cf7a".

2. Downloads any new objects referenced from the new commits on the remote branch into the local object database (.git/objects).
3. Downloads tags that reference the new items.
Fetch does not change the contents of your working directory or your local refs like "HEAD".
A great visual example of fetch is at https://onlywei.github.io/explain-git-with-d3/#fetch.

Pull

git pull
same as doing a "git fetch" + "git merge"

Rebase

git rebase <branch-name>
make <branch-name> the "base" of the current branch. Conflicts may need to be resolved.
git rebase -i HEAD~3
to go back three commits, note lines in reverse order now
git rebase --abort
stop the rebase and roll back

Stash - store changes off to the side

"stash" saves your changes into a genuine commit onto a side stack that can be used like any other commit.
git stash save
stashes staged and unstaged changes onto stash queue (but not untracked files) and reverts current branch to last commit
git stash save "message"
stash to a name
git stash
appends "save" at the end for you
git stash apply
brings back the stash
if you enter "git stash && git stash apply", it will create a snapshot of your system.
git stash list
shows list of stashes with previous commit
git stash list --stat
shows more info, log options can be used e.g., "--oneline"
git stash apply stash@{1}
brings back stash 1. Does not pop off the top of the queue - leaves it there
git stash drop
removes top stash frame in queue
git stash pop
restores most recently stashed files and removes them from the stack of changesets
git stash save --save-index
keeps staged files, but stashes unstaged
git stash show
shows info on last stash
git stash show stash@{2}
info on 2
git stash clear
removes all stashes
jwiegley recommends this clever use of stash for linux systems:
$ cat <<EOF > /usr/local/bin/git-snapshot
#!/bin/sh
git stash && git stash apply
EOF
$ chmod +x $_
$ git snapshot  

Cherry Picking

git cherry-pick <commit>
will pick that commit into our current branch
git cherry-pick --edit <commit>
allows changing message
git cherry-pick --no-commit <commit> 55aed374
pulls in changes to staging
git cherry-pick -x <commit>
adds in previous SHA-1 number in comments
git cherry-pick --signoff <commit>
adds picker's id

Genealogy, Searching the Family Tree

a3d5fca12
to reference a commit you only need to type enough characters to make it unique, usually 6 or 7 characters are enough.


name^
the parent commit, if more than one parent, the first is used
name^^
the grandparent
name~8
the eighth ancestor, or name^^^^^^^^
name^2
If more than one parent this will get the second parent.
--grep=pattern
matches all commits whose message matches this pattern.
$ git log --grep puppies
--author=pattern
matches all commits whose author matches this pattern

$ git log --author=Linus


--no-merges
matches all commits which have only one parent, i.e., no merged commits

Reflog

"Reflog" is a backup system that keeps track of all our commits, even if we delete branches. The commits will stay around for about 30 days and then get really deleted.
"Reflog" is a history of where HEAD has been pointed.
git reflog
shows commits. Only local to user
   54eb749 HEAD@{0}: checkout: moving ...             
   54eb749 HEAD@{1}: checkout: moving ... 
   eb70244 HEAD@{2}: checkout: moving from ...
   54eb749 HEAD@{3}: pull: Fast-forward
   
let's accidentally delete a branch
$ git branch -d zoo

generates error, not fully merged
$ git branch -D zoo //overrides and deletes $ git log --walk-reflogs //gives more info $ git branch zoo <commit> //creates new branch pointing to deleted branches' last SHA-1 $ git branch zoo HEAD@{1}//same as above $ git checkout zoo

Squash multiple commits into one tidy commit

You can group your commits into a meaningful one before a merge, but it takes two steps:
Step 1:
  git rebase -i HEAD~n //where n is a number of commits to merge (squash)
The "i" option is for interactive.
Step 2: Git will present you with a text file like this:
pick 8a18f54 one
pick 64156a7 two
pick f4a9307 3

# Rebase cd97cab..f4a9307 onto cd97cab (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
Leave the first line as is, but in the following lines change "pick" to "squash". Then save, and exit the editor. [In vim, ESC -> :wq! (save changes)]
Git will then present a message file where you can delete all the checkin comments and replace with a new one for all commits. Save and exit. Enter "git log" to see the change.

Notes

"git gc" will garbage collect and repack your database.
Git has three types of references: tag, branch, and HEAD.
Git may reduce storage space by using a base file and "diffs" from that base file. These are stored in the "pack" subdirectory.
The parent of the current commit is the SHA-1 value of the reference that HEAD points to.
HEAD is usually a symbolic reference to the last commit in the currently checked-out branch. HEAD points to the current branch. In the .git directory, the file "HEAD" contains a pointer to the current branch, which in turn references a commit hash id. Example:
$ cat .git/HEAD
ref: refs/heads/Mitch_895553_MyBranchName

$ cat .git/refs/heads/Mitch_895553_MyBranchName
a3d5fc559d4ba4183964ca6d17e658812eafb141
The HEAD can be set directly to a commit. This is called a "detached head".
A directory named "index" holds all the files to be committed. This is called the "stage", where files are staged before being committed. Files do not go from the working tree to the repository, they have to be added to the staging area first with a "git add " command.
The "working tree" is where the normal files are kept on your filesystem. It has a ".git" directory which contains all the .git objects.
A "hash id" is a SHA-1 hash, a 40 character hex hash. SHA-1 takes text and reduces it down to 40 characters. The hash of a file is just based on contents, name is not hashed. See "git hash-object file". You can usually use abbreviate the hash with the first few characters. The abbreviation needs to be unambiguous and have at least 4 characters.
Almost all operations in Git add data to the db, it doesn't delete or change the database, so it's very forgiving
For passwords setup in Visual Studio, see: https://www.visualstudio.com/docs/git/set-up-credential-managers
Three Rules of Branches
  1. Current branch tracks new commits
  2. When you move to another commit (branch), Git updates the files in your working directory
  3. Unreachable objects may be garbage collected