Git and GitHub Version Control and Collaboration - (Made Easy) logo

- Stephan Hachinger and Mukund Biradar

AGENDA

  • What is Git?
  • What is GitLab?
  • What is Version Control?
  • GitHub for Collaboration
  • Data handling through GitLab
  • Installation Git on Windows /MacOs
  • Git Best Practices
  • Conclusions
Research context

INTRODUCTION

Welcome to a comprehensive guide on the fundamentals of Git and the collaboration platform GitLab. Whether you are a beginner exploring version control or an experienced developer seeking efficient collaboration tools, this guide will provide insights into the world of Git and how GitLab enhances the development workflow.

Diagram

All Projects are stored in GitLab As (Repository)

Research context

Q: What is Git?

Ans: Git is a popular version control system. It was created by Linus Torvalds in 2005, and has been maintained by Junio Hamano since then. It is used for:

  • 1. Tracking code changes
  • 2. Tracking who made changes
  • 3. Coding collaboration

Q: What does Git do?

Ans: Git manages projects with Repositories:

  • - Clone a project to work on a local copy
  • - Control and track changes with Staging and Committing
  • - Branch and Merge to allow for work on different parts and versions of a project
  • - Pull the latest version of the project to a local copy
  • - Push local updates to the main project

Q: Why Git?

Ans: Because...

  • - Over 70% of developers use Git!
  • - Developers can work together from anywhere in the world.
  • - Developers can see the full history of the project.
  • - Developers can revert to earlier versions of a project.
Features:
  • - GitLab hosts your (private) software projects for free.
  • - GitLab is a platform for managing Git repositories.
  • - GitLab offers free public and private repositories, issue-tracking, and wikis.
  • - GitLab is a user-friendly web interface layer on top of Git, which increases the speed of working with Git.
  • - GitLab provides its own Continuous Integration (CI) system for managing the projects and provides a user interface along with other features of GitLab.
Note : GitLab provides unlimited number of private and public repositories for free.


GitLab Overview


GitLab is a web-based Git hosting service that provides a user interface for managing your Git repositories. It offers features such as issue tracking, code review, and continuous integration/continuous delivery (CI/CD).

GitLab serves as a web-based platform for software development and collaboration, supporting version control, project management, CI/CD automation, code quality checks, collaboration, access control, and more. It plays a crucial role in helping teams work together efficiently and manage their software projects effectively.

GitLab is available in both open-source and enterprise editions, catering to a wide range of users and organizations.

Q: What are the most popular languages based on GitLab?

Ans: Although more than 500 programming languages were used in 2022, Java, JavaScript, and Python remain the leading ones. While TypeScript has seen a considerable rise in the last couple of years and remained in the fourth position, PHP is still on its sharp way down ending up at the seventh position in 2022.


Version Control Overview


Version control is a system that tracks changes to a file or set of files over time. It is often used in software development to track changes to source code, but it can also be used for other types of files, such as documents, configuration files, and images.

Version control systems provide several advantages, including the ability to see who made changes to a file and when, revert to a previous version if necessary, collaborate with others without overwriting each other's work, and keep track of the history of a file to see how it has evolved over time.

There are many different version control systems available, but some of the most popular ones include Git.

Benefits of Using a Version Control System


  • Improved productivity : Version control systems reduce the risk of errors and facilitate collaboration.
  • Increased quality : Version control systems make it easier to track down and fix bugs, improving software quality.
  • Reduced risk : Version control systems minimize the risk of data loss or accidental overwriting of changes.
  • Improved communication : Version control systems enhance communication by tracking project progress and team contributions.
  • Increased knowledge : Version control systems help team members learn from mistakes and understand project evolution.

Overall, version control is a valuable tool for anyone who works with files that change over time.

Steps to download and install Git on Windows /MacOs

Q: How to Download Git?

Ans: Follow these steps to download Git:

  1. Step 1: Go to the official website: https://git-scm.com
  2. Step 2: Click on 64-bit Git for Windows Setup and allow the download to complete.
  3. ISA Tab

    Extract and Launch Git Installer

  4. Step 3: Go to your download location and double-click the file to launch the installer.
  5. Step 4: Allow the app to modify your device by selecting Yes in the User Account Control window that appears.
  6. ISA Tab
  7. Step 5: Check the GNU General Public License and click Next.
  8. ISA Tab
  9. Step 6: Select the install location. If you don’t have a reason to modify it, leave it to default and click Next.
  10. ISA Tab
  11. Step 7: A screen for component selection will display. Leave the settings as it is and click Next.
  12. ISA Tab
  13. Step 8: The installer asks you to create a start menu folder. Simply click Next.
  14. ISA Tab
  15. Step 9: Choose the text editor you want to use with Git and click Next.
  16. ISA Tab
  17. Step 10: The following step allows you to give your original branch a new name. ‘Master’ is the default. Leave the default choice selected and press the Next button.
  18. ISA Tab
  19. Step 11: You can adjust the PATH environment during this installation phase. When you run a command from the command line, the PATH is the default set of folders that are included. Continue by selecting the middle (recommended) option and clicking Next.
  20. ISA Tab
  21. Step 12: The following option concerns server certificates. The default choice is used by the majority of users. Simply click Next.
  22. ISA Tab
  23. Step 13: 13: This step deals with how data is structured, and altering this option may create issues. So, it is advised to leave the default selection.
  24. ISA Tab
  25. Step 14: Select the terminal emulator that you wish to use. Because of its features, the default MinTTY is suggested. Click Next.
  26. ISA Tab
  27. Step 15: The installer now prompts you to specify what the git pull command should perform. Leave the default selected option and click Next.
  28. ISA Tab
  29. Step 16: The next step is to decide which credential helper to employ. Credential helpers are used by Git to retrieve or save credentials. Leave the default selection and click Next.
  30. ISA Tab
  31. Step 17: Although the default choices are suggested, this step allows you to select which additional features to activate.
  32. ISA Tab
  33. Step 18: Git offers to install some experimental features. Leave them unchecked and click Install.
  34. ISA Tab
  35. Step 19: Once the installation is complete, launch the Git bash.
  36. ISA Tab

Congratulations, Git is successfully installed!

To check the status or version, use the following command:

git --version

For macOS operating system, Git comes pre-installed by default.

To check the status or version, use the following command:

git --version

To install Git on macOS, you can follow these steps:

1. Check if Git is Already Installed:

Open the Terminal application, and type the following command:

git --version

If Git is already installed, you'll see the version information. If not, it will prompt you to install it.

2. Install Git Using Homebrew (Recommended):

If you don't have Git installed or want to update to the latest version, you can use Homebrew. Homebrew is a popular package manager for macOS.

If you don't have Homebrew installed, you can install it by following the instructions on the official Homebrew website.

Once Homebrew is installed, open the Terminal and run the following command to install Git:

brew install git
3. Install Git Using Xcode Command Line Tools:

Another option is to install Git through Xcode Command Line Tools. This method requires Xcode to be installed.

Open the Terminal and run the following command to install Xcode Command Line Tools:

xcode-select --install

Follow the on-screen instructions to complete the installation.

After installing Xcode Command Line Tools, you should have Git available in your Terminal.

4. Verify the Installation:

After the installation is complete, you can verify that Git is installed by running:

git --version

This should display the installed Git version.

That's it! Git should now be installed on your macOS system, and you can start using it for version control.

Mastering Git and GitLab: A Hands-On Exercise for Version Control and Collaboration

GitLab Setup Image
GitLab and git Agenda
  • 1. Setup GitLab Repository
  • 2. How to use terminal using git
  • 3. Initialize the git and clone repository
  • 4. Add some changes
  • 5. Commit the changes
  • 6. Push the changes to central Repo
  • 7. Check GitLab for changes

Setup GitLab Repository

Login to GitLab

Using your username and password.

GitLab Setup Image

1. Setting Up GitLab Repository

  • Create a new repository on GitLab.
  • GitLab Setup Image
    To start, create a sample project in GitLab.
    1. In GitLab, on the left sidebar, at the top, select Create new () and New project/repository.
    2. For Project name, enter My sample project. The project slug is generated for you. This slug is the URL you can use to access the project after it’s created.
    3. Ensure Initialize repository with a README is selected. How you complete the other fields is up to you.
    4. Select Create project.

    GitLab Setup Image

    Next

    GitLab Setup Image

    Congratulations!

    You've successfully created a new project in your GitLab account. Now, you can proceed to clone this project and begin your work.

2. How to use terminal using git

    GitLab Setup Image
    Git Registration Success!

    You are now registered with Git on your PC. You've set up your username and email using the terminal.

3. Initialise the git and clone repository

    3.1 copy the url
    GitLab Setup Image
3.2 Setting Up a Local Git Repository
GitLab Setup Image
Git project cloned Successfully....!

4. Git Add Changes

    4.1 Git Add single File

    Git add command is a straightforward command. It adds files to the staging area. We can add single or multiple files at once in the staging area. It will be run as:

    $ git add <File name>

    The above command is added to the git staging area, but yet it cannot be shared on the version control system. A commit operation is needed to share it. Let's understand the below scenario.

    We have created a file for our newly created repository in NewDirectory. To create a file, use the touch command as follows:

    $ touch newfile.txt
    $ git add newfile.txt
    GitLab Setup Image
4.2 Git add all file changes

We can add more than one files in Git, but we have to run the add command repeatedly. Git facilitates us with a unique option of the add command by which we can add all the available files at once.

$ git add .

The above command will add all the files available in the repository. Consider the below scenario:

GitLab Setup Image

After adding files

GitLab Setup Image

the above output, all the files have been added. The status of all files is displaying as staged.


Next Step : commit these above changes.

5. Commit the changes

    5.1 The git commit command

    The commit command will commit the changes and generate a commit-id. The commit command without any argument will open the default text editor and ask for the commit message. We can specify our commit message in this text editor. It will run as follows:

    $ git commit
    GitLab Setup Image
5.2 Git add all file changes

The -m option of commit command lets you to write the commit message on the command line. This command will not prompt the text editor. It will run as follows:

$ Git commit -m 'your commit message'
GitLab Setup Image

In the above output, a newfile4.txt is committed to our repository with a commit message.


Next Step : Push changes to repository.

6. Push the changes to central Repository

    Push your local changes to the GitLab repository:

    The push term refers to upload local repository content to a remote repository. Pushing is an act of transfer commits from your local repository to a remote repository.

    GitLab Setup Image
    $ git push origin main
    Or
    $ git push repository_url main
    Pushing Changes:
    GitLab Setup Image

    After making changes locally and committing them, you use git push to send those changes to the remote repository.


Next ---> Check Git Changes in Gitlab Repository

7. Check Git changes in Gitlab

    1. Navigate to your GitLab project page and refresh the page.
    2. Verify that the changes you made locally are now visible in the repository.

    Congratulations! You have successfully executed a GitLab exercise, encompassing project creation, local modifications, and the utilization of Git commands to push changes to GitLab. This fundamental workflow establishes the groundwork for collaborative software development through version control.

Git Cheat Sheet

    1. Git Configuration

    • git config: Get and set configuration variables that control Git's behavior.
    • Set the name: $ git config --global user.name "User name"
    • Set the email: $ git config --global user.email "himanshudubey481@gmail.com"
    • Set the default editor: $ git config --global core.editor Vim
    • Check the setting: $ git config -list

    2. Starting a Project

    • git init: Create a local repository.
    • git clone: Make a local copy of the server repository.

    3. Local Changes

    Git Add

    Add a file to staging (Index) area:

    $ git add Filename

    Add all files of a repo to staging (Index) area:

    $ git add *

    Git Commit

    Record or snapshots the file permanently in the version history with a message:

    $ git commit -m "Commit Message"

    4. Track Changes

    Git Diff

    Track the changes that have not been staged:

    $ git diff

    Track the changes that have staged but not committed:

    $ git diff --staged

    Track the changes after committing a file:

    $ git diff HEAD

    Track the changes between two commits:

    $ git diff Git Diff Branches: $ git diff <branch 2>

    Git Status

    Display the state of the working directory and the staging area:

    $ git status

    Git Show

    Shows objects:

    $ git show

    5. Commit History

    Git Log

    Display the most recent commits and the status of the head:

    $ git log

    Display the output as one commit per line:

    $ git log -oneline

    Displays the files that have been modified:

    $ git log -stat

    Display the modified files with location:

    $ git log -p

    Git Blame

    Display the modification on each line of a file:

    $ git blame <file name>

    Remote

    Git Remote

    Check the configuration of the remote server:

    $ git remote -v

    Add a Remote

    Add a remote for the repository:

    $ git remote add <remote-name> <repository-url>

    Fetch Data

    Fetch the data from the remote server:

    $ git fetch

    Remove Remote Connection

    Remove a remote connection from the repository:

    $ git remote rm <remote-name>

    Rename Remote Server

    Rename remote server:

    $ git remote rename <old-name> <new-name>

    Show Additional Information

    Show additional information about a particular remote:

    $ git remote show <remote-name>

    Change Remote URL

    Change remote URL:

    $ git remote set-url <remote-name> <new-url>

    Git Push/Pull

    Push data to the remote server:

    $ git push <remote-name> <branch-name>

    Pull data from the remote server:

    $ git pull <remote-name> <branch-name>

Conclusion

Congratulations! You've explored essential Git commands and concepts, covering local changes, commit history, and remote operations. Git is a powerful version control system that plays a crucial role in collaborative software development.

Remember to adapt these commands based on your project's needs and collaborate effectively with your team using Git and platforms like GitLab.

Keep coding, and may your version control journey be smooth and productive!