Learn about Git versioning tool + GitHub integration.
Hi to see how Git works I think we should start by knowing what it isso lets dive on it.
In this guide we'll learn Git essentials workflow and commands.
Git is a version control system (VCS) designed to manage and track changes to code over time. It allows multiple people to collaborate on a project, manage different versions, and keep a history of changes.
GitHub is a web-based platform that hosts Git repositories and provides tools for collaboration, like issue tracking, pull requests, and code reviews.
Windows:
macOS:
brew install git
Linux:
sudo apt install git
After installing, configure Git with your user information:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
You can check your configuration with:
git config --list
Create a new Git repository in your project folder:
git init
To clone an existing GitHub repository:
git clone https://github.com/username/repo-name.git
See which files have changed:
git status
Stage files (prepare them for commit):
git add <filename> # To add specific files
git add . # To add all changes
Commit your changes (save the changes to history):
git commit -m "Describe your changes"
View a list of commits:
git log
After committing, push changes to your GitHub repository:
git push origin main # or master depending on the branch
Get the latest changes from the remote repository:
git pull origin main
Branches allow you to work on different features or bug fixes without affecting the main project. To create a new branch:
git checkout -b feature-branch-name
Switch between branches:
git checkout main
To merge your branch into main
, first switch to main
:
git checkout main
git merge feature-branch-name
When multiple people make changes to the same file, Git may encounter conflicts. Conflicts need to be resolved manually:
git add conflicted-file
git commit -m "Resolved conflict"
A Pull Request (PR) is a request to merge changes from one branch into another (usually from your branch into main
).
Push your branch to GitHub:
git push origin feature-branch-name
Create a pull request on GitHub:
Before merging, other developers can review your code. They might suggest changes, which can be made by updating your branch and pushing the changes.
Using SSH allows you to securely interact with your GitHub repository without needing to enter your password every time you push or pull changes.
Generate a new SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
When prompted, press Enter to accept the default location. Optionally, you can set a passphrase for extra security.
Start the SSH agent:
eval "$(ssh-agent -s)"
Add your SSH key to the agent:
ssh-add ~/.ssh/id_ed25519
Copy your SSH key to the clipboard:
cat ~/.ssh/id_ed25519.pub
Copy the entire output.
Now, your GitHub account is configured to use SSH for secure communication.
GitHub allows third-party applications to access your account to automate workflows or provide additional tools.
You can always revoke or modify permissions from the same settings page.
GitHub has deprecated password-based authentication for Git operations, so a Personal Access Token (PAT) is used as a secure alternative for authentication.
Go to GitHub:
Access Developer Settings:
Generate a New Token:
Set Token Scopes:
repo
: Full control of private repositories.workflow
: Write access to actions and workflows.admin:repo_hook
: Read/write access to repository hooks.Generate and Store the Token:
To use your personal access token, you’ll replace your GitHub password with the token when performing Git operations:
git clone https://github.com/username/repo.git
When prompted for your password, use the personal access token instead of your password.
review before merging to main
.
main
can lead to conflicts and unstable code.This guide serves as a starting point for developers who are new to Git. Follow these steps and best practices to confidently manage your projects and collaborate effectively using Git and GitHub.