rickumali (130) [Avatar] Offline
#1
I have uploaded a new PDF of lab answers. Please find it at:

http://tech.rickumali.com/learngit/

This new version contains answers for Chapter 7. The exercise instructions were a little mangled, and I'm going to post an errata in the Errata thread.
Segilo (2) [Avatar] Offline
#2
In answer/hint PDF (dated: March 22, 2016), I have found Ch 7.5.2 appeared twice.
I think the latter(p.7) should be 7.5.3.

This is also referred as 7.5.2 in Ch 9.5.3 (p.20).
rickumali (130) [Avatar] Offline
#3
Thank you for posting this errata to the answer guide. I'll take a look and fix it, per what you described. Thank you again!
rickumali (130) [Avatar] Offline
#4
I updated the learngit-answerhint.pdf to address these defects. Thank you again for posting!
IamHammer (2) [Avatar] Offline
#5
So I've spent a lot of time trying to figure out what I was doing wrong with Lab 7.5.1...

1 Create a new directory named bigger_file, and make a Git repository.
mkdir bigger_file
cd bigger_file
git init

2 Download the zip file named LearnGitMoL_SourceCode.zip from the book’s
website. Unzip this file, and then copy the lorem-ipsum.txt file from the ZIP
This Git repository.
cp ../SourceCode/lorem-ipsum.txt .

3 Commit this file into the repository.
git add lorem-ipsum.txt
git commit -m "Adding lorem-ipsum.txt from SourceCode directory"

4 Copy lorem-ipsum-change.txt from the zip file of the previous step, and add it
to the directory. (You’ll need this again, so perhaps save this file to a good location.)
cp ../SourceCode/lorem-ipsum-change.txt .
git add lorem-ipsum-change.txt

5 Rename (or copy) this new file to lorem-ipsum.txt.
mv lorem-ipsum-change.txt lorem-ipsum.txt


Notice I used the command line move not git mv on the previous line. So I have changed the name (and effectively the contents of lorem-ipsum) in the working directory. The repository still has the original file contents. Running git status at this point will tell you
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   lorem-ipsum-change.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    lorem-ipsum-change.txt
        modified:   lorem-ipsum.txt


This may lead you to check the git GUI and check the diff there. The lorem-ipsum.txt file that is unstaged has a diff of the following:

@@ -1,19 +1,18 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc facilisis
venenatis felis, scelerisque consectetur lacus viverra in. Phasellus
scelerisque pretium nibh, ac porttitor massa vestibulum non. Aenean ornare
-convallis enim, et semper neque sagittis ut. Proin blandit dui vitae quam
+convallis enim, et change here. Proin blandit dui vitae quam
fermentum, non lacinia lectus molestie. Vivamus tempus viverra dui. Cras in
luctus felis. Pellentesque dictum tortor ipsum, id iaculis felis ultrices ut.
Nunc id velit dignissim, suscipit est ac, euismod libero. Phasellus fringilla,
nibh sit amet consectetur auctor, lorem magna euismod ante, tempor lobortis mi
mi sagittis odio. Donec fermentum sem vel neque pulvinar eleifend.

Nullam posuere condimentum rutrum. Vivamus vitae elit a tellus interdum
accumsan. Duis pretium viverra condimentum. Curabitur dapibus aliquet semper.
-Aliquam pretium nunc aliquam pellentesque molestie. Suspendisse potenti. In a
-erat sed ante rutrum commodo. Fusce congue nunc aliquet tellus dignissim
+Aliquam pretium nunc deleted below molestie. Suspendisse potenti. In a fringilla.

Cras non odio gravida, blandit risus sed, tempor lectus. Donec vel congue
mauris. Donec ultrices malesuada dictum. Suspendisse molestie pulvinar quam eu
dignissim. Aliquam eget faucibus neque. Phasellus at nibh dapibus urna
@@ -26,10 +25,11 @@ mattis, leo vel malesuada bibendum, magna orci aliquet lectus, a vulputate
augue neque a enim. Maecenas eu augue eros. Pellentesque vestibulum augue porta
sem convallis, nec vulputate ante ullamcorper. Nam laoreet, sapien in fermentum
elementum, ante massa vehicula eros, non luctus ipsum leo a metus.

Nunc velit lacus, pellentesque sit amet libero ac, rutrum facilisis est. Ut
+added a new line here do not be afraid added new line here do not be afraid
aliquam justo sit amet ornare condimentum. Curabitur nibh est, vehicula at
sodales non, posuere in quam. Quisque ornare rutrum arcu ut porttitor. Nunc
blandit justo eget nisl accumsan, eu auctor urna pretium. Proin non condimentum
tortor, non fermentum lectus. Ut et convallis sapien. Duis luctus viverra diam,
rutrum egestas purus. Duis blandit feugiat lorem, in pellentesque orci tempus

Which is not what is displayed in the answer guide. Exit the GUI and type
git diff

This gives you the difference between the lorem-ipsum.txt in the working directory with that of the file in the repository. It seems like that's what should have happened in the GUI, but I get different results in bash.

My gripe is I can't figure out how to get the git status to report that the file is recognized as being renamed!
rickumali (130) [Avatar] Offline
#6
Hi IamHammer,

Thank You for putting in all the details of your session. The log helped me spot the issue, and it was a case of unclear writing on my part.

In Step 4, you performed a 'git add', but I did not intend for readers to use 'git add'. However, I did write "and add it to the directory", which is not clear.

Try it again making the above change in Step 4. I've also put below my session to get the diff output from the answer guide.

I'll post another reply about the rename business.

% mkdir bigger_file
% cd bigger_file
% git init
Initialized empty Git repository in /Users/rumali/gitbook/bigger_file/.git/
% cp ../lorem-ipsum.txt .
% git add lorem-ipsum.txt
% git commit -m "Adding lorem-ipsum.txt from source directory"
[master (root-commit) 76a8cdb] Adding lorem-ipsum.txt from source directory
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum.txt
% cp ../lorem-ipsum-change.txt .
% mv lorem-ipsum-change.txt lorem-ipsum.txt
overwrite lorem-ipsum.txt? (y/n [n]) y
% git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   lorem-ipsum.txt

no changes added to commit (use "git add" and/or "git commit -a")
% git diff
diff --git a/lorem-ipsum.txt b/lorem-ipsum.txt
index dd194e9..b3ee228 100644
--- a/lorem-ipsum.txt
+++ b/lorem-ipsum.txt
...
rickumali (130) [Avatar] Offline
#7
Hi IamHammer,

As for your question about making Git recognize the rename, you will need to indicate as such with 'git mv OLD NEW' (or use 'git rm OLD' on the file that has been removed, and 'git add NEW' on the new file). This is all broken down in 7.2 of the book.

However, in the original session, we rename a file that we added to the index but never committed! Below is a new session that sets up our situation.

I'll have more to say afterwards.

% mkdir bigger_file
% cd bigger_file
% git init
Initialized empty Git repository in /Users/rumali/gitbook/bigger_file/.git/
% cp ../lorem-ipsum.txt .
% git add lorem-ipsum.txt
% git commit -m "Adding lorem-ipsum.txt from source directory"
[master (root-commit) 1103fb1] Adding lorem-ipsum.txt from source directory
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum.txt
% cp ../lorem-ipsum-change.txt .
% git add lorem-ipsum-change.txt
% mv lorem-ipsum-change.txt lorem-ipsum.txt
overwrite lorem-ipsum.txt? (y/n [n]) y
% git diff
diff --git a/lorem-ipsum-change.txt b/lorem-ipsum-change.txt
deleted file mode 100644
index b3ee228..0000000
--- a/lorem-ipsum-change.txt
+++ /dev/null
@@ -1,36 +0,0 @@
-Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc facilisis
-venenatis felis, scelerisque consectetur lacus viverra in. Phasellus
-scelerisque pretium nibh, ac porttitor massa vestibulum non. Aenean ornare
-convallis enim, et change here. Proin blandit dui vitae quam
-fermentum, non lacinia lectus molestie. Vivamus tempus viverra dui. Cras in
-luctus felis. Pellentesque dictum tortor ipsum, id iaculis felis ultrices ut.
-Nunc id velit dignissim, suscipit est ac, euismod libero. Phasellus fringilla,
-nibh sit amet consectetur auctor, lorem magna euismod ante, tempor lobortis mi
-mi sagittis odio. Donec fermentum sem vel neque pulvinar eleifend.
-
-Nullam posuere condimentum rutrum. Vivamus vitae elit a tellus interdum
-accumsan. Duis pretium viverra condimentum. Curabitur dapibus aliquet semper.
-Aliquam pretium nunc deleted below molestie. Suspendisse potenti. In a
-fringilla.
-
-Cras non odio gravida, blandit risus sed, tempor lectus. Donec vel congue
-mauris. Donec ultrices malesuada dictum. Suspendisse molestie pulvinar quam eu
-dignissim. Aliquam eget faucibus neque. Phasellus at nibh dapibus urna
% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   lorem-ipsum-change.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    lorem-ipsum-change.txt
	modified:   lorem-ipsum.txt

% ls
lorem-ipsum.txt


git status at this point says you have a new file (lorem-ipsum-change.txt) which we added to the index but have not yet committed. It also says it detected a deletion of this same file, and the modification of the original file.

Here's the rest of the session in which I try to follow the suggestions from git status:

% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   lorem-ipsum-change.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    lorem-ipsum-change.txt
	modified:   lorem-ipsum.txt

% ls
lorem-ipsum.txt
% git add lorem-ipsum-change.txt
% git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   lorem-ipsum.txt

no changes added to commit (use "git add" and/or "git commit -a")
% git add lorem-ipsum.txt
% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   lorem-ipsum.txt


This last status is satisfying to me. When I did 'git add lorem-ipsum-change.txt' in the above session, Git knew that the file was removed. (You could have also used 'git rm lorem-ipsum-change.txt'.) Git forgets about this file, but it saw that lorem-ipsum.txt (an existing file) was modified. Git only records "the final state of things."

I experimented with committing lorem-ipsum-change.txt after the add with 'git add lorem-ipsum-change.txt', to see if it would detect it as a rename.

% mkdir bigger_file
% cd bigger_file
% git init
Initialized empty Git repository in /Users/rumali/gitbook/bigger_file/.git/
% cp ../lorem-ipsum.txt .
% git add lorem-ipsum.txt
lorem-ipsum.txt
% git add lorem-ipsum.txt
% git commit -m "Adding lorem-ipsum.txt from source directory"
[master (root-commit) 016eb6d] Adding lorem-ipsum.txt from source directory
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum.txt
% cp ../lorem-ipsum-change.txt .
% git add lorem-ipsum-change.txt
% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   lorem-ipsum-change.txt

% git commit -m "Adding lorem-ipsum-change.txt"
[master 37b6e00] Adding lorem-ipsum-change.txt
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum-change.txt
% mv lorem-ipsum-change.txt lorem-ipsum.txt
overwrite lorem-ipsum.txt? (y/n [n]) y
% git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    lorem-ipsum-change.txt
	modified:   lorem-ipsum.txt

no changes added to commit (use "git add" and/or "git commit -a")
% git rm lorem-ipsum-change.txt
rm 'lorem-ipsum-change.txt'
% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    lorem-ipsum-change.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   lorem-ipsum.txt

% git add lorem-ipsum.txt
% git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    lorem-ipsum-change.txt
	modified:   lorem-ipsum.txt

% git commit -m "Dropping lorem-ipsum-change.txt on lorem-ipsum.txt"
[master 343781a] Dropping lorem-ipsum-change.txt on lorem-ipsum.txt
 2 files changed, 3 insertions(+), 39 deletions(-)
 delete mode 100644 lorem-ipsum-change.txt
% git log --stat -n 1
commit 343781a7211c41897f4abd11a7a5c5703e926f33
Author: Rick Umali <rumali@firstfuel.com>
Date:   Thu Sep 15 09:31:52 2016 -0400

    Dropping lorem-ipsum-change.txt on lorem-ipsum.txt

 lorem-ipsum-change.txt | 36 ------------------------------------
 lorem-ipsum.txt        |  6 +++---
 2 files changed, 3 insertions(+), 39 deletions(-)


This is less satisfying to me, but Git does record the final state of things: lorem-ipsum-change.txt was deleted, and lorem-ipsum.txt was modified. However, it doesn't record that this final state was generated by a rename. Below is a session in which I try to explicitly do a 'git mv'.

% mkdir bigger_file
% cd bigger_file
% git init
Initialized empty Git repository in /Users/rumali/gitbook/bigger_file/.git/
% cp ../lorem-ipsum.txt .
% git add lorem-ipsum.txt
% git commit -m "Adding lorem-ipsum.txt from source directory"
[master (root-commit) 3a385ea] Adding lorem-ipsum.txt from source directory
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum.txt
% cp ../lorem-ipsum-change.txt .
% git add lorem-ipsum-change.txt
% git commit -m "Adding lorem-ipsum-change.txt"
[master 7b3c75e] Adding lorem-ipsum-change.txt
 1 file changed, 36 insertions(+)
 create mode 100644 lorem-ipsum-change.txt
% git mv lorem-ipsum-change.txt lorem-ipsum.txt
fatal: destination exists, source=lorem-ipsum-change.txt, destination=lorem-ipsum.txt


This last session makes me laugh, but hopefully you can accept it (like I have). Git seemingly will not rename a file into an existing committed file. I interpret this to mean that we can never record the final state as being generated from a rename, only as a delete and modify as in the previous sessions.

Sorry that this is so lengthy! Please let me know what you think!
IamHammer (2) [Avatar] Offline
#8
I'm glad you went through all of those possibilities. I got the fatal error option, and the first status of modified, but I couldn't be sure I wasn't supposed to be looking for an explicit 'renamed' status.

I think at fatal I threw my hands up and decided to post to the forum.
Thank you for the prompt and thorough response!