Navigate into the lesson_01_files
directory (this assumes you are currently in your jsd
directory)
$ cd lesson_01_files
Create a new directory named nursery_rhymes
$ mkdir nursery_rhymes
Navigate into the nursery_rhymes
directory using the cd
command
$ cd nursery_rhymes
nursery_rhymes
is where all the files for this project will reside
Let’s create a new git repository to track all the changes inside your nursery_rhymes
project
Initialize a new repository by running the following command:
$ git init
Create two new files using the touch
command
$ touch humpty_dumpty.txt jack_be_nimble.txt
Open up your text editor and add the following content to humpty_dumpty.txt
and save the file
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.
Next, add the following text to jack_be_nimble.txt
and save the file
Jack be nimble,
Jack be quick,
Jack jump over
The candlestick.
Let’s stage the file so the changes can be moved from the “working directory” to the “staging area”
$ git add .
git add .
will add all files in the working directory to the staging area
Next, let’s commit the changes
$ git commit -m "add initial nursery rhymes"
Branches allow developers work on a new feature without making any disruptive to changes to the main project (i.e. master branch) during the development of the feature
Git branches are created using the git branch {branch-name}
command
Let’s create a new branch that will allow us to experiment with “remixes” to the Humpty Dumpty nursery rhyme
First let’s create a new branch named “humpty_dumpty_remix”
$ git branch humpty_dumpty_remix
Confirm that the new branch was successfully created by running git branch
(without the “name” option)
$ git branch
You should receive the following output:
humpty_dumpty_remix
`* master
Note that you should have two (2) branches listed: master and humpty_dumpty_remix
Also note that the asterisk (*) denotes which is currently active; we are currently “on the master branch”
Press the q
key to escape from the git branch
output in your terminal
In order to actually make changes on your new branch, we need to check it out so that it will be our “active branch”; otherwise, any changes we make now will be associated with our master branch
Run the following command to check out our humpty_dumpty_remix branch
$ git checkout humpty_dumpty_remix
git checkout {branch_name}
allows developers to switch between branches in their local repository
Output should look similar to the following:
Switched to branch 'humpty_dumpty_remix'
You are now “on the humpty_dumpty_remix branch”
Open humpty_dumpty.txt in your text editor and change replace the last two lines of the nursery rhyme with the following lines; save the file:
He didn't get bruised he didn't get bumped
Humpty Dumpty bungee jumped!
Run git diff
to observe the line-by-line differences; your output should look like the following:
diff --git a/humpty_dumpty.txt b/humpty_dumpty.txt
index 9516671..b5eeca0 100644
--- a/humpty_dumpty.txt
+++ b/humpty_dumpty.txt
@@ -1,4 +1,4 @@
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
-All the king's horses and all the king's men
-Couldn't put Humpty together again.
+He didn't get bruised he didn't get bumped
+Humpty Dumpty bungee jumped!
Let’s stage the file so the changes can be moved from the “working directory” to the “staging area”
$ git add humpty_dumpty.txt
Next, let’s commit the changes
$ git commit -m "remix humpty dumpty nursery rhyme"
Run git log
to review the latest commit history
commit c6e54274bb0d6984e21622cb58274f48a80dc7d9 (HEAD -> humpty_dumpty_remix)
Author: Kareem Grant <kareem@getuserwise.com>
Date: Mon Aug 5 18:58:02 2019 -0400
remix humpty dumpty nursery rhyme
commit 7edef5f21ddf4b42afb72c079cd03f3e2267e32c (origin/master, master)
Author: Kareem Grant <kareem@getuserwise.com>
Date: Mon Aug 5 16:36:50 2019 -0400
Add initial nursery rhymes
Note that our recent commit lives on the humpty_dumpty_remix branch
We’re happy with our changes and now we’re ready to merge the changes into the master branch
First, we need to move back to the master branch (we are currently on the humpty_dumpty_remix branch)
We accomplish this by using the git checkout {branch_name}
command
$ git checkout master
The output should confirm that you successfully switched back to the master branch
Switched to branch 'master'
Now let’s merge the recent changes from our humpty_dumpty_remix branch into the master branch
Merges in git are accomplished using the git merge
command
$ git merge humpty_dumpty_remix
The output should be similar to the following:
Updating 7edef5f..c6e5427
Fast-forward
humpty_dumpty.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
The “fast-forward” reference means that Git was able to do an expedited merge because no conflicts were found (reference)
Run git log
to review the latest commit history
commit c6e54274bb0d6984e21622cb58274f48a80dc7d9 (HEAD -> master, humpty_dumpty_remix)
Author: Kareem Grant <kareem@getuserwise.com>
Date: Mon Aug 5 18:58:02 2019 -0400
remix humpty dumpty nursery rhyme
commit 7edef5f21ddf4b42afb72c079cd03f3e2267e32c (origin/master)
Author: Kareem Grant <kareem@getuserwise.com>
Date: Mon Aug 5 16:36:50 2019 -0400
Add initial nursery rhymes
Notice that the HEAD (pointer which usually points to the most recent commit) is now on the master & the humpty_dumpty_remix branch; this means that our latest commit are up-to-date on both branches
Next, we’ll delete the feature branch because it’s no longer needed (the feature has been merged to master)
To delete a branch we’ll use the git branch -d {name_of_branch}
$ git branch -d humpty_dumpty_remix
The expected output should look similar to the following:
Deleted branch humpty_dumpty_remix (was c6e5427).
Let’s create and check out a new branch that we’ll use to experiment with additional changes to jack_be_nimble.txt`
Enter the following command to create a new branch and check it out at the same time
$ git checkout -b update_jack_be_nimble
Here’s what the output should look like
Switched to a new branch 'update_jack_be_nimble'
git checkout -b {new_branch_name}
uses the -b flag to checkout the newly created branch immediately after creating the branch
Let’s makes some updates to our other nursery rhyme
Run git branch
to confirm that you are currently on the update_jack_be_nimble branch
Open jack_be_nimble.txt
in your text editor and replace all references to “Jack” with “Jill”; save the file
Stage the file so the changes can be moved from the “working directory” to the “staging area”
$ git add jack_be_nimble.txt
Next, let’s commit the changes
$ git commit -m "change references from jack to jill in 'jack be nimble'"
Next, we’re going to create a merge conflict on purpose and learn how to properly handle those conflicts
Run git branch
to confirm that you are currently on the master branch
Open jack_be_nimble.txt
in your text editor and change all references to “Jack” to “Jennifer”; save the file
Stage the file so the changes can be moved from the “working directory” to the “staging area”
$ git add jack_be_nimble.txt
Next, let’s commit the changes
$ git commit -m "change references from jack to jennifer in 'jack be nimble'"
Let’s see what happens when we attempt to merge the update_jack_be_nimble branch into the master branch
Ensure you are currently on the master branch and run the following command:
$ git merge update_jack_be_nimble
You should receive output that resembles the following:
Auto-merging jack_be_nimble.txt
CONFLICT (content): Merge conflict in jack_be_nimble.txt
Automatic merge failed; fix conflicts and then commit the result.
Git is informing us that it was not able to automatically merge the two branches due to a merge conflict
Open jack_be_nimble.txt in your text editor; you should see the following:
<<<<<<< HEAD
Jennifer be nimble,
Jennifer be quick,
Jennifer jump over
=======
Jill be nimble,
Jill be quick,
Jill jump over
>>>>>>> update_jack_be_nimble
The candlestick.
Note that some additional text has been added to our file:
<<<<<<< HEAD
=======
`>>>>>>> update_jack_be_nimble
Think of these new lines as “conflict dividers”
The =======
line is the “center” of the conflict.
All the content between the center and the <<<<<<< HEAD
line is content that exists in the current branch master which the HEAD ref is pointing to
Alternatively all content between the center and >>>>>>> update_jack_be_nimble
is content that is present in our merging branch
We’re going to fix the merge conflict by directly editing the file and removing the conflict dividers and any changes that we’ve determined (after discussing with our team) should be updated and/or deleted
It’s been determined that the new name of our nursery rhyme subject should be “Jill”
Remove all the conflict lines and the lines that start with “Jennifer”; save the file
jack_be_nimble.txt should look like the following after the changes:
Jill be nimble,
Jill be quick,
Jill jump over
The candlestick.
Next, run git add jack_be_nimble.txt
to stage our changes
Then commit the changes using git commit
$ git commit -m "merged and resolved conflict in jack_be_nimble.txt"
Run a git log
to view the updated commit history; the output should look similar to the following:
On branch master
commit fd13059a4f83b0746ab95bb9aaf34c74ce8f74f6 (HEAD -> master)
Merge: e82c6db 8fddf9d
Author: Kareem Grant <kareem@getuserwise.com>
Date: Tue Aug 6 09:57:34 2019 -0400
merged and resolved conflict in jack_be_nimble.txt
commit e82c6db31408abfbe34a9917c75fa3e7be4e6053
Author: Kareem Grant <kareem@getuserwise.com>
Date: Tue Aug 6 09:35:06 2019 -0400
change references from jack to jennifer in 'jack be nimble'
commit 8fddf9dd40a77f94b10cab353f56e1fabddeeefb (update_jack_be_nimble)
Author: Kareem Grant <kareem@getuserwise.com>
Date: Tue Aug 6 09:32:59 2019 -0400
change references from jack to jill in 'jack be nimble'
commit 7f102fe037156f74aa891e05c7779edddcb27531
Author: Kareem Grant <kareem@getuserwise.com>
Date: Tue Aug 6 09:27:56 2019 -0400
remix humpty dumpty nursery rhyme
commit 8409ae16180d09e67187330718c86310600bbb70
Author: Kareem Grant <kareem@getuserwise.com>
Date: Tue Aug 6 09:26:30 2019 -0400
add initial nursery rhymes
Congrats! You’ve successfully resolved our merge conflict