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
Go to GitHub and log in to your account.
Click on the + icon in the top right corner and select New repository.
Minimize image
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.
2. Clone the Repository to Your Amazon Linux Machine
Connect to your Amazon Linux machine.
Install Git (if not already installed):
sudo yum update -y
sudo yum install git -y
3. Clone your repository from GitHub:
- Go to your GitHub repository and copy the URL of the newly created 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
3. Make Changes to a File and Commit Them
- Create a new file or edit an existing one. For example:
vi Temperature.py
2. Check the status of your repository:
This will show which files are modified, added, or untracked.
git status
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.
5. Commit the changes:
git commit -m "added Temperature.py"
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"
7. View the commit history:
Use git log to see the commit history.
git log
4. Revert a Commit:
- 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>
2. Use git log again to see the updated commit history.
5. Revert the Revert:
- 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>
6. Push the Changes Back to GitHub
- Before pushing, go to your GitHub account settings, navigate to Developer Settings, then Personal Access Tokens (classic), and click Generate new token.
2. Ensure to checkmark repo to give the token access to repositories.
3. After generating your token, make sure to copy it.
4. Push your changes to the GitHub repository:
git push origin main
- 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.
Subscribe to my newsletter
Read articles from Muzammil Jan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Muzammil Jan
Muzammil Jan
Software Engineering student at Dha Suffa University, Karachi. Exploring the world of DevOps & Cloud! Love learning & giving back to open source communities. Connect with me on https://www.linkedin.com/in/muzammiljan/