DEV Community

Cover image for The Power of Git: A Guide to Collaborative Version Control
BekahHW for OpenSauced

Posted on • Edited on • Originally published at opensauced.pizza

The Power of Git: A Guide to Collaborative Version Control

When I started learning to code, no one told me what Git was or why it was important. It was one of those words that I pretended that I understood because everyone just assumed that I knew what it meant. But up to that point, I had been using built in IDEs, and I hadn’t used git. For one of my bootcamp projects, I had to have a certain number of commits to pass. I forgot about commits, and only had about three. I went through and did the whole project over so I could add the required number of commits. When you’re working on a simple project like I was, one that no one else would check out or contribute to, it can be less than intuitive to remember to commit. I just wanted my project to work as quickly as possible. I know now that I was probably taking the wrong approach to learning. Because moving into tech, I’d be working collaboratively, and these tools could be incredibly helpful to ensuring that collaborative relationships worked well. Hopefully, this beginner’s guide to Git will help you understand the usefulness of Git and how you can use it as part of your toolbox.

What is Git?

Git is a distributed version control system that allows developers to manage their source code and collaborate on projects. It allows you to track changes made to files over time, and creates a centralized repository and a comprehensive history of changes, which allows for a more seamless teamwork experience and organized code management.

I like to think of Git as a time machine for your code.

Imagine you're writing a book. You start with your first draft and make changes as you go along. Git acts as your trusty time machine, allowing you to travel back and forth in time, capturing each version of your story.

Each time you make changes to your book, you create a "commit" in Git, which is kind of like taking a picture of your story at that specific moment. These commits are organized in chronological order, which gives you a timeline of your writing process.

If you decide to experiment with a different plotline or character development, you can create a new "branch" in Git. This branch is like a parallel universe where you can make changes without affecting the main storyline. You can switch between branches, explore different ideas, and later merge them back into the main story.

Git's version control features allow you to navigate through the history of your book effortlessly. You can compare different versions, see what changes were made, and even change back to an earlier draft if needed.

Just like a time machine can help writers manage the evolution of their stories, Git helps developers track and manage the the changes to a codebase, collaborate with others, experiment with new features, and create a record of the project's history.

Benefits of Git in Collaborative Development

Historically, collaborating on code involved manual file sharing or maintaining multiple versions. If you’re lucky, you don’t know how confusing and inefficient this often turned out to be. Git addressed these challenges by providing central code storage and records of changes.

Features and Benefits of Git

  1. Decentralized Development- With Git, each team member has their own repository on their local machine-also called a local copy-which allowed for independent work without requiring an internet connection.

  2. Effortless Synchronization- By cloning repositories, team members can create local copies of the codebase. This means that everyone has their own version to work on and they can use Git to share the changes between repositories.

Getting Started with Git

If you want to get some hands-on experience using git, check out OpenSauced's Intro to Open Source Course or sign-up for one of our workshops that are listed on the bottom of our #100DaysOfOSS Challenge docs.

Remote Repositories and Collaboration

Git allows for collaboration by enabling developers to work on the same codebase from remote locations.

Cloning a Repository

Cloning is the process of creating a copy of a remote repository onto your local machine. When you clone a repository, you create a local version that includes the entire codebase, commit history, branches, and other repository-related information.

To clone a remote repository:
$ git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Branching

Branching and merging allows multiple developers to be working on the same code base at the same time and reduces the incidence of conflicts.

To create a new branch:
$ git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

You should replace with the name for your new branch. For example, if you want to create a branch called "feature-x", you would use the following command:

$ git branch feature-x
Enter fullscreen mode Exit fullscreen mode

Committing Changes

Committing changes is part of Git's core functionality. Remember, committing is like capturing a snapshot of what the codebase looks like at that moment. A commit message is a description of what changes you’ve made. Here's an example:

To commit changes:
$ git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Scenario: Imagine you've added a new feature and want to save your progress. By committing your changes, you create a snapshot of the code at that specific point, making it easy to track and revert if needed.

Pushing Changes

When you push changes, that means that you send your local code changes, commits, and branches to a remote repository. You’re updating the remote repository with your local commits and making them accessible to others who have access to the same repository.

To push changes to a remote repository:
$ git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

By pushing changes, your saved local changes are shared with others. Other team members can then fetch and merge your changes into their local repositories, creating an up-to-date version of the project.

Merging and Fetching

Merging: Merging is the process of combining changes from one branch or commit into another branch.

The command git merge <branch-name> is used to merge changes from one branch into another. In this case, replace <branch-name> with the name of the branch you want to merge into your current branch. For example, if you want to merge changes from a branch named "main" into your current branch, you would use the following command:

$ git merge main
Enter fullscreen mode Exit fullscreen mode

Scenario: You're working on a team project and need to develop a new feature without interfering with the main codebase. By creating a branch, you can isolate your work, commit changes, and merge them back into the main branch when you’re ready. If updates have been made to the main branch that you need to add to your branch, you can merge those in as well.

Fetching: The process of retrieving changes from a remote repository without automatically merging them into your local branch. When you fetch, Git compares the commit history between your local repository and the remote repository, identifying any new commits or branches in the remote repository that you don't have locally.

To fetch changes from a remote repository:
$ git fetch <remote>
Enter fullscreen mode Exit fullscreen mode

Sometimes there are merge conflicts (for example, changes to the same lines of code). When that happens, Git will ask you to manually resolve the conflicts.

To resolve merge conflicts you’ll need to look at the conflicting sections, edit the files remove the conflict, and then commit the new changes.

If you want to fetch and merge at the same time-which is convenient but allows for less flexibility-you can run this command:

$ git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Git Stash

Imagine you’re in the middle of working on a feature branch, but you suddenly need to switch to another branch for a bug fix. Instead of committing unfinished changes or losing them, you can use Git stash to temporarily save your modifications. This lets you to switch to the bug fix branch, address the issue, and then return to your feature branch and apply the saved stash to continue working from where you left off.

To stash your changes, run the following command:

 $ git stash save "Stash message"
Enter fullscreen mode Exit fullscreen mode

To see a list of your stashes, you can use the command:

$ git stash list
Enter fullscreen mode Exit fullscreen mode

When you're ready to apply your stashed changes back to your working directory, use the command:

$ git stash apply
Enter fullscreen mode Exit fullscreen mode

If you have multiple stashes, you can specify a specific stash using its ID or index (git stash apply stash@{2}).

If you no longer need a stash, you can remove it from your stash list by running:

$ git stash drop
Enter fullscreen mode Exit fullscreen mode

To apply a stash and remove it from the stash list in one step, you can use the command:

$ git stash pop
Enter fullscreen mode Exit fullscreen mode

This is equivalent to running git stash apply followed by git stash drop for the most recent stash.

Git Rebase

Say you have been working on a feature branch for a while, but during that time, the main branch has had several new commits. Before merging your feature branch, you can use Git rebase to incorporate the latest changes from the main branch into your branch. This makes sure your feature branch is up to date and avoids potential conflicts during the merge process.

Here's an overview of how to use Git rebase:

  1. Start a Rebase: To start a rebase, make sure you are on the branch that you want to apply the changes to (the target branch). Then, run the following command:
   $ git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

Make sure you replace <branch-name> with the name of the branch containing the commits you want to apply onto the target branch.

  1. Resolve Conflicts: During the rebase process, Git may encounter conflicts if there are conflicting changes between the commits being applied and the existing commits on the target branch. Git will pause the rebase and ask you to resolve these conflicts manually. You can use Git's conflict resolution tools, such as editing the conflicting files, selecting the desired changes, and using git add to mark the conflicts as resolved.

  2. Continue the Rebase: After resolving conflicts for a particular commit, you can continue the rebase process by running:

   $ git rebase --continue
Enter fullscreen mode Exit fullscreen mode

This will apply the resolved commit and proceed with the remaining commits in the sequence.

  1. Abort the Rebase: If you decide to cancel the rebase, you can abort the rebase by running:
   $ git rebase --abort
Enter fullscreen mode Exit fullscreen mode

This will revert your branch to its state before the rebase started.

It's important to note that Git rebase rewrites the commit history by applying the changes from one branch on top of another branch.

There’s a lot more that you can do with Git, and I know it might seem overwhelming, but think of it like a toolbox. There’s lots of different tools in there and you might only know how to use the screwdriver in the hammer, but as you progress through different projects, you can continue to learn how to use the other tools in there. And when you do, you’ll find that collaborating on projects becomes easier because you’re using the right tools.

Top comments (9)

Collapse
 
lalami profile image
Salah Eddine Lalami

Nice 🔥 ,
I just write new article about git commit message. , this could helpful too : dev.to/idurar/why-you-should-use-e...

Why you should use emoji in github commit<br>

Collapse
 
stonediggity profile image
Liam Stone

Great Git summary dude. I think it's good to include some of the diagrams where it shows branchers/PRs/merging etc but your coverage of all the major functions is so good. Thank you!

Collapse
 
ruthiel profile image
Ruthiel Trevisan

Great article!
Thanks!

Collapse
 
thecodermehedi profile image
Mehedi Hasan

Wow, thanks for this. Great article.

Collapse
 
lalitpatil1634 profile image
Lalit LP

Nice explain...

Collapse
 
matthiasxcoder profile image
Matthiasxcoder

I love git

Collapse
 
sajjadsadeh profile image
sajjad

This is a good article to introduce git, thanks you

Collapse
 
360hadi profile image
Hadiuzzaman HADI

nice article

Collapse
 
chantal profile image
Chantal

Saved it! Good staff.