Git and GitHub on Amazon Linux: From Cloning Repositories to Committing Code

Git and GitHub on Amazon Linux: From Cloning Repositories to Committing Code

What is Git?

Git is a distributed version control system which is used to tracking changes in the source code, enabling multiple developers to work together on non-linear development.

It offers efficient branching and merging, enabling seamless collaboration and version control. Its distributed nature allows offline work with full project history on each user's machine.

What is Github?

GitHub is a web-based interface that that hosts Git repositories, allowing developers to store, manage, and collaborate on code projects. It offers tools for version control, issue tracking, and code review, making it a popular choice for open-source and private projects.

What is Version Control? What are the types of version control systems?

Version control is a system that tracks and manages changes to files over time. It allows multiple users to collaborate on a project by keeping a history of changes, enabling them to revert to previous versions, compare changes, and resolve conflicts when working on the same files.

Centralized Version Control Systems (CVCS):

In CVCS, a single central server holds all the versioned files, and users check out files from this central repository. All changes are committed back to this central location, which is the single source of truth.

Distributed Version Control Systems (DVCS):

In DVCS, every user has a full copy of the entire repository, including its history. Users can work independently, and changes can be pushed and pulled between different repositories, allowing for more flexible collaboration and offline work.

What are the benefits of using a DVCS instead of a CVCS?

Reliable backup copies: When a team member clones a repository, she essentially creates an offsite backup, so if something catastrophic happens, like a server crash, every team member’s local copy becomes a backup.

Parallel Development: DVCS supports better branching and merging, allowing multiple developers to work on different features simultaneously without stepping on each other's toes.

Offline Work: Developers can commit changes, view history, and create branches locally without needing a network connection. This makes it easier to work offline and sync changes later.

Better Collaboration: DVCS allows for more flexible workflows. Developers can work independently in their own local repositories, and then share their changes with others. This enables easier branching, merging, and more collaborative development practices.

A guide on how to create a new GitHub repository, clone it to your Amazon Linux machine, make changes, commit them, and then push those changes back to GitHub:

1. Create a New Repository on GitHub

  1. Go to GitHub and log in to your account.

  2. Click on the + icon in the top right corner and select New repository.

Minimize image

selecting New repo

3. Enter a repository name, and optionally a description.

4. Choose whether the repository should be Public or Private.

5. Optionally, add a README file, .gitignore, and a license.

6. Click Create repository.

repo created


2. Clone the Repository to Your Amazon Linux Machine

  1. Connect to your Amazon Linux machine.

  2. Install Git (if not already installed):

sudo yum update -y
sudo yum install git -y

git installation verified

3. Clone your repository from GitHub:

  • Go to your GitHub repository and copy the URL of the newly created repo.

copying github url repo

  • Run the following command, replacing your-username with your GitHub username and your-repository-name with the name of your repository:
git clone https://github.com/your-username/your-repository-name.git

4. Navigate into the cloned repository:

cd your-repository-name

switched to cloned repo


3. Make Changes to a File and Commit Them

  1. Create a new file or edit an existing one. For example:
vi Temperature.py

a sample code in python

2. Check the status of your repository:

This will show which files are modified, added, or untracked.

git status

untracked file

3. Add the file to the staging area:

git add Temperature.py

4. Check the status again:

Now, git status will show that the file is staged for commit.

tracked file

5. Commit the changes:

git commit -m "added Temperature.py"

file committed

6. Configure your Git username and email if you haven't done so before:

git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"

username/email configured

7. View the commit history:

Use git log to see the commit history.

git log

commit history


4. Revert a Commit:

  1. Use git revert followed by the commit hash to create a new commit that reverses the changes from the specified commit:
git revert <commit-hash>

commit reverted

2. Use git log again to see the updated commit history.

git log

5. Revert the Revert:

  1. If you want to undo the revert, use git revert on the revert commit to create a new commit that re-applies the original changes:
git revert <revert-commit-hash>

reverted the revert


6. Push the Changes Back to GitHub

  1. Before pushing, go to your GitHub account settings, navigate to Developer Settings, then Personal Access Tokens (classic), and click Generate new token.

new token generated

2. Ensure to checkmark repo to give the token access to repositories.

repo checkmark

3. After generating your token, make sure to copy it.

token copied

4. Push your changes to the GitHub repository:

git push origin main

pushed in github

  • Note: If your branch is not named main, replace main with your branch name.

5. If this is your first push, Git might ask for your GitHub credentials. When prompted for a password, paste your Personal Access Token.

6. To verify, navigate to your GitHub repositories and ensure your changes have been successfully pushed.

verified