git on Windows

This article explains how git on Windows can be used with the git bash or the PowerShell.

Git is getting more popular every day. It is not only a very modern version control system, it is also lightweight and robust. However, using git on Windows is not as trivial as just installing Linux, or running sudo apt-get install git on a Linux machine (same goes for yum or other package managers).

What I can only recommend is installing Chocolatey. It is a package manager (much like the formerly named two for Linux) and really useful for installing software. It enables automatic installations and therefore boosts productivity. For installation we just post the following into the command prompt (or we use the argument of the Command parameter in the PowerShell):

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString(''))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin

Once we installed Chocolatey we can start using the PowerShell. The following command should also work in the command prompt. It will run the git installation. Note, however, that administrative rights are usually recommended for installation. Therefore it could be essential to start the PowerShell with elevated rights.

choco install git.install

Now that we have installed git, we also have the git bash. The git bash is a useful program that simulates the Linux bash together with highly useful programs (such as vim). However, as we probably do not want a second-grade Linux clone, but an empowering Windows experience, a PowerShell alternative is required. Lucky for us this work has already been done. We use:

choco install poshgit

Using posh-git enables some interesting possibilities. Notably it will change the command prompt in directories, which are under git source control. We get:

  • Prompt for git repositories - shows the current branch and the state (added, changed, removed) within.
  • Tab completion for git commands.

The installation of posh-git is mainly done via Chocolatey, but there could be a problem with the execution policy. First verify that the execution of scripts is allowed. This can be done by executing the command Get-ExecutionPolicy (should be RemoteSigned or Unrestricted). If scripts are not enabled, running the PowerShell as Administrator should help. Here we can call Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm to change the execution policy.

At this point we hopefully got git from the PowerShell up and running. Now we can configure git globally. The configuration is reduced to setting two (personal) pieces of information: The desired user name and the associated email address.

git config --global "Your user name"
git config --global "Your email address"

Creating an SSH key can be done, e.g., via the git bash. For instance for generating a new SSH key pair (public and private) we can just run:

ssh-keygen -t rsa -C "Key for some remote git mirror"

Let's assume we saved the key combination in the file(s) my_key / (in the folder .ssh of our home directory). We can now add the SSH key for communication by running an ssh-agent instance and including the key in the agent program:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/my_key

Testing the connection to your mirror is also possible by using ssh -vT, where is the address of the remote mirror.

Finally we can start. We either start from skretch by creating a new directory and calling git init there, or we clone from an existing source using git clone

Posh-git starts the SSH agent with some useful settings on startup. This is another yet point in favor of posh-git. If the ssh-agent has problems (cannot be found) when starting the PowerShell, we probably have to add it to the paths. We can either do it globally, or just an environment variable for the current PowerShell instance.

I prefer to do the last. Therefore I'll edit my PowerShell startup script (usually found in the WindowsPowerShell folder of my documents) and insert the following line in the startup:

$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

Please make sure that ssh-agent.exe can really be found in "C:\Program Files(x86)\Git\bin" (replace Program Files(x86) with your localized version).

Traditionally we will have a remote repository called origin, when we cloned the repository from an existing mirror. However, in some versions of git the global configuration also gives us such a remote, if we did not get the repository from a mirror.

The new way is to alter the default origin for the locally set repository (if needed). For instance we could do:

git remote set-url origin

This sets the origin of the local repository to the given URL. The only remaining problem is that no default branch has been set for this remote. However, this could be set in two commands:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

The previous command connects the branch master with the remote origin. If we use git push origin, we will use the master branch. On the other side, git push origin devel will still push the selected branch (devel).

Created . Last updated .


Sharing is caring!