Getting to grips with git – the stash, working directory, index, local repo, remote repo

When starting git it’s really helpful to know there are a bunch of locations your source can exist. These are the:

  •  stash
  • working directory
  • index
  • local repo
  • remote repo

I don’t know if this cheat sheet helps or just overwhelms but some may find it useful: http://www.ndpsoftware.com/git-cheatsheet.html#loc=workspace

 

git stash

The docs say:

git-stash - Stash the changes in a dirty working directory away

https://git-scm.com/docs/git-stash

but there are plenty of ambiguities here.

stash

What exactly does stash mean?

And what’s a dirty working directory?

Say you’ve got this:

in your current directory your gut reaction might be that it will store your changes to both A and B. After all you’ve changed both and they’re both in your working directory.

That’s not the case:

B is ignored. You need to add it to the repo to stash it.

To stash B, use git stash -u

 

Note: a dirty working directory would seem to indicate a changed file in the current working directory. It isn’t.

dirty state means modified tracked files and staged changes

https://git-scm.com/book/en/v1/Git-Tools-Stashing

working directory is a repository created with the git init command.

https://stackoverflow.com/questions/36201342/git-where-exactly-is-the-working-directory

https://stackoverflow.com/questions/53744931/clarifying-the-working-directory-vs-current-directory-in-git

 

Useful discussions:

https://stackoverflow.com/questions/20642980/does-git-dirty-mean-files-not-staged-or-not-committed-glossary-conflict

 

View stash

List stashes with:

git stash list

View contents of stash with:

git stash show -p stash@{0}

 

Apply stash

Apply stash with:

git stash apply stash@{0}

Note that this won’t remove the stash from the list.

 

If you attempt to stash it again, you’ll just add another identical entry into your stash list.

To remove it from the list use:

git stash drop stash@{0}

 

https://git-scm.com/book/en/v1/Git-Tools-Stashing

 

git log –all

Despite git’s power, it can be very un-intuitive.

E.g. you run git log expecting to see all log messages. Not true. You have to run git log –all to see all of them (* see Note 1 below).

And the output is not obvious. e.g.

commit c751111
Merge: fc07fdf 4059fc6
Author: <author>
Date:   Mon Apr 20 14:42:08 2015 -0400

WIP on dev/app/admin/categories: fc07fdf with auto categories

commit 4059fc6
Author: <author>
Date:   Mon Apr 20 14:42:08 2015 -0400

index on dev/app/admin/categories: fc07fdf with auto categories

commit fc07fdf
Author: <author>
Date:   Mon Apr 20 14:39:01 2015 -0400

with auto categories

Let’s go through each line:

1: shows the SHA-1 reference of that commit

2: it seems to have been a merge between 2 other commits

3: author details

4: date

5: this is an auto-generated stash message. <WIP => work in progress> on <name of branch>: <commit SHA-1 reference> <commit message>

The next line of interest is

index on dev/app/admin

Presumably this was auto-generated by git. I don’t have a clue there.

 

Note 1: another example of the unintuitiveness of git. It turns out that git log –all does NOT show all the log messages. Here’s what it actually does:

http://stackoverflow.com/questions/29756637/what-does-git-log-all-do

warning: push.default is unset; its implicit value has changed in Git 2.0 from ‘matching’ to ‘simple’.

I recently tried pushing a new branch and got this:

Just enter this to change the setting:

More here: http://stackoverflow.com/questions/13148066/warning-push-default-is-unset-its-implicit-value-is-changing-in-git-2-0

Mac as a Development platform

I sometimes feel as if I spend half my time making fixes caused by Apple upgrades.

This morning I found my previously hand-crafted php.ini had been removed.

So, each time Apple does an upgrade I have to go back in and update php.ini with various customizations (like max post size, etc).

This also happened to Apache. Apple did an upgrade and I lost my virtualhosts.

And other things change too – like I now have to use mysql.server start rather than using mysqld. I can understand that things need to change but why not provide a graphic tool and just change things under the hood.

Now I try restarting MySQL and I get:

So clearly another update has happened that I’ll need to fix.

My experience on Linux has always been much smoother. I’m starting to wonder if I should try using it again as a development platform.

How does WordPress load templates?

The short answer:

– go see https://codex.wordpress.org/Template_Hierarchy

However, here’s a worked-out example.

I have a page called News with the slug “news”.  Here’s how WordPress loads the necessary page:

1. Apache runs your root /index.php file

2. This require’s

3. wp-blog-header then:

require_once’s wp-load.php

runs wp() and

require_once’s template-loader.php

4. template-loader.php does a few checks and then runs a huge if, elseif:

Lovely!

It turns out that the example given sets $template to ‘wp-content/themes/<theme name>/page-news.php’.

How does it do that? Well, if we search for the function get_page_templates() it turns out there are 3 occurrences in the WordPress code (in wp-admin/includes/theme.php and in various wp-includes files – class-wp-theme.php, template.php).

So, which one is it using?

The answer:

wp-includes/template.php which returns the value here:

Where is get_page_template_slug() defined?

In wp-includes/post-template.php. This works out the post using the post_id.

Then it works out the template using get_post_meta() (which is defined in post.php).

get_post_meta simply returns the result of another function get_metadata which is defined in meta.php.

The full code:

In meta.php > get_metadata it checks $meta_key. It cycles through a bunch of values in $meta_cache until it gets to $meta_key = ‘_wp_mf_page_template’.

The code:

cycles through the $meta_cache values just to decide the template! I had 343 values!

I literally gave up here as, whilst I spotted a $meta_key = “_wp_page_template” it continued to cycle through the $meta_cache values. Without wasting more time on this I can only assume it found the value here.

 

 

 

 

Google Contacts – I can’t find any of my Contacts any more

I have all my contacts in Google Contacts.

If I want to find someone I usually type their name in the Google Contacts search box and I can find them in seconds.

This has all changed now.

The latest version of Google Contacts now searches Google+. E.g. take a look at this:

Google Contactshttp://www.snowcrash.eu/wp-content/uploads/2015/04/Google-Contacts-588x487.png 588w, http://www.snowcrash.eu/wp-content/uploads/2015/04/Google-Contacts.png 622w" sizes="(max-width: 300px) 100vw, 300px" />

Here’s a search I did recently for someone called Mark.

Why does Google think it makes any sense whatsoever to search all the Marks in the entire world?

I’ve tried clicking all the menu options at the top and on the left such as “Frequently contacted”, etc.

I have literally no idea how to search my Contacts any more. Fortunately, I have my contacts synced with my iPhone and now do all my searches using the Contact app there.