Developer 101: A Quick Guide to Git Version Control
As a software developer, team collaboration via version control tools has been on the rise with most people working remotely or hybrid. Writing code and collaborating with others, as well as managing your projects has become part of a developers timeline. But where do you start when it comes to version control? Git being the most popular, it is the go to answer. Other tools in this space are, for example; Subversion (SVN), Mercurial, Bitbucket, Team Foundation, Tortoise SVN, Apache SVN amongst others.
Today we’ll look at Git and explain some of the essential technical terms you’ll encounter along the way, including also, some code examples.
What is Git?
Git is a distributed version control system (DVCS) designed to help developers track changes in their code, collaborate with others, and manage their projects effectively. With Git, you can keep a complete history of your codebase, work on new features without disturbing the main code, and collaborate with team members seamlessly.
Now, let’s dive into some fundamental concepts and terms you’ll encounter when you start using Git.
Repository
A repository, often abbreviated or referred to as a“repo,” is a container for your project. It’s like a folder that holds all your project files and their entire history. Repositories can be local (on your computer) or remote (hosted on a server, like GitHub or GitLab).
# Initializing a Repository
git init
This initializes a new Git repository in your project directory.
Commit
A commit is a snapshot of your code at a specific point in time. It represents a set of changes you’ve made to your code. Commits are like saved progress points in a video game. You create a commit to save your progress and provide a description of what you’ve changed using a commit message.
# Creating a Commit
git add . # Stage changes or add files to commit
git commit -m "Added a new feature"
The git add .
command stages all the changes, and git commit
creates a commit with the provided message. In the context of version control systems like Git, “staging” refers to the process of preparing changes you’ve made to your files for the next commit. Before a change is committed, it needs to be explicitly staged. Staging allows you to select which modifications you want to include in the upcoming commit, giving you fine-grained control over your version history.
You do this by using the git add
command. For example, if you want to stage all changes, you can use git add .
, or if you want to stage specific files, you can use git add filename
. Staging doesn't commit the changes but marks them as "ready to be committed."
Branch
A branch is a parallel line of development within a repository. When you create a branch, you can work on a new feature or bug fix without affecting the main codebase. Once your changes are complete and tested, you can merge the branch back into the main code.
# Creating a Branch
git checkout -b feature-branch
This command creates a new branch called “feature-branch” and switches to it.
Clone
To create a copy of a remote repository on your local machine, you “clone” it. This action allows you to work on the code locally, and all the history and branches are copied to your computer.
# Cloning a Repository
git clone https://github.com/username/repo.git
Usually you’d replace the https://github.com/username/repo.git
URL with the actual git repository URL.
Fork
Forking is a way to create a personal copy of someone else’s repository. You might fork a repository to contribute to an open-source project or work on your own version of the project without affecting the original code.
Pull
Pulling is the act of fetching changes from a remote repository and merging them into your local repository. This ensures that your local copy is up-to-date with the latest changes from other contributors.
# Pulling Changes
git pull origin master
This fetches changes from the remote repository’s “master” branch and merges them into your current branch.
Push
Pushing is the process of sending your local commits to a remote repository. It’s how you share your changes with others working on the same project.
# Pushing Changes
git push origin feature-branch
This pushes the commits from your “feature-branch” to the remote repository.
Merge
Merging combines changes from one branch into another. It’s a way to bring the work you’ve done on a feature branch back into the main code.
# Merging a Branch
git checkout main
git merge feature-branch
This merges the changes from “feature-branch” into the “main” branch.
Rebasing
Rebasing is another way to integrate changes from one branch into another by moving or reapplying commits to the target branch. This can help keep your commit history clean and linear.
# Rebasing a Branch
git checkout feature-branch
git rebase main
In this example, we switch to the feature branch (feature-branch
) and then rebase it onto the main
branch. This process will reapply the commits from feature-branch
on top of the latest main
branch, making the commit history appear more linear.
Pull Request (PR) or Merge Request (MR)
A pull request (PR) or merge request (MR) is a request to merge your changes from a branch into another branch, often the main code. It’s a common practice in collaborative development where your changes are reviewed before they are merged.
Conflict
A conflict occurs when Git can’t automatically merge changes because the same lines of code have been modified in different ways. Resolving conflicts requires manual or automatic resolution using a code editor or IDE capable of intervention.
<<<<<<< HEAD
Your changes
=======
Other changes
>>>>>>> branch-name
Git Workflow
There are various Git workflows, like Git Flow or GitHub Flow, which provide guidelines on how to use Git effectively within a team. These workflows define branching strategies and how to manage releases and features.
Git is an essential tool for every software developer, and understanding its core concepts and terms is crucial for effective version control. With Git, you can collaborate seamlessly, track changes, and manage your projects efficiently. As you gain more experience, you’ll explore advanced Git features and become a Git master. Start by creating your first repository, making commits, and exploring the world of version control!
For more you can go to: