HOW GIT AND GITHUB WORK: A BEGINNER’S GUIDE TO VERSION CONTROL AND COLLABORATION
Introduction
If you’ve never used Git or GitHub, this article is perfect for you. If you’ve prior experience with Git and Github, this tutorial provides an efficient refresher. This tutorial teaches you the basics of Git and GitHub and you will learn the following at the end of the class:
- Meaning of Git and GitHub
- Reasons to start using Git and GitHub
- Difference between Git and GitHub
- How to start using Git and GitHub
- Everyday Git commands
It takes years of focused practice to become a great Git and GitHub user. This course will improve your technical skills in Git and GitHub but will not instantly transform you into a great Git and GitHub user.
What are Git and Github?
In 2005, Linus Torvalds created Git, a free, open-source software. Git is a widely used version control system that allows developers to track changes, collaborate effectively, and manage different versions of their codebase. By understanding Git, you can easily track changes, revert to previous versions, and collaborate seamlessly with other team members, which enhances code quality and productivity.
In April 2008, a team of four developers: Tom Preston-Werner, Chris Wanstrath, P. J. Hyett and Scott Chacon launched GitHub. And in October 2018, Microsoft acquired GitHub, and since then GitHub continued to operate independently as a community, platform and business.
GitHub is a popular web-based hosting platform that utilises Git for version control. It enables developers to host and share their code repositories, collaborate with teammates, and contribute to open-source projects. Proficiency in GitHub allows for effective collaboration, code reviews, issue tracking, and seamless integration with other tools and services.
There are two other popular Git hosting services: GitLab (owned by GitLab Inc.) and BitBucket (owned by Atlassian). We’ll use GitHub as our hosting service.
Why you should learn Git and GitHub
In this section, we’ll consider 10 important reasons why you should learn Git and Github. Learning Git and GitHub goes beyond managing code versions; it also allows you to build a professional presence, collaborate effectively, demonstrate your skills, and learn from the broader developer community. Embracing these tools can significantly enhance your career prospects and establish you as a competent and engaged member of the software development community.
- Version Control:
Git is a widely used version control system that allows developers to track changes, collaborate effectively, and manage different versions of their codebase. By understanding Git, you can easily track changes, revert to previous versions, and collaborate seamlessly with other team members, which enhances code quality and productivity.
2. Collaboration and Remote Work:
Many software development teams use Git and GitHub to host and share their code repositories, collaborate with teammates, and contribute to open-source projects. By learning Git and GitHub, you gain the ability to effectively collaborate with other developers, contribute to group projects, and participate in open-source communities. Demonstrating your experience with collaborative workflows through pull requests, code reviews, and issue tracking can be a significant asset when seeking team-based opportunities.
3. Code Backup and Recovery:
Git and GitHub provide a reliable backup system for code repositories. By committing changes to Git and pushing them to GitHub, your code is safely stored and can be recovered in case of accidental deletions, system failures, or other unforeseen events. This ensures that your work is safeguarded and can be restored easily.
4. Branching and Collaboration Workflow:
Git’s branching capabilities enable developers to work on multiple features or fixes concurrently without interfering with each other’s work. It allows for creating separate branches for new features, bug fixes, or experiments, which can be merged back into the main codebase once tested and reviewed. This branching workflow promotes collaboration, code isolation, and easy management of different development tasks.
5. Command-Line Efficiency:
The ability to use terminal commands allows developers to navigate through directories, run scripts, install packages, and perform various tasks more efficiently. Terminal commands provide a powerful and flexible way to interact with your development environment and execute commands quickly without relying on graphical user interfaces.
6. Automation and Scripting:
Terminal commands are often used in automation and scripting tasks. By writing scripts and utilizing command-line tools, developers can automate repetitive tasks, build deployment pipelines, and perform complex operations with ease. This improves productivity, reduces manual errors, and enables efficient development workflows.
7. Industry Standard:
Git and GitHub have become industry standards in software development. Proficiency in these skills is highly valued by employers, as they facilitate efficient collaboration, code management, and adherence to best practices. Familiarity with these tools and concepts demonstrates your professionalism and ability to work effectively in a team.
Also, actively contributing to open-source projects or creating your repositories not only showcases your skills but also increases your visibility among other developers and potential employers. Many recruiters and hiring managers review GitHub profiles to evaluate a candidate’s coding abilities and level of engagement in the development community.
8. Portfolio Development:
Another important reason to learn Git and GitHub is their role in showcasing your work and building a professional portfolio. Git and GitHub provide an excellent platform to showcase your coding projects and contributions. By hosting your code repositories on GitHub, potential employers, clients, or collaborators can easily access and review your work. Having a well-organized and well-documented GitHub profile can act as a professional portfolio, demonstrating your skills, coding style, and project involvement.
9. Version Control Best Practices:
Learning Git encourages you to adopt version control best practices, such as committing small and meaningful changes, writing descriptive commit messages, and adequately managing branches. These practices not only improve the maintainability of your projects but also align with industry standards and demonstrate your commitment to producing clean and organized code.
10. Learning from Others:
GitHub is a treasure trove of open-source projects, code samples, and programming resources. By exploring various repositories and following projects that interest you, you have the opportunity to learn from experienced developers, understand different coding techniques, and stay up-to-date with emerging technologies and trends.
Difference between Git and GitHub
There’s a frequent assumption that using Git or GitHub would result in a similar experience. However, any developer will tell you that these two things are entirely different. One is that they serve various purposes and carry out distinct tasks. Despite the name, they aren’t even owned by the same company. In this section, I have tabulated the differences between Git and GitHub using their features.
How to start using Git and GitHub
Step 1: Download and Install Git
To use Git, we’ll have to download and install Git on our local computer. We’ll use CLI to communicate to Github in Step 2.
Step 2: Create a GitHub Account
Visit github.com to create your personalised account.
Sign up for a free plan or make a premium investment. GitHub also provides discounts to students, NGOs, and educators.
Step 3: Connect Git to GitHub
Basically, we will be learning 2 different methods to connect our Git project to GitHub.
Method 1: Create the repository, clone it to your PC, and work on it (Recommended). Don’t know what a repository is? Read about Git Repository
Method 1 involves creating a repository on GitHub and cloning it to your local computer and making changes and pushing your changes back to GitHub.
Create a new repository by clicking the “new repository” button on the GitHub web page.
Choose a name for your first repository, add a brief description, check the ‘Initialize this repository with a README’ box, and click on the “Create repository” button.
Congratulations! Your first GitHub repository is created.
Your next assignment is to get a copy of the repository on your computer. To do that, you need to “clone” the repository on your computer.
To clone a repository means that you’re taking a repository that’s on the server and cloning it to your computer — just like downloading it. On the repository page, you need to get the “HTTPS” address.
You must utilize your terminal once you have the repository’s address. Use your terminal to run the following command:
git clone [HTTPS ADDRESS]
Replace “[HTTPS ADDRESS] with the project URL you copied earlier.
Running this command will make a local copy of the repository hosted at the given address.
Now, your repository is on your computer. You need to move in it with the following command.
cd [NAME OF REPOSITORY]
As you can see in the above picture, my repository name is “my-app-name” and this command made me move into that specific directory.
Whenever you clone a project, Git will initialize a repository on your computer. If you want, you can access your project with the computer user interface instead of using the above ‘cd’ command on the terminal.
Next, in that folder, we can create files, modify them, and save them locally. To save them in a remote place — like GitHub — we have to do a process called a “commit”. To do this, get back to your terminal. If you closed it, like I previously stated, use the ‘cd’ command.
cd [NAME OF REPOSITORY]
There are 4 steps in a git commit: ‘status’, ‘add’, ‘commit’ and ‘push’. The first (status) step is optional. All other following steps must be performed within your project. Let’s discuss them one by one.
- “status”: The first thing you need to do is to check the files you have modified. To do this, you can type the following command to make a list of changes appear.
git status
2. “add”: here, you can add all files you want to upload with the following command. Running the below command will upload all files with the provided names:
git add [FILE_NAME_1] [FILE_NAME_2] [...]
While running the following command will upload all files
git add .
3. “commit”: After adding the files of our choice, we need to write a descriptive message to explain what we have done. This message may be useful later if we want to check the commit history. For example:
git commit -m “Initial Commit”
4. “push”: Finally, we can put our work on GitHub. To do that we have to ‘push’ our files to Remote. Remote is a duplicate instance of our repository that lives somewhere else on a remote server. To do this, we must know the remote’s name (Mostly remote is named origin). To figure out that name, type the following command.
git remote
The above image says that our remote’s name is origin. Now we can safely ‘push’ our work by the following command:
git push origin main
To confirm that everything went well, go to our repository on the GitHub web page and refresh the page, we can see the index.html file that we’ve pushed to remote — GitHub!
Method 2: Create your project locally then create the repository on GitHub and push it to remote.
You may have an exciting project on your computer that you want to turn into a GitHub repository or you would want to create the project first on your PC, and then push it to a GitHub repository. Whichever way, Git allows us to achieve this goal. Follow the following steps to create your project locally then create the repository on GitHub and push it to the remote:
Step 1: Navigate to the directory where you have your existing project or where you want to save your new project.
Step 2: Open the already existing project folder in your terminal or create a new project folder and open it in your terminal.
Step 3: Make sure you are in the desired project folder. By default, any directory on our computer is not a Git repository — but we can turn it into a Git repository by executing the following command in the terminal.
git init
Step 4: The next thing we may want to do after converting our directory to a Git repository is to inspect the files we have by running the following command:
git status
Step 5: Here, I have added 2 files: index.html and style.css. Your output might look slightly different from mine depending on your project files. Therefore we need to add these files to our GitHub repo. Run:
git add [FILE_NAME_1] [FILE_NAME_1] [...]
OR
git add . // to add all files
After the staging area (the add process) is complete, we can check whether the files are successfully added or not by executing the [git status]
If those particular files are in green like the below picture, you’ve done your work!
Step 6: Next is to commit our changes. Run:
git commit -m “Add index.html and style.css sample files”
Step 7: When we create our repository on GitHub and clone it to our computer, a remote will already be associated with it (Method 1). However, if I start my repository on my computer, it does not have a remote assigned to it, thus I must add that remote (Method 2).
So, in order to add that remote, we must first go to GitHub. Create a new repository in GitHub and name it whatever you like. Then select “Create repository” from the drop-down menu.
NOTE: When creating a new repository via the GitHub web interface, please do not include a README file in Method 2.
After clicking the “Create Repository” button, your repository should look like the image below:
Step 8: Copy the HTTPS address. Now we’ll create the remote for our repository.
git remote add origin [HTTPS ADDRESS]
Let’s check whether we have successfully added the remote or not by the following command:
git remote
And if it outputs “origin” we’ve added the remote to our project.
NOTE: If you want you can change the “origin” name by running the following command:
git remote add [REMOTE NAME] [HTTPS ADDRESS]
Step 9: Now, we can push our project to GitHub without any issues!
git push origin master
After completing these steps one by one, if you go to GitHub you can find your repository with the files!
Everyday Git Terminal Commands
Git is a powerful version control system, that empowers developers to efficiently manage their codebase and collaborate effectively with others. In this section, we will explore 25 essential Git terminal commands that play a pivotal role in optimizing development workflows and ensuring seamless project collaboration.
Conclusion
Mastering Git and GitHub enhance your productivity, collaboration, and code management capabilities, making you a more efficient and effective developer in both individual and team-based projects. These skills are highly sought after in the software development industry and contribute to successful and streamlined development workflows.
Thank you for taking the time to read this. I just went over the fundamentals of Git and GitHub. I strongly advise you to read more Git and GitHub-related publications. I hope you found this article useful.
Check out my Medium post on “7 Habits that will make you a better programmer.“
Thank you.
Have fun coding!