DEV Community

Arslan Ali
Arslan Ali

Posted on • Originally published at Medium on

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

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

Photo by Yancy Min on Unsplash

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 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 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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.

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.
Enter fullscreen mode Exit fullscreen mode

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, 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. For that, we just need a few things:

  • Open up your Github 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

  • 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_"
Enter fullscreen mode Exit fullscreen mode

Make sure to replace email@example.com with your no-reply email address that you copied from Github 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)
...
Enter fullscreen mode Exit fullscreen mode

If that’s correct, then congratulations! You have successfully configured everything. From here on, all your commits will be recognized by your Github 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.

Top comments (0)