barn

Git Branching and Merging—Study Guide

Note: Most of the links have been removed from this page.

o

Git users create a branch when they want to experiment with a different idea. If the branch experiment works, they merge the branch into the project; if not they discard it.

A video of the following tutorial is available at link

Set Up a Git Project

  1. As you've done before, create a new project with three files in it: file1.txt, file2.txt, file3.txt
  2. Put some distinguishing text in each of the three files, for example, This file (file1.txt) contains beautiful writing, changing the 1 to 2 and 3 in the other two files.
  3. Stage and commit these files whilev you are still in the master branch.
  4. Run git log --oneline and make a note of the SHA a 6- or 7-character code in gold.

Check for Branches

If at some point, you are not sure what branches you have, rin the git branch command. If you just see master (or whatever you've renamed itb to), you've got only that one branch.

Create a Branch

To create a new branch:

  1. Enter, for example, git branch experiment (the new branch will be based on the last commit). Make sure the master branch (or whichever branch the new branch will be based on is clean).
  2. You can create a new branch and switch to it with one command, for example: git checkout -b experiment.
  3. If you enter git status, you will not be able to see the new branch because you are on a separate branch (typically the master branch).
  4. To see all available branches, git branch (the * in the output indicates the current branch):
    git branch output showing two branches
    In this image, you can see the output of the initial git branch command, then the command to create a branch, then the ensuring git branch command showing both branches.
  5. For your instructor, take a screen capture of the output of the preceding command, and save it.

Switch to a Newly Created Branch and Edit a file

To switch to a different branch, your working directory must be clean.

  1. Run the git checkout branch_name to switch to your new branch: git branch checkout experiment
  2. In this new branch, edit file1.txt. Change beautiful to glorious
  3. Stage and commit file1.txt: git commit -am "Changed one word to file1.txt in experiment branch"
  4. Run git log --oneline and notice that you have two commit SHAs (the gold 7-character codes):
    git log output showing two branches
    In this image, you can see the output of the commit of the three files in the experiment branch. Next, you can see the output of the git log --oneline command: now we've got two commits!
  5. Now switch to the master branch: git checkout master
  6. Run git log --oneline and note that the SHA you did in experiment has vanished. Only the SHA of the initial commit is showing:
    git log output showing two branches
  7. Take a screen capture of the output of git status, and save it.
  8. Check out these other git branch commands:
    • To delete a branch, enter the command git branch -d experiment (or --delete) If Git prevents the delete because you have not merged the new branch yet, enter git branch -D experiment
  9. To rename a branch, enter git branch --move experiment experimented

Merge Branches

To merge a branch to another:

  1. Make sure both branches involved in a merge are "clean"—no impending changes. To check, enter git status on both branches.
  2. Switch to receiving branch (to which the new branch will be merged, usually "master": git checkout master
    To recollect what the changes were: git diff one_branch..another_branch: for example, git diff master..experiment
    Example: In my project, I had a master branch and an experiment branch; one line in master had the word "beautiful"; in experiment, "glorious." To view, I ran git diff master..experiment:
    git diff output showing changes in two branches
    To get a different perspective, can use --oneline and --color_words:
    git diff output showing changes with --color-words
  3. To see what branches are not merged, enter git branch --no-merged
  4. To see what branches are merged, enter git branch --merged. Take a screen capture of the output of this command, and save it.
  5. To do the merge git merge experiment (or whatever you call it).
    Here is the output:
    Output of git merge command
    If you look at file1.txt in the working directory, you will see that the change originally to file1.txt in experiment has been made. (Fast-forward in not covered in this course.)
  6. To double-check things:
    • In my case, run git diff master..experiment—no output, meaning no difference now between the two branches.
    • Run git branch -- merged—in my case, experiment is not green.
  7. At this point, you can deleted the branch that was merged (in my case, experiment).

Complete This Unit

To reassure your instructor that you have gotten through this unit successfully:

E-mail those screen captures to your instructor at admin@mcmassociates.io (click this link).

Return

Return to your bookmarked course main page.

Related Information

Please note that whenever there is a reference to "remote," the context is GitHub, which we'll cover in coming units.

Git Branching - Branches in a Nutshell. From git-scm.com

Git Branches: List, Create, Switch to, Merge, Push, & Delete. From nobledesktop.com

Maintained by admin@mcmassociates.io.