Collaboration: The main game of git and GitHub

Archit Singh
Knowledge Brewery
Published in
5 min readJun 28, 2020
An image in which people are joining each others hand representing collaboration
Photo by Perry Grone on Unsplash

Collaboration in the workplace takes into account employees’ ideas, skills, experiences, and opinions. When individuals work together openly, processes and goals become more aligned, leading the group towards a higher success rate of achieving a common goal.

With that being said, let’s get started !!

So far, we have discussed the basics of git, i.e. how to create a new repository, how to make our first commit etc. (Refer to the previous post for basics of git and GitHub)

Let’s suppose you’re working on a new project. You may have a lot of ideas and features popping in your head while you’re working on it; some of them might be easy to implement and some might not be. Maybe you’re working with other people who are all kind of doing their own thing. This is where branching comes in!

Git Branch

An image showing branching in git
Photo from snapblox.com

Branches in git are a part of our everyday development process. It can be understood as a separate place where we can try out our new ideas without affecting the main branch. Once we are done with the changes, it is totally up to us to decide whether we want to include them in the main branch or not.

A branch is effectively a pointer to the snapshot of the changes that you have made in your project. If you want to add a new feature, no matter how big or small it is, you can create a new branch to encapsulate your changes and you can do whatever you want to do on that branch until you decide it’s time to merge it.

An image showing branching in git
Photo showing branching in git from Atllassian.com

The diagram above visualizes a repository with two isolated lines of development, one for a little feature, and one for a longer-running feature. By developing them in branches, it’s not only possible to work on both of them in parallel, but it also keeps the main master branch free from questionable code.

Working with branches

When you create a branch, all Git needs to do is create a new pointer, it doesn’t change the repository in any other way.

To create a branch:

git branch "branch-name"

To list all the branches in your repository:

git branch

To delete a branch:

git branch -d "branch-name"

However, this is a safe operation as it prevents you from deleting the branch if it has unmerged changes. To delete a branch forcefully even if it has unmerged changes :

git bnrach -D "branch-name"

To operate further on the resulting branches the git branch command is commonly used with other commands like git checkout.

Git Checkout

In Git terms, a “checkout” is the act of switching between different versions of a target entity. With this command, you can navigate between the branches that you’ve created in git. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

Think of it as a way to select which line of development you’re working on.

The git checkout command works hand in hand with the git branch command. Once you’ve created a branch using the git branch command, you can switch over the new branch using the git checkout command.

However, both these operations can be performed by a single command:

git checkout -b "new-branch-name"

The above example simultaneously creates and checks out <new branch>. The -b option is a convenience flag that tells Git to run git branch <new-branch> before running git checkout <new-branch>.

By default, git checkout -b will base the new-branch off the current HEAD. An optional additional branch parameter can be passed to git checkout. In the above example, <existing branch> can be passed which then bases the <new-branch> off the <existing-branch> instead of the current HEAD.

git checkout -b <new-branch> <existing-branch>

So now, if you have some existing branches in your repository and you want to switch between them :

$ git branch
master
branch_1
branch_2
$ git checkout branch_2

So far, we have covered how to create a branch in git and how to checkout from a branch and head over to another branch.

Once you are done adding your new feature, you might want to include in the master branch and this can be achieved with the help of git merge command.

Git Merge

An image showing git merge
Photo showing merge operation in git from Atlassian.com

The git merge command lets you take the independent lines of development and integrate them into a branch in which you want to. Although this command can be used to merge multiple branches into one, it’s commonly used to merge two branches into a single one.

How git merge actually works?

Git merge takes two commit pointers, usually the branch tips and tries to find a common base commit between them. Once a common base commit is found, it combines the changes of each queued merge commit sequence.

Say we have a new branch feature that is based off the master branch. We now want to merge this feature branch into master.

An image showing how the git merge process works

Invoking the merge command will merge the specified branch feature into the current branch, here master. ( The merge algorithm is automatically determined by git)

So, after we have merged the two branches, the branch structure looks somewhat like this :

An image showing how the git merge process works

How to merge two branches into one using the git merge command?

Let’s say you have two branches one of which has the new feature that you’ve recently implemented and you want to merge this new branch into the master branch.

  • git checkout the branch you want to merge into
  • type git merge branch_name where the branch_name is the name of the branch you want to merge from

So, if we are on the master branch and want to merge into the new_branch, then :

git merge new_branch

So far, so good !!

Now you know how to collaborate with people while working along using git and GitHub. In the next post, we’ll be talking about more about git: rebase, editing commits, resetting, reverting, git log, git hooks etc.

I’d love to hear about your suggestions if any in the comments section.

Thanks for sticking by. There a lot more to come. Thank you and have a nice day !!

--

--

Archit Singh
Knowledge Brewery

I’m a 22-year-old undergrad majoring in Information Technology, always looking forward to a career in the field of Computer Science.