Caitlin Palmer-Bright

Aspiring Full Stack Developer

Git without the jargon – Part 2

Welcome back! If you missed part 1 of this tutorial feel free to check it out here.  We’ve covered the basics of creating a Git repo and pushing it to GitHub, and now we’re going to look into branching.

Branching is super helpful when multiple people are working on the same project, but can be great for individuals who want to work on different aspects or features without getting them mixed up. To picture how branching works, imagine you were working on a group assignment (ugh, I know) . You make a start, and send a copy to your partner. Their copy is like a separate git ‘branch’. It’s from the same project, but not the same exact version. If you continue to make changes and they add in some changes, you’re going to end up with two versions of the same project. In real life that could be a nightmare to combine them, but git makes it nice and simple through the process of merging! Ah version control, what would we do without you?

Creating a New Branch

Now we’ll keep things nice and simple to start. There’s only really two commands you need to know for this – git branch and git checkout.

$ git branch

This command will show you all the branches you have in your local repo. There’s a handy asterisk next to the one you’re currently working with.

$ git checkout branch-name

This will allow you to switch between branches – the branch-name lets you tell git exactly which branch you want to look at. I like to think of it as two words – hey, check out that cool branch over there!  Now you can’t use this until you  actually make a different branch to switch to, which you can do by using…

$ git checkout -b new-branch-name

I said there was only two commands, but maybe this is two and a half – this is using git checkout but adding the -b afterwards allows you to create a new branch and check it out at the same time. Super efficient!

Hint: use descriptive names for your branches, it will make it much easier to remember which is which is you get a few going at once!

Now here’s a quick look at the output your terminal will give you when you use these commands. I’m looking at my branches, creating a new branch, and then switching around a few times (while always looking to see which branch I’m on at any time)

Alright, so go ahead and make your new branch, make sure it’s ‘checked out’ and let’s learn how to…

Make and Push Changes

Step 1. Make your changes

So now that you’re in your new branch, any changes you make will only apply to that branch, not the ‘master’ branch you started with. In my case I’m going to add a proper header to my site and change the styling.

Step 2. Add any new files

If you’ve created any new files in your project don’t forget to add them to your repo using git add . or git add file-name.

Step 3. Commit your changes

See, still familiar from last time! Remember to use a short but descriptive commit message.

$ git commit -m "Made a pretty header"
Step 4. Push your changes

Watch out – this step is *slightly* different to Part 1. Originally we used ‘git push origin master’ or just ‘git push’.  You want to remind  git that you’re pushing to a separate branch, so use the following

$ git push origin new-branch-name

‘origin’ is how we refer to the  remote repo, and then we specify the name of the branch we’re working on – this should be the same name as you’ve given your new branch locally.

Ok – so far so good. We have a new branch, it’s got some changes and it’s on GitHub. Have a look to see all those commands in action.

Merging Your Branch

So now that your branch is on GitHub, you need to merge it into your master branch. If you were working with other people it’s expected that someone else would check over your code and merge it for you, and at some point you’ll be doing that for other people. But for now, you’re on your own, buddy. Let’s get to it.

Step 1. Go to your new Branch on GitHub

You can find this by clicking the ‘branches’ button or by selecting your branch in the branches dropdown box.

Step 2. Compare and Make Pull Request

There is a nice big green button to press for this! Once you’re on the pull request page you can add a description if you like, and if you scroll down the page you can see the differences between the old and new versions of your branch. If you’re satisfied that you want to merge these changes into your master branch click ‘Create Pull Request’.

Step 3. Merge the Pull Request

As I mentioned earlier, it’s better to have someone look over your code before it’s merged when working in a team, but for the purposes of this tutorial, or any projects you’re working on by yourself, you get to press another big happy green button. Click ‘Merge Pull Request’ and then ‘Confirm Merge’. At this point you should delete your ‘new-branch’ since all its code is now merged into your master branch.

Well look at you go, you made a branch, committed and pushed your changes and then merged it into your master. Not so tough after all, is it? If my explanations above didn’t quite cut it, feel free to follow along in the video below where I show and explain all the steps again.

Tutorial done! You can go forth and Git! There’s some great git interactives on my resources page if you want to practice and also learn some more advanced git techniques. Please leave me a comment below if you liked these tutorials or if there’s something else you’d like me to blog about – I’m always keen for more ideas.

Happy coding!

Leave a Reply

Your email address will not be published.

*