Workflow and Git
In this lab series you will use Git to both version control your code and for submissions. It is therefore good to have a solid understanding of how Git works and to follow the instructions carefully.
Learn more about Git here:
On this page we provide instructions for the use of Git and GitLab that you will need during the lab series. You are welcome to use the terminal to issue Git commands and/or the built-in interface in Visual Studio Code.
Overview of the workflow.
General tips for avoiding problems
Never use
git add .,git commit -aor similar. Instead add the files you want to manage withgit add fil.py fil2.pyand then make a commit.Write clear commit messages; it helps enormously when something goes wrong.
Use
git statusandgit logto see what is happening, for example before making a commit to check that everything is correct.Se till att
git statussäger “Working tree clean” innan ni byter branch eller körgit pull.Never use
-funless you really know what you are doing.
Starting a new lab
Each lab should be performed on a separate branch that you name labX, where X is replaced by the lab number. When you create a branch it is important to think about which branch you are branching from. When you start the first lab there is only one branch to branch from, main. For the subsequent labs there are two scenarios:
The previous lab is approved and you have completed the merge request.
The previous lab is not yet approved and you want to start the next lab anyway.
In case a) you simply branch from main since the latest code is already there.
This is done in the terminal using
$ git checkout main
$ git pull
$ git checkout -b labX
When it comes to case b) it can get a bit trickier. Since you may need the code from the previous lab it may be necessary to branch from the labY branch, where Y is the number of the previous lab.
$ git checkout labY
$ git pull
$ git checkout -b labX
You can also use the interface in VS Code to check out branches by clicking on the field in the bottom left corner:
branch-vscode
In the image above main is the active branch.
Working with a lab
När ni arbetar med en labb rekommenderas det att kontinuerligt
versionshantera er kod, för att ni lättare ska kunna “gå tillbaka” ifall
något blivit fel eller försvunnit. Allt arbete med en labb ska ske på en
särskild branch, exempelvis labX. Säg att ni gjort några ändringar
som ni känner er nöjda med, då är det dags att göra en commit. Börja
gärna med att köra
$ git status
git-status
You may then see that server/main.py and server/readme.md are modified, but also that Git has found a file called database.sqlite. We do not want to version control database.sqlite and it is therefore important that it does not end up in any commit. We ignore it for now and make our commit:
$ git add server/main.py server/readme.md
$ git commit -m "Add User model"
$ git push
Här kan det uppstå ett litet felmeddelande då git push kommer att
försöka göra en push mot main grenen. Ni kommer därför behöva sätta
er nya branch (lab x) till “default”, detta gör ni genom att använda
kommandot som dyker upp i felmeddelandet i terminalen (ni kommer att se
ett kommando som börjar med ‘git’ som ni kan använda) läs
teminalmeddelandet noga.
To get rid of database.sqlite when running ``git status`` you can add the filename to your .gitignore file (see lab 0).
The commands above are of course also represented in the VSCode interface.
git-vscode
The file list represents the output from git status, a click on the small plus sign next to a file corresponds to git add and the checkmark at the top corresponds to git commit where the message is taken from the text field. Clicking the ... button you can also find options for pushing, etc.
Working in parallel with a previous lab
Valde ni att börja med labben trots att föregående labb inte var godkänd? Då kan ni behöva göra en “merge” för att använda er förbättrade gamla kod i den labben ni arbetar med nu.
git-merge
Denna procedur representerar alltså pilen markerad med “merge”, längst till höger (vi tar labb 2 och labb 3 som exempel):
Make sure your merge request for lab 2 is approved and completed (the code is on the main branch).
Se till att ni inte har några icke-commitade filer på branchen lab3 (
git statusvisar “On branch lab3, nothing to commit, working tree clean”)Switch branch to main:
$ git checkout mainMake sure you have the latest changes:
$ git pullSwitch back to the branch for lab 3:
$ git checkout lab3Do your merge:
$ git merge main.
This means that the changes on main are also applied to lab3.
Most of the time a merge can happen completely automatically, but sometimes so-called conflicts arise. It may be that changes affecting the same line(s) have occurred in both lab3 and on main and then you as a programmer must decide which of the changes you actually want.
A conflict after git merge lab2 can look like this in the terminal:
git-conflict
If you open the file containing a conflict in VSCode this may be shown:
git-conflict-vscode
I detta gränssnitt kan ni enkelt välja vilken av ändringarna ni vill ha
kvar genom att klicka på exempelvis “Accept Current Change” för att få
med ändringen som gjordes på branchen lab3 men inte den som gjordes
på lab2.
If you open the file in another text editor (without a conflict resolution interface) you will see that a number of lines have simply been added to mark the conflict:
git-conflict-file
To resolve the conflict you can do the following:
Välja ett alternativ i VSCode (“Accept …”) alternativt ta bort den rad ni inte vill ha i valfri texteditor samt ta bort alla rader med <<<<<<< och >>>>>>>. Gör detta för samtliga filer som innehåller konfliker.
$ git add fil(er)-som-hade-konflikt$ git commit -m "Merge lab2 into lab3"$ git push
You should now have the approved code from lab 2 in your lab3 branch, and you can just keep working!
Demonstration
Se till att git status matar ut “working tree clean”, d.v.s. att ni
har gjort alla commits ni ska och se till att ni har all relevant kod
framme och applikationer redo. Får ni sedan godkänt på den muntliga
redovisningen kan ni gå vidare till att lämna in koden.
Don’t forget to push so that the code you demonstrated is also on GitLab.
Submission
Efter godkänd muntlig redovisning är det dags att lämna in koden så att er labbassistent kan göra en bedömning. Vi kommer använda GitLabs “merge requests” till detta för att kunna ge bra feedback samt ge er övning att använda merge requests inför ert utvecklingsarbete i kommande projekt.
När ni pushat er branch, gå till gitlab.liu.se och navigera till ert projekt. Klicka på “Merge Requests” i sidofältet.
GitLab is usually good at detecting that you have recently pushed to a branch, and therefore suggests creating a merge request for that branch.
mr-create
Klicka på “Create merge request”. Ange titlen “Lab X” (byt ut X mot numret på den labben ni lämnar in), eventuell beskrivning samt ange er labbassistent som “Reviewer”. Klicka sedan på “Submit merge request” och invänta bedömning från er labbassistent.
If you want to update the code in a merge request, simply make a new commit on that branch and push to GitLab.
When your lab assistant has assessed your submission, either comments have been added (revision required), as in this case:
mr-comment
Har ni fått kommentarer behöver ni utföra ändringar, göra commits och pusha till samma branch igen. Meddela därefter er labbassistent via mail att ni genomfört de begärda ändringarna så att hen kan kolla igenom koden på nytt. Ange TDDD83 - Lab X Komplettering som ämne samt bifoga en länk till er merge request. Klicka ej på “Resolve thread”, det är labbassistentens uppdrag.
Alternatively you have (directly or after some changes) received approval, as in this case:
mr-approved
Har ni fått godkänt kan ni klicka på “Merge” för att få er kod till er
main branch.