Git Branching and Merging—Study Guide
Note: Most of the links have been removed from this page.
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
- As you've done before, create a new project with three files in it: file1.txt, file2.txt, file3.txt
- 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.
- Stage and commit these files whilev you are still in the master branch.
- 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:
- 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).
- You can create a new branch and switch to it with one command, for example: git checkout -b experiment.
- 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).
- To see all available branches, git branch (the * in the output indicates the current branch):
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.
- 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.
- Run the git checkout branch_name to switch to your new branch: git branch checkout experiment
- In this new branch, edit file1.txt. Change beautiful to glorious
- Stage and commit file1.txt: git commit -am "Changed one word to file1.txt in experiment branch"
- Run git log --oneline and notice that you have two commit SHAs (the gold 7-character codes):
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!
- Now switch to the master branch: git checkout master
- Run git log --oneline and note that the SHA you did in experiment has vanished. Only the SHA of the initial commit is showing:
- Take a screen capture of the output of git status, and save it.
- 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
- To rename a branch, enter git branch --move experiment experimented
Merge Branches
To merge a branch to another:
- Make sure both branches involved in a merge are "clean"—no impending changes. To check, enter git status on both branches.
- 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:
To get a different perspective, can use --oneline and --color_words:
- To see what branches are not merged, enter git branch --no-merged
- To see what branches are merged, enter git branch --merged. Take a screen capture of the output of this command, and save it.
- To do the merge git merge experiment (or whatever you call it).
Here is the output:
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.)
- 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.
- 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.