3

# 3Installing Git

## Changing the Git default settings

Git Bash starts life, well, looking like a colourful version of MS-DOS 6.2; and it works in much the same way—thank God it’s so easy to learn.

### 3.4.1

Before we do anything else, we need to set ourselves up as a user on the local Git system. This means giving Git a user name and an email address.

The email address is purely an internal thing, Git will not use it as an actual email address, it will never send anything to it, try to verify it or pass it to any other party. It uses it to identify the user that has made a change to a file within a Git repository.

• The username and email address supplied here don’t need to match the username and email account used to set up your GitHub account (see § 4.1)—they don’t need to, but in my case they do, it just make identifying who has made changes easier.

Start Git Bash and at the $prompt enter the following commands: $ git config --global user.name "your-username"

Don’t actually enter your-username, that would be silly. Enter the username you want to use. My username is practicalseries-lab so I entered:

 $git config --global user.name "practicalseries-lab" You won’t get much of a response if you did it right, it just displays the$ prompt on the next line.

• Git Bash does this a lot—no news is good news.
If it hasn’t complained, it’s probably worked.
“You are in a maze of twisty little passages, all alike”.

Next comes the email settings—in much the same way:

 $git config --global user.email "your-email" In my case it’s: $ git config --global user.email "lab@practicalseries.com"

These commands are great, so blindingly obvious—so easy to learn don’t you think?

This is what it looked like on my screen (Figure 3.25):

How can I tell it’s worked? You ask.

The answer is you can’t, not yet, not until we’ve configured the text editor. That’s next:

### Configuring a default text editor

I’m assuming you are going to use Notepad++ as the default text editor for Git Bash (this is the one I installed in § 3.2.1). Notepad++ works well with Git, some of the others (Brackets†1 for example) don’t work so well.

†1 Just to clarify; I’m using Brackets as the interface to Git. When I say Brackets doesn’t work with Git, I mean as a command line editor in Git Bash. Brackets and its extensions manage Git in the Brackets development environment very well.

Ok, you’ll like this; it’s a trip down memory lane. To make Notepad++ the default text editor for Git Bash we have to adjust the Windows Environment Variable. I think the last time I had to do this was when I was using WordPerfect 5.1 and Lotus 123. It was 1989, I was 23, MS-DOS was the operating system of choice, Margret Thatcher was prime minister and my children hadn’t been born—happy times.

Environment Variables (can’t believe I’m talking about this—it’s like talking about my Grandmother’s mangle or outside lavatories), they still exist in Windows, though most programmes just set them up as part of the installation process or use the registry instead. In Windows, Environment Variables describe the computer environment within which programmes run; they define common names (the name of the computer for example) and default file extensions.

The best known Environment Variable is PATH, this contains a list of directory paths. If a user types a command without specifying the path, the operating system will search the current directory and then each of the paths listed in the PATH environment variable trying to find it. Git Bash relies on the PATH environment variable to find any programme we wish to execute—of course it does, that’s what programmes did in the eighties.

To make Notepad++ the default text editor for Git Bash, we must add the path to the notepad++ executable file to the PATH environment variable.

If you have a Notepad++ icon on your desk top, you can find the path by right clicking it and selecting . This opens the Shortcut dialogue box (Figure 3.26).

The information we need is in the box, it’s everything up to the last reverse oblique (i.e. everything but the filename), highlighted in blue in the above figure.

In my case (and probably your case too if you didn’t change where the programme was installed) it is:

Highlight it and copy it to the clipboard ().

Now to change the PATH Environment variable. To do this we need the system properties screen. On Windows 7, click the button and then right click and select . On Windows 10, right click the button and select . Alternatively, use the shortcut (this is the Windows key and the pause/break key—top right of your keyboard), this works on either.

The system properties screen looks like this (Windows 7 left and Windows 10 right):

In either case, click advanced system settings and then click (this is much the same on Windows 7 and Windows 10), Figure 3.29.

This opens the Environment Variable dialogue box:

Again this looks pretty much the same in Windows 7 and Windows 10. In the lower window () scroll down until you find the variable, highlighted in Figure 3.32.

Select the line and click edit—things are a little bit different now between Windows 7 and Windows 10. Windows 7 first:

In Windows 7 this opens a simple edit dialogue box (Figure 3.32). Click inside the box and cursor right to the end of the line (by default the box will be showing you the end of the line—there will be a lot of stuff in there).

Add a semicolon (;) and then hit to past in the path copied from the Notepad++ shortcut properties (Figure 3.26). In my case (and probably yours) I added:

Note the leading semicolon (this separates one path from another, see Figure 3.32).

That’s it, click repeatedly and close the System Properties window.

In Windows 10, thing are a bit easier. Clicking opens a slightly more intuitive edit dialogue box:

Click and paste the link in the empty line at the bottom (Figure 3.33).

Again, repeatedly click , close the System Properties window and we’re done.

#### Setting the Git Bash core text editor

Now if all that has worked, we can tell Git Bash to use Notepad++ as the default (core) text editor.

Start Git Bash and type the following easy to understand command:

 $git config --global core.editor "notepad++ -multiInst -nosession" And that’s it—“has it worked?” You ask. Well if it has worked when you type the following, you should see the global configuration file open in Notepad++ (this is the global file edit command, just go with it for now): $ git config --global -e

The file is the .gitconfig file I talked about earlier (§ 3.3.1). It looks like this:

Finally, we can see that the username and email is configured (§ 3.4.1) and that the core editor is set to Notepad++ (obviously, your file will have different user information).

While this file is open, there is something important to note:

If you look at your Git Bash window, it will look something similar to Figure 3.35:

You can see that Git Bash has not returned with the $prompt, Git Bash is waiting for the text editor to close and this is exactly what we want it to do. Generally, Git Bash should wait for the triggered application to finish before it moves on†2. Close Notepad++ and Git Bash will return to normal with a$ prompt indicating it is waiting for your next instruction.

Now, I don’t know about you, but this stuff doesn’t seem that easy to learn to me. I’m not sure I agree with their statement “Git is easy to learn”.

†2 This is exactly what it didn’t do with Atom, it just dropped through to the $prompt. I think this is a problem with Git (or Atom itself) running under Windows. It is for this reason that I’m not using Atom. ### 3.4.3 ### Configuring a default difference tool If you didn’t install the difference and merge tool in § 3.2.2 then don’t bother doing this, skip to section 4. When Git detects a conflict in a file (this happens when two different users have modified the same section of a particular file and Git doesn’t know which version to select, see § 2.4.1), it will try to display the conflict by using a difference or comparison tool. Git Bash has a rudimentary difference tool built into it, but it is limited and difficult to use. To overcome this, we can configure Git Bash to use a third party difference tool, these generally have a graphical interface, accept the same arguments as the built in tool and are much easier to use. In my case I use P4Merge, the thing we downloaded and installed in § 3.2.2. To tell Git Bash to use P4Merge as its diff tool, enter the following commands: $ git config --global diff.tool p4merge $git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"$ git config --global difftool.prompt false

You can cut and paste the commands from the above list. In Git Bash use:

• —insert selection from clipboard

• —copy selection to clipboard

• In the second line, the path C:/Program Files/Perforce/p4merge.exe is the path to the P4Merge executable; it’s probably the same on your machine if you didn’t change the defaults. If you did you will have to type in the correct path for your PC.

Again note that in Git Bash, any Windows reverse oblique (\) in the path must be replaced by an oblique (/).

To see if all this has worked, look in the global configuration fie by typing:

 $git config --global -e This time you should have: ### 3.4.4 ### Configuring a default merge tool The merge tool is going to be P4Merge (same as the difference tool), this is because the package does both functions. Merging is the process of resolving a conflict displayed with the diff tool; normally, this is by selecting one modification over another, again § 2.4.1. The commands for setting the default merge tool are identical to those used to set the default diff tool, but use the word merge instead of diff. Enter the following: $ git config --global merge.tool p4merge $git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"$ git config --global mergetool.prompt false

Again to see the changes have been made, edit the global configuration file:

 $git config --global -e That’s all the default files set up. ### 3.4.5 ### Defining an alias There’s one more thing to do. In Git bash, we can trigger Notepad++ just by typing its name at the$ prompt:

 $notepad++ But this is rather a lot to type each time. There is a fairly common abbreviation for Notepad++ and that is npp, better to just be able to type npp. To do this, we must assign an alias to notepad++. Aliases are assigned in another of the Git Bash configuration files, this time it is a file called .bash_profile. We’ll edit it from within Git Bash itself type: $ notepad++ ~/.bash_profile

This will open or create the .bash_profile file in Notepad++ (it will be empty).

alias npp='notepad++ -multiInst -nosession'