Appendix C. Configuring Git

Over time, you will find little shortcuts that help you use Git at the command line. Personally I’ve found those who are the most frustrated with it are the ones with the least amount of customization. There are two types of configuration settings you will be making when working with Git: global settings, which apply to all repositories that you work on; and local settings, which only apply to the current repository. An example of a global setting might be your name, whereas your email might be customized based on personal projects and work projects.

Global settings are stored in the file ~/.gitconfig, and local settings are stored in the file .git/config for the specific repository you are working in. You will always be able to go back and edit your settings if you want to.

You can check to see what value is set. For example, Example C-1 shows you how to check what your name is set to.

Example C-1. Display a configured value
$ git config --get user.name

You can also get list of all values currently set (Example C-2).

Example C-2. Display all configuration values currently set
$ git config --list

A list of all variables is available from the command page for config. This is also available by running the command:

$ git help config

Identifying Yourself

In order to get credit for your work, you will need to tell Git who you are. We will store your name (Example C-3) and email (Example C-4) globally. Because it’s a global setting, you don’t need to be in a specific repository to make the change.

Example C-3. Configure your name
$ git config --global user.name 'Your Name'
Example C-4. Configure your email address
$ git config --global user.email '[email protected]'

It might be appropriate to use specific email addresses for some repositories (for example, if you are working on a work versus personal project). You can specify the changes should only be applied to a specific repository by completing the following steps:

  1. Navigate to the directory that holds the repository you want to configure.

  2. Apply the configuration command, substituting --global for --local.

For example:

$ git config --local user.email '[email protected]'

Changing the Commit Message Editor

By default, Git will use the system editor. On OS X and Linux, this is typically Vim. I really like Vim, so that’s what I use. It is a bit hardcore though, so you might want to change your editor to something else.

Check to see which editor Git will use by running the following command:

$ git config --get core.editor

You Must Quit to Commit

The commit will only be stored in Git when you quit the editor, not just save the commit message. This may affect your choice of text editors.

If you would like to use Textmate, use the following command:

$ git config --global core.editor mate -w

If you would prefer to use Sublime, use the following command:

$ git config --global core.editor subl -n -w

If you want to change the editor for Windows, you will need to include the full path to the application file. As applications are typically installed in the folder C:Program Files, you will need to wrap the path in quotes. Additionally, when you use Bash to call git config, you must quote the value, resulting in a double quoted string:

$ git config --global core.editor '"C:Program FilesVimgvim.exe" --nofork'

For additional editors, check the configuration instructions for your editor of choice.

Adding Color

Reading huge walls of text can be difficult. Add some color helpers to your command line to make it easier to see what Git is doing:

$ git config --global color.ui true
$ git config --global diff.ui auto

Customize Your Command Prompt

If you are working from the command line, you get zero clues about what is going on with your files, until you explicitly ask Git about them. This is tedious to keep having to ask. It’s like when you were eight and sat in the back of the car whining at the driver saying, “Are we almost there yet?”

Instead of having to explicitly ask, I’ve modified my command-line prompt to tell me which branch I currently have checked out and whether or not I’ve made changes to any of the files in my repository. This is a fairly common hack, but every developer will have their own little quirks on how they implement it. Searching the Web for “bash prompt git status” will yield lots of results. My own prompt is fairly simple, but others have added a lot more details to their prompt. For example: Show your git status and branch (in color) at the command prompt or local file status. As with all things technical, the more you add initially, the more you will need to debug if it does not work right away.

I have found the fancy prompts to be quite fussy to set up, and ended up giving up on the really detailed ones. I recommend starting with something really simple and then adding to it if you really need more information. The simple change in color, along with the name of the branch, actually suits me just fine and is less distracting without all the extra information.

Ignoring System Files

We have all done it: accidentally added one of OS X’s .DS_Store system files, or a temporary .swp text editor file. You can save yourself a little embarrassment by setting up a global ignore file so that Git prevents these files from being committed to any local repository you create or work on. A comprehensive list of files to ignore is available. Pick and choose the most appropriate for your system and your projects.

Once you have a list of the files you want to ignore, complete the following steps:

  1. Create a new text file named .gitignore_global and place it in your home directory.

  2. Notify Git of the configuration file to use by running the following command:

$ git config --global core.excludesfile ~/.gitignore_global

You may also have project-specific files, or even output directories (such as build directories), that you don’t want to commit to your repository. For each repository, you can have a custom “ignore” file that will further limit which files can be tracked by Git:

  1. Create a new text file named .gitignore and place it in the root directory for your repository.

  2. To this file add the names of the files you want Git to never add to the repository. Each filename should have its own line. You can use pattern matching as well, such as *.swp for temporary editor files.

This change will need a new commit in your Git repository:

$ git add .gitignore
$ git commit -m "Adding list of files to be ignored."

Line Endings

This section is especially important if you work on a cross-platform team with developers on OS X, Linux, and Windows.

You should set the line endings globally, but adding the setting to each repository as well will ensure greater success for those who may not have explicitly set line endings:

$ git config --global core.autocrlf input

To explicitly have all contributors use the right line endings, you will need to add a .gitattributes file to your repository that identifies the correct line ending, text files that should be corrected, and binary files that should never be modified.

Create a new text file named .gitattributes in the root directory of your repository (the same directory the .git folder is in). An example of a new file is as follows:

# Set the default behavior for all files.
* text=auto

# List text files that should have system-specific line endings on checkout.
*.php text
*.html text
*.css text

# List files that should have CRLF line endings on checkout, and not
# be converted to the local operating system.
*.sln text eol=crlf

# List all binary files which should not be modified.
*.png binary
*.jpg binary
*.gif binary
*.ico binary

Add the file to the staging index:

$ git add .gitattributes

Commit the file to the repository:

$ git commit -m "Require the right line endings for everyone, forever."

Fixing Line Endings

If you are in the unfortunate position of having to standardize line endings mid-project, you will need to complete the following steps:

  1. Decide on the “official” line ending for your repository with your team.

  2. Edit each of the affected files to reset the line endings. When this happened to my “friend,” she used Vim and the setting :set ff=unix. You may prefer to reset the line endings by simply opening each of the files with your text editor and re-saving each file; or use a command line utility such as dos2unix.

  3. Add and commit the updated files to the repository.

  4. Add the file .gitattributes to your repository as described in the previous section.

  5. Push the changes to the code hosting server.

  6. Ask everyone else on your team to update their work using the command rebase so that the “bad” line endings are not reintroduced into the repository accidentally.

  7. Pour yourself a hot chocolate or whisky. You’ve earned it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset