Splitting a subpath out into a new repository with Git

From time to time you may find that you want to make a new repository from a subpath of an existing repository. Perhaps you’re moving some code out into a library or just want to have a common submodule across projects. Thanks to git, it’s easy to do this without losing the history of that subpath in the process.

The Good Stuff

Splitting a subpath into a repository is a fairly straightforward process, even if the command is hard to remember. For this example, we’ll split lib/ out of the GitHub gem repository, removing empty commits but retaining the path’s history.

git clone git://github.com/defunkt/github-gem.git
# Clone the repository we're going to work with
# Initialized empty Git repository in /Users/tekkub/tmp/github-gem/.git/
# remote: Counting objects: 1301, done.
# remote: Compressing objects: 100% (769/769), done.
# remote: Total 1301 (delta 724), reused 910 (delta 522)
# Receiving objects: 100% (1301/1301), 164.39 KiB | 274 KiB/s, done.
# Resolving deltas: 100% (724/724), done.

cd github-gem/
# Change directory into the repository

git filter-branch --prune-empty --subdirectory-filter lib master
# Filter the master branch to the lib path and remove empty commits
# Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (89/89)
# Ref 'refs/heads/master' was rewritten

Now we have a re-written master branch that contains the files that were in lib/. We can add a remote to the new repository and push, or do whatever we want with the repository.

in Git | 223 Words

Using git over proxy

I was trying to clone the thrift repository at a clients office when this problem occurred.
Upon some Googling, I got this really useful link (thanks to Emil Sit)
which explained how git can be used over an http proxy for those git servers
which don’t allow http method as an alternate/bypass to the git protocol.

Okay, so here’s what’s needed to be done;

  • Typed in the below lines (quoted) into a shell script called git-proxy and put it in $(HOME)/bin directory.
    Of course; its executable bits has to be set with: chmod a+x $(HOME)/bin/git-proxy.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #!/bin/sh
    # Use socat to proxy git through an HTTP CONNECT firewall.
    # Useful if you are trying to clone git:// from inside a company.
    # Requires that the proxy allows CONNECT to port 9418.
    #
    # Save this file as gitproxy somewhere in your path
    # (e.g., ~/bin) and then run
    # chmod +x git-proxy
    # git config --global core.gitproxy git-proxy
    #
    #
    # Configuration. Common proxy ports are 3128, 8123, 8000.
    _proxy=yourproxyhost
    _proxyport=yourproxyport
    exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport

    Note: I replaced proxy.yourcompany.com with appropriate proxy-server details
    for my organization and the value for _proxyport with an appropriate value
    used in my organization (instead of the default 3128).

  • Installed the package socat with the command; sudo apt-get install socat
  • Ran the command git config –global core.gitproxy git-proxy to configure ‘git’ to use the git-proxy.
  • With the http_proxy environment variable already set using:
  • export https_proxy=https://10.15.11.132:443/

    I could now use git and get the repositories without any problems.