- Basic knowledge of Git commands.
- A GitHub account.
By the end of this tutorial, you should be able to:
- Clone a repository.
- Make changes and push them.
- Raise a pull request (PR).
- Resolve merge conflicts.
Start by forking the existing repository. This repository is located on a remote server, which is typically GitHub for most projects. By forking it, you create a local copy which you can edit and push back to the remote repository.
When you fork the project, make sure to unselect the option to Copy the main branch only.
Next, clone the repo:
$ git clone https://github.com/[YOUR_GITHUB_ID]/Introduction_to_Git/
If you check the repo, you should see the two branches main
and dev
:
$ git branch -a
Checkout the remote dev
branch and then create a new branch from dev using the following command:
$ git checkout remotes/origin/dev
$ git checkout -b [your_name]-choice
Replace [your_name]
with your actual name.
Open the provided text file greek_gods.tx
in the repository. Choose one Greek god from the following list and replace the one in the file:
- Hera
- Poseidon
- Demeter
- Athena
- Apollo
- Artemis
- Ares
- Aphrodite
- Hephaestus
- Hermes
- Dionysus
Save and close the file.
Commit your changes and push them to your branch on the remote GitHub repository.
$ git add .
$ git commit -m "Chose my Greek god"
$ git push origin [your_name]-choice
Now, return to the GitHub repository online:
- Click on "Pull Requests".
- Select "New Pull Request".
- For the base repository, choose the original repository (not your fork) and its
main
branch. For the head repository, pick your fork and your branch ([your_name]-choice
). - Review your changes and click "Create Pull Request".
- Add a title and description for your PR and submit.
Given the pre-existing change in the original repository's dev
branch, you should encounter conflicts during the PR. Proceed to the next steps to resolve them.
The git diff
command allows you to view differences between your working directory and the index (staged changes). This is helpful to review changes before committing.
After you've made changes but before committing:
$ git diff
This will show you the changes in your working directory compared to the last commit.
If you've already staged the changes (i.e., after using git add
):
$ git diff --staged
During a merge conflict:
$ git log --merge
This command will show you the history of the commits that are causing the conflict, helping you make a more informed decision on how to resolve it.
Open the text file on GitHub by selecting the option to Resolve Conflicts
, and on the web editor you'll see conflict markers:
<<<<<<< HEAD
Apollo
=======
Athena
>>>>>>> [commit_hash]
To resolve this:
- Decide which choice to keep or combine them (in this case, keep your changes).
- Remove the conflict markers (
<<<<<<<
,=======
,>>>>>>>
). - Save the file.
After resolving all conflicts, create the PR.
When you encounter a merge conflict:
$ git mergetool
This will open a GUI that displays the conflict. Follow the tool's instructions to resolve the conflict and then save and exit.
Finally, merge the PR.
Congratulations! You've now tackled changes, navigated conflicts, and initiated a PR. Handling merge conflicts can seem daunting at first, but with practice, it becomes intuitive. This exercise imparts the collaborative flow of team projects using Git and GitHub, enhancing your version control skills.