In software development, collaboration, tracking changes, and sustaining a healthy code base are all crucial. The solution is version control systems, and Git is the most common in this category. For collaboration, Git is typically paired with GitHub, a site for creating repositories of Git code that people can share and collaborate on. You can develop and collaborate on projects from anywhere in the world.
If this is your first time using version control, the terms and workflows surrounding the technology can seem daunting. This guide will help demystify the new terminology of Git and GitHub, and you’ll be able to start working with version control in no time.
What is Version Control?
Version control is simply a system that records changes to a file or set of files in order to recall specific versions later. Version control allows multiple people to work on the same project at the same time without over writing each other’s work.
There are two types of version control:
- Centralized version control systems (CVCS) — e.g. Subversion (SVN)
- Distributed version control systems (DVCS) — e.g. Git
Git is a DVCS, meaning that every developer has a full copy of the project history.
What is Git?
Git is a free and open-source distributed version control system created by Linus Torvalds in 2005. It allows developers to:
- Track changes in their code
- Revert to earlier versions of their code
- Work on independent features using branches
- Merge contributions from multiple contributors
What is GitHub?
GitHub is an online system that allows users to work with Git. The platform offers:
- Cloud hosting for Git-based repos.
- Collaboration for teams.
- Issue tracking.
- Pull requests for code review.
- Integration with CI/CD pipelines.
Getting Started with Git
To get started with Git, you need to install it on your computer:
On Windows:
- Download from https://git-scm.com/downloads
- Run the installer, choosing the default options.
On macOS:
You can use Homebrew:
bash brew install git |
For Linux:
Use the package manager:
bash sudo apt install git #Debian/Ubuntu sudo yum install git #RedHat/cenOS |
Basic Git Workflow
Here is a simple overview of the Git workflow:
- Initialize a Git Repository in Your Project Directory
- Open your project directory and run:
- Configure Git (only once)
- Add Files to Staging Area
- Commit Changes
- Push to GitHub
- Pull from Others
Let’s discuss each of these steps more fully.
1. Initialize a Git Repository
Change directories to your project and run:
bash git init |
2. Configure Git (only once)
bash git config –global user.name “your name” git config –global user.email “you@example.com” |
3. Add files to staging area
bash git add filename.txt # Add specific file git add . # Add all files |
4. Commit changes
bash git commit -m “Add initial project files” |
5. Push to GitHub
First, create a repository on GitHub. Next, you will connect your local repository to GitHub:
bash git remote add origin https://github.com/yourusername/your-repo.git git push -u origin main |
Cloning a Repository
To clone a repository that already exists on GitHub:
bash git clone https://github.com/user/repo.git |
Branching and Merging
One of Git’s great strengths is its ability to use branches. A branch is a distinct line of development, and you can create them and switch back and forth between them at will.
Create a New Branch:
bash git checkout -b new-feature |
Switch Between Branches:
bash git checkout main |
Merge a Branch:
bash git checkout main git merge new-feature |
Resolving Merge Conflicts
Resolving Merge Conflicts If the same part of a file is changed by two different branches, the merge is not automatic, and Git will ask you to resolve the issue. In your file, look for something like these lines:
text <<<<<<< HEAD current change ======= Incoming change >>>>>>> branch-name |
Make edits, choosing the code you want to keep, then add and commit the resolved file.
GitHub Features
Pull Requests
Pull requests, or PRs, are how changes are proposed and discussed before merging into the main branch. PRs are a key part of collaborative development, and pull requests allow teams to collectively review code together.
Issues
GitHub Issues allow tracking for bugs, enhancements, and tasks. Issues serve as a to-do list, as well as a discussion space for a team.
Best Practices for Git and GitHub
- Make frequent commits: Think small with frequent commits, and include meaningful commit messages.
- Use .gitignore: Use .gitignore, and don’t commit unnecessary files such as build artifacts or secrets.
- Use good messages: When writing commit messages, start them with a verb, e.g., “Fix bug in login logic.”
- Make feature branches: Don’t work directly off of the main branch.
- Pull Often: Keep your local copy updated with your remote copy.
- Review PRs: Use pull requests for peer reviews and for teams to collaborate.
Useful Git Commands Summary
Command | Description |
git init | Initialize a new Git repository |
git clone [url] | Copy a repository from GitHub |
git status | See current changes and state |
git add [file] | Stage changes for commit |
git commit -m “msg” | Save staged changes |
git push | Upload changes to GitHub |
git pull | Download changes from GitHub |
git branch | List branches |
git checkout [branch] | Switch branches |
git merge [branch] | Combine changes |
Conclusion
While Git or GitHub may seem scary at first, with just a small amount of practice, it will become a valuable part of your toolbox. Not only will version control protect your work, but it also enables collaboration and improves code quality. Mastering Git or GitHub will be a big milestone along your code journey, no matter if you are creating your very own personal project or contributing to open source along the way.
🚀 Do yourself a favor and start by creating a simple project and uploading it to GitHub today. The best way to learn is by doing!