Version Control: Git Basics
Tags: webdev, beginners, programming, tutorial
Published: 2024-04-26 15:58:04

Version control and collaboration are at the core of software development, with popular tools such as Git and GitHub enabling this with ease.

Version control is the record-keeping system that tracks alterations made to code, enabling collaboration, experimentation, and error recovery with utmost precision.

The obvious way to achieve this would be to save and label multiple versions of your code each time significant developments are made. 

Although it is good to store copies of your project in different locations throughout development, it can quickly begin to occupy a lot of storage.

For many beginners, using Git and GitHub can seem daunting and confusing, perhaps even unnecessary for smaller scale projects.

However, it is essential for any large project to use a more comprehensive form of version control, so it is best to start practising with smaller projects.

This article aims to provide an introduction to Git and GitHub suitable for beginners, emphasising the importance of version control and collaboration.

## What is Git?

Git is a distributed version control system that enables developers to track changes to their codebase efficiently. 

Paired with GitHub, a cloud-based platform for hosting Git repositories, Git revolutionises collaboration by providing a centralised hub for code sharing, feedback, and iteration.

Understanding Git and GitHub is paramount for developers at every level of expertise, as these tools lay the foundation for effective collaboration, version control, and project management.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtehof5yjy69kwx3cfb5.jpg)

Before we get started with using Git or GitHub, let’s clear up some of the key terms that will be mentioned throughout this article:

- **Repositories:** Serve as the digital warehouses for project files and their entire history of changes. 
- **Commits:** Within repositories, developers utilise commits to capture snapshots of their code at different points in time. This allows for seamless tracking of modifications and reversions when necessary.
- **Branches:** Enable developers to create divergent paths of development without disrupting the main codebase. Branches facilitate experimentation, feature development, and bug fixes in isolation, fostering a modular and organised workflow.
- **Merges:** Changes from one branch can be seamlessly incorporated into another. This process ensures code consistency and collaboration among team members, enabling parallel development efforts to converge harmoniously.

## Getting Started with Git

- First off, we need to install Git, which you can do by clicking here.
- With Git installed, open up Git Bash, which is where you’ll be entering commands. It’s crucial to configure Git with your name and email address, which will be used to identify your commits. This can be done using the following commands in the terminal:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mp4z2c255nar74r0d0ac.png)

- Now you want to navigate to the file location of your project using the cd (change directory) command, followed by the file path. For example: _cd Documents/myProject_ . If you don’t have a project file to use, simply create a new folder, and add a text file.
- You can now initialise the repository at that location, by simply entering _git init_ into the command terminal.
- Now go ahead and manually make a simple change to a file in your repository. This can be as simple as adding a space, or an annotation.
- With your change made, you can then enter the _git status_ command in the terminal. It should show you that a file has been edited, but it hasn’t been staged yet.
- To stage the change we made, you need to enter the _git add *_ command in the terminal. This will stage all files with changes made, which means they will be updated if a commit is made. Alternatively you can select a specific file to stage by using _git add fileName_ .
- You can then once again use _git status_ , which will display staged files with green text, and  files that aren't staged with changes in red.
- For the changes to be made to the repository, we need to make a commit and leave a message describing the changes that have been made. For example: _git commit -m “Added a space.”_

## Linking Git to GitHub

- Head over to the GitHub website by [clicking here](https://github.com/), and register for an account.
- Then click the + icon in the top right corner and select “new repository”. Give it a name and set it to private for now, then click “create repository”.
- Before you can link Git to GitHub, you need to generate SSH keys. In Git Bash, run the following command, replacing the email with the one associated with your GitHub account:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbupana9es1shv43kvkl.png)

- Press Enter to accept the default file location. You will be prompted to enter a passphrase, but you can leave it empty if you prefer.
- Once the SSH key is generated, you need to add it to your GitHub account. Open the file containing your SSH key in a text editor, copy its contents, and then navigate to your GitHub account settings. Under “SSH and GPG keys,” click on “New SSH key,” paste your key, and give it a descriptive title.
- To link your local repository to GitHub, go to your GitHub repository’s page and copy its SSH URL. Then, in Git Bash, navigate to your project directory and run the following command:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jipk00bsj0xutxqw1jq.png)

- Replace “username” with your GitHub username and “repository” with the name of your GitHub repository.
- Now when you want to update your GitHub repository with your local Git repository, you can simply use _git push_ . Alternatively, if you want to update your local repository based on your GitHub repository you can use _git pull_ .

## Understanding Git Workflow

Branches allow for parallel development and experimentation without affecting the main codebase. 

Use git branch <branch_name> to create a new branch and git checkout <branch_name> to switch to it. This enables developers to work on features or fixes independently.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsj529i7xkdlh38ynga4.jpg)

Forking is a way to create a copy of a repository under your GitHub account. 

This is useful for contributing to open-source projects or collaborating with others. 

After making changes in your fork, you can propose them to the original repository through a pull request. 

This process facilitates code review and integration of changes into the main project. 

It fosters collaboration and enables developers to contribute to projects they don’t have direct write access to.

## Conclusion

Git is a powerful tool for version control, essential for modern software development. 

We’ve covered the basics, from understanding Git’s role to getting started, linking Git to GitHub, and exploring Git workflow. 

Embrace version control to streamline collaboration, track changes, and ensure project integrity. 

As you continue your journey with Git, explore its advanced features and best practices to enhance your development workflow. 

Keep learning and experimenting to unlock Git’s full potential and elevate your coding experience.

We encourage you to go through the further reading below to enhance your understanding.

Ready to simplify API integrations in your project? 

Contact us at APIDNA for a free demo of our new platform by [clicking here](https://apidna.ai/contact-us/), leveraging autonomous agents for simplified integrations.

## Further Reading

[An Intro to Git and GitHub for Beginners (Tutorial) – HubSpot](https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners)

[Getting Git right – ATLASSIAN](https://www.atlassian.com/git)

[Git and GitHub Tutorial Version Control for Beginners – FreeCodeCamp](https://www.freecodecamp.org/news/git-and-github-for-beginners/)