How to setup Git the proper way! — Part-3: Setting up ssh-agent and public email
Tags: github, gitforbeginners, git, sshagent
Published: 2021-02-20 17:20:33

---
title: How to setup Git the proper way! — Part-3: Setting up ssh-agent and public email
published: true
date: 2021-02-20 15:57:22 UTC
tags: Github,gitforbeginners,git,sshagent
canonical_url: https://medium.com/@techyarsal/how-to-setup-git-the-proper-way-part-3-setting-up-ssh-agent-and-public-email-b2b3c7a4e5ff
---

### How to setup Git the proper way! — Part-3: Setting up ssh-agent and public email

![](https://cdn-images-1.medium.com/max/1024/0*SasNgcBPDdFYQD7K)<figcaption>Photo by <a href="https://unsplash.com/@yancymin?utm_source=medium&amp;utm_medium=referral">Yancy Min</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Hi, this is the third and final part of this series. Here we will set up the **ssh-agent** to start automatically and store our **SSH** passphrase **for the current session**. That way, we don’t have to enter our **SSH** passphrase every time we access our **remote repository**. Plus, we will also use our **public** [**Github**](https://github.com/) **email** with git so that our **private email address** is not visible in our commit history.

### 1: Setup ssh-agent and add your keys

Now that we have successfully set up **SSH** key with our [**Github**](https://github.com/) account, here are the steps for starting **ssh-agent** automatically and let it manage our **SSH**  keys.

- Select and copy the following code:

```
env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; 
}

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running

agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [! "$SSH_AUTH_SOCK"] || [$agent_run_state = 2]; then
    agent_start
    ssh-add
elif ["$SSH_AUTH_SOCK"] && [$agent_run_state = 1]; then
    ssh-add
fi

unset env
```

- Now, we need to edit our  **.bashrc** file. Run the following command, which will open up the  **.bashrc** file in the **nano** text editor:

```
$ nano ~/.bashrc
```

- **Make sure you are on a blank line** , and then **right-click** and paste the code that you copied from above. If you are on **Git Bash** , you can paste by pressing _the_ **INSERT** key (or **SHIFT** + **INSERT** , if that doesn’t work).
- Once the code is pasted in the file, press **CTRL** + **X** from your keyboard and it will ask you if you want to save the changes. Type **y**, and press **Enter**. The editor will save the file and then exit.
- Now, run the following command to source/apply the code that we added into  **.bashrc**.

```
$ source ~/.bashrc
```

- It will start the **ssh-agent** and ask you for an **SSH passphrase** for your key(s), that you created while generating **SSH** keys in [**part-2**][part-2].

**That’s it, that’s all you need to do! The ssh-agent will start automatically when you launch your terminal.**

**Note: You still have to provide your SSH passphrase** , but only when you run your terminal **for the first time** after a logout or reboot. The prompt may look something like this:

```
> Initializing new SSH agent...
> succeeded
> Enter passphrase for /c/Users/_you_/.ssh/id_rsa:
> Identity added: /c/Users/_you_/.ssh/id_rsa (/c/Users/_you_/.ssh/id_rsa)
> Welcome to Git (version _1.6.0.2-preview20080923_)
>
> Run 'git help git' to display the help index.
> Run 'git help ' to display help for specific commands.
```

The ssh-agent process will continue to run **until you log out, shut down your computer, or kill the process.**

### 2: Use public Github email for commit identification

Well, at first we specified our **private email address** with git and if we commit anything and then push it to [**Github**](https://github.com/), it will publicly display our **private email address**. Now, here we will see how we can avoid that, while still being able to identify ourselves with [**Github**](https://github.com/). For that, we just need a few things:

- Open up your [**Github**](https://github.com/) account. Click on your profile image icon on the top-right corner and select the **Settings**  option.
- In the settings, select the **Emails** option. There you will see many options related to your email address.
- Scroll down to the **Keep my email addresses private** check-box, and **make sure it’s checked** if you don’t want your email addresses to be publicly visible.
- You will see some text is written below that check-box. There you will find an email address, that looks like this:

**some-numbers+username@users.noreply.github.com**

For example, in my case this is what it looks like:  
**76753986+techyArsal@users.noreply.github.com**

![my public no-reply email address on github](https://cdn-images-1.medium.com/max/782/1*nAbeh8EEyYF7gUp-2ur8rA.png)

- Just **select and copy** this email address.
- Then open up your **terminal** and paste the following command to set this email address with the **git** utility on your system:

```
$ git config --global user.email "_your\_email@example.com_"
```

Make sure to replace **email@example.com** with your **no-reply email address** that you copied from [**Github**](https://github.com/) **’** s email section.

- Now to make sure everything’s set up properly, run the following command and make sure that **user.name** and **user.email** match with your **username** and **no-reply email address**. In my case, that would look something like this:

```
$ git config --list
> user.name=techyArsal
> user.email=[76753986+techyArsal@users.noreply.github.com](mailto:76753986+techyArsal@users.noreply.github.com)
...
```

If that’s correct, then **congratulations!** You have successfully configured everything. From here on, all your commits will be recognized by your [**Github**](https://github.com/) username and profile. And you don’t have to share your **email address** publicly to do that!

I know that was quite some work, but trust me, it’s absolutely worth it!

#### Thanks for reading! I would really appreciate your thoughts and views on this. If there’s anything you want to add or share, please leave a comment.

[part-2]: https://dev.to/techyarsal/how-to-setup-git-the-proper-way-part-2-setting-up-ssh-key-mfb