• 12 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/21/19

Learn branch management with git merge, git fetch, and git pull

Log in or subscribe for free to enjoy all this course has to offer!

How does git merge work?

We have talked about and demonstrated how to contribute to an open source project.  After you pushed your branch to GitHub, you created a pull request for the project owners. Let's say that the project owner really likes your README file, and wants to integrate it into the master branch.  How will they add your branch to the master branch? The command git merge! 

Let's see how it works with a quick example: A developer, Christie, has a local repository from the master branch.  She creates a new branch called topic at commit E of the master branch.

Topic Branch created at commit E of Master Branch
The topic branch created at commit E of the master branch

Let's say you are the project owner, and are working on the master branch. A pull request comes in with the code from Christie's topic branch. To merge the topic branch to the master branch, you type out:

git merge topic

Now the topic and the master branch will merge to create a new commit.  In the picture below, this new commit is represented by the letter H: 

Commit H is a new merge of the topic and master branch
commit H is a new merge of the topic and master branch

As you can see, when two branches are merged using the git merge command, it provides both branches' commit histories in the merge.  This gives you a clear history of when the branch was created, and when it was merged to one commit. Neat, huh? 

Keep your repository up do date with git fetch & git merge

Git fetch is a command that fixes an issue that can happen when a team starts working on separate local repositories on their respective computers.  Git was created to allow this type of decentralized work. It just brings up one problem...How do you know if any changes have been made to the main repository?

You see, if you're working on a branch from an outdated main, you may stand the chance of having an incompatible or outdated branch. 😱

Git software is on it! 😉 The command to bring in all the latest updates is  git fetch

Let's say you are working on Christie's topic branch on your local repository. You decide to use git fetch to update the main branch in your local repository. After using  git fetch, you notice that the main branch has two new commits.  You decide to integrate the updated master branch with your topic branch. Guess what command you use? 

git merge, of course! 

After using git merge, you will have an updated master branch connected with your topic branch. That seems like a lot of work though.  Don't you want to update the changes that you pulled with git fetch anyway?  Why is it a two-step process?

That is where the next command comes in!

Update your local repo with git pull

Git Pull is a combination of  git fetch and  git merge in one command. Let's backtrack and try the update again using the git pull command. Let's say that you have the following commits on the master branch in your local repository:

master branch in your local repository
Master branch in your local repository

Master on origin just means the master branch you have on your local repository.  This helps distinguish it from the master branch on the remote repository. Now you have an update from the remote repository showing the following commits on the master branch:

new commits in the remote repo
New commits in the remote repo

Using  git pull   will fetch the new commits and merge them with the master on origin ( the master branch you have on your local repository).

updated master in local repo using git pull
Updated master in local repo using git pull

Let's recap!

That was some heavy lifting!  Let's do a quick review:

  • git fetch  to get the changes from the remote repo.

  • git merge  integrates those changes from the remote repo.

  • Use   git pull  to combine the git fetch and git merge commands in one go!

Going further:

If you want a good article that talks about the difference between these three commands, check out this Git Pull article

Example of certificate of achievement
Example of certificate of achievement