Some tips and tricks on Git collected here and there.

Table of Contents

  1. Table of Contents
  2. Credentials Management
  3. Line Endings Configuration

Credentials Management

The question addressed here is - What I need to do to get my credentials stored in a safe way, and avoid getting prodded for them every time I access a remote like GitHub. And, switching to SSH key pair authentication is either inconvenient or not possible.

The procedure is this:

  1. Set up low-risk credentials - This is not Git-specific topic, but useful nevertheless. The idea is to use access tokens instead of an account password when accessing the site repo (GitHub, TFS). These tokens may have limited permitted operations scope, thus lowering damage done by them being exposed. Process here is:
    1. Find a way to generate a token on the used remote - Eg. for GitHub that can be done over https://github.com/settings/tokens.
    2. Use generated token instead of the account password when accessing that site repos.
  2. See what credential helpers are available for Git installation -
    $ git help -a | grep credential-
    

    Some common options here are:

    Helper Description
    cache In-memory temporary storage
    store Plaintext file (usually ~/.git-credentials)
    manager Windows Credential Store
    wincred Deprecated in favor of manager
  3. Set the credential helper -
    $ git config --global credential.helper <helper>
    

More info on this topic -

Line Endings Configuration

Ok, this is a common topic but still worth reiterating. Basically, aim is to get a consistent line endings for text files, both in the repository and in the working directory. These would be:

  1. All LF in the repo.
  2. On Windows machine, all CRLF except for the files marked as LF (eg. Bash scripts).
  3. On Linux machine, all LF except for the files marked as CRLF (eg. Visual Studio project file).

Steps to do this are:

  1. Set line ending conversion for commit/checkout:
    1. (on Windows) Set core.autocrlf, so that all checked out text files have CRLF newline -
      $ git config --global core.autocrlf true
      
    2. (on other LF-using platforms) These platforms do not require conversion from Git default LF line endings, so:
      1. See that core.autocrlf is unset or false as this option is intended for Windows use -
        $ git config --get core.autocrlf
        
      2. In addition, check that core.eol is unset or native as we will be using native line endings -
        $ git config --get core.eol
        
  2. Set repo configuration for files that should keep their line endings regardless of the platform (eg. Bash scripts when checked out on Windows, or Visual Studio project when checked out on Linux):
    1. Create .gitattributes file in the root directory for files to be configured.
    2. State expected line endings for required paths, eg. *.sln text eol=crlf.
  3. Renormalize line endings, and commit renormalized endings (if there is any) -
    (repo)$ git add --renormalize .
    (repo)$ git commit -m "Renormalize line endings"
    

More info on this topic -