Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab5: Traveling between parallel universes

IUT d'Orsay, Université Paris-Saclay

Objectives

The goal of this lab is to understand the following commands and concepts:

Merging divergent branches

When a merge cannot be done automatically, we end up with divergent branches, with manual merges to perform and potential conflicts to resolve.

Merge without conflicts

First, let’s try to reproduce divergent branches.

  1. Create a Lab5/ directory with a file merge-without-conflict.txt containing three arbitrary lines of text on lines 1, 3, and 5.

  2. Synchronize your Local Repo with the Remote Repo.

  3. Locally, change the third line of text of merge-without-conflict.txt.

  4. Commit this change without pushing.

  5. In the Web IDE, change the fifth line of text of merge-without-conflict.txt in order to simulate a change made on another workstation or by a collaborator.

  6. Commit this change.

You should now have two versions of merge-without-conflict.txt.

For example, in the Local Repo, you have:

merge-without-conflict.txt
First line of text

Third line of text

Fifth line

In the Remote Repo, you have:

merge-without-conflict.txt
First line of text

Third line

Fifth line of text
  1. Run git push and observe the error.

Git detects that changes were made on the Remote Repo that are not present in our Local Repo, and suggests running git pull first to update our Local Repo.

  1. Run git pull and observe the error. Have you understood why we created divergent branches? If not, call your instructor.

  2. Run git graph and observe.

We must now perform a manual merge.

  1. Run the following command.

git merge origin/main

This merge is considered an (automatic) merge without conflicts because the modifications coming from the two branches are on different blocks of lines.

  1. Git performed an add automatically and asks you to write a commit message for the merge operation, with an auto-generated message already filled in. You may modify this message if you wish.

  2. Once the commit is done, check that the contents of merge-without-conflict.txt combine both origin/main and main.

For example:

merge-without-conflict.txt
First line of text

Third line of text

Fifth line of text
  1. Run git graph and observe.

  2. Run git push to synchronize your Local Repo with the Remote Repo.

Merge with conflicts

In the previous example, we saw a merge where the changes made to the two versions of the same file did not overlap, which allowed Git to automatically merge the changes.

This time, we will again create and merge divergent branches, but with changes that are in conflict.

  1. Create a file merge-with-conflict.txt in Lab5/ with a single line of arbitrary text.

  2. Synchronize your Local Repo with the Remote Repo.

  3. Locally, add a second line of text to the file merge-with-conflict.txt.

  4. Commit this change without pushing.

  5. In the Web IDE, add another second line of text to merge-with-conflict.txt.

  6. Commit this change.

You should now have two versions of merge-with-conflict.txt.

For example, in the Local Repo, you have:

merge-with-conflict.txt
First line of text
Second line of text

In the Remote Repo, you have:

merge-with-conflict.txt
First line of text
Another second line of text
  1. When running git push or git pull, you will receive the same errors as before regarding divergent branches.

  2. Run git graph and observe.

We must now perform a manual merge.

  1. Run git merge origin/main.

This time, the automatic merge fails because the two versions of merge-with-conflict.txt have different modifications on the same line. Git tells you that you must resolve the conflict(s) in merge-with-conflict.txt, then commit that resolution.

  1. Run git status and observe.

  2. Open merge-with-conflict.txt in a text editor. You should see the following lines:

merge-with-conflict.txt
First line of text
<<<<<<< HEAD
Second line of text
=======
Another second line of text
>>>>>>> origin/main
  1. Replace this block of text (between <<<<<<< HEAD and >>>>>>> origin/main) with the final version of your choice (which may differ from the two proposed versions). For example:

merge-with-conflict.txt
First line of text
Merged line of text

This time, our (manual) merge with conflicts does not come with an automatic add and commit.

  1. Add, commit, and push this conflict resolution.

  2. Run git graph and observe.

Branches

Divergent branches are branches created by accident, but we can also create branches intentionally to better manage our project (for example, to follow collaborative workflow principles in your SAÉ project).

Creating and traveling between branches

  1. Create a branch experiment/branch by running the following command.

git branch experiment/branch
  1. Switch branches by running the following command.

git checkout experiment/branch
  1. Create a file experiment-branch.txt in Lab5/ with one line of arbitrary text.

  2. Add and commit this change.

  3. Synchronize this new branch with your Remote Repo by running the following command.

git push --set-upstream origin experiment/branch
  1. Go back to the main branch by running the following command.

git checkout main

Note that experiment-branch.txt does not appear on the main branch because the commit that created this file is on the experiment/branch branch.

  1. Create a file main-branch.txt in Lab5/ with one line of arbitrary text.

  2. Add and commit this change.

  3. Run git graph and observe.

Merging two branches

  1. Use what you have learned to merge the experiment/branch branch into the main branch.

  2. Run git graph and observe.

Creating a branch from a past commit

Sometimes it is useful to create a branch from a commit in the past

  1. Go back to an older commit using git checkout.

  2. Run git status and observe that you are in the detached HEAD state.

We can now create a branch from this commit and make changes that will be saved in our history.

  1. Create a branch experiment/old-commit using git branch.

  2. Switch branches using git checkout.

  3. Make changes, then add and commit them on the new branch.

  4. Run git graph and observe.

  5. Synchronize this new branch with your Remote Repo.

Listing branches

  1. List the branches in your Local Repo with the following command.

git branch

You should see main, experiment/branch, and experiment/old-commit.

  1. List the branches in your Local Repo and your Remote Repo with the following command.

git branch -a

You should see six branches, including the remote versions of the local branches.

  1. List the branches in your Local Repo that are already merged into the main branch with the following command.

git branch --merged main

You can also combine the -a and --merged options with the following command.

git branch -a --merged main
  1. List the branches in your Local Repo that are not yet merged into the main branch with the following command.

git branch --no-merged main

Similarly, you can also combine the -a and --no-merged options.

git branch -a --no-merged main

Stashing across multiple branches

We have seen how to stash work in progress on a branch before checking out commits in the past or another branch. Naturally, we also want to be able to stash work in progress on several different branches without mixing them up.

  1. On the main branch, make a few changes without adding or committing.

  2. Stash your work in progress with the following command.

git stash push -a -m "main: work in progress"
  1. Do the same on the experiment/branch and experiment/old-commit branches, with the messages "experiment/branch: work in progress" and "experiment/old-commit: work in progress" respectively.

  2. Check the state of the stash stack with the following command.

git stash list

You can see a number for each stash along with its associated message.

  1. Let’s go back to the main branch and re-apply the changes from main with the following command.

git stash apply stash@{<the correct number>}
  1. Check the state of the stash stack again. Note that the stash from the main branch is still present.

  1. Delete the stash containing work from the main branch with the following command.

git stash drop stash@{<the correct number>}
  1. Check the state of the stash stack again and note that the stash numbers have changed.

  2. Repeat questions 5 to 7 for the other two branches to get familiar with the commands around git stash.

Deleting branches

We cannot delete a branch while we are on that branch, so first go back to the main branch.

  1. To delete a local branch that is already merged into the main branch (for example experiment/branch), use the following command.

git branch -d <branch name>
  1. To delete a local branch that is not yet merged into the main branch (for example experiment/old-commit), use the following command.

git branch -D <branch name>
  1. To delete the same branches on the Remote Repo, use the following command.

git push origin --delete <branch name>

For example:

git push origin --delete experiment/branch
  1. Go back to the objectives and check off the points you have mastered. Practice the commands and concepts you do not fully understand yet. Ask your instructor for help if needed.