Git for Windows tip: How to copy and paste into Bash

Per default the only way to copy and paste into the Git Bash is to click on the git icon in the top left corner and select Edit->Mark/Copy/Paste. This is actually not a property of Git Bash. It is just how the Windows console runner works i.e. the standard command prompt.

There are two solutions to be able to copy/paste with the keyboard and directly with the mouse. The first is to use a different console, see Scott Hanselman’s post on Console2. I haven’t tried this yet so let me know in the comments if you use Console2 and can recommend it.

The other solution is to enable QuickEdit Mode under Options->Edit Options in the properties for the console. To open the Properties dialog, click the Git icon in the top left corner of the console and choose Properties in the menu.


Now you can select text with the mouse and right click to copy. Pasting is done by either right clicking with the mouse or pressing the Insert key. Still more awkward than using Ctrl-C and Crtl-V so maybe it is time to check out Console2.

Git for Windows tip: Setting $HOME and the startup directory

Git for Windows opens bash in the the user profile directory per default and I wanted to change it to the directory with my Github projects instead. I had to try a couple of approaches before finding the solution.

Setting $HOME

The first approach I tried was setting the $HOME environment variable. There are a couple different ways of doing this (like messing around with the etc/profile file) but easiest for me was using Windows’ environment variables as Git for Windows/msysgit has access to them. The Home directory for msysgit is set to the Windows environment variable %USERPROFILE%  if no $HOME variable exists. So just create a $HOME environment variable in Windows (see screenshot below)  and msysgit bash will use that as the default. Now you can use the command cd $HOME to go directly to your new home directory.


But msysgit still opens up in my user profile directory…

Unfortunately, this does not help with the startup directory problem. The solution is actually really simple. Right-click on the msysgit shortcut and in the Start in field, enter your desired startup directory. Easy if you know how.


Git for Windows tip: setting an editor

The first time I tried to do git commit on msysgit with no commit message (no –m switch), it opened up Vim so that I could write my commit message. Luckily for me I’d read enough Vim jokes on Twitter to know that :q or :q! would get me out. But there is no way I’d be able to actually write a commit message, save and quit in Vim (that’d probably take about 3 weeks of studying the documentation). Much easier to change the editor for git to Notepad++ or some other more familiar text editor.

The first step is to create a bat file (I called mine npp.bat) with the path to Notepad++ and the appropriate switches:

"C:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"

This opens a new instance of Notepad++ with no sessions or tabs or plugins which is what I want when writing a commit message. Place the bat file in the git subdirectory of msysgit (c:\msysgit\git on my system) and then set the core.editor variable in git config:

git config --global core.editor c:/msysgit/git/npp.bat

See this answer on Stackoverflow for more variations on this.

The next step is to get Notepad++ to work from the command line. I want to be able to write:

notepad .bashrc

and Notepad++ should open the .bashrc file. This is actually nothing to do with msysgit and involves replacing the default notepad application with Notepad++ in Windows. And the easiest way is to use a program called Notepad Replacer. Install this and voila, it now works. If you ever want to revert to the default notepad then just uninstall Notepad Replacer.

The other option is create a script file just like the npp.bat file but without the switches and name it npp with no extension and place it in the bin folder. Now I can write:

npp .bashrc

and it opens the file in Notepad++. This approach also works for any third party editor such as Word or Excel.

EDIT: An alternative way to achieve the same result is to install Gitpad to change the default editor to Notepad (or the default text editor).

Git for Windows tip: Setting shell aliases with msysgit

As msysgit uses a bash shell, you can set really handy aliases for the different git commands. For example gs instead of git status and ga instead of git add are the ones I use the most. Check out the great Git Immersion tutorial from Edgecase for a list of aliases. I can also recommend creating the got and get aliases. It’s amazing how often I write got instead of git.

Using the bash shell can be unfamiliar to a Windows user. So if you want to set aliases in msysgit it is not very obvious where they should go. They could go into the .bashrc file or the bash_profile file. From a linux perspective the bash_profile configuration file is executed when logging in while the .bashrc file is executed every time a bash shell window is opened. The .bashrc file is located in the c:\Users\YourUsername directory and the bash_profile file is in the etc directory. In the case of msysgit I fail to see that there is any real practical difference; both are executed when you start up msysgit. So I just used .bashrc and it works great.

If there is no file named .bashrc then you’ll have to create it. Windows 7 will not allow you to create a file with a dot (period) as the first character in the filename and with no extension. So the easy way to do this is to name your file .bashrc. (with an extra dot at the end) and Windows will automatically rename the file to just .bashrc (with no dot at the end).

My .bashrc file (almost identical to the Git Immersion profile file that is linked above):

alias less='less -r'
# --show-control-chars: help showing Korean or accented characters
alias ls='ls -F --color --show-control-chars'
alias ll='ls -l'
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'
alias got='git '
alias get='git '