Git - Source Control Management

De Marijan Stajic | Wiki
(Redirigé depuis How to use Git)
Aller à la navigation Aller à la recherche

Concept

Git banner.png

Imagine you have a company with several developers, all of whom are working on their own environments but on the same codebase. To avoid problems like when two people are working on the same file, we can use Git.

Git is a Source Control Management (SCM) helps all developers to work on the same application simultaneously and collaborate efficiently. You can configure project organisations and define different access levels for each group and user.

To summarise, Git is the command-line tool used to facilitate collaboration among multiple developers, and GitHub is the Git-based publicly accessible repository where you push and find your code. With GitHub, you can share your project with others along with documentation, and people can contribute to your project as well.


Share ressource git 2.png

How to use it (Linux)

In this section, it is explained how to use Git on a Linux system and GitHub as a hosted service.

Prerequisites

Here are the prerequisites you should complete if you want to transfer files from your machine to your hosted service :

1. Generate a personal access token on your GitHub account :

  • Settings / Developer Settings / Personal access tokens (classic) / Generate new token (classic)

2. Once the token is generate, Create a repository :

  • Your repository / New / Public or Private / ...

3. Then, install the Git package on your machine :

marijan# apt-get install git -y

4. Then, you have to fill in the following information to be able to commit :

marijan/workplace/test_public# git config --global user.name "Marijan Stajic"
marijan/workplace/test_public# git config --global user.email "marijan@stajic.me"

Create and upload files

If you have created new files and would like to upload them to your repository, follow the instructions below :

1. First, create a folder and initialise it as a Git repository :

marijan# mkdir <folder>
marijan# cd <folder>
marijan/<folder># git init

2. Add the remote repository. Specify your token so you don't need to repeat your username and password when pushing :

git remote add <name> https://<token>@github.com/<username-of-repo>/<repo>

3. Next, edit or create a file in this folder :

marijan/<folder># vim <file>

4. Check if it is ready to be pushed :

marijan/workplace/test_public# git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   <file>

no changes added to commit (use "git add" and/or "git commit -a")

5. To prepare it, add this file to the staging area :

marijan/workplace/test_public# git add <file>

6. Add your commit. The commit message explains what you changed in the file :

marijan/workplace/test_public# git commit -m "First edit"

6. Specify which branch you would like to push your file to. By default, it will be the master branch. You can change it by running the following command :

marijan$ git checkout -b "<branch>"

7. Finally, push your file to the repository, specifying the name of the branch you are on :

marijan$ git push <name> <branch>

Download, edit and upload files

If you have a repository which is already created and you would like to download it and edit files, follow the instructions below :

1. First of all, clone the repository on your machine:

marijan$ git clone https://github.com/<username-of-repo>/<repo>

2. Then, move inside the folder and switch to the desired branch :

marijan$ cd <repository>
marijan/<repository>$ git checkout -b "<branch>"

3. Now, if you list the files in this folder, you will find the files which are in your repository :

marijan/<repository>$ ls -l
total 24
-rw-r--r-- 1 root root 364 Jun 25 10:34 <file1>
-rw-r--r-- 1 root root  55 Jun 25 10:34 <file2>
-rw-r--r-- 1 root root 188 Jun 25 10:34 <file3>

4. Next, edit or create a file in this folder :

marijan/<repository># vim <file1>

5. Check if it is ready to be pushed :

marijan/workplace/test_public# git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   <file1>

no changes added to commit (use "git add" and/or "git commit -a")

6. To prepare it, add this file to the staging area :

marijan/workplace/test_public# git add <file>

7. Add your commit. The commit message explains what you changed in the file :

marijan/workplace/test_public# git commit -m "Edit"

8. Add the remote repository. Specify your token so you don't need to repeat your username and password when pushing:

git remote add <name> https://<token>@github.com/<username-of-repo>/<repo>

9. Finally, push your file to the repository, specifying the name of the branch you are on:

marijan$ git push <name> <branch>

Remote Repositories

In the example above, we have used a hosted services for our repository. But if you would like to store you document only for your, you can host the repository by installing your own repository server.

Indeed, GitHub, GitLab, etc. are only hostes services for public where you can store privately your work or share with others. But if it is for your own company, you should perhaps setting up your own repository server.

Then, when you are working with multiple collaborators, and you are editing and pushing the same file as a other colleagues, your Source Control Management (Git in this case) will handle this.

It will adapt the file with the both modification but if both users were editing the same line at the same time, it will ask which one it should keep it.