Return to a previous version of your code in case of problems
Track the evolution of your code step by step
Work collaboratively without risking deletion of other contributors’ changes
Variants
There are several alternatives to Github, the most well-known being Gitlab which is a fork of Github that adds improvements and offers the possibility of self-hosting. The operation remains similar.
Git works similarly to incremental backup, where a set of folders/files will be added to the project.
Subsequently, a complete history of project changes will be available - these changes can be ignored, added, deleted or merged.
Git offers a branch system, which allows for example, to develop content on the main branch without altering it. This is because Git will have previously made a kind of secondary clone of the branch
(using the command git checkout -b mybranch).
The Working directory corresponds to the project folder on the machine.
A “commit” is a version of the previously indexed folders/files, which is different from the subsequent version.
The git commit command allows you to validate the changes made to the indexed or “staged” files by Git.
NOTE
These changes are applied from the “working directory” and are no longer in “Stage”, they are moved to the “repository”.
TIP
It is recommended to name commits with a different name for each modification step. git commit -m step1 allows you to name (indicate concisely the content of the commit, max. 60 characters). git commit --amend -m "step2" allows you to change the name of an existing commit!
A branch is a copy of the project at a given time T, all modifications made to it will only affect that one.
This allows you to have multiple states of the project at the same time.
The main branch is the main branch (previously called master before October 2020). This branch is often the production branch, therefore stable, of the project.
There may be other branch names like: dev or staging, these are non-stable development branches, tests and verifications will be made on them once the development is finished.
If the result is conclusive, it may be possible to merge these branches to the main or master branch.
Branches are often used to recreate a test / preproduction and production environment.
ℹ️ Git can be compared to a tree:
The trunk of the tree is the main branch of the project (most of the time: main or master)
The branches are elements that come from the trunk but go in different directions
The leaves are one on top of the other, more or less numerous and fall or remain on the branches like commits.
Conclusion: in winter there are more commits 😉
The git branch command allows you to list the branches of the repository:
1
list
2
*master
Here there are two branches: list and master now main.
The * indicates that I am currently in the master branch.
NOTE
The -d option deletes a branch (git branch -d mybranch)
The -b option creates a new branch (git checkout -b mybranch)
The git checkout mybranch command allows you to change branches:
git checkout mybranch
1
Switchedtobranch'mybranch'
It is possible to create a branch from a commit with one of the following commands:
1
gitcheckout-bmynewbranch<commit_sha>
TIP
Note that git checkout <sha> also allows you to return to a commit.
To get the sha (hash) of the commit, we can use the command:
it log --oneline --graph
1
* 5aaf865DeletionofREADME
2
* 6746dbcAddingtesttext
3
* 931dc0fCreationofREADME
The sha is made up of the hexadecimal characters after the ”*” :icon-arrow-right: 931dc0f, for example.
The git stash command allows you to put a commit aside so that the current branch becomes clean, therefore without data in the stage area. This situation is suitable when a commit has not been made and the modifications are still in the stage.
In practice, the git status command returns that there are several changes (in the stage area) that have not been committed.
Stash allows you to put them aside, to be able to insert them into another branch, which can be useful when you have made a mistake in the branch!
After executing the git stash command if you run the git status command again, it returns that the working directory is clean (stage empty).
Now I can go to a new branch or an existing one before inserting the changes made:
I can now enter it in: Settings>SSH and GPG keys, in the title section I can put any name and in the key section I paste ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAKC91uWop9DhfNh23i6u0yUlhEkGv0IOQKzhU5ltKBkAG contact@contactit.fr, validate with Add SSH key.