Git - Configuration

Git - Configuration

Configuration of Git

After installing Git, configuration is a crucial step that tailors Git to work according to your preferences and environment. Git configuration allows you to set user identity, define aliases, choose a default editor, manage merge tools, handle file line endings, and more. This document provides a comprehensive guide to Git configuration, covering global and repository-level settings, configuration files, common parameters, and best practices.

Understanding Git Configuration Levels

Git allows settings to be applied at different levels:

  • System Level: Applies to all users on the system. Located at /etc/gitconfig.
  • Global Level: Applies to the current user. Stored in the user’s home directory, usually ~/.gitconfig.
  • Local Level: Applies to the current repository. Stored in .git/config within the repository.

View Git Configuration

git config --list

View Specific Scope

git config --system --list
git config --global --list
git config --local --list

View Value of a Specific Key

git config user.name

Setting User Information

Set User Name and Email

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Override for a Specific Repository

cd myproject/
git config user.name "Project Specific Name"
git config user.email "project.email@example.com"

Check Current Settings

git config --global user.name
git config --global user.email

Configuring Default Text Editor

Set Nano as Default

git config --global core.editor "nano"

Set VS Code as Default

git config --global core.editor "code --wait"

Set Vim as Default

git config --global core.editor "vim"

Working with Aliases

Aliases simplify frequently used commands.

Common Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'

Usage

git co main
git br
git cm -m "New commit"
git last

Core Git Settings

Enable Colored Output

git config --global color.ui auto

Set Default Branch Name

git config --global init.defaultBranch main

Enable Filemode

git config --global core.fileMode false

Enable Symlinks

git config --global core.symlinks true

Line Ending Configuration

Handling line endings is important when working across Windows and UNIX systems.

Convert CRLF to LF on Commit (Recommended on Windows)

git config --global core.autocrlf true

Do Not Modify Line Endings (Recommended on UNIX/macOS)

git config --global core.autocrlf input

Disable Conversion

git config --global core.autocrlf false

Global Git Ignore File

Create a Global Ignore File

touch ~/.gitignore_global

Add Patterns

# Inside ~/.gitignore_global
*.log
.DS_Store
node_modules/
.env

Set the Global Ignore

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

Merge and Diff Tools

Set a Merge Tool

git config --global merge.tool meld

Set a Diff Tool

git config --global diff.tool meld

Use External GUI Tool

git mergetool
git difftool

Credential Management

Enable Credential Helper (cache for 15 minutes)

git config --global credential.helper cache

Set Cache Timeout (e.g., 1 hour)

git config --global credential.helper 'cache --timeout=3600'

Store Credentials Permanently (insecure)

git config --global credential.helper store

GPG Signing of Commits

Enable GPG Signing

git config --global commit.gpgsign true

Set GPG Key

git config --global user.signingkey ABC123456789

Check If a Commit is Signed

git log --show-signature

SSH Configuration

Use SSH Instead of HTTPS

git remote set-url origin git@github.com:username/repo.git

Configure SSH Key

ssh-keygen -t rsa -b 4096 -C "you@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Git Templates

Set a Custom Template Directory

git config --global init.templateDir '~/.git-template'

Create a Hook in Template Directory

mkdir -p ~/.git-template/hooks
echo -e "#!/bin/sh\necho 'New repo created!'" > ~/.git-template/hooks/post-init
chmod +x ~/.git-template/hooks/post-init

Initialize a New Repo Using Template

git init

Managing Configuration Files Directly

Open Global Config

nano ~/.gitconfig

Sample Content of .gitconfig

[user]
    name = John Doe
    email = john@example.com
[core]
    editor = nano
    autocrlf = input
[color]
    ui = auto
[alias]
    co = checkout
    br = branch
    cm = commit
    st = status
[credential]
    helper = store

Advanced Configuration Options

Enable Rebase by Default on Pull

git config --global pull.rebase true

Show Only One-Line Logs

git config --global format.pretty oneline

Set Pager for Logs

git config --global core.pager 'less -F -X'

Resetting Configuration

Unset a Config Value

git config --global --unset user.name

Remove All Global Configuration

rm ~/.gitconfig

Remove Repository Configuration

rm .git/config

Scope Precedence

Git checks configuration in the following order:

  1. Repository (.git/config)
  2. Global (~/.gitconfig)
  3. System (/etc/gitconfig)

Customizing Git Prompt (Optional)

Enable Git Prompt in Bash

echo 'source /usr/share/git/git-prompt.sh' >> ~/.bashrc
echo 'PS1='\''[\u@\h \W$(__git_ps1 " (%s)")]\\$ '\'' ' >> ~/.bashrc
source ~/.bashrc

Best Practices

  • Always configure username and email before committing
  • Use aliases for productivity
  • Use a secure credential manager
  • Set global ignore rules
  • Enable GPG signing for secure commits

Git configuration allows you to personalize your development environment, secure your repositories, and streamline daily operations. By setting your username, editor, aliases, credentials, merge tools, and more, you can drastically improve productivity and avoid common pitfalls. Understanding how to control configuration at various levels (local, global, system) is key to managing complex projects and teams effectively.

Once configured, Git becomes not just a tool, but a powerful extension of your workflow. Take the time to explore and refine your Git settings for a more efficient and secure development experience.

Beginner 5 Hours
Git - Configuration

Configuration of Git

After installing Git, configuration is a crucial step that tailors Git to work according to your preferences and environment. Git configuration allows you to set user identity, define aliases, choose a default editor, manage merge tools, handle file line endings, and more. This document provides a comprehensive guide to Git configuration, covering global and repository-level settings, configuration files, common parameters, and best practices.

Understanding Git Configuration Levels

Git allows settings to be applied at different levels:

  • System Level: Applies to all users on the system. Located at /etc/gitconfig.
  • Global Level: Applies to the current user. Stored in the user’s home directory, usually ~/.gitconfig.
  • Local Level: Applies to the current repository. Stored in .git/config within the repository.

View Git Configuration

git config --list

View Specific Scope

git config --system --list git config --global --list git config --local --list

View Value of a Specific Key

git config user.name

Setting User Information

Set User Name and Email

git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

Override for a Specific Repository

cd myproject/ git config user.name "Project Specific Name" git config user.email "project.email@example.com"

Check Current Settings

git config --global user.name git config --global user.email

Configuring Default Text Editor

Set Nano as Default

git config --global core.editor "nano"

Set VS Code as Default

git config --global core.editor "code --wait"

Set Vim as Default

git config --global core.editor "vim"

Working with Aliases

Aliases simplify frequently used commands.

Common Aliases

git config --global alias.co checkout git config --global alias.br branch git config --global alias.cm commit git config --global alias.st status git config --global alias.last 'log -1 HEAD'

Usage

git co main git br git cm -m "New commit" git last

Core Git Settings

Enable Colored Output

git config --global color.ui auto

Set Default Branch Name

git config --global init.defaultBranch main

Enable Filemode

git config --global core.fileMode false

Enable Symlinks

git config --global core.symlinks true

Line Ending Configuration

Handling line endings is important when working across Windows and UNIX systems.

Convert CRLF to LF on Commit (Recommended on Windows)

git config --global core.autocrlf true

Do Not Modify Line Endings (Recommended on UNIX/macOS)

git config --global core.autocrlf input

Disable Conversion

git config --global core.autocrlf false

Global Git Ignore File

Create a Global Ignore File

touch ~/.gitignore_global

Add Patterns

# Inside ~/.gitignore_global *.log .DS_Store node_modules/ .env

Set the Global Ignore

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

Merge and Diff Tools

Set a Merge Tool

git config --global merge.tool meld

Set a Diff Tool

git config --global diff.tool meld

Use External GUI Tool

git mergetool git difftool

Credential Management

Enable Credential Helper (cache for 15 minutes)

git config --global credential.helper cache

Set Cache Timeout (e.g., 1 hour)

git config --global credential.helper 'cache --timeout=3600'

Store Credentials Permanently (insecure)

git config --global credential.helper store

GPG Signing of Commits

Enable GPG Signing

git config --global commit.gpgsign true

Set GPG Key

git config --global user.signingkey ABC123456789

Check If a Commit is Signed

git log --show-signature

SSH Configuration

Use SSH Instead of HTTPS

git remote set-url origin git@github.com:username/repo.git

Configure SSH Key

ssh-keygen -t rsa -b 4096 -C "you@example.com" eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

Git Templates

Set a Custom Template Directory

git config --global init.templateDir '~/.git-template'

Create a Hook in Template Directory

mkdir -p ~/.git-template/hooks echo -e "#!/bin/sh\necho 'New repo created!'" > ~/.git-template/hooks/post-init chmod +x ~/.git-template/hooks/post-init

Initialize a New Repo Using Template

git init

Managing Configuration Files Directly

Open Global Config

nano ~/.gitconfig

Sample Content of .gitconfig

[user] name = John Doe email = john@example.com [core] editor = nano autocrlf = input [color] ui = auto [alias] co = checkout br = branch cm = commit st = status [credential] helper = store

Advanced Configuration Options

Enable Rebase by Default on Pull

git config --global pull.rebase true

Show Only One-Line Logs

git config --global format.pretty oneline

Set Pager for Logs

git config --global core.pager 'less -F -X'

Resetting Configuration

Unset a Config Value

git config --global --unset user.name

Remove All Global Configuration

rm ~/.gitconfig

Remove Repository Configuration

rm .git/config

Scope Precedence

Git checks configuration in the following order:

  1. Repository (.git/config)
  2. Global (~/.gitconfig)
  3. System (/etc/gitconfig)

Customizing Git Prompt (Optional)

Enable Git Prompt in Bash

echo 'source /usr/share/git/git-prompt.sh' >> ~/.bashrc echo 'PS1='\''[\u@\h \W$(__git_ps1 " (%s)")]\\$ '\'' ' >> ~/.bashrc source ~/.bashrc

Best Practices

  • Always configure username and email before committing
  • Use aliases for productivity
  • Use a secure credential manager
  • Set global ignore rules
  • Enable GPG signing for secure commits

Git configuration allows you to personalize your development environment, secure your repositories, and streamline daily operations. By setting your username, editor, aliases, credentials, merge tools, and more, you can drastically improve productivity and avoid common pitfalls. Understanding how to control configuration at various levels (local, global, system) is key to managing complex projects and teams effectively.

Once configured, Git becomes not just a tool, but a powerful extension of your workflow. Take the time to explore and refine your Git settings for a more efficient and secure development experience.

Related Tutorials

Frequently Asked Questions for GitHub

Teams use GitHub for version control, code sharing, pull requests, and project management.

SSH allows secure communication with GitHub for pushing and pulling code without passwords.

A release marks a specific version of code, often used for deployments or tagging milestones.

Git is a distributed version control system for tracking changes in source code efficiently.

It shows the current state of the repository, including staged, unstaged, and untracked files.


GitHub Pages hosts static websites directly from a GitHub repository.

Git is a tool; GitHub is a platform using Git for remote code collaboration.

Use git revert <commit> to undo changes by creating a new commit.

git commit saves staged changes to the local repository with a message.


Issues track bugs, tasks, or feature requests, allowing discussion and assignment.

Merging combines changes from different branches into one branch, typically main or master.


git push uploads local repository changes to a remote repository like GitHub.

GitHub Actions automates workflows like building, testing, and deploying code with CI/CD pipelines.

.gitignore specifies files and directories Git should ignore and not track.

git init initializes a new Git repository in your local project directory.

git add stages changes in files for the next commit.

A pull request proposes changes from one branch to another, usually for review and merge.

A branch allows parallel development by creating independent code versions from the main project.

GitHub is a cloud-based platform for hosting and managing Git repositories collaboratively.

The default branch name is usually main, previously known as master.

Cloning downloads a copy of a GitHub repository to your local machine.

git pull fetches and merges changes from a remote repository to your local branch.

A commit records a snapshot of file changes with a message and unique ID.

A repository stores project files, folders, and version history for collaborative development.

A fork creates a personal copy of another user's repository for independent development.


A GitHub milestone is a way to track progress on a specific goal or release by grouping related issues and pull requests.

To merge a pull request, review the proposed changes and click "Merge pull request" to integrate them into the base branch.

GitHub labels are tags that help categorize and prioritize issues and pull requests, making it easier to manage and filter them.​

To create a GitHub issue, navigate to the "Issues" tab of your repository and click "New issue."

After making changes in your forked repository, navigate to the original repository and click "New pull request" to propose your changes.

A merge conflict occurs when GitHub cannot automatically merge changes due to conflicting modifications in the same part of a file.​

To use GitHub Actions, create a YAML file in the .github/workflows directory of your repository to define your workflow.

To resolve a merge conflict, manually edit the conflicting files to combine changes, then commit the resolved files.

A branch in GitHub is a parallel version of a repository, allowing you to work on different features or fixes without affecting the main codebase.​

To add a collaborator, go to your repository's settings, select "Collaborators," and enter the GitHub username of the person you want to add.​

A GitHub Gist is a simple way to share code snippets or text, useful for sharing small pieces of code or notes.

A fork creates a personal copy of someone else's repository, allowing you to propose changes. A clone creates a local copy of a repository on your machine.​

To create a GitHub repository, log in to your GitHub account, click the "+" icon in the top right corner, and select "New repository."

To set up GitHub Pages, navigate to your repository's settings, scroll to the "GitHub Pages" section, and select the source branch.

To create a GitHub Gist, log in to your GitHub account, click the "+" icon, and select "New Gist."

A GitHub organization is a shared account where multiple people can collaborate on repositories, issues, and other GitHub features.​

The GitHub CLI is a command-line interface that allows you to interact with GitHub directly from your terminal, enabling operations like creating issues and pull requests.

o use GitHub Copilot, install the extension in a supported IDE, such as Visual Studio Code, and start typing code to receive suggestions.

To create a GitHub organization, click your profile picture in the top right corner, select "Your organizations," and click "New organization."

GitHub Copilot is an AI-powered code completion tool developed by GitHub in collaboration with OpenAI, providing suggestions as you code.​

GitHub is a web-based platform for version control and collaboration, allowing developers to host and review code, manage projects, and build software together.​

To install the GitHub CLI, download the appropriate version for your operating system from the official GitHub CLI website and follow the installation instructions.

line

Copyrights © 2024 letsupdateskills All rights reserved