python_introduction/learning_git.md

291 lines
9.2 KiB
Markdown
Raw Normal View History

2022-05-02 22:21:04 +02:00
# About
2022-05-02 11:10:45 +02:00
2022-05-02 13:57:29 +02:00
Git is a Version Control System.
It is an advanced open source project.
That is the most popular VCS with his advantage like security, efficiency, speed etc.
He was developed in 2005 by Linus Torvalds, the Linux's father.
Many companies projects using Git (Google, Gnome, Netflix, etc).
In 2016 12 million people were using Git.
All project histories were saved in the work file.
You can publish, with all histories, projects in a Git platform like Gitea, GitHub, Bitbucket, GitLab, etc.
With this tool, you can commit projects, compare projects.
That is the best tool for teamwork.
2022-05-02 22:25:49 +02:00
![git flow](./assets/git-flow.png)
2022-05-02 22:21:04 +02:00
# Git via `bash`
## Initialing a git repo
To initialize an empty git repository, you can tap _**git init**_ in a text editor like Git Bash.
This command will create a git repository in the initial branch **master**.
You will be able to add, commit, push, etc.
2022-05-02 22:21:04 +02:00
```
waldek@metal:~/Documents/my_first_git_repository$ git status
fatal: not a git repository (or any of the parent directories): .git
waldek@metal:~/Documents/my_first_git_repository$ git init .
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/local/waldek/Documents/my_first_git_repository/.git/
waldek@metal:~/Documents/my_first_git_repository$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add"
```
If you want to see if you are in a git repository, you can use _**git status**_.
He will inform you which branch that you are and how many commits in this repository.
Be careful with commands because you have to be in a folder.
In the preceding example, the new repository was in the "my_first_git_repository" folder.
2022-05-02 22:21:04 +02:00
## What's in this repo
You can see all files of your git repository with the command **_ls_**.
2022-05-02 22:21:04 +02:00
```
waldek@metal:~/Documents/my_first_git_repository$ ls -la
total 12
drwxr-xr-x 3 waldek waldek 4096 May 2 22:06 .
drwxr-xr-x 4 waldek waldek 4096 May 2 22:06 ..
drwxr-xr-x 7 waldek waldek 4096 May 2 22:07 .git
waldek@metal:~/Documents/my_first_git_repository$ ls -la .git/
total 40
drwxr-xr-x 7 waldek waldek 4096 May 2 22:07 .
drwxr-xr-x 3 waldek waldek 4096 May 2 22:06 ..
drwxr-xr-x 2 waldek waldek 4096 May 2 22:06 branches
-rw-r--r-- 1 waldek waldek 92 May 2 22:06 config
-rw-r--r-- 1 waldek waldek 73 May 2 22:06 description
-rw-r--r-- 1 waldek waldek 23 May 2 22:06 HEAD
drwxr-xr-x 2 waldek waldek 4096 May 2 22:06 hooks
drwxr-xr-x 2 waldek waldek 4096 May 2 22:06 info
drwxr-xr-x 4 waldek waldek 4096 May 2 22:06 objects
drwxr-xr-x 4 waldek waldek 4096 May 2 22:06 refs
waldek@metal:~/Documents/my_first_git_repository$
```
## Adding and tracking content
```
waldek@metal:~/Documents/my_first_git_repository$ cat readme.md
# Hello world
I am a paragraph.
I can contain multiple sentences, each on one line.
A blank line separates multiple paragraphs.
1. lists
1. will
1. auto increment in most HTML render engines
* sublists
* are possible
* on multiple
* levels
1. but I keep counting...
2022-05-02 22:23:17 +02:00
[I will become](https://kernel.org) a link.
And I'm a line:
---
2022-05-02 22:21:04 +02:00
waldek@metal:~/Documents/my_first_git_repository$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.md
nothing added to commit but untracked files present (use "git add" to track)
waldek@metal:~/Documents/my_first_git_repository$
```
Now we can `add` and `commit` our changes.
```
waldek@metal:~/Documents/my_first_git_repository$ git add readme.md
waldek@metal:~/Documents/my_first_git_repository$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.md
waldek@metal:~/Documents/my_first_git_repository$ git commit -m "adds a readme file"
[master (root-commit) 8278e12] adds a readme file
1 file changed, 22 insertions(+)
create mode 100644 readme.md
waldek@metal:~/Documents/my_first_git_repository$ git status
On branch master
nothing to commit, working tree clean
waldek@metal:~/Documents/my_first_git_repository$
```
We can use `git log` to inspect the list of commits.
Add the `--no-pager` to bypass `less` and print the output straight to the console.
```
waldek@metal:~/Documents/my_first_git_repository$ git --no-pager log
commit 8278e1273d1438920c1063cdca179eb70d5926d8 (HEAD -> master)
Author: waldek <waldek@mailbox.org>
Date: Mon May 2 22:16:50 2022 +0200
adds a readme file
waldek@metal:~/Documents/my_first_git_repository$
```
## The main workflow
1. edit your files
1. `git status` to verify what's new or has been modified
1. `git add` the files you want to commit
1. `git commit -m "A RELEVANT COMMIT MESSAGE"`
1. `git push` to wherever you have your repository
1. repeat step one...
# Git via Atom
# Git via Pycharm
## Starting a version controlled project
2022-05-02 12:07:01 +02:00
2022-05-02 14:58:17 +02:00
In Pycharm, you have to enable a version control integration.
There are many choices, but we will use Git.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_00.png)
![screenhot](assets/git_pycharm_source_01.png)
As you can see, Git appears in the toolbar and a git window appear bottom of the window.
And you can identify the current branch that you are in.
So the default branch name is **master**.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_02.png)
Let's try to write some code.
2022-05-02 14:58:17 +02:00
And add all contents that we write in a git repository.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_03.png)
In the commit window, we can see which file had changed.
But we had three important things too.
2022-05-02 14:58:17 +02:00
We have a textBox and two buttons.
In the textBox, you can write a comment when you commit.
The _Commit_ button is used to save all change of your project in a git repository.
2022-05-02 14:58:17 +02:00
The _Commit and Push_ is used to commit and push the project in a Git.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_04.png)
2022-05-02 14:58:17 +02:00
When you press the _Commit and Push_, a new window appears.
You can see which branch and which commit that you push.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_05.png)
2022-05-02 22:21:04 +02:00
## Creating an online repository
If you use Gitea and you want to create a new repository.
That is pretty simple, you have to press the button **New Repository**.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_06.png)
And then, you can add the repository name, and others parameters like description, the visibility, etc.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_07.png)
When you have finish, you have to press the button **Create Repository**.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_08.png)
Et voila, you have created a new repository on Gitea and you receive an https link.
This link is used if you want to share your project with someone, or you want to clone this repository.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_09.png)
If you want to work in your repository in Pycharm, you have to clone it.
In the toolbar, you can click on Git and you will find the button **Clone**.
Then, a window will be open, and you have to copy and paste the https link of your repository.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_10.png)
When the clone is successfully does, you can change the project.
And if you have finished your job, you can **Commit and Push**.
If you want to commit and push all changes that you have made, you have to check all changes in the left window.
And then, you can press **Commit and Push**.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_11.png)
In the bottom git window, you can see all commits, and their comments, of this repository.
If you clone a another repository, you will be able to see all commits that people made and their comment.
That is pretty cool !
2022-05-02 22:21:04 +02:00
## Adding some changes to our local code
2022-05-02 12:07:01 +02:00
In Gitea, you can see what the git repository contain and the last comment of commits.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_12.png)
If you check all changes and you want to **Commit and Push**, don't forget the comment.
That is important for a good teamwork.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_13.png)
If you commit twice, you are able to verify how many commits and when commits have done.
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_14.png)
2022-05-02 11:20:09 +02:00
<!---
2022-05-02 11:17:14 +02:00
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_15.png)
2022-05-02 11:20:09 +02:00
-->
2022-05-02 11:17:14 +02:00
2022-05-02 22:21:04 +02:00
## Cloning the remote project into a new project
In the main menu of PyCharm, you can click in **Get from VCS**.
If you click in it, you have just copy and paste the https link of the git repository.
2022-05-02 12:07:01 +02:00
2022-05-02 11:15:06 +02:00
![screenhot](assets/git_pycharm_clone_00.png)
You have to trust the git repository.
2022-05-02 11:15:06 +02:00
![screenhot](assets/git_pycharm_clone_01.png)
2022-05-02 11:15:06 +02:00
![screenhot](assets/git_pycharm_clone_02.png)
![screenhot](assets/git_pycharm_clone_03.png)
![screenhot](assets/git_pycharm_clone_04.png)
![screenhot](assets/git_pycharm_clone_05.png)
![screenhot](assets/git_pycharm_clone_06.png)
2022-05-02 11:20:09 +02:00
<!---
2022-05-02 11:17:14 +02:00
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_16.png)
![screenhot](assets/git_pycharm_source_17.png)
2022-05-02 11:20:09 +02:00
-->
2022-05-02 11:17:14 +02:00
2022-05-02 22:21:04 +02:00
## Updating the original project
2022-05-02 12:07:01 +02:00
2022-05-02 11:10:45 +02:00
![screenhot](assets/git_pycharm_source_18.png)
![screenhot](assets/git_pycharm_source_19.png)
![screenhot](assets/git_pycharm_source_20.png)