Git Configuration

Initial Setup

The first thing you need to do after installing Git is to set your default details as follows:
git config --global user.name "John Smith"
git config --global user.email "john.smith@example.com"
These details will apply to every repository you have on your machine. However sometimes you need a different e-mail address for personal or work stuff, in which case change into that repository's directory and execute this:
git config user.email "jsmith@example2.com"
You can add a --local to the previous command, if you prefer. If you need to confirm this is working as expected then the following will return the e-mail address based on your current location:
git config user.email
This is something you should really check before your first commit.

It is important to note that "local" overrides "global" which in turn overrides "system", hence setting a local e-mail address will override the global one. Please see Git Config for more git config related commands and options.

Author

Typically git will combine the username and e-mail address to make the author, which following on from the example about would be "John Smith <john.smith@example.com>".

Other Setup

git config --global http.proxy http://hostname:port - essential when you are behind a proxy server, note that this works when your Git repo is on https.

Aliases

Using aliases in git is a very handy option. Let's say you like to look at your git log in a specific way, for example, you often type the following:
git log --pretty=format:"[%an] at [%ad]%n [%h] %s" --max-count=5
The --max-count=5 can be shortened to -n 5 or even just -5 but the formatting is hard to remember, so let's create an alias for the logging command, with the format option.
git config --global alias.mylog 'log --pretty=format:"[%an] at [%ad]%n [%h] %s"'
Which can then be used thus:
git mylog -5
Hopefully you can see that in the command above "mylog" is replaced with the alias definition from the "git config" line, specifically the part between the single quotes and that this also allows the appending of the -5 to limit the rows. As this demonstrates aliases in git are both useful and flexible. You will notice the use of --global, this is because there is little point in putting this in a single repository. When the time comes to remove an alias then the following will remove it
git config --global --unset alias.mylog

Ignoring Files

So, the easy way to ignore a file is to add it to your .gitignore file. Usually this will be in the root directory of the repository and contain a list of files, file extensions and directories to be ignored. You can add comments to the files by starting a line with a #, which is a good idea, and allows you to remember why you are ignoring some files. You can even commit your .gitignore file to Git, that way, everyone is ignoring the same files. This is all explained at Git - gitignore Documentation.

However when working on a Mac you will probably find you need to keep ignoring files like .DS_Store which will apply to every Git repository you use. However you may not want this in your .gitignore as others might not be on a Mac. In which case you can use a computer wide ignore file as follows:

  • Enter this: vi ~/.gitignore
  • Add a couple of lines as follows:
    # Mac Specific
    .DS_Store
  • Enter the following command: git config --global core.excludesfile ~/.gitignore

You might prefer to call the file .gitignore-global but that is just personal preference. Oh and note that all .DS_Store files are ignored in all directories, from a Git perspective. It is worth looking at github/gitignore: A collection of useful .gitignore templates for some good example files.

Attribute Files

This sounds seem a resonably advanced topic that is documented at Git - gitattributes Documentation. If you see a files called .gitattributes then you have found the git attributes file. It is primarily used for setting which files are treated as text, which as binary and also how line endings are processed. It is also used for working with large files and the Git Large File Storage (LFS) extension.