DEV Community

Samuel Grasse-Haroldsen
Samuel Grasse-Haroldsen

Posted on • Edited on

Version Control - A Central Tenet In Software Development (Part 2)

Part 2

This is part 2 of my series of blogs on git. Here's a link to part 1: Version Control - A Central Tenet In Software Development (Part 1). In this installment I'll be going over some of the simpler aspects of git.

Branches

Branches are the bread and butter of git. They allow developers to experiment with new features without directly changing the main branch of production! And they are quite easy to work with. The only commands we need are git branch and git switch. git branch will list all the branches of a repo. It can also create a new branch if we add a word as an argument like this: git branch experiment. I'm sure you've guessed from context that git switch experiment will take you to the experiment branch. Upon the creation of a new branch, all files are identical to the original main branch. Changes will be reflected in all branches until they are committed in a specific branch.

Merging

Once we have made changes on a new branch, and they are ready to be merged into our main branch, we will run the command git merge experiment. This will merge our experiment branch with our main branch. Hopefully there are no merge conflicts because who wants that? But if there are conflicts we can manage with the tools git has given us. git diff will show us the differences between our two branches we've attempted to merge. Now we'll try our luck at conflict resolution by editing the files that git complained about. If we open the conflicting file we will be shown where the conflicts are(aka where the files are different). Once we have changed the file to our liking, we will now add them and commit them. Boom! Our new feature is now in our main branch!

Delete A Branch

You can easily delete a branch once you are finished merging with the d option git branch -d experimental.

Forward

With this new knowledge we can easily add new features in a reliable way. We can create new branches, merge them into our main branches, and delete them. Next week we'll go over how to collaborate with others.

Bonus Tool!

gitk is a very useful program that allows us to visually see our git trees: changes in our commits, merge conflicts, branches, etc. It's super useful and I highly recommend it! Here's a screenshot of my real-recipes application! Notice how I cleaned up those messy nested authorization if statements. Alt Text

Top comments (0)