Storing GIT Credentials Locally

When working with Git repositories, it’s often convenient to store your credentials locally to avoid repeatedly entering your username and password or personal access token. Git provides several methods for managing credentials, including caching and using credential helpers. Here, we’ll discuss how to store Git credentials locally on different operating systems and how to set a token for each project folder.

Storing Git Credentials Locally

On Linux

To store Git credentials locally on a Linux system, you can use the git config command with the credential.helper setting set to cache. This will cache your credentials for a specified period, typically 15 minutes, before requiring you to re-enter them.

1
2
3
4
5
6
```bash
# Open your terminal and navigate to your Git repository folder
cd /path/to/your/git/repo

# Set Git to cache your credentials
git config --global credential.helper cache

### On macOS

On macOS, you can use the `osxkeychain` credential helper, which securely stores your Git credentials in the macOS Keychain. This way, you don't need to re-enter your credentials each time you interact with a Git repository.

```markdown
```bash
# Open your terminal and navigate to your Git repository folder
cd /path/to/your/git/repo

# Set Git to use the macOS Keychain as the credential helper
git config --global credential.helper osxkeychain

### On Windows

On Windows, Git also provides a credential helper that can store your credentials securely. However, it typically uses the Windows Credential Manager. To enable it, you can use the `wincred` helper:

```markdown
```bash
# Open your Git Bash or Command Prompt and navigate to your Git repository folder
cd C:\path\to\your\git\repo

# Set Git to use the Windows Credential Manager as the credential helper
git config --global credential.helper wincred

## Removing Stored Credentials

If you ever need to remove the stored credentials, you can use the following command:

```markdown
```bash
# Remove the credential helper configuration (Linux, macOS, and Windows)
git config --unset credential.helper

# Or, if you want to remove it globally
git config --global --unset credential.helper

## Setting a Token for Each Project Folder

To set a personal access token for a specific Git project folder, you can include it in the repository URL when you clone or modify the repository's configuration in the local `.git/config` file.

### Cloning with a Token

You can clone a Git repository with a personal access token directly in the URL like this:

```markdown
```bash
# Clone a repository with a token included in the URL
git clone https://[USERNAME]:[email protected]/john/shell-configuration.git

### Modifying the `.git/config` File

Alternatively, you can manually edit the `.git/config` file in your local repository and add the token to the remote URL. Here's an example:

```markdown
```bash
# Open the .git/config file in your repository
nano ~/.git/config

# Inside the file, find the [remote "origin"] section and modify the URL
[remote "origin"]
    url = https://[USERNAME]:[email protected]/example/example-server.git

Replace `[USERNAME]` with your Git username and `TOKEN` with your personal access token. This will set the token for that specific Git repository.

By following these steps, you can store Git credentials locally and set personal access tokens for specific project folders, enhancing security and convenience when working with Git repositories.
0%