A Simple Introduction to Git and GitLab in Software Engineering

Slide Note
Embed
Share

Explore the basics of version control, Git, and GitLab in software engineering. Discover centralized vs. distributed version control systems, the installation process of Git, and the concept of GitLab. Learn how these tools help manage changes, track modifications, and facilitate collaboration in software development.


Uploaded on Sep 11, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. A simple Introduction to Git and GitLab CS330E Elements of Software Engineering I Dr. Fares Fraij

  2. On-line resources Git website: http://git-scm.com/ Git Book: http://git-scm.com/book/ GitLab: https://gitlab.com/ GitLab basics: https://docs.gitlab.com/ee/gitlab-basics/

  3. Outline What is version control? What is Git? How to install Git? How to configure Git? What is GitLab? How does Git work? How to create an account on GitLab? In class practice

  4. What is version control? Software tools that help a software team manage changes to source code over time keeps track of every modification to the code in a special kind of database Keeps track of who made what changes and when Allows for the ability to revert the code to a previous revision

  5. Centralized VS Distributed version control systems Centralized version control systems There is a single central copy of source code on a server, and programmers will commit their changes to this central copy. Committing a change simply means recording the change in the central system. Other programmers can then see this change. They can also pull down the change, and the version control tool will automatically update the contents of any files that were changed. Examples: CVS, Subversion (or SVN) and Perforce.

  6. Centralized VS Distributed version control systems Distributed version control systems Do not rely on a central server to store all the versions of a project s files. Instead, every developer clones a copy of a repository and has the full history of the project on their own hard drive. This copy (or clone ) has all of the metadata of the original. The act of getting new changes from a repository is usually called pulling, and the act of moving your own changes to a repository is called pushing . In both cases, you move change sets (changes to files groups as coherent wholes), not single-file diffs. Examples: Git, Mercurial and Bazaar.

  7. What is Git? Git is a free and open source distributed version control system Originally created by Linus Torvalds in 2005.

  8. How to install Git? Mac: http://git-scm.com/download/mac Windows: http://git-scm.com/download/win

  9. How to Configure Git? In your shell, type in the following commands to configure git: $ git config --global user.name "John Galt" $ git config --global user.email jgalt@example.com $ git config --global push.default simple $ git config --global color.ui true If you're using OS X or Linux: $ git config --global core.autocrlf input If you're using Windows: $ git config --global core.autocrlf true

  10. What is GitLab? GitLab is built on top of Git. You can think of GitLab as online storage of Git repositories. It s an integrated set of tools for the full software development lifecycle. GitLab includes Git repository management that acts as your single source of truth in a distributed version control workflow. It also includes code review and dip tools with tons of collaboration and flow management features.

  11. What is GitLab? It also has issue tracker with issue boards and a project wiki. For management, GitLab offers permissions managements so that you can easily assigned user roles and determine access levels for all users. GitLab has other great features and workflow best practices such as built-in continuous integration and deployment that tightly integrates both testing and deployment into your workflow. It also has built-in container registry that allows you to store container images.

  12. How to create an account on GitLab? Visit: https://about.gitlab.com/ Signup for a free account: Hit Sign in > Register .

  13. How does Git work? Here is a basic overview of how Git works: Create a "repository" (project) with a git hosting tool (like GitLab) Copy (or clone) the repository to your local machine Add a file to your local repo and "commit" (save) the changes "Push" your changes to your main branch Make a change to your file with a git hosting tool and commit "Pull" the changes to your local machine Create a "branch" (version), make a change, commit the change Open a merge (or pull) request" (propose changes to the main branch) "Merge" your branch to the main branch

  14. In class Practice Sign in to your GitLab account and create a project (or repository) Clone your repository to your local machine Add a file to your local repository and push it on GitLab Pull changes from your Git repository on GitLab Use a Git branch to merge a file

  15. In class Practice This opens the New project page. Sign in to your GitLab account and create a project (or repository) In your dashboard, click the green New project button or use the plus icon in the upper right corner of the navigation bar.

  16. In class Practice Clone your repository to your local machine On your local machine, create a folder to contain your git repos. Then, make sure to switch to the folder using cd command. $ mkdir git $ cd git $ On GitLab, select to the repo you want to clone. In your dashboard, click the Clone blue button. Then, select HTTPS from the dropdown menu and copy the link using the Copy URL to clipboard button

  17. In class Practice On your local machine, use clone command to get a copy of the remote repo. $ git clone https://gitlab.com/fareszf/cs330e.git Cloning into 'cs330e'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. Go into the cloned repo to work in it. $ cd cs330e $

  18. In class Practice Add a file to your local repository and put it on GitLab On your local machine, make sure that you re in the top level of your local repo. Type in the following line to create a file with contents. $ echo "Hello World!" > hello.txt

  19. In class Practice Check the status of your local repo using git status command $ git status On branch main Your branch is up to date with 'origin/main'. Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track)

  20. In class Practice Adding the file will move changes from the working directory to the Git staging area. The staging area is where you prepare a snapshot of a set of changes before committing them to the official history. $ git add hello.txt

  21. In class Practice Check the status again! $ git status On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: hello.txt

  22. In class Practice Committing the changes. $ git commit -m first commit" [main b6164f2] first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 hello.txt Note: The git commit takes the staged snapshot and commits it to the project history (local repo).

  23. In class Practice Check the status again. $ git status On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: hello.txt Note: Now you can see the new file has been added (staged) and you can commit it when you are ready.

  24. In class Practice Sending the committed changes to GitLab. $ git push origin main Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://gitlab.com/fareszf/cs330e.git 51c6b1b..b6164f2 main -> main

  25. In class Practice Go to the cs330e repo on GitLab. In your dashboard, click the commits hyperlink. You will see a list of two commits. If you click on first commit, you will see the file hello.txt.

  26. Use a Git branch to merge a file Branches is a handy tool when working with a team. It allow you to work on your part of code and only share it when you are ready. The default branch name in Git is main. As you initially make commits, you're given a main branch that points to the last commit you made. Every time you commit, it moves forward automatically. You create a branch, work on your part of the code, and merge your work into the main branch when its done.

  27. Use a Git branch to merge a file On your local machine, make sure that you re in the top level of your local repo. Create a branch called dev $ git branch dev Switch to the new branch $ git checkout dev Switched to branch 'dev'

  28. Use a Git branch to merge a file On your local machine, make sure that you re in dev branch. $ git status On branch dev nothing to commit, working tree clean Create a file with contents. $ echo "Welcome!" > welcome.txt Stage and commit $ git add welcome.txt $ git commit -m "third commit"

  29. Use a Git branch to merge a file At this point, you are done and want to merge your changes back into the main branch. First, you need to switch back to main. $ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. Merge dev back to main $ git merge dev Updating 12c2748..3fd5685 Fast-forward welcome.txt | Bin 0 -> 22 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 welcome.txt

  30. Use a Git branch to merge a file To push the new changes to remote repo on GitLab $ git push origin main If you are not going to use the branch again, you can delete it. $ git branch -d dev

  31. Resolving Merge Conflicts In this exercise, you need to work with a partner. While on On GitLab Create a private project (or repository) call it conflict Create an empty file call it, project1.py Add your partner as a maintainer to the repository conflict Clone your repository to your local machine Open the file project1.py and add a function to it Def fun1(): pass Push your changes to conflict (add, commit, and push) At this point, assume that your partner does not know about your changes. While you were adding a function to project1.py , your partner was also adding another function. Def fun2(): pass Your partner will have no problems adding and committing their changes since it happens locally. But what happens when your partner tries to push?

  32. Resolving Merge Conflicts You may receive an error message similar to the following To https://gitlab.com/fareszf/conflict.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://gitlab.com/fareszf/conflict.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. The message says that someone else, in this case your partner, has pushed before and you have to pull those changes.

  33. Resolving Merge Conflicts Your partner needs to pull changes After your partner pulls, they may receive the following error remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From https://gitlab.com/fareszf/conflict 9df5f1e..ddf5046 main -> origin/main Auto-merging project.txt CONFLICT (content): Merge conflict in project.txt Automatic merge failed; fix conflicts and then commit the result.

  34. Resolving Merge Conflicts The message suggests that there is a conflict in project1.py . So, let s open the files and see. The file may have contents similar to the following. Note that git has added some tags, <<<<<, ====, and >>>>> to mark the regions of the file that have the conflict. <<<<<<< HEAD def fun2(): pass ======= def fun1(): pass >>>>>>> ddf5046c3c9044f4fb65e35607e5e4419baba86a

  35. Resolving Merge Conflicts Since you and your partner modified the same file, git was unsure if there should be one function, e.g., fun1, one function, e.g., fun2, or two separate functions. In this case, git might not know what to do, but you and your partner has plans to construct these two functions So, you need to keep the two functions. What you need to do is to delete the tags <<<<<, ====, and >>>>> keeping changes of you and your partner Now, your partner need to add, commit, and push You also need to pull at this point to get your partner s updates

  36. What is merge request? Owner can share their code with others. Other people can make code changes and send a request to the owner to pull/merge their code changes into the owner s repository. This request is called a merge/pull request. Merge request allows you to start a conversation around code changes between the owner and other code contributors.

  37. Merge Request Task #1: Merge request from dev to main Locally clone a repo, create a dev branch, modify, push to remote dev On GitLab, request merge to main, accept Locally, checkout main, git pull Task #2: Merge request from fork to original repo (from owner repo to contributor repo) On your GitLab account, fork a repo, clone locally, modify, push, merge request to the original repo, accept.

  38. Taks #1: Merge request from dev to main You need to work with a partner to perform this task. (You VS Your Partner) You While on GitLab, create a public project (or repository) call it task1 . Add your partner as a Maintainer to this repo.

  39. Merge request from dev to main Your partner On your local machine, clone the remote repo merge . Create a dev branch and switch into it. $ git branch dev $ git checkout dev Create a new file with contents. $ echo "Merge exercise." > merge.txt Push to dev branch on remote. $ git push origin dev On GitLab, got to the dev branch and hit create merge request appears on the top right corner. Hit submit merge request

  40. Merge request from dev to main You On your GitLab account, slelect Merge requests item from the menu to the left. Select the only merge request you have. Hit the green button Merge . Your Partner: Merger dev with main and git pull to sync with remote. git checkout main git merge dev git pull

  41. Task #2: Merge request from fork to original repo Merge request from fork to original repo (from owner repo to contributor repo) You need to work with a partner to perform this task. (You VS Your Partner) You While on GitLab, create a public project (or repository) call it task2 .

  42. Task #2: Merge request from fork to original repo Your partner On your GitLab account, select Explore projects and All . Search for the project task2 Select the project task2 and hit fork at the top right corner of the screen. If forking is successful, you will see the project task2 in your namespace.

  43. Task #2: Merge request from fork to original repo Your partner On your local machine, clone the remote repo task2 . Create a dev branch and switch into it. $ git branch dev $ git checkout dev Create a new file with contents. $ echo task#2: Merge exercise." > merge.txt Push to dev branch on remote. $ git commit m new commit $ git push origin dev

  44. Task #2: Merge request from fork to original repo Your Partner git checkout main git merge dev Git push origin main

  45. Task #2: Merge request from fork to original repo Your partner On your GitLab account, slelect Merge requests item from the menu to the left. Select New merge request . Select the source branch to be main . Hit compare branches and continue . Hit Submit merge request Ask your partner to check their account and accept the merge request.

  46. Task #2: Merge request from fork to original repo You On your GitLab account, slelect Merge requests item from the menu to the left. Select the only merge request that you have. Hit merge

  47. References https://www.atlassian.com https://gitlab.com/ https://git-scm.com/

Related


More Related Content