git learning notes

Posted by insanityonline on Thu, 17 Feb 2022 15:37:37 +0100

git and GitHub notes

1,git

1.1 version control

At present, the most popular is distributed version control system

  • The server saves all updated versions of the file
  • The client is a full backup of the server, not just the latest version of the file

A little = >

  • Network operation, support multi person collaborative development
  • After the client is disconnected from the network, it supports offline and local submission of version updates
  • After the server fails or is damaged, it can be restored with the backup of any client

1.2 basic concept of GIT

1.2.1 what is git

git is an open source distributed version control system. It is the most advanced and popular version control system in the world. It can handle project version management from very small to very large quickly and efficiently

Features: the larger the project, the more complex it is, and the more collaborative developers there are, the better it can reflect the high performance and high usability of git

1.2.2 characteristics of GIT
  1. Record snapshots directly instead of comparing differences
  2. Almost all operations are performed locally

Explanation:

svn is a version control based on difference, which is to record the changed files. They store a set of basic files and the differences that each file accumulates over time

  • Benefits: save disk space
  • Disadvantages: time consuming and inefficient
  • Each time you switch versions, you need to apply each difference based on the basic file to generate the file corresponding to the target version

git snapshot is to regenerate a new file based on the original file version, which is similar to backup. 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

1.2.3 three areas

Projects managed by git are divided into three areas: work area, temporary storage area and git warehouse

  • Work area: the area where work is processed (correcting test papers)
  • Temporary storage area for completed work, waiting to be submitted (temporary storage)
  • Final storage area (put into warehouse)
1.2.4 three states
  • Modified - modified indicates that the file has been modified, but the modified result has not been put into the temporary storage
  • Staged - staged, indicating that the current version of the modified file has been marked to be included in the list of next submission
  • Submitted - committed, indicating that the file has been safely stored in the local Git warehouse
1.2.5 basic workflow
  1. Modify files in the workspace
  2. Hold the changes you want to submit next time
  3. Submit the update, find the file in the temporary storage area, and permanently store the snapshot in the git warehouse

1.3 install and configure git

1.3.1 download git

https://git-scm.com/download/win

1.3.2 configuring user information

The first thing is to set up your user name and email

git config --global user.name ""
git config --global user.email ""
// The first is the user name; The second is the mailbox

If the -- global option is used, the command only needs to be executed once to take effect permanently

1.3.3 global configuration file of GIT

Through git config -- global user Name "" and git config -- global user The user name and email address configured by email "" will be written to C: \ users \ Dell In gitconfig file

1.3.4 check configuration information
# View all global configuration items
git config --list --global
# View the specified global configuration item
git config user.name
git config user.email
1.3.5 obtaining help information
# To open the help manual for the git config command
git help config

# Open in the terminal
git config -h

1.4 basic operation of GIT

   Ctrl+ins  Copy( ins In the upper right corner of the keyboard Insert)
   Shift+ins Paste( shift The bottom left corner of the keyboard CTRL An up shift key above the key, which can also be used for Chinese English conversion)

1.4.1 two ways to obtain git warehouse
  1. Convert local directories that have not been versioned into git repositories
  2. Clone an existing git repository from another server
1.4.2 initialize warehouse in existing directory

If you have a project directory that has not yet been versioned and want to use git to control it, you need to perform the following two steps:

  1. In the project directory, right-click to open "Git Bash"
  2. Execute the git init command to convert the current directory into a git repository

The git init command creates a file named The hidden directory of git. This hidden directory is the git warehouse of the current project, which contains the necessary initial files. These files are a necessary part of the git warehouse

1.4.3 four states of documents in the work area

The four states are divided into two categories

Not managed by GIT: not tracked

  • Files not managed by git

Managed by git: unmodified, modified and temporarily stored

  • Unmodified: the contents of the files in the workspace are consistent with those in the git warehouse
  • Modified: the content of the file in the workspace is inconsistent with that in the git warehouse
  • Staged: the modified files in the workspace have been placed in the staging area and are ready to save the modified files to the git warehouse

git status -s

  • ?? Indicates files that are not tracked (two red question marks)
  • A indicates a file that has been staged and has been tracked (green a)
  • M indicates that the content of the tracked file has changed, but it has not been put into the temporary storage area (red m) (the modified files that have not been put into the temporary storage area are marked with red m in front of them)
  • M indicates that the files that have been tracked and changed are sent to the temporary storage area (green m)
  • D indicates that a file has been deleted in the work area and git warehouse. The deletion is to delete the file you want to delete at the next submission (green d)

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-BwvCq43h-1620388516841)(git notes. assets/image-20200910134711196.png)]

The final result of GIT operation: keep the files in the workspace in the "unmodified" state

1.4.4 check the status of documents
# git status to see what state the file is in
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.html

nothing added to commit but untracked files present (use "git add" to track)
# Note that untracked files indicates files that are not tracked
1.4.5 display file status in a streamlined manner
# Show file status in a streamlined manner
git status -s 
git status --short

Untracked files are preceded by a red double question mark

# ??  Represents a file that is not tracked
$ git status --short
?? index.html
$ git status -s
?? index.html

1.4.6 tracking new documents
# git add index.html means to start tracking a file
# Indicates that the index has been tracked HTML file
$ git add index.html
# Run git status - s, A indicates the file newly added to the temporary storage area, and A is green 
$ git status -s
A  index.html
# Below the line Changes to be committed, it indicates that it has been tracked and is in the temporary state
$ git status
On branch master

Initial commit

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

        new file:   index.html



1.4.7 submit updates

The staging area now has an index The HTML file is waiting to be submitted to the git repository for saving. You can execute the git commit command to submit. The - m option is followed by the message submitted this time, which is used to further describe the submitted content

For example:

git commit -m "New index.html file"
# Submit an index to the warehouse The following code in HTML indicates success
$ git commit -m "Submitted a index.html"
[master (root-commit) e8a908f] Submitted a index.html
 1 file changed, 14 insertions(+)
 create mode 100644 index.html

# If the following representative appears, it means that the files in the workspace are in the "unmodified state" and no files need to be submitted
$ git status
On branch master
nothing to commit, working tree clean

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-iZYfQSpJ-1620388516844)(git notes. assets/image-20200910154258909.png)]

1.4.8 modify the submitted documents

Currently, index The HTML file has been tracked by git, and the index.html file in the workspace and git repository Keep the content of HTML file consistent. When we modify the index of the workspace After HTML content, run git status and git status -s commands again. The contents are as follows:

# Indicates a file that has been submitted and modified
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
$ git status -s
 M index.html

1.4.9 temporary storage of modified files

At present, the index of the workspace The HTML file has been modified. If you want to temporarily save this modification, you need to run the git add command again. This command is a multi-functional command, which mainly has the following three functions

  1. You can use it to start tracking new files and put them in the staging area
  2. Put the tracked and modified files into the temporary storage area
  3. Mark conflicting files as resolved

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-IzqOsTVc-1620388516846)(git notes. assets/image-20200910160216483.png)]

1.4.10 submit documents that have been temporarily stored

Run git commit -m "the modified file has been submitted" command again to transfer the index recorded in the temporary storage area HTML snapshot, submit to git warehouse for saving

$ git commit -m "Initialized index.html,Resubmit"
[master 954c645] Initialized index.html,Resubmit
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git status
On branch master
nothing to commit, working tree clean

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-fiIZUvGa-1620388516848)(git note. assets/image-20200910162105416.png)]

1.4.11 cancellation of modification of documents

Revoking the modification of a file refers to restoring the modification of the corresponding file in the workspace to the version saved in the git warehouse.

Result of operation: all modifications will be lost and cannot be recovered!!!

High risk!!! Please operate with caution!!!

git checkout – add the name of the undo file

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-enljwim-1620388516850) (GIT notes. assets/image-20200910162359099.png)]

1.4.12 add multiple files to the staging area at one time

If a large number of files need to be temporarily stored, you can use the following command to add all new and modified files to the temporary storage area at one time

git add .

1.4.13 cancel temporary storage of files

If you need to move the corresponding file from the staging area, you can use the following name

git reset HEAD file name to be removed

1.4.14 skip staging area

The standard workflow of Git is workspace = > staging area = > git warehouse, but sometimes it is a little cumbersome. At this time, you can skip the staging area and directly submit the modifications in the workspace to git warehouse. At this time, the GIT workflow is simplified to workspace = > git warehouse

Git provides a way to skip using the temporary storage area. As long as you add the - a option to git commit when submitting, GIT will automatically temporarily store all the tracked files and submit them together, so as to skip the git add step

git commit -a -m "description"

$ git status -s
 M index.css

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$ git commit -a -m "Skip staging area and commit"
[master 2f99439] Skip staging area and commit
 1 file changed, 6 insertions(+)

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$ git status -s

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$ git status
On branch master
nothing to commit, working tree clean

1.4.15 removing files

There are two ways to remove files from the git Repository:

  1. Remove the corresponding files from the git repository and workspace at the same time
  2. Remove only the specified files from the git repository, but keep the files in the workspace
# Remove index. From git warehouse and workspace at the same time JS file
git rm -f index.js
# Remove index. From git repository only CSS, but keep the index in the workspace CSS file
git rm --cached index.css

# Delete the corresponding files from git warehouse and workspace at the same time
$ git rm -f index.js
rm 'index.js'

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
# A green D indicates that the file to be deleted is submitted next time
$ git status -s
D  index.js

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
# Indicates that only files deleted in git warehouse
$ git rm --cached index.css
rm 'index.css'

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    index.css
        deleted:    index.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.css


dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
# ??  It means that the files that have not been tracked are different from those in git
$ git status -s
D  index.css
D  index.js
?? index.css

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
# Delete the files you want to delete above and skip the operation of temporary storage area
$ git commit -a -m "delete index.js  Delete in warehouse index.css"
[master 7585d37] delete index.js  Delete in warehouse index.css
 2 files changed, 6 deletions(-)
 delete mode 100644 index.css
 delete mode 100644 index.js

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$ git status -s
?? index.css

dell@DESKTOP-SM71TU9 MINGW64 /f/Dark horse annual class 2020.8.30/Four stage front and rear end interaction/Chapter II git and github/project_01 (master)
$

1.4.16 ignore files

Generally, there are always some files that do not need to be included in git management, and we don't want them to always appear in the list of untracked files. In this case, we can create a file named gitignore configuration file, which lists the matching patterns of files to be ignored

Documents The format specification of gitignore is as follows

  1. Comments begin with #
  2. Directories and folders end with /
  3. Prevent recursion starting with /
  4. To! The beginning indicates negation
  5. You can use the glob pattern to match files and folders (glob refers to a simplified regular expression)
1.4.17 glob mode

The so-called glob pattern refers to a simplified regular expression

  • The asterisk * matches zero or more arbitrary characters
  • [abc] match any character listed in square brackets (this case matches an a or a b or a c)
  • question mark? Match only one arbitrary character
  • Use a dash in square brackets to separate two characters, indicating that all within the range of these two characters can be matched (such as [0-9]) and that all numbers from 0 to 9 can be matched
  • Two asterisks * * indicate matching any intermediate directory (for example, a/**/z can match a/b/z or a/b/c/z)
1.4.18 .gitignore file case
# Ignore index CSS this file
index.css

# Ignore all files and folders in the test directory under any directory
test/
  
# Ignore all a documents
*.a

# But track all lib a. Even if you ignore it A documents
!lib.a
# Only TODO files in the current directory are ignored, not subdir/TODO
/TODO
# Ignore all files and folders in any directory called build
build/
# Ignore Doc / notes Txt, but do not ignore Doc / server / arch txt
doc/*.txt
# Ignore the doc / directory and all its subdirectories pdf file
doc/**/*.pdf
1.4.19 view submission history

Press Q to end

# List all submission history in chronological order, with the most recent submission at the top
git log

# Only the latest two submission histories are displayed, and the numbers can be filled in as needed
git log -2

# Displays information about the last two submission histories on one line
git log -2 --pretty=online

# Display the information of the last two submission history on one line, and customize the output format
# %h represents the inter write hash value submitted,% an represents the author's name,% ar represents the author's revision date, which is displayed in the way of how long ago,% s submission description
git log -2 --pretty=format:"%h | %an | %ar | %s"
# Only the latest two submission histories are displayed, and the numbers can be filled in as needed
$ git log -2
commit 7585d3730a9ee2c4648efd472004a92c72e61d2f
Author: Stupid dog <7524246+yygddg@user.noreply.gitee.com>
Date:   Thu Sep 10 18:14:24 2020 +0800

    delete index.js  Delete in warehouse index.css

commit 2f99439af28c224f4164ea9928b86a2bc384fe65
Author: Stupid dog <7524246+yygddg@user.noreply.gitee.com>
Date:   Thu Sep 10 17:56:53 2020 +0800

    Skip staging area and commit
# Displays information about the last two submission histories on one line
$ git log -2 --pretty=oneline
7585d3730a9ee2c4648efd472004a92c72e61d2f delete index.js  Delete in warehouse index.css
2f99439af28c224f4164ea9928b86a2bc384fe65 Skip staging area and commit
# Display the information of the last two submission history on one line, and customize the output format
# %h represents the inter write hash value submitted,% an represents the author's name,% ar represents the author's revision date, which is displayed in the way of how long ago,% s submission description
$ git log -2 --pretty=format:"%h | %an | %ar | %s"
7585d37 | Stupid dog | 87 minutes ago | delete index.js  Delete in warehouse index.css
2f99439 | Stupid dog | 2 hours ago | Skip staging area and commit


1.4.20 fallback to the specified version
# Show all submission history on one line
git log --pretty=oneline
# Use the git reset --hard command to fallback to the specified version according to the specified id
git reset --hard id
# In the old version, use the GIT reflog -- pretty = online command to view the history of command operations
git reflog --pretty=oneline
# Jump to the latest version again according to the latest submission id
git reset -hard id 

1.4.21 summary
  1. Command to initialize git warehouse
    • git init
  2. Commands for viewing file status
    • Git status or git status -s
  3. The command to add all newly added and modified files to the staging area at one time
    • git add .
  4. Command to submit files in the staging area to the git warehouse
    • git commit -m "submit information"
  5. Skip staging area and submit directly
    • git commit -a -m "submit information"

2,GitHub

2.1 what is open source

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-zlP8FDaA-1620388516853)(git note. Assets / image-2020091020245802. PNG)]

2.2 open source license agreement

Five common

  • BSD
  • Apache Licence 2.0
  • GPL
  • LGPL
  • MIT

Characteristics of GPL

  • An infectious open source agreement that does not allow modified and derived code to be released and sold as closed source commercial software
  • The most famous software project using GPL is Linux

MIT features:

  • It is the least restrictive agreement at present. The only condition is that the modified code or distribution package must contain the license information of the original author
  • For example, jQuery, node js

2.3 why open source

The core idea of open source is: I am for everyone and everyone is for me

  • Open source gives users more control
  • Open source makes learning easy
  • Open source is the real security

2.4 open source project hosting platform

The website dedicated to storing the source code of open source projects for free is called open source project hosting platform.

  • GitHub (one of the best open source project hosting platforms in the world)
  • Gitlab (better support for code privacy, so there are more enterprise users)
  • Gitee (also known as codecloud. It is a domestic open source project hosting platform. It has fast access speed, pure Chinese interface and friendly use)

Note: the above three open source project hosting platforms can only host the project source code managed by Git. Therefore, their names begin with Git

2.5 what is GitHub

GitHub is the world's largest open source project hosting platform. Because only git is supported as the only version control tool

  1. Pay attention to your favorite open source projects and call for their likes
  2. Contribute to your favorite open source projects
  3. And open source projects to discuss bug s and raise requirements
  4. Make a copy of your favorite project and modify it as your own project
  5. Create your own open source project
  6. etc

2.6 register GitHub account

  1. Visit the home page of GitHub's official website, https://github.com/
  2. Click the "Sign up" button to jump to the registration page
  3. Fill in the available user name, email address and password
  4. By clicking the arrow, the verification picture will be straightened
  5. Click the "Create account" button to register a new user
  6. Log in to the email filled in step 3 and click the activation link to complete the registration

2.7 two access modes of remote warehouse

HTTPS and SSH

  • https: zero configuration, but every time you visit the warehouse, you need to repeatedly enter the GitHub account and password to access successfully
  • SSH: additional configuration is required; However, after the configuration is successful, you do not need to enter the GitHub account and password repeatedly every time you visit the warehouse
  • In the actual development, it is recommended to use SSH to access the remote warehouse

2.8 upload local warehouse to GitHub remote warehouse based on https

Cases on the official website

When there is no ready-made git warehouse locally

# Create using terminal commands README.md Document and write the initial content as # test_01
echo "# test_01" >> README.md
# The following three lines of code will initialize the local git warehouse and submit the modification of the file to the local git warehouse
git init
git add README.md
git commit -m "first commit"
git branch -M master # The teacher didn't explain, this line of code
# Associate the local warehouse with the remote warehouse, and name the remote warehouse origin 
git remote add origin https://github.com/daidaigou-ye/test_01.git
# Push the content from the local warehouse to the remote GitHub warehouse
git push -u origin master
                

There is a ready-made git warehouse locally

git remote add origin https://github.com/daidaigou-ye/test_01.git
git branch -M master  # The teacher didn't explain, this line of code
git push -u origin master

Compressing objects: 100% (14/14), done. Indicates that it has been uploaded to the remote warehouse

2.9 A new file is created locally and needs to be uploaded to the remote warehouse (submit the code to the remote warehouse for the second time)

Created a new index JS files need to be submitted to the remote warehouse

git push -u origin master indicates the code to be executed when sending a file to the GitHub remote warehouse for the first time

The second time, the third time, etc. git push needs to be executed

$ git status -s
?? index.js

$ git add .

$ git status -s
A  index.js

$ git commit -m "First submitted js file"
[master 59fb001] sew with long stitchesYi Rao℃ProviderょYes js File name
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.js

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 353 bytes | 353.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/daidaigou-ye/test_01.git
   870ce50..59fb001  master -> master

2.10 SSH key

Function: realize login free encrypted data transmission between local warehouse and GitHub

Benefits: login free identity authentication and data encryption transmission

SSH key consists of two parts:

  1. id_rsa (private key file, which can be stored on the computer of the client)
  2. id_rsa,pub (public key file, which needs to be configured in GitHub)

2.11 generate SSH key

  1. Open Git Bash
  2. Paste the following command and put your_email@example.com Replace with the email filled in when registering GitHub account
    • ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  3. Tap the Enter key three times in a row to enter the C:\Users \ user name folder Generate ID in ssh directory_ RSA and id_rsa.pub two files
$ ssh-keygen -t rsa -b 4096 -C "1457240655@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/dell/.ssh/id_rsa):
Created directory '/c/Users/dell/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/dell/.ssh/id_rsa
Your public key has been saved in /c/Users/dell/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:iixpNFgM4DRFkcEpS4voJTIuZBPuRdpH1MUBgtdHvMI 1457240655@qq.com
The key's randomart image is:
+---[RSA 4096]----+
|+o+=Boo.*+.      |
|o*.* o.o +       |
|=.@ o . . .      |
|=% + . E .       |
|O.B .   S        |
|.= + . .         |
|. + o .          |
| . .             |
|                 |
+----[SHA256]-----+

2.12 configure SSH key

  1. Open ID with Notepad_ rsa. Pub file, copy the text content inside
  2. Log in to GitHub in the browser and click avatar = > Settings = > SSH and GPG keys = > New SSH key
  3. Will ID_ rsa. Paste the contents of the pub file into the text box corresponding to the key
  4. Fill in any name in the Title text box to identify where the key comes from

2.13 check whether the configured SSH key is successful

  1. Open git bash
  2. Enter ssh -T git@github.com
  3. You will see that the code sprinkled with red contains one yellow and two green
$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 
# Enter yes immediately after
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi daidaigou-ye! You've successfully authenticated, but GitHub does not provide shell access.
# Configuration succeeded

2.14 upload local warehouse to GitHub remote warehouse based on SSH

First switch the access mode to SSH

# First Association
git remote add origin git@github.com:daidaigou-ye/test_02.git

git push -u origin master

2.15 clone the code of GitHub remote warehouse to local

  1. Open git bash
  2. Execution code: git clone remote warehouse address
$ git clone https://github.com/daidaigou-ye/test_02.git
Cloning into 'test_02'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 247 bytes | 1024 bytes/s, done.

3. Branching

3.1 concept of branch

The branch is the parallel universe in science fiction movies. When you are trying to learn Git in front of the computer, another one is trying to learn SVN in the parallel universe

If two parallel universes don't interfere with each other, it won't affect you now

However, at some point in time, two parallel universes merge, and as a result, you learn both Git and SVN

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-Gd2JV9bV-1620388516854)(git notes. assets/image-20200911120548298.png)]

3.2 role of branches in actual development

When conducting multi person collaborative development, in order to prevent mutual interference and improve the experience of collaborative development, it is recommended that each developer develop project functions based on branches

[the transfer of external chain pictures fails. The source station may have an anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-ZE0wNl8C-1620388516855)(git notes. assets/image-20200911120932086.png)]

3.3 master main branch

When initializing the local Git warehouse, Git has created a branch called master by default. Usually, we call this master branch the main branch

In practice, the main function of the master branch is to save and record the completed function code of the whole project

Programmers are not allowed to modify the code directly on the master branch, because the risk is too high and it is easy to cause the whole project to crash

3.4 functional branches

Since programmers cannot directly develop functions on the master branch, there is the concept of function branch

Function branch refers to the branch dedicated to developing new functions. It is temporarily branched from the master main branch. When the new functions are developed and tested, they need to be merged into the master main branch, as shown in the figure

[the transfer of external chain pictures fails. The source station may have an anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (IMG ldfezyhp-1620388516856) (GIT notes. Assets / image-2020091112335508. PNG)]

3.5 view branch list

Use the following command to view the list of all branches in the current Git warehouse

Note: the * sign in front of the branch name indicates the current branch

git branch
$ git branch
* master

3.6 create new branch

Use the following command to create a new branch based on the current branch. At this time, the code in the new branch is exactly the same as the current branch

git branch New branch name

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-KwEKN6aY-1620388516857)(git note. assets/image-20200911124258328.png)]

After creating a branch, the user is still in the master main branch

3.7 switching branches

You can use the following commands to switch to the specified branch for development

git checkout Branch name

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zRU4hZ2l-1620388516858)(git notes. assets/image-20200911173306113.png)]

$ git branch
  login
* master

$ git checkout login
Switched to branch 'login'

$ git branch
* login
  master

3.8 fast creation and switching of branches

Note: there is an unwritten agreement to create branches based on the master main branch

Use the following command to create a new branch with the specified name and immediately switch to the new branch

# -b means to create a new branch
# checkout means switching to the newly created branch
git checkout -b Branch name

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-NY1pmXMW-1620388516859)(git notes. Assets / image-2020091184455306. PNG)]

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git branch
  login
* master

$ git checkout -b reg
Switched to a new branch 'reg'

$ git branch
  login
  master
* reg

3.9 consolidated branches

After the code development test of the function branch is completed, you can use the following command to merge the completed code into the master main branch

Note: if you want to merge the code of branch C into branch A, you must switch the line to branch A, and then run the git merge command to merge branch C

# Switch to the master branch
git checkout master
# Run the git merge command on the master branch to merge the code of the login branch into the master branch
git merge login

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-pRnB4tHD-1620388516860)(git notes. Assets / image-2020091185934952. PNG)]

Example: merge login into master

First again login On the branch, the code is written and submitted to the local warehouse
$ git status
On branch login
nothing to commit, working tree clean

Switch to the branch you want to merge into
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

 Execute merge branch code
$ git merge login
Updating d2b77d9..102dc4b
Fast-forward
 index.css  |  6 ++++++
 index.html | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

In fact, in the main branch, we didn't write anything, but login When you switch to the main branch, the file size will change,

3.10 delete branch

After merging the code of the function branch into the master main branch, you can use the following command to delete the corresponding function branch

git branch -d branch name

$ git branch -d login
Deleted branch login (was 102dc4b).

When deleting this branch, jump to another branch first, and then delete it

If there is a branch that has not been merged into the main branch, you will be prompted if you use git branch -d branch name

the branch 'branch name' is not fully merged

if you are sure you wang to delete . run 'git branch -D reg' (reg here is the branch name)

3.11 branches - Branch consolidation in case of conflict

If the same file is modified differently in two different branches, Git cannot merge them cleanly. At this time, we need to open these files containing conflicts and solve the conflicts manually

# Suppose: during merging the reg branch into the master branch, the code conflicts
git checkout master
git merge reg

# Open the file containing the conflict, resolve the conflict manually, and then execute the following command
git add .
git commit -m "The problem of branch merge conflict is solved"

Requirement: merge the reg branch into the master branch (index.html has been modified on both the reg branch and the master branch)

# Switch to the master branch first
$ git merge reg
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
# When this code appears, it indicates the index The HTML file modifies the same file on two different branches
# The following picture is the problem after executing the above merge code

# Solution
# Step 1: select one of the four options and save it (in this case, choose to adopt the current change)
# Step 2: git add
# Step 3: git commit -m ""
$ git status -s
UU index.html  

$ git add .

$ git commit -m "Resolve the conflict and submit to the local warehouse"


After the code is merged (the same file is modified on two different branches), the vscode looks like the following figure

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-YFPAyfzi-1620388516861)(git notes. assets/image-20200911220016547.png)]

3.12 branch - push local branch to remote branch

If this is the first time to push the local branch to the remote warehouse, you need to run the following command:

By default, the name of the remote warehouse is called origin

be careful! Only the first push branch needs to take the - u parameter. After that, you can directly use git push to push the code to the remote branch

# -u means to associate the local branch with the remote branch. The - u parameter is required only when pushing for the first time
git push -u Alias local branch name of the remote warehouse:Remote branch name

# Actual case: there is a payment branch locally, which needs to be pushed to the remote origin warehouse, and this branch is renamed pay
git push -u origin payment:pay

# If you want the name of the remote branch to be consistent with the name of the local branch, you can simplify the command
git push -u origin payment

Case: push reg to GitHub remote warehouse

# Check the branch first
$ git branch
* master
  reg

# Push the local reg branch to the remote warehouse and rename the branch register
$ git push -u origin reg:register

[the transfer of external chain pictures fails. The source station may have an anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-igun6R1L-1620388516862)(git notes. Assets / image-2020091122624333. PNG)]

3.13 branches - view the list of remote branches

git remote show the name of the remote warehouse

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/daidaigou-ye/test_02.git
  Push  URL: https://github.com/daidaigou-ye/test_02.git
  HEAD branch: master
  Remote branches:
    master   tracked
    register tracked
  Local branches configured for 'git pull':
    master merges with remote master
    reg    merges with remote register
  Local ref configured for 'git push':
    master pushes to master (fast-forwardable)

3.14 tracking branches

Tracking branch refers to downloading the remote branch from the remote warehouse to the local warehouse. The following code needs to be executed

# From the remote warehouse, download the corresponding remote branch to the local warehouse and keep the names of the local branch and the remote branch the same
git checkout The name of the remote branch
# Examples
git checkout pay

# From the remote warehouse, download the corresponding remote branch to the local warehouse, and rename the downloaded local branch
git checkout -b Local branch name remote warehouse name/Remote branch name
# Examples
git checkout -b payment origin/pay
$ git branch
* master
  reg

# Delete reg branch
$ git branch -d reg
Deleted branch reg (was cdbc9dc).

# Clone a branch of the remote warehouse from the remote warehouse
$ git checkout register
Switched to a new branch 'register'
Branch 'register' set up to track remote branch 'register' from 'origin'.


$ git branch
  master
* register

3.15 pull the latest code of remote branch

Pull the latest code of the current branch from the remote warehouse to keep the code of the current branch consistent with that of the remote branch

Running this code on that branch means updating the corresponding code on that branch

git pull

Case: premise: on the register branch, modify the index JS code

$ git pull
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 2.04 KiB | 5.00 KiB/s, done.
From https://github.com/daidaigou-ye/test_02
   cdbc9dc..cd60fa4  register   -> origin/register
Updating cdbc9dc..cd60fa4
Fast-forward
 index.js | 2 ++
 1 file changed, 2 insertions(+)

3.16 Delete remote branch

# Delete the remote branch with the specified name in the remote warehouse
git push Remote warehouse name --delete Remote branch name
# Examples
git push origin --delete pay

Case: Delete remote resist branch

$ git push origin --delete register
To https://github.com/daidaigou-ye/test_02.git
 - [deleted]         register

4. Summary

  1. Be able to master the use of basic commands in Git
    • git init
    • git add .
    • git commit -m "submit message"
    • Git status and git status -s
  2. Ability to create and maintain remote warehouses using GitHub
    • Able to configure SSH access of GitHub
    • Ability to upload local warehouse to GitHub
  3. Be able to master the basic use of git branch
    • git checkout -b name of the new branch
    • New branch name - git push
    • git checkout branch name
    • git branch

5. Basic steps of uploading items to code cloud and publishing them

  1. Code cloud creates a new warehouse. heimamm

  2. Use git submission to submit the local website to the new warehouse of code cloud

    • Right click the root directory of the website -- Git Bash Here

    • If this is the first time to submit using git, please configure the global option

      git config --global user.name "user name"
      git config --global user.email "Your email address"
      
    • Initialize warehouse

      git init
      
    • Put local files in staging area

      git add .
      
    • Put the local files in the local warehouse

      git commit -m 'Submit dark horse face-to-face website'
      
    • Link remote warehouse

      git remote add origin Your new warehouse address
      
    • push files from local warehouse to remote warehouse

      git push -u origin master
      
  3. Code cloud deployment publishing static website

    • In the current warehouse, click the "service" menu

    • Select Gitee Pages

    • Select the start button

    • After a while, you will get the address, and you can use this address to preview the web page