« Git - Source Control Management » : différence entre les versions
m (→How to use it) |
|||
Ligne 12 : | Ligne 12 : | ||
[[Fichier:Share ressource git 2.png|none]] | [[Fichier:Share ressource git 2.png|none]] | ||
= How to use it = | = 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) | * 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''' : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan# apt-get install git -y | marijan# apt-get install git -y | ||
</pre> | </pre> | ||
4. Then, you have to '''fill in the following information''' to be able to commit : | |||
<pre class="linux"> | |||
marijan/workplace/test_public# git config --global user.name "Marijan Stajic" | |||
marijan/workplace/test_public# git config --global user.email "marijan@stajic.me" | |||
</pre> | |||
== Create and upload files == | |||
If you have created new files and you would like to upload them on your repository, follow the instructions bellow : | |||
1. First of all, you need to create a folder and initialise it as a Git repository : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan# mkdir workplace | marijan# mkdir workplace | ||
Ligne 30 : | Ligne 49 : | ||
marijan/workplace# git init | marijan/workplace# git init | ||
</pre> | </pre> | ||
2. Next, you can '''edit or create a file''' in this folder and | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan/workplace | marijan/workplace/test_public# vim hello.txt | ||
</pre> | </pre> | ||
3. Now, '''check''' if it is '''ready''' to be pushed : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan/workplace/test_publicc# git status | marijan/workplace/test_publicc# git status | ||
On branch main | On branch main | ||
Ligne 48 : | Ligne 68 : | ||
no changes added to commit (use "git add" and/or "git commit -a") | no changes added to commit (use "git add" and/or "git commit -a") | ||
</pre> | </pre> | ||
3. To prepare it, you have '''to add''' this file to the '''staging area''' : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan/workplace/test_public# git add hello.txt | marijan/workplace/test_public# git add hello.txt | ||
</pre> | </pre> | ||
4. Then, you can '''add your commit'''. The commit is just to explain what you changed in the file : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan/workplace/test_public# git | marijan/workplace/test_public# git commit -m "First edit" | ||
</pre> | </pre> | ||
5. Now, add the | |||
== Download, edit and upload files == | |||
Here are some common '''command-line operations''' for using Git on a '''Linux''' shell : | |||
3. Now, you can '''copy a repository''' from a source like '''GitHub''' : | |||
<pre class="linux"> | <pre class="linux"> | ||
marijan/workplace/test_public | marijan/workplace# git clone https://github.com/MarijanStajic/test_public.git | ||
</pre> | </pre> | ||
8. You have '''to connect''' to the repository using the '''token generated''' at the beginning, the '''name of your GitHub account''' and the '''name of the repository''' : | 8. You have '''to connect''' to the repository using the '''token generated''' at the beginning, the '''name of your GitHub account''' and the '''name of the repository''' : | ||
<pre class="linux"> | <pre class="linux"> | ||
Ligne 70 : | Ligne 99 : | ||
</pre> | </pre> | ||
= 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'''. | 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'''. |
Version du 25 juin 2024 à 10:05
Concept
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.
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 you would like to upload them on your repository, follow the instructions bellow :
1. First of all, you need to create a folder and initialise it as a Git repository :
marijan# mkdir workplace marijan# cd workplace marijan/workplace# git init
2. Next, you can edit or create a file in this folder and
marijan/workplace/test_public# vim hello.txt
3. Now, check if it is ready to be pushed :
marijan/workplace/test_publicc# 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
3. To prepare it, you have to add this file to the staging area :
marijan/workplace/test_public# git add hello.txt
4. Then, you can add your commit. The commit is just to explain what you changed in the file :
marijan/workplace/test_public# git commit -m "First edit"
5. Now, add the
Download, edit and upload files
Here are some common command-line operations for using Git on a Linux shell :
3. Now, you can copy a repository from a source like GitHub :
marijan/workplace# git clone https://github.com/MarijanStajic/test_public.git
8. You have to connect to the repository using the token generated at the beginning, the name of your GitHub account and the name of the repository :
marijan/workplace/test_public# git remote set-url origin https://token@github.com/MarijanStajic/test_public.git
9. Finally, you can push your file to your Git repository in the main branch :
git push origin main
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.