git learning notes

Keywords: git github

1, Git basic introduction

1. Git structure

2. Local and remote Libraries

2.1 intra team collaboration

The difference between pull and clone:

  • Clone a version library as like as two peas from the remote server to the local version, which is copied from the entire version library, called clone. (clone is a copy of the library to your local, which is a local scratch process, which includes local initialization operations).

  • Get the update of a branch from the remote server and update the local library, which is called pull. (pull refers to synchronizing the updated part of a local version of the library to your local library)

  • git clone is the first step of remote operation, usually cloning a version library from a remote host

    $ git clone ##   
    This command generates a directory on the local host with the same name as the version library on the remote host
    
    If you want to make a different directory name, you can use the directory name as git clone The second parameter of the command.
    eg: $ git clone #Version library URL# #Local directory name#  
    
    git clone Supports multiple protocols, except http(s),also ssh,git,Local agreement.
    
  • git pull is used to retrieve the update of a branch of the remote host and merge it with the specified local branch. Its full format is a little complicated.

    eg:$ git pull ##Remote host( origin) #Remote sharing(next)#: #Local share(master)#
    If the remote branch is merged with the current branch, the part after the colon can be omitted.
    eg:$ git pull #Remote host(origin)# #Remote sharing(next)# 
    Equivalent to 1. $ git fetch origin   2.$git merge origin/next 
    

    2.2 cross team collaboration

2, Git command line operation

1. Local library initialization

Create folder WeChat

Y@SYY MINGW64 /e/Git
$ mkdir WeChat

Enter WeChat for local initialization

Y@SYY MINGW64 /e/Git/WeChat
$ git init
Initialized empty Git repository in E:/Git/WeChat/.git/

View explicit files, hidden files

Y@SYY MINGW64 /e/Git/WeChat (master)
$ ll	#View explicit file
total 0

Y@SYY MINGW64 /e/Git/WeChat (master)
$ ls -la	#view hidden files
total 4
drwxr-xr-x 1 Y 197121 0 Oct 22 09:39 ./
drwxr-xr-x 1 Y 197121 0 Oct 22 09:37 ../
drwxr-xr-x 1 Y 197121 0 Oct 22 09:39 .git/

View the. git / directory

Y@SYY MINGW64 /e/Git/WeChat (master)
$ ll .git/
total 7
-rw-r--r-- 1 Y 197121  23 Oct 22 09:39 HEAD
-rw-r--r-- 1 Y 197121 130 Oct 22 09:39 config
-rw-r--r-- 1 Y 197121  73 Oct 22 09:39 description
drwxr-xr-x 1 Y 197121   0 Oct 22 09:39 hooks/
drwxr-xr-x 1 Y 197121   0 Oct 22 09:39 info/
drwxr-xr-x 1 Y 197121   0 Oct 22 09:39 objects/
drwxr-xr-x 1 Y 197121   0 Oct 22 09:39 refs/

Note: the. git directory stores subdirectories and files related to the local library. Do not delete or modify them indiscriminately.

2. Set signature

2.1 form

User name: syy

email address (it can be set casually and can not exist): 1204201145@qq.com

2.2 function

  • Identify different developers

  • The signature set here has nothing to do with the account and password for logging in to the remote library (code hosting Center)

    system

2.3 command

  • Project level / warehouse level: only valid within the current local library:

    Keywords: git config user.name user name

    git config user.email mailbox

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git config user.name syy
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git config user.email 1204201145@qq.com
    

    Information storage location:. /. git/config file

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ cat .git/config
    [core]
            repositoryformatversion = 0
            filemode = false
            bare = false
            logallrefupdates = true
            symlinks = false
            ignorecase = true
    [user]
            name = syy
            email = 1204201145@qq.com
    
    
  • System user level: the range of users who log in to the current operating system

    Keywords: git config --global user.name username

    git config --global user.email mailbox

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git config user.name syy_glb
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git config user.email 1204201145_glb@qq.com
    

    Information storage location: ~ /. gitconfig file

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ cat ~/.gitconfig
    [filter "lfs"]
            clean = git-lfs clean -- %f
            smudge = git-lfs smudge -- %f
            process = git-lfs filter-process
            required = true
    [user]
            name = syy_glb
            email = 1204201145_glb@qq.com
    

Note: entering the system path cd represents the system

Y@SYY MINGW64 /e/Git/WeChat (master)
$ cd ~

Y@SYY MINGW64 ~
$ pwd
/c/Users/Y
  • Level priority

    Proximity principle: the project level takes precedence over the system user level, both of which sometimes adopt the project level

    Your signature.

    If there is only system user level signature, the system user level signature shall prevail. Neither of them has permission restrictions that are not allowed

3. Basic operation

3.1 status view

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git status
On branch master	#Current branch

No commits yet	#Local warehouse
nothing to commit (create/copy files and use "git add" to track)	#Buffer

View workspace and staging area status

3.2 adding files

Keyword: git add [file name]

Add new / modify from workspace to staging area

Y@SYY MINGW64 /e/Git/WeChat (master)		#Edit and create files
$ vim good.txt

Y@SYY MINGW64 /e/Git/WeChat (master)
$ cat good.txt	#view file contents
123456
i love u
do u know that

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git add good.txt	#submit document
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git status	#View cache status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   good.txt

3.3 submission

Keyword: git commit -m "commit message" [file name]

Commit the contents of the staging area to the local library

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git commit -m "This is my first commit for the new file:good.txt" good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory
[master (root-commit) e81d234] This is my first commit for the new file:good.txt	#Submit information
 1 file changed, 3 insertions(+)
 create mode 100644 good.txt
 
Y@SYY MINGW64 /e/Git/WeChat (master)	#The cache has been emptied
$ git status
On branch master
nothing to commit, working tree clean

3.4 viewing history

Keyword: git log (all records can be displayed)

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git log
commit c40cd8e1800b3e8b9bb7e91dfcac13fd08e489d7 (HEAD -> master)
Author: syy <1204201145@qq.com>
Date:   Fri Oct 22 10:49:07 2021 +0800

    This is a change.

commit e81d2342afc55955e5441a86f8d6e079d73a09a9
Author: syy <1204201145@qq.com>
Date:   Fri Oct 22 10:38:06 2021 +0800

    This is my first commit for the new file:good.txt

Expand: view the log in a single line (if rollback is performed, only the previous records can be displayed)

​ git log --pretty=oneline

​ git log --oneline

git reflog: you can view the number of fallback steps. HEAD@{2} means you can go back to the current version by fallback 2 steps.

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git log --pretty=oneline
c40cd8e1800b3e8b9bb7e91dfcac13fd08e489d7 (HEAD -> master) This is a change.
e81d2342afc55955e5441a86f8d6e079d73a09a9 This is my first commit for the new file:good.txt

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git log --oneline
c40cd8e (HEAD -> master) This is a change.
e81d234 This is my first commit for the new file:good.txt

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git reflog
04e5a05 (HEAD -> master) HEAD@{0}: commit: insert aaaaaabbbbb
c40cd8e HEAD@{1}: commit: This is a change.	
e81d234 HEAD@{2}: commit (initial): This is my first commit for the new file:good.txt

3.5 forward and backward

Essence: operation head pointer

  • Operation based on index value (recommended)

    git reset --hard local index value

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git reflog
    04e5a05 (HEAD -> master) HEAD@{0}: commit: insert aaaaaabbbbb
    c40cd8e HEAD@{1}: commit: This is a change.	
    e81d234 HEAD@{2}: commit (initial): This is my first commit for the new file:good.txt
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git reset --hard c40cd8e
    HEAD is now at c40cd8e This is a change.
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ cat good.txt
    123456
    i love u
    do u know that
    i know that
    
    
    • Use the ~ symbol: you can only go back

      git reset --hard HEAD~n: indicates n steps backward

3.6 delete and retrieve files

Premise: before deletion, the status of the file when it exists is submitted to the local library.

Keyword: git rm file

Retrieval operation: git reset --hard [pointer position]

The delete operation has been committed to the local library: the pointer position points to the history

The delete operation has not been committed to the local library: the pointer position uses HEAD

3.7 differences in comparison documents

  • git diff [file name]

Compare the files in the workspace with the staging area

  • git diff [historical version in local library] [file name]

Compare the files in the workspace with the local library history

Compare multiple files without file names

4. Branch Management

4.1 branch benefits

At the same time, promote the development of multiple functions in parallel to improve the development efficiency.

During the development of each branch, if the development of one branch fails, it will not have any impact on other branches. Delete the failed branch and start again.

4.2 branch operation

  • Create branch

    Keyword: git branch

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git branch hot-fix
    
  • View branch

    Keywords: git branch -v

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git branch -v
      hot-fix 5650d7c this is a test.txt
    * master  5650d7c this is a test.txt
    
  • Switch branch

    Keyword: git checkout branch

    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git checkout hot-fix
    Switched to branch 'hot-fix'
    
  • Merge branch

    Step 1: switch to the branch to be modified (merged and new content added)

    git checkout [merged branch name]

    Step 2: execute the merge command

    git merge

    Y@SYY MINGW64 /e/Git/WeChat (hot-fix)
    $ git checkout master
    Switched to branch 'master'
    M       good.txt
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ git merge hot-fix
    Already up to date.
    
    Y@SYY MINGW64 /e/Git/WeChat (master)
    $ cat good.txt
    123456
    i love u
    do u know that
    i know that
    aaaaaaa --edit by hot-fix
    
    
    • Conflict resolution

      Performance of conflict

    Conflict resolution:

    Step 1: edit the file and delete special symbols

    Step 2: modify the file to a satisfactory level, save and exit

    Step 3: git add [file name]

    Step 4: git commit -m "log information"

    Note: the commit must not contain a specific file name at this time

5. Git principle

5.1 hash

Hash is a series of encryption algorithms. Although different hash algorithms have different encryption strength, they have the following advantages:

Several common points:

① No matter how large the amount of input data is, if the same hash algorithm is input, the length of the encryption result is fixed.

② The hash algorithm is determined, the input data is determined, and the output data can be guaranteed to remain unchanged

③ The hash algorithm determines that if the input data changes, the output data must change, and usually changes greatly

④ Hash algorithm is irreversible

The bottom layer of Git adopts SHA-1 algorithm.

Git relies on this mechanism to fundamentally ensure data integrity.

5.2 git version saving mechanism

5.2.1 file management mechanism of centralized version control tool

Information is stored in the form of a file change list. Such systems regard the information they hold as a set of basic information

The difference between the file and each file gradually accumulated over time.

5.2.2 git file management mechanism

Git sees data as a set of snapshots of a small file system. Git updates the current file system every time it commits an update

Make a snapshot of all files and save the index of this snapshot. For efficiency, if the file is not modified,

Git will no longer re store the file, but only keep a link to the previously stored file

The working mode can be called snapshot flow.

6. GitHub use

6.1 creating a remote library address alias

git remote -v view all current remote address aliases

git remote add [alias] [remote address]

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git remote add origin https://github.com/SUYAOYANG/TEST01.git

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git remote -v
origin  https://github.com/SUYAOYANG/TEST01.git (fetch)
origin  https://github.com/SUYAOYANG/TEST01.git (push)

6.2 push

git push [alias] [branch name]

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git push origin master

6.3 cloning

git origin [remote address]

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git clone https://github.com/SUYAOYANG/TEST01.git

6.4 pulling

pull=fetch+merge

git fetch [remote library address alias] [remote branch name]

git merge [remote library address alias / remote branch name]

git pull [remote library address alias] [remote branch name]

6.5 cross team collaboration

First fork other people's warehouses, then submit your own files, and then click pull request to submit your own merge request (which will commit your own ideas). When others see it, they can chat and communicate with you here in pull request, and they can also see what files you commit and what modifications you have made. Finally, if you pass, Others click merge to merge the code.

Branch name]**

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git push origin master

6.3 cloning

git origin [remote address]

Y@SYY MINGW64 /e/Git/WeChat (master)
$ git clone https://github.com/SUYAOYANG/TEST01.git

6.4 pulling

pull=fetch+merge

git fetch [remote library address alias] [remote branch name]

git merge [remote library address alias / remote branch name]

git pull [remote library address alias] [remote branch name]

6.5 cross team collaboration

First fork other people's warehouses, then submit your own files, and then click pull request to submit your own merge request (which will commit your own ideas). When others see it, they can chat and communicate with you here in pull request, and they can also see what files you commit and what modifications you have made. Finally, if you pass, Others click merge to merge the code.

Posted by Rushy on Fri, 22 Oct 2021 05:14:08 -0700