“This is my 12th day of the first Gwen Challenge 2022.First challenge in 2022”

Git, a distributed version control system

Introduction to the

What is version control

Revision control is a software engineering technique used during development to manage the history of changes made to files, directories, projects, etc., so that you can view the history of changes and back it up to restore previous versions.

  • Realize cross-region multi-person collaborative development
  • To track and record the history of one or more files
  • Organize and protect your source code and documentation
  • Statistical workload
  • Parallel development, improve development efficiency
  • Track and record the entire software development process
  • Reduce developer burden, save time, and reduce human error

In short, it is a technique for managing collaborative development projects with many people.

Common version control tools

The main version controllers are as follows: Git SVN CVS VSS TFS….The most influential and widely used are Git and SVN

Version control classification

Local version control

Local record file every update/manual save backup, can make a snapshot for each version, or record the patch file,Suitable for personal use such as RCS. Like the following:Manual record backup, 1.0 2.0... Version of the record...

Centralized version controlCommon: SVN

All release data is stored on the server, and developers work together to synchronize updates or upload their own changes from the server

All version data is stored on the server

  • The user has only the previously synchronized version locally.
  • Without an Internet connection, users cannot see historical versions, switch version verification issues, or work in different branches.

Moreover, all data is kept on a single server

  • There’s a very high risk that the server will get corrupted and lose all of its data,Of course you can back up regularly. Products: SVN, CVS, and VSS (I don't have an in-depth understanding of Git at present.)

Disadvantage: must network ability to work, if be in LAN still can, bandwidth is enough big, speed is enough fast, if fall in Internet, if net speed is slow, wonder.

Distributed version controlCommon: Git

Everyone owns all the code!There are security risks! All version repositories are synchronized locally to each user

  • This allows you to view all version history locally and submit it offline locally. Just when you’re connected to the InternetPush (upload)Go to the appropriate server or other user.

Since all version data is stored for each user, it only takes one user’s device to recover all data, thus increasing the local storage footprint. Disadvantages: less information (at least few Chinese information) learning cycle is relatively long. It’s not conventional thinking. Code confidentiality is poor, once the developer has cloned the entire library, all code and version information can be fully disclosed.

Advantages and disadvantages of SVN and Git

The SVN is a centralized version control system, and the version library is centralized on the central server

  • And when I work, I use my own computer
  • So first get the latest version from the central server, and then work. After finishing the work, you need to push your finished work to the central server.
  • Centralized version control system must be networked to work, which requires high network bandwidth.

The difference is that if your company’s SVN server fails, you can take a vacation.

Git is a distributed version control system

  • Without a central server, everyone’s computer is a complete repository of versions
  • You don’t need to be connected to the Internet when you work, because the versions are all on your computer.
  • The method of coordination is like this: for example, you change file A on the computer, and other people also change file A on the computer. Then, both of you just push their changes to each other, and each of you can see the other’s changes. Git can directly see what code and files are being updated!

Git is the most advanced in the worldDistributed version control system

Git is the most advanced distributed version control system in the world. Git is free and open source, and was originally developed to support the Linux kernel (so many of the commands are the same as Linux)~ to replace BitKeeper!



Linus Benedic Torvalds, father of Linux and Git, Finland, 1969

Git download + installation + configuration

download

IO/I use: git-2.21.0-64-bit. exe

The installation

D:\WSMwork\Git\Git There are more things over time. I didn’t even know it was there!

No brain next step can! Once installed, it’s ready to use! The first few steps are omitted, but the emphasis is on several options: under normal circumstances, the two interfaces should be selected first

In the “Configuring the Line Ending Conversions” option the first option in the “Configuring the Line Ending Conversions” option. Second option: if the project is cross-platform and installed on a Unix system, select; Third option: non-cross-platform projects, select

Activation:

Git entries will appear in the Start menu after a successful installation

  • Git Bash: Unix and Linux style command line, most used, most recommended
  • Git CMD: Windows style command line
  • Git GUI: Graphical Git GUI, not recommended for beginners, try to familiarize yourself with common commands first

Common Linux commandsLinux

The father of Git is the father of Linux, git kernel is liunx… So many of the common commands are Linux-like!

configuration

After installing Git, you need to configure your Git account:

  • Git is identified by a combination of mailbox usernames.Open the Git – Bash. Exe
 Configure the global user name
 git config --global user.name 'Enter a user name'
 ​
 Configure the global mailbox
 git config --global user.email '[email protected]' 
 The user name and mailbox can also be configured for each project.
 ​
 ​
 Check the system config
 git config --system --list
 ​
 # query the current user (global) configuration
 git config --global  --list
Copy the code

Set user name and email (user ID, required)

You can see the configured user information in C: Users\Administrator\.gitconfigTry to fill in your own, because this is the back of the remote repository github connection

Git configuration files are stored locally.

Git config -l Git\mingw64\etc\ gitConfig

I just installed less configuration…

Git three regions

Workspace + repository + staging area

  • Working Directory: Where the project code is normally stored** is the directory on your computer’s local hard drive
  • Temporary storage area: For temporary storage of your changes,In fact, it is just a file that holds information about the list of files to be submittedGeneral storageGit Directory version areaGit /index file, so we call the staging area sometimes called index.
  • Repository: There’s aHide the directory. GitIt is GitLocal version library It’s a secure place to store data that you’ve submitted to all versions of. Where HEAD points to the latest version put into the repository

DirectoryA directory, also known as a repository, managed with GitContains: workspaces and Git version libraries

  • Directoryc can be understood as a common directory, throughgit initThe command is initialized to oneGit Management DirectoryAnd then it automatically generates oneThe git repositoryFiles File by file in the Directoryc folderThe workspaceAll files to be managed
  • The created file is initially Untracked for the workspace: the state of the file becomes Staged with the add command
  • Passage Ten: Staged state Git commit synchronizes the changes to the repository for backup version status!

Git automatically creates the first master branch for us by default, and a pointer to master is called HEAD

When you add a file to the Git repository, you do it in two steps. The first step is to add the file to the Git management repository with Git Add. To actually add local file changes to the staging area, the second step is to use ·git commit· commit changes; This essentially commits the contents of the staging area to the current branch’s default master primary partition.

Remote warehouse:

Git repositories (Remote Directory) can be divided into four working areas:

  • Remote: a server that hosts code that can simply be thought of as a computer on your project team for Remote data exchange

Custom creationDirectory Git manages repositories

Find any drive letter locationIt is not recommended that the path contain Chinese ~After Git is installed, right click to bring up the selectionGit Bash Here, then it will pop up, enter the command:git initInitialize the Git repository

Git folder created successfully.This is git’s local repositoryAnd it’s automatically created for usThe master branch.The master branch is already associated by default

Git Git

View file status + Commit changes

 Check the status of all files in the current path
 git status
 ​
 # Check the file status under the current fileGit status folderCopy the code

File status!!

  • Untracked: Not tracked. This file is in a folder but is not added to a Git repository and is not involved in version control. Git The Add state becomes Staged.
  • Unmodify: Indicates that the file is imported and not modified. That is, the snapshot content of the file in the version library is the same as that in the folder. A file of this type can be Modified in two ways: If it is Modified, it becomes Untracked if you use Git rm to move the library
  • Modified: The file has been Modified, only Modified, without any other action. There are two ways to override this type of file: To enter passively staged files, use Git add to discard the modifications and return to the unmodify state. Git checkout takes the file from the library and overwrites the current modification!
  • Passage Ten: Staged state Run git commit to synchronize the changes to the repository. At this time, the files in the repository and the local file become the same again, and the file state is Unmodify. Run git reset HEAD filename to cancel the temporary save and the file status is Modified

The following are introduced: ↓↓↓


  • First we created a file called abc.txt
  • usegit statusCheck the status of the abc. TXT file.The file names are shown in red, indicating files that are not managed by Git.

Staging area ~ submit

 Add files to the staging areaGit add file# submit
 git commit -m 'comments'fileGit commit
 # -m 'to add a comment to the file
 ​
 git status 
 # view file:
 # nothing to commit,working directory clean
 Git will tell you if a file has been modified after it has been submitted.
Copy the code

Modify the submitted file:

Change the add — commit file and view it in git statusGit does things to filesManagement tracking Need to once againgit add git commitUpdate the synchronization file ~You can see that the file state changes tomodified

Log, version, and comparison are different

 View the version logs of all files in the current path
 git log
  
 View the version log information of the specified file in the current path
 git logfile# View the log information of the file in one line: author, time, comment
 git log- pretty = oneline file# check version number informationGit reflog fileCopy the code

The logRow shows

Check the fileVersion number information

Version rollback and shuttle

The best part of Git is the maintenance and management of files and project versions. By default, the bottom layer listens to the submitted files and automatically saves the corresponding version number ~ You can switch between different versions ~ If a bug appears in the updated version of a project, you can directly return to the previous version for convenience. 😂

Git reset --hard HEAD~int # git reset --hard HEAD~int Shuttle to the specified versionCopy the code

Executing the command above will take you back to the previous version/version in the submitted project! Git is powerful!! No pictures here… Check the files of the rollback version. Best effect! .

Compare file differences/differences

 # Compare with the previous fileGit diffCopy the code

Undo file modification

 # undo file changes that are not added to the staging areaGit checkout file;# file status Modified -- > unmodify
 ​
 # undo file changes that have been added to the staging area but not committed
     # 1. Go back to the previous version of the library fileGit reset HEAD;# 2. Checkout file againGit checkout file;# undo committed file changes that have been added to the staging area
 Back to the previous versionGit reset - hard HEAD ^Delete file is a basic Linux command...Git rm -f fileGit status git status git status git status
 git commit -m 'delete document'fileCopy the code

Branch operation:

By default, Git is created with a branch named Master, and each member is assigned a branch.

  • Each branch can be understood as an independent operator and does not affect each other;
  • When Git switches branches, the current workspace will also become the workspace of the corresponding branch:
If branch A has ABC files ** switch ** if branch B has ABC files, it does not have ABC files. Branch B can pull /push/upload/merge files from branch ACopy the code
  • Multiple branches can create and modify a project, maintained by the master master ~
 Create a branchGit branch name (such as dev) // Create (dev) branch# view branch
 git branch
 ​
 # Switch branchesGit checkout branch name // Switch to xx branch# Pull branch pulls the target branch code to this branch;
 ​
 ​
 # push branch
 ​
 ​
 # merge branchesGit mergeCopy the code

Branch merge (no conflicts)

 # Suppose I now modify the file on the new branch
 # then go back to the main branch, I didn't change the main branch, only the new branch changed, so merge
 Git will update files in the master branch directly
Copy the code

Branch merge (conflicting)

 # if I modify the file on the new branch
 # then go back to the main branch, and I'm going to change the main branch, so merge
 Git will keep all the modified code in a special format.
Copy the code

Results:

Git formats it and puts it into merge mode: The only way to exit is to modify the file! And override commit Add COMMIT Note that the commit does not need to be attached to a file! Git commit -m Don’t write file names! Otherwise, you can’t exit the merge mode, this time just to commit ~

Delete the branch

Git branch -d Specifies the branch nameCopy the code

Git ignores file commits

Sometimes we don’t want to include files in version control, such as database files, temporary files, design files, etcIgnore files for version control!!

Ignore files with.gitignore

  • We need to create it in the root of the project.gitignorefile
  • The way to use the.gitignore file is simple. In the gitignore file, a line represents an ignore rule if it is a file with “. A string with a suffix that Git will ignore. “*” means to select all, if no “. It’s a folder.

.gitignore

 # for comments
 *.txt        # ignore all.txt files so upload will not be selected!! lib.txt# except lib.txt
 /temp        Only TODO files in the root directory of the project are ignored, excluding other directories temp
 build/       Ignore all files in the build/ directory
 doc/*.txt    # ignore doc/notes.txt but not doc/server/arch.txt
Copy the code