As a developer, one of the most important skills one must learn is the art of managing and working with large codebases while collaborating with other developers, ensuring the code you write is well documented and free of bugs.
Open source programs allow young developers to learn this specific art, and with Hacktoberfest officially kicking off this month, there's no better opportunity to have this session on Git and GitHub to help you take your first steps into the world of open source with us.
More about open source programs will be discussed in detail during this three-day bootcamp. For now, let’s start with the fundamentals.
A Version Control System (VCS) is a tool that helps track changes to files over time. It allows multiple people to collaborate on a project, maintain version history, and revert to previous states if needed. VCS is crucial for software development because:
- It enables collaboration among team members.
- Keeps a history of changes.
- Facilitates the rollback of mistakes.
- Tracks and manages code across branches.
- Other examples include Subversion(centralized VCS), Mercurial etc.
Git is one of the most popular VCS, known for its distributed nature. Key advantages of Git include:
- Speed: Git is very fast compared to other VCS.
- Distributed: Every developer has a full copy of the repository, allowing for offline work.
- Efficient Branching: Git provides lightweight and flexible branching and merging strategies.
On most Ubuntu systems, Git comes pre-installed, so no installation steps are required. You can verify this by running the following command:
git --version
To install Git on Windows, you can follow these steps:
- Visit the official Git website at https://git-scm.com/downloads.
- Download the Git installer for Windows.
- Run the installer and follow the on-screen instructions.
- Once the installation is complete, you can open a command prompt and verify the installation by running the following command:
git --version
For Linux systems, you can use the package manager to install Git. For example, on Ubuntu, you can run the following command:
sudo apt-get install git
To set your username and email in Git, use the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
This is the name that shows up in commits so that git can identify who did what commit. You can also set it locally for a repo, using the --local flag instead of the --global flag.
git init
Initializes a new Git repository in the current directory.
git clone <repository_url>
Clones an existing repository from a remote location (Like github, which we will look at later on).
git status
Displays the state of the working directory and the staging area.
git add <file>
Stages file changes for the next commit.
git commit -m "message"
The below can be used too, but it will open a prompt and ask you to type the "message"
git commit
Commits staged changes with a descriptive message.
git log
Shows the commit history of the repository.
git rm file_name
Git untracks the file and also removes it from the git repository/directory.
- Subject Line: Use a concise summary (max 50 characters) in the imperative mood (e.g., "Add new feature").
- Body (optional): Include detailed explanation if needed:
- Explain the "why" behind the change.
- Describe the impact or reasoning.
- Issue References: Link to related issues using
Fixes #<issue-number>
orCloses #<issue-number>
to automatically close them. - Formatting:
- Keep subject line under 50 characters.
- Use present tense, imperative mood.
Guide:
- Create a new directory.
- Navigate to the directory.
- Initialize a new Git repository using
git init
.
- Create or modify a file in the repository.
- Use
git add <file>
to stage the changes. - Use
git commit -m "your message"
to commit the changes. - View the commit history with
git log
.
Branches allow developers to work on separate features without affecting the main codebase.
git branch <branch_name>
git checkout <branch_name>
git merge <branch_name>
Merges changes from the specified branch into the current branch. We will face issues with this merge part later, don't worry for now
- git fetch: Retrieves changes from the remote repository without applying them.
- git pull: Fetches and merges changes from the remote repository into the current branch.
- git revert: Reverts a specific commit by creating a new commit that undoes the changes.
- git reset: Moves the current branch pointer to a previous commit, potentially discarding commits.
Merge conflicts occur when Git cannot automatically merge changes from different branches. This usually happens when the same lines of code are modified differently in each branch.
- Open the conflicting files and manually resolve the differences.
- After resolving, use
git add
to mark the conflict as resolved. - Finally, commit the resolved changes using
git commit
.
- Version Control is essential for collaboration and code management.
- Git provides fast, distributed version control with efficient branching.
- You’ve learned basic and advanced Git commands, including handling merge conflicts.
Hacktoberfest is an event that happens every october, to celebrate the world of open source. Hope you have watched the video to register.
- You must do 4 pull requests to be said to participate
- They used to give goodies, not anymore
- Great chance to enhance your git and github skills. also beneficial from google summer of code point of view
- There are repos that have the hacktoberfest tag on them, search for those and try to contribute to those.
- You need not code, there are no-code contributions too, as a part of the readme project. [Click here for more](Refer https://hacktoberfest.com/participation/#low-or-non-code)
GitHub is a web-based platform that provides hosting for Git repositories. It is used for version control and collaboration, allowing developers to work together on projects from anywhere. Key features of GitHub include:
- Hosting Git repositories.
- Enabling collaboration through pull requests and issues.
- Providing tools for continuous integration (CI) and project management.
- Alternatives to github include, gitlab, sourceforge, codeberg
A typical workflow on GitHub includes:
- Repositories: A central place where the project files are stored.
- Branches: Used to work on different features or fixes without affecting the main codebase.
- Commits: Snapshots of changes made to the code.
- Pull Requests: A request to merge changes from one branch into another.
-
On GitHub, click the "New" button in the repository section to create a new repository.
-
Give the repository a name, optionally add a README, and click "Create Repository".
-
If you are working with someone else's repo and you are not a collaborator in it, you are required to fork it before cloning it.
-
A Github fork creates a personal copy of a repository, allowing you to make changes and propose updates via pull requests.
git clone <repository_url>
- Clone the repository to your local machine to start working on it.
- Modify files in your local repository, then stage and commit the changes using Git.
git add <file>
git commit -m "Your message"
git branch <branch_name>
git checkout <branch_name>
- Create and switch to a new branch where you will make changes.
git push origin <branch_name>
- Push your changes to the new branch on GitHub.
- On GitHub, open the repository and click the "New Pull Request" button.
- Select the branch you want to merge from and the branch you want to merge into (e.g., from
feature-branch
tomain
). - Provide a title and description for the pull request and submit it for review.
- Clone the repo you want to work with onto your local machine.
git fetch
- In Git, fetching means getting the latest changes from a remote repository (like GitHub) to your local repository, but without merging them into your work yet
git remote show origin
- This command is used to check if the local machine is upto date with the changes/commits in the remote repo
- If the local machine/repo is not upto date with the changes, it shows a message telling 'local out of date'
- Then use the git merge command to merge the new changes of the remote to the local machine.
git pull
- It is a command which is nothing but a combination of both fetch and merge commands
- It fetches the changes from the remote and merges it with the local repo.
Initialize your local repository (if you haven't already):
git init
Add your files to the staging area:
``` bash
git add .
Commit your changes:
git commit -m "Initial commit"
Add the remote repository. You can find the URL of your new GitHub repository on the GitHub website. It looks like https://github.com/username/repo.git:
git remote add origin https://github.com/username/repo.git
Push your changes to the remote repository:
git push -u origin main
- Create a branch in a repository and push changes to it.
- Submit a pull request to merge your changes into the
main
branch.
- Review the submitted pull requests.
- Test the changes and leave comments if necessary.
- Merge the pull request into the
main
branch.
GitHub Project Boards help organize and manage your project tasks.
Github Project Boards allows team members to track their daily tasks, create various task management systems like To-Do Lists,Kanban boards,Sprint sessions and many more.
Roadmaps can be built and linked with other projects as well to ensure a smooth workflow and planned structure of tasks over a long term.
Issues are used to track bugs, enhancements, and other project tasks.
You can use Labels to categorize issues based on their priority or type (e.g., bug, enhancement).
Issues can be assigned on Project Boards and can also be assigned to multiple developers with their seperate Task Cards to ensure proper management and efficiency.
Set Milestones to track the progress of your project and measure what’s been accomplished toward a release or a big goal.
You can invite Collaborators to your repository and manage their permissions by going to the repository settings and selecting "Manage Access."
Google Summer of Code (GSoC) is a global program that brings new contributors into open source software development. Launched in 2005, GSoC pairs students and beginners with mentoring organizations to work on real-world coding projects. Participants gain valuable experience, improve their coding skills, and contribute to open source communities. The program runs annually, offering stipends to contributors who successfully complete their projects. GSoC has helped thousands of developers kickstart their careers and foster a passion for open source. By participating, contributors not only enhance their technical skills but also become part of a global community dedicated to collaborative software development.
Google Solution Challenge is an annual competition organized by Google Developer Student Clubs (GDSC) on behalf of google. The challenge invites university students to develop innovative solutions using Google technologies to address one or more of the United Nations' 17 Sustainable Development Goals (SDGs). Participants work in teams to create impactful projects that tackle global issues such as poverty, climate change, and inequality. The top teams receive mentorship from Google experts, swag, and cash prizes. The challenge not only enhances participants' technical skills but also fosters a sense of community and purpose among young developers.
Major League Hacking (MLH) is a global community that empowers hackers through various programs, including hackathons, workshops, and the MLH Fellowship. The MLH Fellowship is a 12-week remote internship alternative where participants work on real-world projects, collaborate in small groups, and receive mentorship from experienced developers. Fellows can choose from different tracks such as Open Source, Explorer, and Externship, each offering unique learning experiences. The program aims to bridge the gap between academic learning and professional software development, providing valuable hands-on experience. MLH also offers stipends to help offset living and educational expenses during the fellowship.
Feel free to ask any questions or clarifications.
One specificity of Git (and other distributed systems) compared to centralized systems is that each repository contains the whole history of the project. Suppose you create a 100 MB file, modify it 100 times in a way that doesn't compress well. You'll end up with a 10 GB repository. This means that each clone will download 10 GB of data, eat 10 GB of disk space on each machine on which you're making a clone. What's even more frustrating: you'd still have to download these 10 GB of data even if you git rm the big files.
Putting big files in a separate system like git-lfs allow you to store only pointers to each version of the file in the repository, hence each clone will only download a tiny piece of data for each revision. The checkout will download only the version you are using, i.e. 100 MB in the example above. As a result, you would be using disk space on the server, but saving a lot of bandwidth and disk space on the client.
Git LFS (Large File Storage) is a Git extension developed by Atlassian, GitHub, and a few other open source contributors, that reduces the impact of large files in your repository by downloading the relevant versions of them lazily. Specifically, large files are downloaded during the checkout process rather than during cloning or fetching.
Git LFS does this by replacing large files in your repository with tiny pointer files. During normal usage, you'll never see these pointer files as they are handled automatically by Git LFS:
-
When you add a file to your repository, Git LFS replaces its contents with a pointer, and stores the file contents in a local Git LFS cache.
-
When you push new commits to the server, any Git LFS files referenced by the newly pushed commits are transferred from your local Git LFS cache to the remote Git LFS store tied to your Git repository.
-
When you checkout a commit that contains Git LFS pointers, they are replaced with files from your local Git LFS cache, or downloaded from the remote Git LFS store.
-
Install git-lfs on your machine. You can download it from here.
-
Initialize git-lfs in your repository:
git lfs install
- Track files with git-lfs:
git lfs track "*.<file_extension_you want_to_get_tracked_by_lfs>"
- Add and commit the
.gitattributes
file:
git add .gitattributes
git commit -m "Add Git LFS tracking"
- Add and commit your large files:
git add <large_file>
git commit -m "Add large file"
- Push your changes:
git push origin <branch>