Using Git with GitHub and VS Code: Part 2

The previous part of this guide covered cloning a repository and starting a repository on your local machine. We also covered the general steps for working on a project, such as adding to staging and committing staged changes.

This part of the guide illustrates undoing changes, and managing branching and merging.

Undo Changes

The github_demo1 repository contains the colors.py file.

Let’s say you delete the color blue and then add the changes to staging using the command: git add colors.py

To undo the staging: git reset colors.py

The git reset command only removes the changes from staging. It doesn’t modify the actual file. To restore blue to the Python file: git checkout -- colors.py

Suppose you delete the color blue and then add the changes to staging using the command: git add colors.py

Then, you commit the changes: git commit -m "deleted blue"

To undo both the commit and staging: git reset HEAD~1

Finally, to restore the value blue to the Python file: git checkout -- colors.py

Branch and Merge

You can think of branches in Git as development lines that run parallel to the main line. Each branch represents an independent line of development where changes can be made without affecting the main branch until it’s decided to merge them.

In the diagram:

  • Circles represent the commits in each branch.

  • Update 1 and Update 2 branch off from the Main branch, makes their own commits, and eventually merge back into the Main branch.

Create Branch and Make File Change

To find out which branch you're on, use the command: git branch

This will highlight the current branch with an asterisk (*).

Create a branch update1 and switch to that branch: git checkout -b update1

Now type git branch, and you’ll see that you are on the update1 branch.

When you create a new branch from the main branch, Git copies the current state of all files from main. In our case, main has files named colors.py and README.md, and these two files will also be present in the new branch update1.

Let’s update the colors.py file by adding the color orange:

Now we will add and commit the file change to this branch:

Push the change to the update1 branch on the remote repository:

git push origin update1

Merge Change

To merge the update1 change with main, we can:

  • Merge using the command line

  • Merge using the pull request on GitHub

We’ll use the pull request method:

Step 1: Go to your repository on GitHub.

Step 2: Click Compare & pull request.

Step 3: Review the changes and click Create pull request.

Step 4: Once the pull request is created, you can merge it into the main branch by clicking Merge pull request and then clicking Confirm merge.

GitHub indicates that the merge is successful:

To ensure your local main branch is up-to-date with the latest change from the remote repository:

Step 1: Switch to the main branch where you want to pull the changes: git checkout main

Step 2: Pull the latest change from the remote repository: git pull origin main

Resolve Merge Conflicts

Merge conflicts occur when Git isn't clear about what to merge. Let's look at a scenario to understand this: We'll add different colors to colors.py in both the main and update1 branches. On merge, a conflict will arise. We'll resolve the conflict by updating colors.py in both branches to include all colors.

Step 1: Make changes in update1.

  • Switch to update1: git checkout update1

  • Add pink to the colors.py file.

  • Add and commit the change:

git add colors.py
git commit -m "Added pink color"

Step 2: Make changes in main.

  • Switch to main: git checkout main

  • Add yellow to the colors.py file.

  • Add and commit the change:

git add colors.py
git commit -m "Added yellow color"

Step 3: Merge the update1 change into main: git merge update1

Git will now show a merge conflict because colors.py has different values in the main and update1 branches:

Step 4: Edit the file to include both new colors, yellow from the main branch and pink from the update1 branch. The result should look like this:

colors = ["red", "green", "blue", "orange", "yellow", "pink"]

Step 5: Stage and commit the resolved changes:

git add colors.py 
git commit -m "Resolved merge conflict"

Step 6: Push the resolved changes to the main branch: git push origin main

This command updates the remote main branch with the resolved changes, making sure that everyone working on the project has access to the most up-to-date code.

Step 7: To synchronize update1 with main:

git checkout update1
git merge main
git push origin update1

This will ensure both branches (main and update1) will have the same content for the colors.py file.

We have reached the end of the guide. Congratulations! You now have a good understanding of undoing changes, managing branches and merges, and resolving merge conflicts.