🕛 Git SetupPublic
A guide to setting up Git and GitLab for COMP2511.
1. Git Setup
Info
You will need to have installed Git on your local machine if it doesn't already have it.
1.1. Generating an SSH key and adding it to GitLab
Warning
- Run
ssh-keygen -t ed25519
in your terminal and hit enter until it stops prompting. Note the path where it is creating the key. - Navigate to the path where the key has been created. This will typically be in
~/.ssh
. - Print the public key to the terminal so it can be copied by running
cat ~/.ssh/id_ed25519.pub
The key should look like the following:
Note
Do not share your private key with anyone
- Add the copied key to https://nw-syd-gitlab.cseunsw.tech/-/user_settings/ssh_keys.
1.2. Setting your Git Identity
Whenever you make a commit, your current identity is associated with it.
To properly set up your identity so your commits are tagged with your name and email, run the following commands (replacing the place holders with your information)
2. Using Git and GitLab
If you've taken COMP1531 recently and/or are relatively fluent in git, then feel free to move on and skip this.
2.1. Cloning
Cloning a repository (a repository or repo is just a directory that is linked with git) copies to your computer all the files in the repo as well as a complete history of what changes, or commits, created those files. Cloning a repo is necessary before you can start making your own changes.
For each lab and assignment in this course, a repo will be created for you on GitLab. You will use it to store your work as you complete it.
You can find the link to your repo for
2.2. Making a Commit
Now that you have cloned the repo, you are ready to work on the codebase locally.
A commit represents a set of changes to the files in a repository as well as a message describing those changes for human readers. A good use of git involves a lot of commits with detailed messages.
Before you can commit, you have to stage your changes, effectively telling git what changes you actually want to commit and what changes you don't.
Making commits doesn't actually replicate your changes to the remote repository on GitLab. For that you need to push your commits, uploading them to the remote server. When collaborating with others, it is important not only to commit frequently, but also to push often.
In general, the commands to commit and push are as follows:
Follow these steps to see them in action:
- Add a new file called
HelloWorld.java
in the repo directory - Add the following lines of code to the file using your favourite text editor and save.
- Go back to your terminal and enter the following commands:
- MAKE SURE YOU UNDERSTAND THE PURPOSE OF EACH OF THE 3 ABOVE COMMANDS! If you are unsure about any of them, ask your tutor or lab assistant.
- Go back to GitLab and confirm that your changes have been pushed to the server.
2.3. Working With Others
Usually when you are using git, it is in a team. That means that you will not be the only one who is making the changes. If someone else makes a change and pushes it to the server, your local repo will not have the most up-to-date version of the files. Luckily, git makes it easy to update our local copy with the git pull
command.
This command checks the remote server that your local repo is linked to and makes sure that all of your files are up to date. This ensures that you don't accidentally do things like implement the same thing someone else has already done and also lets you use other people's work (e.g. new functions) when developing.
Pulling regularly is one of the most important practices in git!
Unfortunately, at the moment you are just working individually. But GitLab still gives us a nice way to practice a git pull
.
2.4. Summary
- View your repo on GitLab.
- Click on the
HelloWorld.java
file - Click 'Edit' on the right-hand side.
- Add a Java comment to the top of the file as shown below and click the 'Commit Changes' button at the bottom of the screen
- This will have changed the
HelloWorld.java
file on the server but not on your local environment. To fetch these changes use the git pull command from your terminal - Confirm that your version of
HelloWorld.java
now has the changes you made on the web page
2.5. Branching
Branches are a vital part of git and are used so people can work on separate parts of the codebase and not interfere with one another or risk breaking a product that is visible to the client. Breaking something on one branch does not have an impact on any other.
Good use of git will involve separating parts of the project that can be worked on separately and having them in their own feature branch. These branches can then be merged when they are ready.
Useful commands for branches:
Follow these instructions to create a branch:
- Make your new branch with:
git checkout -b first_new_branch
- List your branches to see that you have indeed swapped (use the above commands)
- Open the
HelloWorld.java
file and change the comment at the top of the file to Javadoc style comment as shown below:
- Try to push your changes to the server using the commands you learnt in the Making a commit section
- The above step should have given you the following error:
This means that the branch you tried to make a change on doesn’t exist on the server yet which makes sense because we only created it on our local machine.
- To fix this, we need to add a copy of our branch on the server and link them up so git knows that this new branch maps to a corresponding branch on the server
Note: The final step only needs to be done for the first time you try to push using a new branch. After you have run this once, you should go back to simply using git push
2.6. Merging
Merging branches is used to combine the work done on two different branches and is where git's magic really comes in. Git will compare the changes done on both branches and decide (based on what changes were done to what sections of the file and when) what to keep. Merges are most often done when a feature branch is complete and ready to be integrated with the master branch.
Since we have finished all that we are going to do (and think there are no bugs) on our first_new_branch we can merge it back into master.
NOTE: It is strongly recommended, both in this course and in general, to always ensure the code on the master
branch compiles and is free of bugs. The latter is naturally harder to achieve than the former, but you should endeavour to keep master as stable as possible.
Another recommendation is to merge master into your branch before merging your branch into master as this will ensure that any merge into master will go smoothly.
In general, merges are done by:
Note: A successful merge automatically uses the commits from the source branch. This means that the commits have already been made, you just need to push these to the server (git push
)
To merge your changes from above:
- Switch back to the master branch using one of the commands from the above section
- Merge in the changes you made in the other branch
git merge first_new_branch
- Push the successful merge to the server to update the master branch on the server
2.7. Merge Conflicts
Merge conflicts are the one necessary downside to git. Luckily, they can be avoided most of the time through good use of techniques like branches and regular commits, pushes and pulls. They happen when git cannot work out which particular change to a file you really want.
For this step we will engineer one so you can get a taste of what they are, how they occur and how to fix them. This will be the LAST time you will want one. The process may seem involved but it is quite common when multiple people are working at a time.
Follow these steps:
- Change line 3 of
HelloWorld.java
to
- Add, commit and push your changes
- Switch to your
first_new_branch
- Change line 3 of
HelloWorld.java
- Add, commit and push your changes
- Merge master into your current branch
- This sequence of steps should make a merge conflict at the third line of
HelloWorld.java
2.8. Resolving a Merge Conflict
Resolving a merge conflict is as simple as editing the file normally, choosing what you want to have in the places git wasn't sure.
A merge conflict is physically shown in the file in which it occurs. <<<<<<<
marks the beginning of the conflicting changes made on the current (merged into) branch. =======
marks the beginning of the conflicting changes made on the target (merged) branch. >>>>>>>
marks the end of the conflict zone.
e.g.,
This above example could be solved in many ways, one way would be to just use the changes made on the target branch and delete those made on the current branch. Once we have decided on this we just need to remove the syntax. The resolved file would be as follows
We would then just commit the resolved file and the merge conflict is finished!
To fix the conflict you created:
- Open the
HelloWorld.java
file and decide which change you want to keep - Remove the merge conflict syntax
- Add, commit and push the resolved merge conflict
Last updated on