Chapter 13: About Version Control

When working alone or with a team, Version Control allows you to backup your code for later retrieval.


What you will learn

  • About Git
  • Using Git with a team
  • How Version Control works

Key Terms

  • Commit: to backup changes in code to a local Git repository.
  • Push: to send local code repository to a remote server for yourself/others to access.
  • Pull: to download a remote repository and merge it in to your local repository.

Imagine that you are working on a personal app project. You just downed three Red Bulls and are feeling hyper-productive (literally). You write line after line of code and it's beginning to look as though the new feature you've been building is nearly finished.

Little to your knowledge and due to a foolish lack of testing, somewhere around line 3,498 you created a serious bug that will cause major issues in your app.

You've written 5,000 lines of code since then! Surely, you'll be up all night trying to find the source of the bug. At least you drank those Red Bulls... 😳

Never fear! This is where Version Control swoops in and saves the day.

Now let's return to the start of our story: Imagine you are working on a personal app project and have set up Version Control for your project. Every significant change made you push to a remote server. You write another significant portion of code and push that to the remote server, too. This continues until you have pushed code several more times.

Suddenly, you identify a serious bug in your code. Instead of having to parse through our entire code base, we can revert back to a previous state that our code was in.

Nice. 👌

About Git

The most prevalent and popular system for managing Version Control is called Git. You should be using Git throughout this book to push all your projects online.

This way, you are being a smart coder and saving online backups and you're building a portfolio to someday show a potential employer.

Git is relatively easy to learn and use – even for a beginner and it can streamline the process of working with multiple developers on the same application. With Git, it is easy to push your changes, pull down the changes of others onto your local machine, and fix merge issues when trying to bring them together.

Using Git with a team

Let's imagine another scenario. First, we have just hired two new developers – Sam and Pete.

Sam is working on building a killer new feature and makes a couple changes to the code base on his local machine.

Pete is working on a different feature, but in the same app. Pete makes some changes to the code base on his local device.

Do you see the problem here? At this point, Sam and Pete both have differences in the code that they are working on (Figure 1.13.1). How can the two become one so that both features can be included?

Figure 1.13.1 Screen Shot 2016-10-16 at 1.29.48 PM.png

Before Version Control, Sam would have had to send his updated code files to Pete somehow. Then Pete would have had to identify and copy and paste in Sam's changes into his project and save it. Then, Pete would have to send the updated project including his changes back to Sam so that they both can have the current project (Figure 1.13.2).

Figure 1.13.2 Screen Shot 2016-10-16 at 1.34.01 PM.png

This situation is only between two developers. Imagine how the complexity of this issue increases with a larger team of developers!

How Version Control works

As previously stated, Version Control is an amazing way to handle backing up your code base and being able to revert back to previous changes if needed. It also makes working with others way easier!

Imagine a development team with three developers named Jim, Sam, and Harry.

They are all working on an app and implementing various features.

Jim begins by writing some code and saves it into his version of the project. Sam does the same and Harry does too. Now all three of them have different versions of the same app (Figure 1.13.3).

Figure 1.13.3 Screen Shot 2016-10-16 at 1.43.07 PM.png

Now, Jim can now commit (push) his changes to a remote server (Figure 1.13.4).

Figure 1.13.4 Screen Shot 2016-10-16 at 1.44.55 PM.png

Once that is finished, Sam can access the remote server and pulls down Jim's changes. Version Control automatically merges in Jim's changes to Sam's project (Figure 1.13.5).

Figure 1.13.5 Screen Shot 2016-10-16 at 1.46.22 PM.png

The two projects are now merged. Sam can now push his unique changes (including Jim's changes) back to the remote server so that another developer can access the updated project (Figure 1.13.6).

Figure 1.13.6 Screen Shot 2016-10-16 at 1.48.42 PM.png

Harry the Junior Developer can now pull the project down to his local machine, make changes and add his features, then push his code to the remote server to merge them in and update the project (Figure 1.13.7).

Figure 1.13.7 Screen Shot 2016-10-16 at 1.50.43 PM.png

Wrapping up

Version Control is obviously extremely helpful when working alone and when working with a team. It is efficient, effective, and makes for a better development process overall.

The days of manually sending, merging, and re-sending code are gone. Git is an extremely helpful tool to learn. In the next chapter we will learn the basics of Git.

Exercise

For some good practice, create a new file in Terminal called title.txt using the touch command. Follow the steps necessary to add and commit the new file. Run git status to check that everything worked properly. If you get the message On branch master nothing to commit, working directory clean, revert back to a previous commit to remove the file title.txt.