Git

From WhyAskWhy.org Wiki
Revision as of 07:56, 7 October 2012 by Deoren (talk | contribs) (Found another really useful git tips/tricks page)
Jump to: navigation, search


My scratch notes on Git. Someday this page may actually be useful.

Questions

By answering these questions, I'll have a "cheat sheet" of useful tips/tricks to consult in the future.


How to retrieve the hash for the current revision in your git repo?

This is probably a poorly worded question, but essentially if you have cloned a repo and are trying to:

  1. Note the current revision/hash
  2. Upgrade to the latest version

I have this habit when upgrading extensions for MediaWiki as I like to document what changes have been made so I can have a starting point for troubleshooting odd behavior that shows up.

So far I've found that all of these work, just with varying degrees of feedback/change required [1]:

  • git show
    
    • This gives the most information, ranging from a colorized diff of the last commit, the last commit log and the colorized commit hash.
  • git rev-parse HEAD
    
    • This gives just the commit hash, but it is probably the best approach for automated work.
  • git pull
    

I've also found that when you run git pull (in an existing repo of course) that the commit hashes (short notation) will be shown and can be used with git log to see the changelog for the revision you just had up to the revision you have now.

Example of the output from upgrading the Interwiki MediaWiki extension from commit 9e79d4b213000035ac72817cdaaa6541412ef811 to commit 5510cd9b322becb9cb66a1a4080ddfe5ba78a47f:

cd /var/www/example.org/mediawiki/extensions/interwiki
git pull
remote: Counting objects: 60, done
remote: Finding sources: 100% (56/56)
remote: Getting sizes: 100% (8/8)
remote: Compressing objects: 100% (4/4)
remote: Total 56 (delta 34), reused 52 (delta 34)
Unpacking objects: 100% (56/56), done.
From https://gerrit.wikimedia.org/r/p/mediawiki/extensions/Interwiki
   9e79d4b..5510cd9  master     -> origin/master
Updating 9e79d4b..5510cd9
Fast-forward
 Interwiki.alias.php |    4 +-
 Interwiki.i18n.php  |   73 +++++++++++++++++++++++++++++++++++++++++---------
 Interwiki_body.php  |   19 ++++++++++---
 3 files changed, 76 insertions(+), 20 deletions(-)

This line lists the short version of the commit hashes:

Updating 9e79d4b..5510cd9

and as mentioned previously, you can use the two as shown here to get the changelog between the two commits:

git log 9e79d4b..5510cd9

Only showing a snippet, but it includes the next in the series (not the entry for 9e79d4b itself) and the last. The ... indicates the portion I cut out.

commit 5510cd9b322becb9cb66a1a4080ddfe5ba78a47f
Author: Translation updater bot <l10n-bot@translatewiki.net>
Date:   Tue Oct 2 13:46:02 2012 +0000

    Localisation updates from http://translatewiki.net.

    Change-Id: I01fc03d8cbf6731e01a5cd2c6effb80cc47dc4ff

...

commit f32b40b2b89cf3c38d8e6b3d9a5c01599acba079
Author: Siebrand Mazeland <s.mazeland@xs4all.nl>
Date:   Thu Aug 30 14:57:43 2012 +0200

    Replace deprecated User::editToken with User::getEditToken

    Change-Id: I3382a27ee788eb5dc6b7b033987326ce1ab1dc91


How can you tell what branch or tag your working copy is on?

What if you are not seeing a tag that you know is available?

For example I had a copy of the MediaWiki 1.19.1 tag checked out [2] and even though a 1.19.2 tag was available I didn't see it when running git tag -l.

Running git fetch [3] seemed to do the trick:

remote: Counting objects: 96, done
remote: Finding sources: 100% (49/49)
remote: Getting sizes: 100% (58/58)
remote: Compressing objects: 100% (11/11)
remote: Total 49 (delta 30), reused 36 (delta 28)
Unpacking objects: 100% (49/49), done.
From https://gerrit.wikimedia.org/r/p/mediawiki/core
   49857f8..f25ee70  REL1_19    -> origin/REL1_19
   566852d..944e196  master     -> origin/master
 * [new tag]         1.19.2     -> 1.19.2


How do you switch from one tag to another?

Using our earlier example, let's switch from the 1.19.1 tag to the 1.19.2 tag.

git checkout 1.19.2

Previous HEAD position was d0c0aab... Merge "Bump $wgVersion and add release-notes for 1.19.1" into REL1_19
HEAD is now at f25ee70... Release notes and increment version


How do you create a Git repository and prep it for sharing via HTTP?

mkdir /var/www/git/repos/project.git
cd /var/www/git/repos/project.git
git --bare init
chown -R www-data:www-data /var/www/git/repos/project.git
sudo -u www-data git update-server-info
cd /var/www/git/repos/project.git
mv hooks/post-update.sample hooks/post-update


What if you wanted that Git repository to be a clone of another repository?

git clone --mirror <remote repository>

or

git clone --mirror /work/repo


How do you share a Git repository via HTTP?

Several different web servers could be used for this, but here is an example using Apache 2.2.x. I've tried to make it pretty bare so it could apply the widest possible setups. For my setup I use a local LDAP server (same box as web server) instead of a password file. I feel this helps with user account/password management.

Note: For the SSL portion, the SSLCipherSuite directive may need a closer look as I have had that value in a conf file for several years and there may be a better approach to use.

##################################################################
# vhost settings for git.example.org
##################################################################

<VirtualHost *:8080>
    DirectoryIndex index.html
    UseCanonicalName off

    ServerName git.example.org
    ServerAdmin webmaster@example.org

    DocumentRoot /var/www/example.org/git

    # Enable SSL for this vhost
    SSLEngine on

    # Path to certificate and key
    SSLCertificateFile   /etc/ssl/mycerts/wildcard.example.org_server.crt

    # Should be kept where only root can get to it.
    SSLCertificateKeyFile /etc/ssl/private/wildcard.example.org_server.key

    # allow all ciphers for the initial handshake,
    # so export browsers can upgrade via SGC facility ...
    SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

    <Location />
         SSLOptions +StrictRequire

         # REQUIRE SSL for all connections
         # http://httpd.apache.org/docs/2.1/mod/mod_ssl.html#sslrequiressl
         SSLRequireSSL

         # ... but finally deny all browsers which haven't upgraded
         SSLRequire %{SSL_CIPHER_USEKEYSIZE} >= 128
    </Location>

#########################################################
# misc git repos
#########################################################

    Alias /repos /var/www/git/repos
    <Location /repos>
        DAV on
        Options ExecCGI FollowSymLinks Indexes

        #####################################
        # Apache auth options
        #####################################

        Satisfy All
        AuthUserFile /usr/local/apache/access/git-repos.htpasswd
        Require valid-user

    </Location>

    # The goal is to apply the CGI options to any git repo's hooks dir
    # under /var/www/git/repos
    <DirectoryMatch "^/var/www/git/repos/.+/hooks/.+">
        SetHandler cgi-script
        Options +ExecCGI
    </DirectoryMatch>
</VirtualHost>


References

Manual Pages

git fetch


Sharing a Git repository via HTTP(s)


Unsorted links

These are just Git related links that I've dumped from my bookmarks folder. I'll go back and prune duplicates.