Skip to content
Logo Theodo

Use git, ssh and npm on windows with Git Bash

Nicolas Ngô-Maï4 min read

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:

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 and add these to C:\Applications\ (where you will have sufficient rights to execute them):

Git portable edition comes with Git Bash included.

You should now have a folder which looks like this :

Applications
├─ git
│  ├─ git_bash.exe
│  └─ etc
│     ├─ profile (edited)
│     └─ node_env.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\ you can type:

ls /c/Applications

Open git bash and type the following command:

export PATH=$PATH:/c/Applications/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\node.

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

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

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

source "etc/node_env.conf"

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 https://github.com/theodo/planning-bdc.git && cd planning-bdc
npm install
npm run watch

And here you go!

Use SSH in your shell

And now how about using SSH to connect to github?
Indeed, Git Bash comes with a lot of features from Linux like grep, find, sed, and even ssh and scp!
To create a SSH key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

As in Linux, the key file location will be in ~/.ssh:

cat $HOME/.ssh/id_rsa.pub

Add it to Github and run:

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

And here you go again!

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 node_env.conf :


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

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

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

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

  PROXY_PORT="8080"
  echo "PROXY_PORT=\"${PROXY_PORT}\"" >> etc/node_env.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 :

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 node_env.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!

Liked this article?