Hey DevOps explorer! π Today we're diving into Project 2 of my 7-Day DevOps Challenge, where I connect my Java web app hosted on AWS EC2 to GitHub using Git. By the end of this post, youβll understand how to:
β
Install Git on your EC2 instance
β
Set up a GitHub repo
β
Track and commit code
β
Push code from EC2 to GitHub using a personal access token
π Why This Matters
Version control is a must-have skill for any developer. Whether you're flying solo or collaborating on a team, Git and GitHub help you keep your code safe, traceable, and easy to manage.
βοΈ Step 1: Install Git on EC2
sudo dnf update -y
sudo dnf install git -y
Git is a version control tool that tracks your code changes. Installing it lets you start managing your code history on the server itself.
π Step 2: Set Up GitHub & Create a Repo
Head to GitHub, create a free account (if you havenβt), and start a new repository β this is where your code will live in the cloud.
π‘ A GitHub repository is like a magic folder that tracks every change in your project.
π± Step 3: Initialize Local Git Repo
git init
This sets up Git inside your project folder on EC2, creating a master
branch.
βοΈ Step 4: Track and Commit Code
git add .
git commit -m "Initial commit"
git add .
stages your files, and git commit
saves the snapshot. Think of it as telling Git, βHey, these are the changes I care about.β
π Step 5: Link to GitHub and Push
First, copy the HTTPS URL from your GitHub repo. Then:
git remote add origin https://github.com/your-username/your-repo-name.git
git branch -M master
git push -u origin master
Oops! Git might ask for your username and password, but GitHub no longer accepts passwords.
π Step 6: Use a GitHub Token Instead
Go to GitHub β Settings β Developer Settings β Personal Access Tokens.
Generate a token with repo
access and copy it. Use it when Git asks for a password.
π A GitHub token is a secure key that acts like a password for Git operations.
π‘ Bonus: Editing & Pushing Again
I updated index.jsp
, then ran:
git add .
git commit -m "Updated homepage"
git push
And just like thatβmy changes appeared live in the GitHub repo. Magic!
π What I Learned
Services used: EC2, Git, GitHub, VS Code
Concepts learned: version control, repo setup, staging/commits, GitHub tokens
β± Project Reflection
This project took me around 2 hours. The trickiest part? Setting up GitHub authentication.
The best part? Watching my code go live in my repo with one command.
This is just the beginning. Stay tuned for Project 3 tomorrowβCI/CD, here we come! π₯
Top comments (0)