Note: You will need to work with a partner for this lab. (We'll call the partners, Partner A and Partner B.)
Only Partner A should perform the following steps:
Only Partner B should perform the following steps:
Both partners should now log on to the server.
Create a project directory:
cd ~/projects mkdir project-1063 cd project-1063
Next, both partners should clone the newly created repository. Note that the actual name of the repository will depend on the user name of Partner A. Adjust the URL appropriately by replacing PARTNER_A_USER_NAME with the actual Partner A user name.
git clone https://github.com/PARTNER_A_USER_NAME/TheStory
Depending on your configuration, you may need to enter your username and password for GitHub. However, after this point, your credentials should be cached and you shouldn't need to enter them again and again.
Now, enter the directory and then pull the contents from the repository.
cd TheStory git pull
Partner B on the Command Line
Partner B should now perform the following:
- Create a new file, "story.txt", with emacs:
Next, copy and paste the following text into the file. Then, save the file and exit.
Whilst we were talking, we heard a sort of sound between a yelp and a bark. It was far away; but the horses got very restless, and it took Johann all his time to quiet them. He was pale and said, "It sounds like a wolf - but yet there are no wolves here now." "No?" I said, questioning him. "Isn't it long since the wolves were so near the city?"
Check the status of your local git repository by typing:
In order to track the file, type:
git add story.txt
Check the status again:
In order to commit your changes to your local repository, type:
Emacs will open enabling you to type a description of what you've changed (added, modified, deleted). This should be a useful message. For ideas about how to ensure that your message is useful, read The Art of the Commit (by David Demaree). After entering your message, save and exit emacs as usual.
Check the status again:
Let's push the changes to the remote repository:
Verify that your changes are visible on GitHub by going to the same URL that you used for the
git clone operation.
Partner A on the Command Line
Partner A can now receive the changes made by Partner B. To do so, Partner A should execute:
To check the directory contents, execute:
Open the file in emacs:
Next, copy and paste the following text to the end of the file. Then, save the file and exit.
"Long, long," he answered, "in the spring and summer; but with the snow the wolves have been here not so long."
What changed from the previous commit? Have a look with:
Now, check the status:
Now, use git to add, commit, and push your changes to your remote repository on GitHub.
Partner B in Emacs
Partner B can now receive the changes made by Partner A, but let's be smarter and use the features available from within emacs.
Start emacs, opening "story.txt".
Pull the latest changes from the remote GitHub repository. There's no need to exit emacs; emacs contains a built-in client for interacting with many source control systems. Enter the sequence: C-x v +
Emacs has now executed git pull and displayed the results in a separate window. You'll note that the text in the window indicates that "story.txt" was changed. You may also note that the buffer (for story.txt) hasn't yet changed. In order to refresh the buffer, type M-x revert-buffer and answer "yes". (Don't worry! If you forget to do this emacs will warn you.)
Add the following text to the end of the file:
Whilst he was petting the horses and trying to quiet them, dark clouds drifted rapidly across the sky. The sunshine passed away, and a breath of cold wind seemed to drift over us. It was only a breath, however, and more of a warning than a fact, for the sun came out brightly again.
Save the file with C-x s
Within emacs, we can check for differences between the version in the local repository and our uncommitted changes by typing C-x v =
Remember, that to close a window in emacs, with the cursor in that window, type C-x 0 (the last character is a zero). Close the diff window now.
Add and commit this change of adding the third paragraph by typing C-x v v. A new window will open asking you to type in a check-in comment. Do so and then type C-c C-c to exit that window.
To view a log of changes, type C-x v l (the last character is a lowercase L). You can scroll through the log in the same way that you scroll through any other buffer in emacs.
At this point, we've modified the file, added it to our local repository and committed it, but we haven't yet pushed it to the remote repository. We can do that by typing C-x v P
Partner A in Emacs
Partner A should begin, as always, by pulling any remote changes. Begin by opening emacs to edit "story.txt". Then, pull any remote changes with C-x v +
If a change is detected, remember to execute M-x revert-buffer and confirm the change.
Add the following text to the end of the file:
Johann looked under his lifted hand at the horizon and said, "The storm of snow, he comes before long time." Then he looked at his watch again, and, straightway holding his reins firmly - for the horses were still pawing the ground restlessly and shaking their heads - he climbed to his box as though the time had come for proceeding on our journey.
Now, save the file with C-x s. Then, commit the change with C-x v v, adding a commit message, and then typing C-c C-c. But don't push the change just yet!
Partner B Makes Another Change in Emacs
Remember that Partner A has committed changes to the local repository but hasn't yet pushed them. What happens if Partner B makes and pushes a change before Partner A pushes their change?
Partner B should now open emacs to "story.txt" and paste the following text to the end of the file:
Jonathan looked out toward the horizon and said, "The rain, she comes before long time." Then he looked at his watch again, and, holding his reins firmly - for the horses were still restless and shaking their heads - he climbed to his box as though the time had come for proceeding on our journey.
Now, save the file with C-x s. Then, commit the change with C-x v v, adding a commit message, and then typing C-c C-c. Push the file to the remote repository with C-x v P.
Partner A Tries to Push
Remember that Partner B pushed his committed changes to the remote repository but Partner A didn't push his changes yet. Let's see what happens if Partner A tries to push now.
Partner A types C-x v P in an attempt to push.
The attempt to push failed because the remote contains changes that we don't have locally. We'll need to get those changes before we can proceed. We can do that the same way that we normally do, with C-x v +.
Note that the message informed us of a merge conflict in file "story.txt". Be sure that your cursor is positioned in that window ("story.txt") and reload the file from disk by typing M-x revert-buffer.
Git is telling us that it doesn't know how to automatically merge these changes. In other words, there's a conflict between the changes that Partner A made and the changes that Partner B made that can't be resolved automatically. This isn't bad! It's a natural consequence of working together on the same file and it's easily resolved.
Note that in one file there can be multiple conflicts. In order to navigate easily from conflict to conflict you can use C-c ^ n to move to the next conflict and C-c ^ p to move to the previous conflict. If your cursor isn't already positioned on the conflict, try typing C-c ^ n now and note the highlighting.
At this point, you need to decide if you want to keep your change or the other change. To keep your change you'd use C-c ^ m (keep my change) or C-c ^ o (keep other change). For now, keep your own change, so type C-c ^ m. Type C-x S to save the file.
Now that you've resolved the conflicts you can complete the merge. Temporarily put emacs to sleep with C-z.
On the command line, type
git commit. The default message in emacs is often sufficient so you can just type C-x C-c to exit. Your changes have now been committed to the local repository. You can now resume emacs by typing
The last step is to push to the remote. Do so now with C-x v P.