GitHub Exercises
Quick Links
- Exercise 1: Creating a Repository
- Exercise 2: Creating a Branch on a Repository Through GitHub
- Exercise 3: Making Edits on a Repository Branch
- Exercise 4: Creating a Pull Request on GitHub
- Exercise 5: Merging a Pull Request
- Exercise 6: Merging a Pull Request with Conflicts
- Exercise 7: Updating a Branch with the Main Branch on GitHub
- Exercise 8: Deleting a Branch on GitHub
- Exercise 9: Forking a Repository on GitHub
- Exercise 10: Deleting a Repository Through GitHub
The following exercises are designed to familiarize yourself with the different features GitHub has to offer. Each exercise is sequential, but can be returned to later if you need a reminder on how to use a specific feature.
Exercise 1: Creating a repository
In this first exercise, you will create a repository. Repositories are the main way projects are stored on GitHub and act as a folder for all the project files.
Note: Before you begin, you will need to create an account on GitHub. Follow GitHub’s instructions and return to this exercise when you are finished.
- On your GitHub homepage, locate the green “New” button in the “Top repositories” column and click the button.
- Enter a name for your repository. This will be your personal repository, so be as creative as you want with the name.
- Add a description to your repository. For this exercise, add: “this is my first repository.”
- Adjust Visibility adjusts who can see your repository. For these exercises, click the dropdown arrow and use the “Private” option.
- Click the “Add README” button. README files are generally used as longer descriptions for the repository and explain what your project is about.
- Click the “Create repository” button. Ignore the other options.
- GitHub will now load the “<> Code” page (home page) of your new repository.
Exercise 2: Creating a Branch on a Repository Through GitHub
Branches are used in repositories to edit and test new features in a project. When you want to implement these changes, you will send them as a pull request for the repository owner to approve. For this exercise, you will create a branch on your newly created repository. Pull requests will be covered in Exercise 4.
- On the “<> Code” page of your repository, click on the “main” dropdown arrow.
- Click “View all branches.”
- We have not created any branches yet, so click on the green “New branch” button.
- A pop-up window will appear on your screen. Enter a name for your branch under "New branch name." For public branches, entering detailed and purposeful names for your branches is important for others who may reference your repository
- Click the green “Create new branch” button.
- Your new branch will now appear alongside the “main” branch under “Your branches” and “Active branches”.
Exercise 3: Making Edits on a Repository Branch
With a new branch created, you can now make edits to your project without overwriting the main branch. For this exercise, you will make an edit within this new branch.
- On the “<> Code” page of the repository, click on the “main” dropdown menu and select your created branch.
- GitHub will refresh, and instead of seeing “main” in the dropdown menu, you will see the name of the branch you created.
- In your new branch, click on the “README” file.
- This will open the "README" file. Here, you can view the contents of the file. On this screen, click the pencil icon.
- GitHub will now open the "README" file in its code editor. Click any line in the file and add text.
- Click the green “Commit changes…” button in the top right corner.
- In the pop-up window, select “Commit directly to the (your branch name here) branch” before proceeding.
- Click on the “Commit changes” button. This will save the changes to your branch.
Exercise 4: Creating a Pull Request on GitHub
When working with a group, you must understand not only the purpose of pull requests but also how to make them. In this exercise, you will practice creating a pull request using your private repository.
- On the “<> Code” page of your repository, click on the “Pull requests” section in the top menu.
- In the top right, click on the green “New pull request” button.
- Below “Comparing changes,” you should see that “base: main” is being compared to “compare: main”. Click on either dropdown menu and select your created branch. (i.e., base: Main ← compare: Your Branch).
- GitHub will now load a comparing changes screen. This screen shows you what edits or adjustments have been made to a file. Click the green “Create Pull Request” button.
- You are now in the “Open a pull request” menu. Under “Title”, add a short and descriptive title for your commit.
- In the “Description” section below, write about the changes you made to the file. Descriptions in pull requests must be appropriately detailed so that others in a public repository can easily understand what changes you made to a file.
- Click the green “Create pull request” button.
- After clicking this button, you will be brought to the pull request’s page.
Note: You can also click the green “Compare and pull request button” and get the same result.
Note: You can scroll down on this screen to see what specifically in the code has changed. A “+” shows that additions were made to a line, and a “-” shows that deletions were made to a line.
Exercise 5: Merging a Pull Request
Merging pull requests is done to save edits made to a file on another branch. When a pull request is made, the owner or other administrators of the repository look through the changes and will choose to accept them or deny them. You will perform a merge using the pull request created in the last exercise.
- If you are not already there, open the “Pull requests” tab in your repository.
- GitHub will now show any currently opened pull requests. Click on the one you just created.
- This page contains the details of the pull request. Scroll down and click the green “Merge pull request” button.
- Before confirming the merge, GitHub will show you the details of the pull request you created. Add any additional information to the message or description, and when you are done, click the green “Confirm merge” button.
- GitHub will now display text that reads: “Pull request successfully merged and closed.”
Exercise 6: Merging a Pull Request with Conflicts
In the previous exercise, you were able to merge a branch without any problems. When multiple people are working on the same project, edits made by different people can conflict with each other, requiring manual edits made to that file. In this exercise, you will create a conflict within your own repository and then resolve it in a pull request.
- If you are not already on your branch’s page, select your branch.
- Click on the “README” file.
- Click on the pencil icon on the right side of the page.
- Edit any of the lines in the “README” file. Take note of which line you edited, as you will need this information later in the exercise.
- Click on the green “Commit changes…” button.
- Write a message and description for your commit.
- Click the green “Commit changes” button in the dialogue box.
- Now you will need to switch branches. Click the “<> Code” page of the repository and select the main branch.
- Click on the “README” file.
- Click the pencil icon.
- Select the same text line you wrote on in the other branch and write something different. If you aren’t sure which line you chose, switch back to the other branch and check the file.
- Click the green “Commit changes…” button.
- Add a message and description in the dialogue box.
- Click the green “Commit changes” button.
- Your file will now have two unique edits made on the same line of a file. Go back to the “<> Code” page and click on the “Pull requests” tab in the top menu.
- Click the green “New pull request” button.
- Under “Compare changes”, click the dropdown arrow to select your branch and compare it to the
main branch.
(i.e., base: Main ← compare: Your Branch) - This screen will display a message that reads “Can’t automatically merge.” Click the green “Create pull request” button.
- Add a title and description to the pull request.
- Click the green “Create pull request” button.
- Your screen will load the pull request details. Scroll down to see a message that reads “This branch has conflicts that must be resolved.” Next to this message, click the “Resolve conflicts” button.
- GitHub will now load the code of the file you edited with the changes from both branches displayed. In a group setting, you would need to confer with your group before proceeding and resolving any conflicts. A few options will be displayed on how these conflicts can be resolved:
- Accept current change: uses the edit on the main branch and discards the edit on the other branch.
- Accept incoming change: uses the edit on the other branch and discards the edit on the main branch.
- Accept both changes: edits made to the file on both branches are kept and merged onto the same line.
- Click on the “Accept both changes” option.
- You will now see that both changes have been added to the file on separate lines of code. Make any additional edits you want and then click the “Mark as resolved” button.
- Double-check that the appropriate changes have been made to the file. Click on the green “Commit merge” to finalize these changes.
- GitHub will now bring you back to the main page of the pull request. Scroll down and click the green “Merge pull request” button.
- Click the green “Confirm merge” button.
- GitHub will now display a “Pull request successfully merged and closed” message. Go back to the file you made changes to using the “<> Code” page to double-check that the changes were successful.
Exercise 7: Updating a Branch with the Main Branch on GitHub
Updating a personal branch is important when working in a repository with multiple people, and changes are often committed. In this exercise, you will create a pull request for your branch that will implement the changes you made to the “README” file on the main branch in the previous exercise.
- If you have not selected it already, click the “main” dropdown arrow and select the branch you created.
- Below the branch dropdown menu, a message will appear that reads “This branch is 1 commit behind main.” Click on the “1 commit behind” part of the message.
- GitHub will be able to automatically merge the changes to your branch. Click on the green “Create pull request” button.
- Scroll down and click the green “Create pull request” button again.
- Click on the green “Merge pull request” button.
- Write a commit message and description.
- Click the green “Confirm merge” button.
- The pull request will successfully close and merge. You can double-check if it was successful by clicking on the “README” file in your branch.
- Click on the “<> Code” page of the repository.
- If it is not already selected, click the “main” dropdown arrow and select your branch.
- Click on the “README” file.
- Verify that the changes made to the “README” are the same between the main branch and your branch.
Note: your branch might say that it is “1 commit ahead of main.” This is nothing to worry about and can be ignored.
Exercise 8: Deleting a Branch on GitHub
When you are finished making changes to a repository, you can delete your branch to avoid cluttering the branch menu of the repository. In this exercise, you will practice manually deleting a branch.
Note: for this exercise, do not use the “Delete branch” button. You may use it in the future, but not for this exercise.
- On the “<> Code” page of the repository, click “branches” next to the “main” dropdown menu. A little squiggle mark and a number should be next to “branches.”
- GitHub will display all the branches in the repository. Find your branch, and next to the ellipses (the three horizontal dots) will be a trash can. Click on the trash can.
- Your branch will still be listed, but under the updated section, it should read “Deleted now.”
- Double-check that the branch is deleted. Click the “<> Code” page in the top menu of the repository.
- Next to the branches, there will be a “1” displayed before “branches.”
Note: you can also click the “View all branches” option in the dropdown menu.
Note: before leaving this screen, an arrow will appear to the branch you deleted. You can click this to restore the branch to the repository. If you leave this screen, you will be unable to recover the branch.
Note: this is the final exercise using a personal repository. If you wish, you may delete this repository, as explained in exercise 10.
Exercise 9: Forking a Repository on GitHub
Since collaboration is a major part of using GitHub, you must know how to properly work on public repositories. This will require creating a copy of a repository for your account, otherwise known as “Forking”. The following exercise will teach you how to fork a repository that you are not the owner of.
- Locate the repository you want to use and click it to open its “<> Code” page.
- Click the “Fork” button.
- Click the green “Create fork” button in the bottom right corner.
- GitHub will create and load a new copy of the repository. Your username will appear before the repository name on this fork. (i.e., username/example.)
Note: Under the repository name, make sure that the name of the repository is available. If not, you will be unable to create a fork.
Exercise 10: Deleting a Repository Through GitHub
A forked repository is where you will create a branch and make your own edits to a project. This way, the main repository will be preserved while you make edits to your branch and forked repository. You will not need the forked repository you created in the previous exercise. This exercise will walk you through deleting a repository.
Note: double-check that you are in the forked repository created in the previous exercise. If you want to keep this repository, you may use the repository from Exercises 1-8. Following the exercise with the incorrect repository could have disastrous consequences.
- Click on the “Settings” page in the top menu of your forked repository.
- On the "Settings" page, scroll down to the very bottom and locate the “Danger Zone.”
- In the “Danger Zone”, click the "Delete this repository” button at the bottom.
- A box will pop up on the screen asking to confirm that you want to delete this repository. Click the “I want to delete this repository” button to continue the deletion process.
- The next page brings up an additional warning. When you delete a repository, everything related to it, including the wiki, issues, comments, packages, secrets, workflow runs, and collaborator associations, will be permanently deleted, and you will be unable to access anything related to this repository. When you have read through the warnings, click the “I have read and understand these effects” button.
- Next, type the name of the repository in the following box. The repository name will be in quotation marks above the typing area.
- Once this is done, click the “Delete this repository” button.
- GitHub will now open your account’s repositories page. The message “Your repository (deleted repository name here) was successfully deleted” will appear on the screen.
Note: if you haven’t signed in for a while, GitHub will need to confirm your account. Type in your password or the passkey associated with your account to continue.
Next, Git practice exercises.