Software Design and Development :: CS 246

04 Teach : Team Activity

Practice with Git

Objectives

Assignment

Before continuing, you should have completed this week's preparation assignment.

You're going to practice using git to track changes to simple text files.

Part I: Creating, Committing and Pushing Changes

  1. Open the command line and navigate to a directory where you'd like to work. Use the git clone command to create a local copy of the repository you created on GitHub during the preparation assignment.

    You can find the URL used to clone your repository on github. In the W04-Prepare reading, you were asked to complete a short GitHub tutorial which resulted in your first GitHub repository. Using the URL from the repository that you created, the command to clone your responsitory would be:

    git clone [URL of the GitHub repository your created from the reading]
    

    You will now have a directory named after your GitHub repository in your current directory.

  2. Using your favorite text editor, create a text file called sample.txt write a short message in it, and save it to the repository directory.

  3. Next, change directories to your git repository directory and use the git status command to view the current status of your repository. The sample.txt file will appear in the list of untracked files. This means it hasn't been added to the git repository yet.

    cd <your repository name>
    git status
    
  4. Next, use the git add command to add this file to the next commit:

    git add sample.txt
    

    If you rerun the git status command, you'll see that sample.txt now appears in the list of changes to be committed.

  5. To commit your changes, execute the git commit command, providing a message describing your changes:

    git commit -m "Added the best file ever."
    
  6. Now, to push your changes up to the copy of the repository on GitHub, execute the git push command.

    git push
    
  7. Reload your repository page on GitHub, and verify that your new file is listed there.

  8. On the repository webpage, click the link showing your commit history:

    A link represented by a small clock with the number of commits listed next to it

    This will show you a list of all the commits you have pushed to the repository.

Congratulations! You now know how to add, commit, and push changes from your local repository to the central repository on GitHub.

Part II: Pulling Changes

Often when using version control systems like GitHub, you're working with a team of other developers who will be making changes to the code as well. As they push their changes to the central repository, you need to pull those changes to your local copy, so that you can stay in sync with what everyone else is doing.

We're going to use GitHub's direct editor to simulate that process now. Normally, we don't edit files directly in GitHub, but we'll do that here in order to simulate another developer pushing a commit to the central repository.

  1. Open up your repository page on GitHub and click on the sample.txt file that you added in Part I.

  2. Click on the pencil icon on the right side of the screen, which will allow you to edit the file directly in GitHub.

    A link to edit the file on GitHub represented by a pencil icon.
  3. Change the text in the file, then at the bottom of the screen enter a commit message in the first field of the Commit Changes box. Then, Click the Commit Changes button at the bottom of the screen.

    A screenshot of the commit changes form on GitHub
  4. If you click on the commit history icon again, you should now see your commit from part one, along with the commit you just made.

    We're pretending that this new commit came from another developer, who made changes in their local repository, then pushed those changes to the central repository on GitHub.

  5. Now you need to use the git pull command to pull the changes from the central repository to your local copy:

    git pull
    
  6. View the contents of the sample.txt file on your computer, and you should see the updated version.

Congratulations! You now know how to pull changes from GitHub to your local repository.

🌟Stretch Challenge🌟

When multiple developers try to change the same files at the same time, this can result in what is called a merge conflict. By default, git is pretty good at merging changes during pulls and pushes. Sometimes however, it can't figure out the best choice, and so it leaves it up to you.

We're going to use GitHub's direct editor to simulate a merge conflict.

  1. Just like we did in Part II, open up GitHub and modify the text in the sample.txt file, making sure you commit the changes.

    Verify that the new changes show up in the commit history on GitHub, but don't pull those changes to your local repository yet.

  2. Now, open sample.txt on your computer, and modify the same line of text that you modified in step 1, but change it to something different.

  3. Add sample.txt to the commit by using git add, then commit the changes using git commit:

    git add sample.txt
    git commit -m "Made the best changes ever."
    
  4. Now, attempt to push your changes to GitHub using the git push command. Observe how it fails with an error:

    ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to ...
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    Remember, one of the fundamental rules of using git is:

    Always pull before you push.

  5. Attempt to fix this problem by executing git pull. This will also fail:

    CONFLICT (content): Merge conflict in sample.txt
    Failed to merge in the changes.
    ...
    When you have resolved this problem, run "git rebase --continue".
    
  6. If you open sample.txt now, you will see something like the following:

    <<<<<<< fa3f0f0d0dc7834b799af3e4f8a0cf21dd033ff6
    Hello World!!!
    =======
    Goodbye World!
    >>>>>>> Goodbye
    

    In this example the part between the hash code and the equal signs shows you the version of the code you're trying to pull down from the central repository.

    The part between the line of equal signs and HEAD shows the version of the code that is in your local repository. Since these lines dont match, git is unable to merge the changes, and a conflict results.

  7. To resolve this conflict, choose the version you want to keep, and delete the other version, along with the merge markers. So if you want to keep your version, you'd delete the line of > symbols, and everything above the equal signs. In the above example, you'd end up with:

    Goodbye World!
    
  8. Once you have resolved the conflict, you need to tell git to continue with the merge. To do this, we tell git that we have finished fixing sample.txt, then tell it to continue with the merging:

    git add sample.txt
    git rebase --continue
    

    If all went well, we should see a message from git telling us that it is applying our version of the commit.

    If you get an error message about `no rebase in progress`, instead of rebasing, you can just commit and push:

    git commit -m "Fixed merge conflict"
    git push
    
  9. As a final step, we use git push to send our merged changes back to the central repository.

Congratulations! You now know how to resolve simple merge conflicts using git.

Make sure everyone on your team understands and receives a copy of this code.

Be sure to complete the accompanying quiz for this assignment on I-Learn.