Use git, ssh and npm on windows with Git Bash

If you are used to develop on Linux and you have to suddenly switch back to Windows for a particular project, it can be really painful to use native tools like putty or power shell to develop.
Don’t worry, there are plenty of solutions to make things right.
For example, you could work on a Linux virtual machine inside your Windows.
Another solution (the one I chose) is to use Git Bash.
This setup has several advantages:

  • It requires no installation, which means it can be set up without admin rights
  • It is rather lightweight and easily packageable
  • You still have access to your windows filesystem via your command line

We are going to configure and package nodejs/npm inside the Git Bash to share it with every developers of your new team.

Get the softwares

First, get the desired softwares:

  • Download Git for Windows Portable (“thumbdrive edition”) > HERE < and install it (ex: in C:\Applications\setenv\git)
  • Download node with npm zip package > HERE < and unzip it (ex: in C:\Applications\setenv\node)

Git portable edition comes with Git Bash included.

You should now have a folder which looks like this :

setenv
├─ git
│  ├─ git_bash.exe
│  └─ etc
│     ├─ profile (edited)
│     └─ setenv.conf
└─ node
   ├─ node
   └─ npm

Configure your shell

In git Bash, a Linux like environement is simulated so you can access your Linux filesystem with Linux style paths.
For example, to print the content of C:\Applications\setenv you can type:

ls /c/Applications/setenv

Open git bash and type the following command:

export PATH=$PATH:/c/Applications/setenv/node

Type then node --version and npm --version to check that node and npm are available.
If not, check that you have the npm and node.exe files in C:\Applications\setenv\node.

To reproduce this configuration when launching your terminal, you can create a custom configuration file in C:\Applications\setenv\git\etc\setenv.conf :

# Include node PATH
PATH=$PATH:/c/Applications/setenv/node

Edit your etc/profile git bash file (Windows location: C:\Applications\setenv\git\etc\profile) and add the following line just before exporting the PATH:

source "etc/setenv.conf"

Reload your bash terminal, you will be asked for your proxy username and your proxy password.
This will create a setenv.var file which will be used when opening the git bash next time.
You should then be able to use npm and node out of the box!
For example, if you want to contribute to this planning burndown chart project:

git clone git@github.com:theodo/planning-bdc.git && cd planning-bdc
npm install
npm run watch

And here you go!

Set up your proxies (optional)

If you get stuck when using git clone or npm install, you may be blocked by a proxy (which requires configuration as well). Find out what the address is following this process.

Add the following lines to your setenv.conf :


if [ -f "etc/setenv.var" ];
then
  source "etc/setenv.var"
else
  touch etc/setenv.var

  read -p "What is your proxy login? " PROXY_LOGIN
  echo "PROXY_LOGIN=\"${PROXY_LOGIN}\"" >> etc/setenv.var

  read -p "What is your proxy password? " PROXY_PASSWORD
  echo "PROXY_PASSWORD=\"${PROXY_PASSWORD}\"" >> etc/setenv.var

  PROXY_ADDRESS="proxy.address.com"
  echo "PROXY_ADDRESS=\"${PROXY_ADDRESS}\"" >> etc/setenv.var

  PROXY_PORT="8080"
  echo "PROXY_PORT=\"${PROXY_PORT}\"" >> etc/setenv.var
fi

# Used for Node Terminal proxy (for packages as shipit)
export HTTP_PROXY=http://${PROXY_LOGIN}:${PROXY_PASSWORD}@${PROXY_ADDRESS}:${PROXY_PORT}
export HTTPS_PROXY=https://${PROXY_LOGIN}:${PROXY_PASSWORD}@${PROXY_ADDRESS}:${PROXY_PORT}
# Npm proxy
npm config set proxy $HTTP_PROXY
npm config set https-proxy $HTTPS_PROXY
# Git proxy
git config --global http.proxy $HTTP_PROXY
git config --global https.proxy $HTTPS_PROXY

This piece of bash code will :

  • Check if a setenv.var file exists
  • If true, it will import it
  • If false, it will create it and fill it with the PROXY_LOGIN, PROXY_PASSWORD, PROXY_ADDRESS and PROXY_PORT variables
  • Then it will configure your node, npm and git with the proxy

Be careful with your HTTPS proxy, it might have a different address / port than your http proxy

Start your Git Bash and you should be asked for the proxy variables. You should now be able to use git clone and npm install for your applications!

You can also use the integrated ssh client.

If you need to change the variables, destroy the setenv.var file and you will be asked for new values.

Conclusion

Now you got a configured and packaged Git Bash, you can adapt it to create your own environement!
Thanks for reading, don’t hesitate to leave feedback!


You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us

  • NicolasB

    I’ve just started to use it, I let you know if I succeeded to set up my environment