Using Git with shared folders and virtual machines

Bridge

By Mike Jackson, Software Architect.

This is a guide on using Git and GitHub within a VMWare virtual machine (VM) which, for whatever reason (e.g. organisational security policies), cannot be connected to a network.

Why write this guide?

This guide arose from our open call collaboration with the Distance project at the University of St. Andrews. They use Windows XP virtual machines for developing their Distance for Windows software. Their interface code, implemented in Visual Basic, is not held under revision control and institutional security policies mean that their XP virtual machines cannot be connected to the network. Please see the blog post on Building a bridge between a virtual machine and the outside world.

Assumptions

  • You have a VMWare virtual machine that cannot be connected to the network directly.
  • Your host machine that run the virtual machine, can be connected to the network.
  • You have a repository on GitHub.
  • You have GitBash installed on both your host machine and VM.

Set up a shared folder

Set up shared folder within the VM using VMWare Player:

  • Select Player => Manage => Virtual Machine Settings...
  • Click Options tab
  • Click Shared Folders
  • Click Always enabled OR Enabled until next power off or suspend
  • Click Add...
  • Select Host path, the folder on the host machine to be shared with the VM e.g.: C:\Users\user\project-shared
  • A name for the shared folder e.g. project-shared
  • Click Next
  • Click Finish
  • Click OK
  • On the VM, under My Computer, the shared folder should appear, for example, as "Shared Folder on 'vmware-host' (Z:) Network Drive".
  • On Git Bash on the VM, this should appear as /z/project-shared.

Set up the repository

Check out the repository on the host:

  • Start Git Bash.
  • Change into the shared folder on the host e.g.
    cd /c/users/user/project-shared
    
  • Clone the forked repository:
    git clone https://github.com/USERNAME/project.git
    cd project​
    
  • You can now use /z/project-shared/project/ from within Git Bash to commit local changes.

Using the repository

When you want changes passed up to GitHub:

  • Within Git Bash on the host, change into the shared folder repository and push the changes to GitHub e.g.
    cd /c/users/user/project-shared/project/
    git push origin master
    

When you want changes pulled down from GitHub:

  • Within Git Bash on the host, change into the shared folder repository and pull the changes from GitHub e.g.
    cd /c/users/user/project-shared/project
    git pull origin master
    

Adding another repository to circumvent shared folder issues

Certain software may not be usable with files hosted within shared folders. For such software, an additional repository, local to the VM, can be introduced providing a Git repository within the VM but outwith the shared folder, which nevertheless can push and pull changes to and from the shared folder repository.

After cloning the repository into the shared folder on the host, set up a local repository on the VM.

  • Start Git Bash.
  • Look at the shared folder repository on the host to see the forked repository e.g.
    ls /z/project-shared/project/
    
  • Clone this into a local repository C:\project-local e.g.
    cd /c
    git clone file:///z/project-shared/project project-local
    
  • There should be a Git remote set up for the shared folder repository e.g.
    cd project-local
    git remote -v
    origin  file:///z/project-shared/project (fetch)
    origin  file:///z/project-shared/project (push)
    
  • In the shared folder repository, set up a Git remote to this clone e.g.
    cd /z/project-shared/project
    git remote add local file:///c/project-local
    git remote -v
    local   file:///c/project-local (fetch)
    local   file:///c/project-local (push)
    origin  https://github.com/USER/project.git (fetch)
    origin  https://github.com/USER/project.git (push)
    
  • You can now use /c/project-local from within Git Bash to commit local changes.

When you want changes passed up to GitHub:

  • Within Git Bash on the VM, change into the shared folder repository and pull the changes from the local repository e.g.
    cd /z/project-shared/project
    git pull local master
    
  • Within Git Bash on the host, change into the shared folder repository and push the changes to GitHub e.g.
    cd /c/users/user/project-shared/project
    git push origin master
    

When you want changes pulled down from GitHub:

  • Within Git Bash on the host, change into the shared folder repository and pull the changes from GitHub e.g.
    cd /c/users/user/project-shared/project
    git pull origin master
    
  • Within Git Bash on the VM, change into the local repository and pull the changes from the shared folder repository e.g.
    git /c/project-local
    git pull origin master