CPEN 221 / Fall 2020 / Lab 4A
Git Branching and Merging
One important aspect of using a source code control system is to manage simultaneous updates to the code base and to merge the changes. To familiarize yourselves with this aspect of git, read A Hacker’s Guide to Git.
Resolving Conflicts
To understand how one can resolve conflicts, follow these steps. This task is set up so that one person can complete all the steps (including creating the conflict) although conflicts are more common when multiple people are contributing to a project. This is not the only way to resolve a conflict but it is intended to be a start.
- Clone your assigned Github repository:
git clone <https://github.com/CPEN-221-2020/><your-repo> lab4awhich will clone the repo in a local directorylab4a. - Change to the
lab4adirectory and create a new branchb1and switch to it:git branch b1followed bygit checkout b1 - Add an empty file named
README.txtin thelab4adirectory and push to Github:
git add README.txt
git commit -m "Added README"
git push origin b1
- Now, from the parent directory for
lab4a, clone branchb1of your repository using the command:git clone -b b1 <https://github.com/CPEN-221-2020/><your-repo> lab4a1which will clone the repo in a local directorylab4a1. (This would allow the two cloned repositories to be on the same machine, if necessary.) - In the
lab4a1directory, add the following line toREADME.txt:Hello from Lab4a1. Then commit and push the change to Github. - In the
lab4adirectory, add the following line toREADME.txt:Hello from Lab4a.Commit this change and execute apull:git pull - Git will indicate that there is a merge conflict.
- Edit
README.txtinlab4ato have the two lines in the correct order (Lab4abeforeLab4a1). You will remove the conflict indicators placed by Git. - Add, commit and push
README.txtfromlab4a1.
Merging Branches
-
In
lab4a, switch to themasterbranch: -
You should not see
README.txtin your directory for now. -
Now add any new file to this directory, then add, and commit this file to the repository.
-
Now, merge the
masterbranch with theb1branch:
- At this point you should see
README.txtand the new file that you added. The two branches have been merged and you can keep working on themasterbranch.