0091_GIT: Advanced

Some transcription My_program v 2.41.2
                                |  | |
                                |  | Patch
                                |  |
                                | Minor
                              Major
                                   
Given a version number MAJOR.MINOR.PATCH, increment the:
    MAJOR version when you make incompatible API changes;
    MINOR version when you add functionality in a backward compatible manner;
    PATCH version when you make backward compatible bug fixes;
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
More information --> https://semver.org/

Installation
    -Windows
        1. Download https://git-scm.com/download/win
        or use PowerShell:
             winget install --id Git.Git -e --source winget
        2. Install by default
        3. Check version
            in Git Bash console: git --version      / if you can see the version - Ok/
                                 git --help         / will provide you more information /


        You have an option:
                    -use Git Bash console
                    -use Terminal in Visual Studio Code (ctrl + ~)
                        *Anyway you have to use Linux commands like cd,ll, mkdir, etc

        4. Create a folder for your repository using cd, mkdir, etc. You may use windows explorer as well
                Example:
                    cd /d/Install/GIT
                    mkdir my_way; cd my_way            
        5. Initialisation the repository
                    git init        / Inside the folder my_way will be created the hidden folder .git /

        6. We have to set a username and email
                Example:
                    git config --global "Radik Makhmudov"
                    git config --global Radik.M@itbumper.com
                    git config --list / check global configuration /

        7. Start using

        The levels 
            1. Working directory
                -create a folder 
                -create a file and edit
                The first state of new folders and files are "Untracked"  / Folders - green dot, files marked "U" in file explorer" /   
                Example:
                    mkdir GIT; cd GIT
                    touch a0001_git
                    git status  /shows the current branch name, untracked files and folders /
                        $ On branch main
                        Your branch is up to date with 'origin/main'.
                        Untracked files:
                        (use "git add <file>..." to include in what will be committed)
                        GIT/
                        nothing added to commit but untracked files present (use "git add" to track)
                
            2. Staging area (index)  /files and folders --> .git/objects
                    Example:
                    git add GIT/    # We gonna add the whole directory named GIT
                    git status
                        $ On branch main
                        Your branch is up to date with 'origin/main'.
                        Changes to be committed:
                        (use "git restore --staged <file>..." to unstage)
                        new file:   GIT/a0001_git
                    Files marked "A" (Added) in file explorer
                    to add * folders and files use git add .

                Tracking status:
                  -untracked    /marks files and directories not added in the index yet
                  -unmodified   /added and commited files have no changes
                  -modified     /commited files have changes since last commit
                  -staged       /marked a modified file in its current version to go into your next commit snapshot


                If file was changed then
                    git status
                        $ On branch main
                        Your branch is up to date with 'origin/main'.
                        Changes to be committed:
                        (use "git restore --staged <file>..." to unstage)
                        new file:   GIT/a0001_git
                        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:   GIT/a0001_git
                    Files marked "M" (Modifided) in file explorer
                    then do
                    git add /GIT or git add GIT/a0001_git   / status will change: "M" will become "A" /
           
            3. Repository   / files and folders --> .git/objects /
                Example:
                    git commit -m "07/22/2023"      
                        $ git commit -m "07/22/2023"
                        [main 436824f] 07/22/2023
                        1 file changed, 89 insertions(+)
                        create mode 100644 GIT/a0001_git

        How to:
            Excluding files
                Just add the file or folder name in the file .gitignore
                Example:
                    I created the file "dont_want_to_track" / Status: Untracked files -->only "dont_want_to_track"
                    In root directory create a file .gitignore, then add the filename "dont_want_to_track" in it. / Status: Untracked files--> only ".gitignore"
                    *If you want to exclude a folder then add /folder in the .gitignore
                    then do
                    git add .
                    git status   / -->new file:   .gitignore, the file "dont_want_to_track" doesn`t show anymore /
            
            Figure out in which branch you work
                Remember. There are two types of branches: local and remote. In the VSC, have a look at the left bottom corner (GIT label)
                Examples:
                    git branche     / shows current local branche /
                    $ git branch
                    * main

                    git branche - a / shows all current and remote branches /
                    $ git branch -a
                    * main
                      remotes/origin/main

            Create a new branch
                Two ways to create a new branche: using terminal and an APP. In the VSC, use tools hidden at the left bottom corner (GIT label)
                Example:
                    git branch New_Branch
                    git branch
                    $ git branch 
                     New_Branch     / just created branch /
                    * main          / * -means currently chosen branch / 
                If you want create a new branch and make it the current use this command:
                git checkout -b New_Branch
                Two ways to create a new branche: using terminal and an APP. In the VSC, use tools hidden at the left bottom corner (GIT label)

            To rename the current branch use: git branch -m new_branch_name

            Delete some branch
                Example:
                    git branch -D New_Branch
                    $ git branch -D New_Branch
                    Deleted branch New_Branch (was 3d3bc2d).
                Before deleting the branch, you have to set on to another branch. You can`t remove the current branch.
            Select branch
                Two ways to create a new branche: using terminal and an APP. In the VSC, use tools hidden at the left bottom corner (GIT label)
                Be aware. If you have modified files, decide in which branch you will commit to the current state.
                Example:
                    git checkout new_branch
                    $ git checkout new_branch
                    Switched to branch 'new_branch'
                    M       GIT/a0001_git

                The header is always set on the last commit in the branch. HEAD-->BRANCH-->THE_LAST_COMMIT

            Merge branch
                When you have completed development in your branch and everything works fine, the final step is merging the branch with the parent (main) branch. This is done with the git merge command.
                GIT merge basically intergrates your feature branch with all of its commits back to main branch.
                Be aware. Before doing a merge, you have to be on the main branch.
                Current branch=Recieving branch
                Merge branch=Feature branch
                After the merge, we get a Merging commit. The Merging commit has two parents.
                Example:
                git checkout main
                git branch
                $ git branch
                * main              / the current branch is main = Ok /
                  new_branch
                git merge new_branch    OR  git merge -m "Merge the new_branch into main" new_branch
                Merge made by the 'ort' strategy.
                .gitignore              | 2 +-
                Bash/Scripts/check_raid | 2 ++
                2 files changed, 3 insertions(+), 1 deletion(-)
                create mode 100644 Bash/Scripts/check_raid

                git log
                commit 42240c8bc689f25766cdb31b286519988b59b2fa (HEAD -> main)
                Merge: 7947760 68172ff
                Author: Radik.M <Radik.M@itbumper.com>
                Date:   Sun Jul 23 23:48:29 2023 -0700

                Merge branch 'new_branch'

                commit 68172ff47d176d53b1c14177ecdad1d7ba8248f7 (new_branch)
                Author: Radik.M <Radik.M@itbumper.com>
                Date:   Sun Jul 23 23:27:15 2023 -0700

                The first commit in the branch new_branch


            Clone remote repository
                git clone <url>  origin     / Copy remote repository to your local repository URL=https or ssh /
                                            / origin is the default name of the remote repository /
                                            
            Add the remote repository
                git remote add origin <url>
                                            / origin is the name of the remote repository /
                After than you have to make a link between local and remote repository / 
                git push -u origin <branch>
                                            / origin and branch of the remote repository /
                You can use pull and push commands when the sync has been done
           
            Send updates from your local to remote repository
                git push        /pushes all local changes to the remote branch /


            Download updates from the remote to the local repository
                git pull       / download and apply all changes (updates) from remote branch to the local /

                git fetch      / downloads updates but don`t make changes in the local repository /

            Get more information about remote repository
                git remote -v
                $ git remote -v
                origin  git@github.com:RadMakGo/my_way.git (fetch)
                origin  git@github.com:RadMakGo/my_way.git (push)

                git remote -vv     / shows how the local repository linked with the remote repository / 
                $ git remote -vv
                origin  git@github.com:RadMakGo/my_way.git (fetch)
                origin  git@github.com:RadMakGo/my_way.git (push)

        

            Create a token on the GitHub web site:
                1. Profile-->Settings-->Developer settings-->Personel access tokens-->Generate new token
                2. Make a note
                3. Choose expiration date
                4. Choose scopes
                5. Generate token

            Connect your local repository to remote repository
                1. Be sure that you are in the local repository
                2. Check that your local repository has no any remote repositories
                    git remote  
                3. Go to the github web site and add a repository with the same name (local repository)
                4. Add the remote repository
                    git remote add origin https://github.com/RadMakGo/my_way.git
                5. Rename the remote brache
                    git branch -M main
                6. Push the local branch main the remote
                    git push -u origin main         / type username and add token /
        
                Type of objects in GIT: (each object has a unique ID: hash-->sha1)
                    -blob   / file /
                    -tree   / folder /
                    -commit / commit /
                    -annotated tag  

                The commit refers to the tree
                Each commit has:
                    -Author`s name and email address
                    -Commit`s description
                    -Parent(s) commit(s)   -->sha1
                    -Tree                  -->sha1


                    $ git log
                    commit bb65eed885c160dc6dc388dd7c4a42a3aead54fa (HEAD -> main, origin/main)
                    Author: Radik.M <Radik.M@itbumper.com>
                    Date:   Sun Jul 23 23:52:42 2023 -0700
                    The two first simbols in the hash are "bb" and it means than you can find 

                    $ ls .git/objects
                                                                                      * 
                    08/  11/  2b/  2f/  36/  3d/  42/  4c/  55/  68/  77/  7e/  83/  86/  9e/  a4/  af/  bb/  c3/  c8/  d8/  e3/  e5/  e7/  f1/    pack/
                    10/  17/  2e/  35/  3c/  41/  43/  51/  62/  75/  79/  80/  85/  92/  a0/  a7/  b0/  bc/  c6/  d4/  da/  e4/  e6/  e9/  info/

                    ls .git/objects/bb
                    65eed885c160dc6dc388dd7c4a42a3aead54fa

                    in git log commit = folder (bb) + 65eed885c160dc6dc388dd7c4a42a3aead54fa ( ls .git/objects/bb )

                    $ git cat-file -t bb65eed         (a small part of the hash)
                    commit              / now we know what the type of object is /

                    $ git cat-file -p bb65eed
                    tree 3576c88ef13ee05539aa73009de93edd1a874a0e                    
                    parent 42240c8bc689f25766cdb31b286519988b59b2fa
                    author Radik.M <Radik.M@itbumper.com> 1690181562 -0700
                    committer Radik.M <Radik.M@itbumper.com> 1690181562 -0700


                    Now we get some info about the tree
                    $ git cat-file -p 3576c88e
                    100644 blob a013ff39131448178649714dbfb00c2f0e562bd8    .gitignore
                    040000 tree 2ede4efe86dff6185dc90015a77c85944e31edbd    .vscode
                    040000 tree e3621d92f5b18e88c4c446e4bf327e8cd2f170ed    Bash
                    040000 tree b0479e5d3b7ad07e3e6e15fb10e5efacb1ce5d6c    GIT
                    100644 blob e488e34f8e74785aa987e1a42222f64538153ac5    README.md

                    $ git cat-file -p a013ff39
                    dont_want_to_track
                    /logs


                    HEAD detached mode
                    $ git log --> take a needed commit hash
                    $ git checkout 4e1d039
                    Note: switching to '4e1d039'.
                    You are in 'detached HEAD' state. You can look around, make experimental
                    changes and commit them, and you can discard any commits you make in this
                    state without impacting any branches by switching back to a branch.
                    If you want to create a new branch to retain commits you create, you may
                    do so (now or later) by using -c with the switch command. Example:
                    git switch -c <new-branch-name>
                    Or undo this operation with:
                    git switch -
                    Turn off this advice by setting config variable advice.detachedHead to false
                    HEAD is now at 4e1d039 07/24/2023

                    Let's find out what the file README.md contents were in that commit.

                    cat README.md
                    # Things happen, baby; it is a life!

                    # My name is Radik and I've created this repository for a few reasons:
                    # I want to get new skills and improve existing skills working with tools like VS Code, Git, scripting (Bash), Doker, K8S, Ansible, etc
                    # The other reason, I want to improve my English skills
                    # A couple of months ago, my son started to study Python and I hope it will be useful for him too.
                    # Yeah, sometimes I'll write in the Russian language.

                    Now we retern to the main branch
                    $ git checkout main 
                    Previous HEAD position was 4e1d039 07/24/2023
                    Switched to branch 'main'
                    Your branch is up to date with 'origin/main'.

                    And view again
                    $ cat README.md 
                    # Things happen, baby; it is a life!

                    # My name is Radik and I've created this repository for a few reasons:
                    # I want to get new skills and improve existing skills working with tools like VS Code, Git, scripting (Bash), Doker, K8S, Ansible, etc
                    # The other reason, I want to improve my English skills
                    # A couple of months ago, my son started to study Python and I hope it will be useful for him too.
                    # I wish me good luck!

                Change url of the remote repository
                    git remote set-url origin new.git.url