Git Basics: Essential Skills for Your Terraform Journey
Before we dive into Terraform, let's ensure you're comfortable with Git. This version control system will be crucial for managing your infrastructure code and collaborating with others.
1. Creating a New Repository
Git allows you to track changes in your code over time. Let's start by creating a new repository:
- Navigate to GitHub (opens in a new tab) and log in or create a new account if you don't have one.
- Click the + icon in the top-right corner and select New repository.
- Name your repository (e.g., terraform-training).
- Choose Public visibility.
Important: Do not commit any sensitive data to the repository as a best practice whether it is Public or Private.
- Check Add a README file.
- Under Add .gitignore select Terraform
- Click Create repository.
2. Setting Up SSH Keys
Note: If you are a Windows user, make sure to run the commands below in PowerShell, rather the Cmd Prompt. You do not need "Run as Administrator" unless specified
SSH keys provide a secure way to connect with GitHub:
-
Open your terminal.
-
Generate a new SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/terraformclub
At the "Enter passphrase" prompt you can leave it empty for no passphase.
-
Add the keys to the ssh-agent:
MacOS
- Start the ssh-agent:
eval "$(ssh-agent -s)"
- Check to see if your ~/.ssh/config file exists in the default location.
open ~/.ssh/config
If the file doesn't exist, create the file.
touch ~/.ssh/config
- Open your
~/.ssh/config
file, then modify the file to contain the following lines.
configHost github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/terraformclub
Windows
- Right-click Start Menu, select "Windows Powershell (Admin)"
Set-Service -Name ssh-agent -StartupType Automatic Start-Service -Name ssh-agent
-
Add your SSH key to the agent:
ssh-add ~/.ssh/terraformclub
-
Copy your public key:
cat ~/.ssh/terraformclub.pub
-
Go to GitHub Settings > SSH and GPG keys > New SSH key.
-
Paste your key and save.
3. Cloning Your Repository
Now, let's get your repository on your local machine. Assuming your repository name is terraform-training
:
git clone git@github.com:yourusername/terraform-training.git
cd terraform-training
4. Making Changes and Committing
As you work on your Terraform configurations, you'll need to commit your changes:
- Make some changes to your files.
- Stage your changes:
git add .
- Commit your changes:
git commit -m "Add initial Terraform configuration"
Tip: Write clear, concise commit messages to document your work effectively.
5. Pushing Your Changes
Finally, let's push your local commits to GitHub:
git push origin main
Next Steps
Congratulations! You've mastered the Git basics needed for this course. Remember to commit and push your work for regularly. In the next section, we'll start exploring Terraform fundamentals.