Skip to main content

How to set up and configure Git for use with GitHub

·4 mins

white and black concrete building
3d octocat
Photo by Roman Synkevych 🇺🇦 on Unsplash.

I recently installed git on a new machine, the basic installation is painless, especially with Laragon. however, there are some additional steps required to configure git to work with GitHub. Notably adding an ssh key, setting the default branch to main, setting your no-reply email, code editor to VS Code and adding a global .gitignore.

Install git #

On Windows, my local development of choice is Laragon. Once Laragon is installed git is available from the command line using cmdr. To use other terminals simply Add Laragon to PATH.

Configure your user account #

Before any commits can be pushed you need to set your user account. Normally this would be your account and email address. When you push a project your email is public! This can lead to spam 😔. GitHub gives us a no-reply email address, which prevents our email from being exposed publicly.

To find your ID and USERNAME open GitHub > settings > emails, tick the option:

  • ☑️ Keep my email addresses private….

Under this tick box is a paragraph:

We’ll remove your public profile email and use ID+NAME@ users.noreply.github.com when performing web-based Git operations…

Copy the email (e.g. 12345678+Pen-y-Fan@users.noreply.github.com) from your id number.

Your USERNAME should be exactly as it is displayed on GitHub, including any capitalisation.

git config --global user.name "USERNAME"
git config --global user.email "ID+NAME@users.noreply.github.com"

Once you set your config you can verify the settings using:

git config --global -l
user.name=USERNAME
user.email=ID+USERNAME@users.noreply.github.com

Remember to tick the option ☑️ Keep my email addresses private.

More details: Setting your commit email address

Set the default branch name #

When you git init a new repo, the default branch should be main or trunk, this can be set in git:

git config --global init.defaultBranch main

You can check the global config:

git config --global -l

You should see:

init.defaultbranch=main

Background information GitHub abandons ‘master’ term to avoid slavery row.

Add an SSH key #

An SSH key allows you to pull and push code using an ssh key generated by your computer and the public key uploaded to GitHub. It is much easier, especially if you use two-factor authentication.

If you are using Laragon, SSH is installed, otherwise, it will be installed with git.

Create an SSH key:

ssh-keygen -t ed25519 -C "your_email@example.com"

If you wish to enter a passphrases enter your phrase twice, otherwise press enter for no phrase. Details: Working with SSH key passphrases

The key will generate several files and save them to a .ssh directory under in your profile. Change directory to the folder:

cd %USERPROFILE%\.ssh

Copy the id_ed25519.pub to clipboard:

clip < id_ed25519.pub

Open GitHub and navigate to GitHub > settings > SSH keys, New SSH key, give it a title, normally your computer name and paste in the key.

Back to your command line:

ssh -T git@github.com

If all is successfully you should get a message like:

Hi your-username! You've successfully authenticated, but GitHub 
does not provide shell access.

More information and troubleshooting SSH: Generating a new SSH key and adding it to the ssh-agent and Adding a new SSH key to your GitHub account

Setup a global .gitignore #

TL&DR;

When editing other projects your IDE config should not pollute the other project. It could also leak sensitive information! That project’s .gitignore should not care about your IDE.

First create a .gitignore file. Your profile is a good place, it could also be in the laragon user setting e.g. C:\laragon\usr\bin\.gitignore

notepad %USERPROFILE%\.gitignore

To ignore a Jet Brains IDE (such as PhpStorm), VSCode and any picture thumbs, add the following to the .gitignore:

.idea
.vscode
Thumbs.db

Add any other files, as per your local environment. e.g. .DS_Store on a mac.

Save the file, then add it to your global git config:

git config --global core.excludesfile %USERPROFILE%\.gitignore

From now on when you open any project, with your IDE or code editor, your IDE config folder will be ignored by git version control.

You can check the global config:

git config --global -l

The output should include this:

core.excludesfile=C:\Users\WINDOWS-USERNAME\.gitignore

Set the default editor #

By default, git uses the vim editor, if you ever get stuck in vim type :q!.

You can choose any default editor, to change to VS code:

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

You can check the global config:

git config --global -l

The full output should look similar to this:

init.defaultbranch=main
user.name=Pen-y-Fan
user.email=12345678+Pen-y-Fan@users.noreply.github.com
core.excludesfile=C:\Users\michal\.gitignore
core.editor=code --wait

Mac and Linux #

For Mac and Linux users most of the commands are the same, the profile directories will be different (e.g. ~/.ssh/ and ~/.gitignore), see the links for further information, many have the exact commends per OS.

Further options #

You may also consider enabling 2FA or even commit signature verification.