Git and GitHub Course – Module 3 – Working with branches

By Prosyscom
In March 3, 2018
126 Views

[ad_1]

git-and-github-course-module-3-working-with-branches

In this third module of the free course of Git and GitHub we will delve into the Branches also known as ramifications or simply branches, they are a powerful feature that have version control systems, especially Git because it facilitates its use against others version control systems.

We will create branches both in GitHub Desktop which is the official GitHub client with graphical interface and also in the terminal from Git.

Full course: en.gburu.net/git-github

Agenda: Module 3 – Working with branches

  1. Objectives of module 3
  2. What are the branches?
  3. GitHub Desktop: create a branch
  4. GitHub Desktop: upload the new branch to the remote repository
  5. GitHub: merge branches and eliminate the created branch
  6. Git: work with branches

Objectives of Module 3

In this third part of the course of Git and GitHub, is focused on you can understand what are the branches, how they can help us, how and when it is good to apply them, make mergers between branches to add changes to the main branch of our project and also how to eliminate branches that we do not use. All this from GitHub Desktop and Git in the terminal.

What are the branches?

When we start a project in Git locally or remotely, we will always have the main branch and by default “master” this is the main branch we have, but we can have more branches. The branches do not allow us to have an alternative path in a project, for example: we have a web page in the master branch, we need a problem quickly, we do not want to make the changes in the branch. of production, this is where the magic happens. We simply create a new branch with the name “problem-1” and quickly and quickly we have our project as we left it in the last commit that a teacher made, and you can make all the changes we want and save them to be able to carry out our corresponding tests, without disturbing or modifying the production project that is in the master branch, and if our solution that applies to the project in the problem-1 branch works, we can make a merger of branches, and we merge the problem-1 branch with the teacher, so the project in the teacher already has the solution to the problem.


Advantages of branching

Thanks to the branches, we can have a project in master and we can add new features without having to work on the main branch, we simply create another one and when we finish the new feature, we merge it. In the same way it can be used to solve problems, and everything we want without affecting the main branch of the project.


Mergers

Being able to merge branches is something incredible because you can be in a different branch to the master and make many changes if you like them, merge them with master and ready. But not everything always goes well, there are 2 answers that we will receive when trying to merge branches:

  1. Fast-Forward: is an algorithm that analyzes the files that you modified for the last time in each branch that you are going to merge, for example: imagine the Pakito website that has 3 files (index.html, index.js and main.css), if in the master pakito branch I modify the HTML and in the new branch that I create I modify the CSS, at the time of merging the branches the Fast Forward algorithm will be applied, because different files were modified in each branch.
  2. Merge Manual: we will have this answer from Git, when we modify the same files in each branch that we are going to merge, like the previous example of Pakito, now the index.html in the 2 branches that try to merge, Git detects this automatically and as it is intelligent it asks you to select which is the best HTML file to merge, if the file index.html that you modified in master or the index.html that you modified in the new branch.

GitHub Desktop: create a branch

As mentioned previously the main branch and by default is the master, in this case our project stored in github.com/gpost1/git-github-2 we have to change the form of how the title of the blog is generated, as we do not know if the change will result we will create a new branch called ‘git-github-rama-1‘, by the following steps:

  1. Create our new branch in GitHub Desktop:
    git-and-github-course-module-3-working-with-branches-1
    git-and-github-course-module-3-working-with-branches-2
    git-and-github-course-module-3-working-with-branches-3
  • We already have our new branch created and we see that we are using it.
  1. Now let’s modify the file ‘index.hbs‘ to change the way the blog title is generated:
    git-and-github-course-module-3-working-with-branches-4
  2. Then we made a commit, as we saw in the previous module of the course:
    git-and-github-course-module-3-working-with-branches-5

GitHub Desktop: upload the new branch to the remote repository

We already have the branch created locally, now we want to upload it to the remote repository hosted on GitHub to compare the changes with respect to the ‘master’ branch or simply so that our teammates see our branch with the changes that we apply to the base project, to a possible future merger, if the changes are productive for us or our team.

  1. After having made the commit and we are going to ‘Publish branch‘:
    git-and-github-course-module-3-working-with-branches-6
  2. If we go to our repository on the web, we see that we now have 2 branches:
    git-and-github-course-module-3-working-with-branches-7
    git-and-github-course-module-3-working-with-branches-8
  3. Now let’s compare the changes with the master branch:
    git-and-github-course-module-3-working-with-branches-9
    git-and-github-course-module-3-working-with-branches-10

GitHub: merge branches and remove the created branch

To merge the changes we made in the branch ‘git-github-rama-1‘ we simply merge it with the ‘master‘ branch, in Git this is achieved thanks to ‘merge‘, we’ll see when we do it in the terminal. Now we are going to merge 2 branches by means of GitHub and then we are going to eliminate the branch that we had created since it could be merged perfectly with the main branch and we will no longer use it.

We will need to open an extraction request (Pull request), it is the previous step to merge a branch, in this step git will analyze our branch to know if it is in good condition for a merger if we can continue it towards a merger. Otherwise, we will have to see what fault is occurring to repair it.

  1. Create a ‘Pull request’:
    git-and-github-course-module-3-working-with-branches-11
    git-and-github-course-module-3-working-with-branches-12
  2. We can go to ‘Pulls requests’:
    git-and-github-course-module-3-working-with-branches-13
  3. Merge the branches:
    git-and-github-course-module-3-working-with-branches-14
    git-and-github-course-module-3-working-with-branches-15
  4. the branches were successfully merged, now we are going to eliminate the one we no longer need:
    git-and-github-course-module-3-working-with-branches-16
    git-and-github-course-module-3-working-with-branches-17
    git-and-github-course-module-3-working-with-branches-18
  • We already created a new branch, we applied changes to the project and now we merged it with the master branch and then we eliminated the branch that we no longer needed.

Git: work with branches

Now we have to perform the same procedures but from the terminal or command console using Git, to create a branch from Git 2 forms: git branch <name-branch> or git checkout -b <name-branch, the latter serves to join 2 commands the branch and then the checkout, the branch serves to create branches and the checkout to access them.

  1. Let’s check if there are branches, and which ones are we using git branch:
    git-and-github-course-module-3-working-with-branches-19
  • We see that there are 2, and we are using the ‘git-github-rama-1‘.
  1. Change to the ‘master’ branch, to do this, just run git checkout master. Master can be any branch name you want to change to.:
    git-and-github-course-module-3-working-with-branches-20
  2. Update local repository with the remote git fetch https://github.com/gpost1/git-github-2.git:
    git-and-github-course-module-3-working-with-branches-21
  3. Create a new branch and access it with git checkout -b rama-2:
    git-and-github-course-module-3-working-with-branches-22
  4. Now that we are in the ‘rama-2’ branch, we are going to modify a file and commit it:
    git-and-github-course-module-3-working-with-branches-23
  1. The changes were accepted, now we are going to merge the branches: ‘rama-2’ with ‘master’. For that, we’re going to use git merge <branch-name>. First of all you have to go back to the master branch, and then ask him to merge its content with the ‘branch-2’:
    git-and-github-course-module-3-working-with-branches-24
  1. Remove branches that we do not need with git branch -D <nombre-rama>:
    git-and-github-course-module-3-working-with-branches-25
  • We already eliminated 2 branches that we do not need anymore.

We finish this module, now you can create branches, apply changes, merge them and delete them with GitHub Desktop or Git from the terminal. See you in the next module of this free Git and GitHub course, you can share this module on social networks or leave your comment!!

We have more courses for you in: en.gburu.net/courses

[ad_2]

قالب وردپرس