Working together

So far we have only worked in the "main" branch (previously this was commonly referred to as the "master" branch). This branch is meant for a stable version of your project and not meant for ongoing development. So far it worked out because you are the only person working in this repository, but imagine when all your team members also make changes on the same code. You would not be able to see easily what is finished and what is still in progress, plus there might be a broken product on "main". So from now on: never push to main. Instead, you should create branches to work in isolation on the new features and functionalities. The branch should have a name which is descriptive about its purpose.

Creating a branch

First, a branch must be created.

Terminal

Use the following command to make a new branch:

git branch <branch_name>
git checkout `branch_name`

Or the shorthand

git checkout -b <branch_name>

When you want to push a new branch, which you will do in the next assignment, you need to push in the following way;

git push -u origin <branch_name>  

Branches are created from the currently checked out commit. Typically you create a new branch from the most recent working version of your product. Sometimes you may want to try a different approach to a problem, then you can go back a few commits and branch from there.

Assignment


For this assignment you are going to create a new branch for adding the required tools for this assignment to the README file. For example, you need Git and SSH to complete this assignment. Decide on a descriptive name and create the branch. Add the requirements and make sure to push the branch to the remote repository.

(You may run into an error The current branch new_branch has no upstream branch., the error should already include instructions on how to resolve this).

Switching to another branch

When you made changes on a branch, you sometimes need to change branches to look at something from someone else (for example when reviewing code). To change branches, you need to do the following;

Make sure that all the updates are visible on your machine by fetching;

git fetch --all 

Now you can view and switch branches by using the following commands;

git branch -a 
git checkout <branch_name>

Switching branches is possible when you have no uncommitted changes. Make sure that all changes are either stashed (see Additional features) or committed before changing branches.

Assignment


Create a new branch called merge_readme. Switch to it, and push it to GitLab. Make sure you can see the new branch on the website.

When on the branch, make one commit which changes multiple lines in the README. Also change the title of the README.

Switch back to the main branch, and then also make a commit here that makes different changes to the README. Make the title of the README different than on the merge_readme branch.