Skip to content

Latest commit

 

History

History
228 lines (141 loc) · 10.5 KB

installing-git.md

File metadata and controls

228 lines (141 loc) · 10.5 KB

Installing and Configuring Git & GitHub

Now, let's be clear - Git is a tricky little sucker, even experienced developers have errors and make mistakes - don't believe me? Try Googling Git memes!

There are a several of different methods for installation, but once done you can create local repositories, implement version control and branching for your local projects, and more. With some additional configuration, such as authentication, you can commit files to remote repositories to which you have permission.

Check to see if git is already installed by typing git --version in your CLI (Powershell, Command Prompt, or Terminal):

CMD prompt

If you have an output similar to the above, you have Git installed - don't worry if your version doesn't match the example. If not installed, move to next step - if it is skip ahead.

Git Installation

Depending on your operating system, you have different options:

Unix based OSes

  • Debian based (e.g. Ubuntu) - Run the following command in your Terminal:

sudo apt install git-all

  • Fedora based (e.g. RHEL or CentOS) - Run the following command in your Terminal:

sudo dnf install git-all

MacOS

You will need the Homebrew package manager first, verify this with which brew or brew help.

If they return an error, find instructions to install it here.

With Homebrew installed use the following command in your MacOS Terminal:

brew install git

Windows

You have two options on Windows:

Winget

Winget is a Linux style package manager for Windows. You can use a single winget command to install the Git module, and use it through VSC code or the CLI. To install type the following command into Windows PowerShell:

winget install --id Git.Git -e --source winget

Dedicated Installer

The more traditional approach is to download the official installer from the GitHub website.

This approach will also give you some more utilities such as Git Bash which provides a Linux Bash terminal in Windows - very handy - although we can also do this with WSL these days.

Once installed type git --version or git version in your Terminal to confirm it returns a version number.

NOTE: You might need to close and reopen your Terminal if you do not get the expected output from git --version. If it still doesn't work you may also need to restart your device.

If all you need to do is clone public repositories, then you can stop here. For example, if you just need a copy of some resources and you don't intend to commit back to a branch you can do so with git clone [repository_name]. However, if you need to make changes to a repository in GitHub we need to do some additional configuration.

Git Configuration

Now you need a GitHub account, create one here.

Verify with your instructor which email address you should use to register a GitHub account, as different bootcamps have different requirements - It is recommended that you have a personal GitHub by graduation regardless, but use the one referenced by your instructor for your project(s).

Next you need to configure Git on your computer, run the following commands one by one within your terminal, the credentials you use should match the ones you used for your GitHub account.

git config --global user.name "[yourName]"
git config --global user.email "[yourEmail]"
git config --global init.defaultBranch main
git config --global core.pager cat
git config --global core.editor "nano"
git config --global pull.rebase false
git config --global fetch.prune true

These commands are setting up Git so it knows who you are and how you want it to behave. Here is a description of what each line is doing:

Command Description
git config --global user.name "Your Name" Is telling Git your name so you can label your work.
git config --global user.email "[yourEmail]" Tells Git your email, which also goes on your commits.
git config --global init.defaultBranch main Sets main as the name for the first branch whenever you start a new project
git config --global core.pager cat Makes Git show full command results right away, without pausing.
git config --global core.editor "nano" Sets nano as the editor Git will use when you need to type a message (like commenting on your commits). You can use vi/vim if you hate yourself!
git config --global pull.rebase false When you update your project from GitHub, this tells Git to merge changes instead of trying to re-organize them.
git config --global fetch.prune true Cleans up old branch references when you're syncing with GitHub, so your list stays tidy.

Some of this may not make sense for the moment, however you will likely be using Github a lot in future, so this will become more clear with time and practice.

Additional Config Commands

  • If you want to confirm or verify the settings you've applied, you can use the following command: git config --list This will display a list of all the current Git configuration settings.
  • If you want to confirm a specific setting Git is currently using, you can use: git config --get user.name This specific command will just show you the configured username. You can replace user.name with any other configuration key to get the respective value.
  • If you need to change a setting in your Git configuration, you can simply run the git config command again with the updated value.

Authenticating with GitHub

To use Git with GitHub we need to authenticate your local machine.

Although you can clone public repo’s without authenticating, to push to your own repositories (or others you've been given permission for) you need to generate some SSH keys.

Bash or Linux

If using Linux, or Windows with either WSL, or Git Bash, you can use the following steps to create an SSH key pair:

  1. In Git Bash generate an ssh key pair by adding your github email address to the following command:
ssh-keygen -t ed25519 -C "[your github email]”
  1. You will be asked where to save the key, press enter to save in the default location which is a hidden .ssh folder in your home directory. Note the key name and change dit if you wish. You will also be prompted to enter a passphrase to secure the key, this is good practice, but leave it blank on this occasion for ease of use.

  2. You now need to add the new key to both the ssh-agent on your computer, and to GitHub so that it trusts your commits. Start the ssh-agent by typing

eval "$(ssh-agent -s)"

You should receive a process ID (pid)

  1. If you view the file location you should see that two keys have been generated, a public ending in .pub, and a private (NOTE: If viewing from Windows File Explorer ensure that you have View > Hidden Items enabled). Add your new private key to the ssh agent with:
ssh-add ~/.ssh/[keyname]

PowerShell

If you are not using Git Bash or WSL you can use PowerShell:

  1. Generate an ssh key pair by adding your github email address to the following command:
ssh-keygen -t ed25519 -C "[your github email]”
  1. You will be asked where to save the key, press enter to save in the default location which is a hidden .ssh folder in your home directory. Note the key name. You will also be prompted to enter a passphrase to secure the key, but leave it blank on this occasion.

  2. Start the SSH Agent with

Start-Service ssh-agent

Confirm with

Get-Service ssh-agent
  1. Add Your Private Key to the Agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

Your private key can be used to verify messages sent with your public key, to facilitate this you need to add your public key to GitHub. If GitHub sends an authentication request encrypted with your public key, you can decrypt it with your private key, mathematically verifying your identify.

Add your Public Key to GitHub as follows:

  1. View your public key using cat [keyname] or a text editor, and copy the contents to your clipboard.

  2. In GitHub navigate to your profile, and find the SSH and GPG keys section. Select Add a new SSH Key, and paste the public key into the text box, then click Add Key.

Creating a Local Repository

Now you can start working from scratch by creating a new repository, or you can clone an existing one.

To convert a local working directory into a repository, navigate to the directory in your CLI (cd to change directory) and type git init. You may convert an existing directory ccontaining code and resources, or create a new one - however, if you create a new one, it needs some files in it to commit.

Confirm the repository has been created by checking for the hidden .git directory.

Use rm -rf .git (i.e. delete the hidden git directory) to revert back to normal.

Adding and Ignoring

Now you have:

  • Git installed
  • Git configured
  • Git Authenticated
  • A local repository with some code and/or data files

Git Add

Nearly there, we just need to tell git what files in our local repository we want it to monitor for changes, and send to git when we make a commit. We do this with git add and either the individual filenames, or most commonly simply git add . to track all files in the current directory.

Once run, confirm the tracked files with git status.

Gitignore

The .gitignore file is simply a text file in Git repositories that tells Git which files or folders to ignore and not track in version control. This helps keep your repository clean and prevents committing temporary files, sensitive data, or system-specific files. Below is an example:

# Secrets
secrets.yml
config/database.yml
*.pem
*.key
*.p12

# Python Project
__pycache__/
*.py[cod]
*.so
.Python
env/
venv/
.env
*.log
.idea/
.vscode/

You can now use git commit and git push to syncronise your local repository with GitHub. You will be prompted to provide a commit message, your first commit when you're populating your repo' is often just "initial commit".

Easy Mode

If you've gotten this far congrats... now for easy mode.

If you have git and Visual Studio Code installed, you can actually do everything from within VSC, including cloning repositories, committing, adding, merging, and lots more, even authenticating. Honestly, this is mostly how you'll do it in the real world.

So, why not do it this way from the start??? Well, there's this...

That's because with the CLI you really understand what's going on... with VSC you're just a user - eugh!