DEV Community

Cover image for Git & GitHub Essentials: Understanding Version Control and Collaboration
Vidit Kushwaha
Vidit Kushwaha

Posted on

Git & GitHub Essentials: Understanding Version Control and Collaboration

Version Control

Imagine Doctor Strange casting spells and constantly modifying reality (the code) to defeat a foe (a bug). Unlike the film's time loop, Strange (the developers) must keep track of the changes. That's a subtle way of introducing version control, so let's describe it as a system for tracking and managing changes to code or other collections of files. It enables many individuals to work on the same project without overwriting each other's modifications, and it includes a method for reverting to prior versions of the project if necessary.

There are some tools that provide version control.

  • Git: It’s distributed version control system that allows multiple people to work on a project at the same time without overwriting each other's changes. It is a popular choice among software developers.
  • Subversion (SVN): It’s a centralized version control system that keeps all the versioned files in a central repository. Often used for large-scale projects where centralized system is preferred.
  • Perforce: It’s a version control system that is designed for large-scale software development projects. It offers features such as atomic commits, branching and merging, and access control.

Google uses its own version control system called Piper, which is a centralized version control tool. Piper is a renowned version control tool that Google uses as a vast repository, distributed over around ten Google data centers.

There are several hosting platforms available for version control systems. Here are a few popular ones:

  • GitHub: GitHub is a web-based version control repository hosting service that is one of the most popular platforms for version control. It offers both public and private repositories, issue tracking, wikis, and collaboration features.
  • Bitbucket: Bitbucket is another web-based version control repository hosting service that is owned by Atlassian. It supports both Git and Mercurial version control systems.
  • GitLab: GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager. It offers features such as continuous integration, continuous deployment, and issue tracking.

Mostly we will be understanding version control system in term of git and GitHub.

Install Git

Here is official guide to install git to your operating system:

Official page: Git - Installing Git (git-scm.com)

GitHub Guide: https://github.com/git-guides/install-git

How to introduce version control into your project?

Introducing version control into your project, git as control system.

The git init command is used to initialize a new Git repository in your local system. It creates a hidden folder named .git in the current directory, which contains all the necessary metadata required for version control.

git init 
Enter fullscreen mode Exit fullscreen mode

Staging, Commits in Git

In Git, a commit is a snapshot of your project at a particular point in time. When you commit, you're creating a new version of your project that you can refer back to later.

To commit changes in Git, you first need to stage them. Staging is the process of selecting which changes you want to include in your next commit. When you stage changes, you're essentially creating a "staging area" where you can group together changes that you want to commit.

To stage changes in Git, you can use the git add command. For example, if you've made changes to a file called name.txt, you can stage those changes with the following command:

# adding all file to stage
    git add .

# adding invidual file to stage  
    git add name.txt

# adding photograph to album
    git commit -m "name.txt file added"

# remove from stage 
    git restore --staged name.txt
Enter fullscreen mode Exit fullscreen mode

Branches in Git

In Git, a branch represents an independent line of development. It's essentially a movable pointer to a specific commit. When you create a new branch, Git creates a new pointer that moves independently of any other branches.

Untitled

Here's an example of how to create a new branch in Git:

git branch <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

This creates a new branch with the specified name, but it doesn't switch to that branch yet. To switch to the new branch, you can use the git checkout command:

git checkout <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

To create and switch into new branch in same command:

git checkout -b <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

Once you've switched to the new branch, you can make changes and commit them just like you would on the master branch.

Here's an example of how to merge a branch back into the main branch:

git checkout main
git merge <branch-to-merge>
Enter fullscreen mode Exit fullscreen mode

Here's an example of how to delete a branch in Git:

git branch -d <branch-to-delete>
Enter fullscreen mode Exit fullscreen mode

This deletes the specified branch. However, if the branch has not been merged yet, Git will prevent you from deleting it. In that case, you can use the -D flag to force the deletion

git branch -D <branch-to-delete>
Enter fullscreen mode Exit fullscreen mode

Git Workflow

A typical Git workflow involves several steps, including cloning a repository, making changes, staging those changes, committing them, and pushing them to a remote repository. Here's an example of what that might look like:

Clone a repository:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Create a branch

The main branch is usually called main. We want to work on another branch, so we can make a pull request and make changes safely. To get started, create a branch off of main.  Typically naming convention for branch after a feature or fix.

For feature feat/<name-of-feature>

For fixing a bug fix/<name-of-fix>

Make changes to file (and make a commit)

Once you've created a branch and moved the HEAD pointer to it by "checking out" to that branch, you're ready to make changes in repository. Next, save your changes. You're ready to start the commit!

git add .
git commit -m "descriptive commit message"
Enter fullscreen mode Exit fullscreen mode

Push your changes to the remote

So far commit is made locally, you're the only one that can see it. When you're ready to push your changes to the remote repository, you can use git push origin <branch-name>. This will push your changes to the remote repository, allowing others to see and use your changes.
origin is the default name for the remote repository from which you cloned your local repository. It is a short name to be used when reading and writing to that remote.

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

You can change or remove remote repository using

# removing and re-adding remote:
git remote set-url origin git://<new-url-here>

#To remove remote use this:
git remote remove origin
Enter fullscreen mode Exit fullscreen mode

Open a pull request

Pushing a branch, or new commits, to a remote repository is enough if a pull request already exists, but if it's the first time you're pushing that branch, you should open a new pull request. Pull request is a comparison of two branches that allows users to propose changes to a repository and request that those changes be merged into the main branch.

Merge into main

Once repository owner or team decide that the pull request looks good, they can merge it. By merging, you integrate the feature branch into the other branch (most typically the main branch). 

If you choose not to merge the pull request, you can also close pull requests with unmerged changes

git checkout main
git merge <branch-to-merge>
Enter fullscreen mode Exit fullscreen mode

Learning by doing

Learning by doing is the greatest way to learn anything.

LearnGitBranching :An interactive git visualization and tutorial. Aspiring students of git can use this app to educate and challenge themselves towards mastery of git!

Conclusion

Overall, Git is a powerful version control system that allows you to track changes to your code over time, collaborate with others, and maintain a history of your code. By following a typical Git workflow, you can make the most of Git's features and ensure that your code is well-organized and easy to manage.

For additional articles, you can visit the blog.viditkushwaha.com.

Top comments (2)

Collapse
 
jangelodev profile image
João Angelo

Hi Vidit Kushwaha,
Thanks for sharing

Collapse
 
viditkushwaha profile image
Vidit Kushwaha

My pleasure @jangelodev ❤️