Changes

Jump to: navigation, search

DPS909 & OSD600 Winter 2017 - Lab 4

9,147 bytes added, 14:38, 2 February 2017
no edit summary
python
bird of paradise
 
Now let's merge our <code>water-animals</code> branch into <code>animals</code>:
 
# Switch to your <code>animals</code> branch
# Merge <code>water-animals</code> into <code>animals</code> (i.e., <code>git merge water-animals</code>)
# Confirm that your <code>animals</code> branch now contains the changes you made to <code>animals.txt</code>
 
Because our <code>water-animals</code> branch was ahead of our <code>animals</code> branch by 1 commit, git was able to do this merge using the '''fast-forward''' merge algorithm, which simply moves the branch ahead to align with the other branch. If you use <code>git log</code> you'll notice that there is no '''merge commit''' this time.
 
Next, let's merge our <code>jungle-animals</code> branch into <code>animals</code> as well. Since both of these branches touch the same lines of the same file, this won't work automatically, and we'll have to fix it manually:
 
# Switch to your <code>animals</code> branch
# Merge <code>jungle-animals</code> into <code>animals</code> (i.e., <code>git merge jungle-animals</code>)
 
Git will respond and indicate that there was an issue merging <code>animals.txt</code>:
 
Auto-merging animals.txt
CONFLICT (content): Merge conflict in animals.txt
Automatic merge failed; fix conflicts and then commit the result.
 
Let's fix the merge conflict. Confirm that we are mid-way through a merge and have a merge conflict using <code>git status</code>. You'll see something like this:
 
On branch animals
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: animals.txt
no changes added to commit (use "git add" and/or "git commit -a")
Open the <code>animals.txt</code> file. Here is what mine looks like:
 
horse
cow
chicken
<<<<<<< HEAD
whale
seahorse
dolphin
=======
monkey
python
bird of paradise
>>>>>>> jungle-animals
 
Notice the presence of <code><<<<<<< HEAD</code>, <code>=======</code>, and <code>>>>>>>> jungle-animals</code>. These are '''conflict markers''' and show the different versions of the lines in question. Because we are merging ''into'' <code>animals</code>, it is our <code>HEAD</code>, and everything between <code><<<<<<< HEAD</code> and <code>=======</code> is what is on this branch. Everything between <code>=======</code> and <code>>>>>>>> jungle-animals</code> is what is on the <code>jungle-animals</code> branch. Because both branches edit the '''same lines of the same file''', git needs us to resolve the conflict. We have a few options:
 
* Use what is in <code>HEAD</code>, and erase the lines from <code>jungle-animals</code>
* Do the opposite and use what is in <code>jungle-animals</code>, and erase what is in <code>HEAD</code>
* Combine the two sets of changes into one change
 
In this case, we just need to combine the entries into a single, longer list. He can simply remove the conflict markers and save the file. Here's what mine looks like when I'm done:
 
horse
cow
chicken
whale
seahorse
dolphin
monkey
python
bird of paradise
 
Now we can <code>add</code> and <code>commit</code> this conflict resolution in order to finish our merge. When we're done, we'll have a new '''merge commit''' that combines the changes from our two branches into <code>animals</code>.
 
==5. Rebasing, Squashing==
 
At this point our <code>animals</code> branch is where we want it, in terms of content; but it's a bit messy in terms of how we got there. Sometimes before we share (i.e., <code>git push</code>) branches to share with colleagues, we want to clean up our history. Wouldn't it be nice if we could take all the different commits we made, and combine them into a single commit that achieved the same result? We can, and it's called a '''rebase'''.
 
First, a few warnings. Unlike a merge, which always keeps your history intact, a rebase will alter your commit history. You should never do this to a branch that has been shared with other developers, since it will erase and re-create commits, which makes it impossible for others to collaborate with you on those commits. A rebase is only something you should do '''before''' you share your commits with others.
 
Let's practice a rebase on our <code>animals</code> branch, and '''squash''' our separate commits into one single commit:
 
# Switch to your <code>animals</code> branch
# Start an '''interactive rebase''': <code>git rebase -i master</code>
 
This will open your editor and show you all of the commits on <code>animals</code> that are ahead of <code>master</code>. Mine looks like this:
 
<pre>
pick 436a838 Adding animals.txt
pick 595f37e Added water animals
pick 3d7af87 Add jungle animals
 
# Rebase 21826a9..3b4d451 onto 21826a9 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
</pre>
 
The comments at the bottom tell us what our options are. We can '''pick''' a commit to include it, '''squash''' a commit to combine it with the previous commit (like using <code>git commit --amend</code>), '''fixup''' a commit to squash and throw away the commit message, etc.
 
In our case, let's modify things to include the first commit, and squash the next two into it. To do so, edit the commit message like so, then save and exit your editor:
 
<pre>
pick 436a838 Adding animals.txt
squash 595f37e Added water animals
squash 3d7af87 Add jungle animals
</pre>
 
Git responds with the same merge conflict we had previously:
 
<pre>
error: could not apply 3d7af87... Add jungle animals
 
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
 
Could not apply 3d7af871d2677c64399c674f6a5937b9bbc48852... Add jungle animals
</pre>
 
If you run <code>git status</code> you'll see this:
 
<pre>
interactive rebase in progress; onto 21826a9
Last commands done (3 commands done):
squash 595f37e Added water animals
squash 3d7af87 Add jungle animals
(see more in file .git/rebase-merge/done)
No commands remaining.
You are currently rebasing branch 'animals' on '21826a9'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
 
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
 
both modified: animals.txt
 
no changes added to commit (use "git add" and/or "git commit -a")
</pre>
 
The reason that our rebase is failing is because git is '''replaying''' our commits one-by-one on top of <code>master</code>, and as it does so, it's hitting a commit that is changing the same lines as a previous one. Once again, edit your <code>animals.txt</code> file to combine the merge conflict:
 
<pre>
horse
cow
chicken
<<<<<<< HEAD
whale
seahorse
dolphin
=======
monkey
python
bird of paradise
>>>>>>> 3d7af87... Add jungle animals
</pre>
 
Should become:
 
horse
cow
chicken
whale
seahorse
dolphin
monkey
python
bird of paradise
 
When you're done, <code>git add animals.txt</code> to signal to git that you've resolve the conflict. Then, you can tell git to '''continue''' running the rebase (i.e., do the next commit in your list): <code>git rebase --continue</code>. Finally, git finishes replaying all our commits, and gives us a chance to alter our new commit message:
 
<pre>
# This is a combination of 3 commits.
# This is the 1st commit message:
Adding animals.txt
 
# This is the commit message #2:
 
Added water animals
 
# This is the commit message #3:
 
Add jungle animals
 
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Feb 2 13:01:46 2017 -0500
#
# interactive rebase in progress; onto 21826a9
# Last commands done (3 commands done):
# squash 595f37e Added water animals
# squash 3d7af87 Add jungle animals
# No commands remaining.
# You are currently rebasing branch 'animals' on '21826a9'.
#
# Changes to be committed:
# new file: animals.txt
#
</pre>
 
You can change it, or leave it as is. Save and exit your editor. In the end, I have a single commit that combines all of my other commits into one:
 
<pre>
$ git log
commit afc5bad7f651be478c69c4e117102bfeb183323c
Author: David Humphrey (:humph) david.humphrey@senecacollege.ca <david.humphrey@senecacollege.ca>
Date: Thu Feb 2 13:01:46 2017 -0500
 
Adding animals.txt
 
Added water animals
 
Add jungle animals
</pre>
 

Navigation menu