Git Guide
Some tips and tricks on Git collected here and there.
Table of Contents
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:
- 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:
- Find a way to generate a token on the used remote - Eg. for GitHub that can be done over https://github.com/settings/tokens.
- Use generated token instead of the account password when accessing that site repos.
- See what credential helpers are available for Git installation -
$ git help -a | grep credential-Some common options here are:
Helper Description cacheIn-memory temporary storage storePlaintext file (usually ~/.git-credentials)managerWindows Credential Store wincredDeprecated in favor of manager - 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:
- All LF in the repo.
- On Windows machine, all CRLF except for the files marked as LF (eg. Bash scripts).
- On Linux machine, all LF except for the files marked as CRLF (eg. Visual Studio project file).
Steps to do this are:
- Set line ending conversion for commit/checkout:
- (on Windows) Set
core.autocrlf, so that all checked out text files have CRLF newline -$ git config --global core.autocrlf true - (on other LF-using platforms) These platforms do not require
conversion from Git default LF line endings, so:
- See that
core.autocrlfis unset orfalseas this option is intended for Windows use -$ git config --get core.autocrlf - In addition, check that
core.eolis unset ornativeas we will be using native line endings -$ git config --get core.eol
- See that
- (on Windows) Set
- 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):
- Create
.gitattributesfile in the root directory for files to be configured. - State expected line endings for required paths, eg.
*.sln text eol=crlf.
- Create
- 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 -