Application method and principle of git

command

#View configuration
git config -l
#View system configuration
git config --system --list
#View current user configuration
git config --global --list
#User ID
git config --global user.name "qqq"  #name
git config --global user.email 2@qq.com   #mailbox
# Create a Git code base in the current directory
$ git init
# Clone a project and its entire code history (version information)
$ git clone [url]
#View specified file status
git status [filename]
#View all file status
git status
#Add all files to staging area
git add . 
#Submit the contents of the staging area to the local warehouse - m: submit information
git commit -m "Message content"
#Upload to public warehouse
git push
#Generate public key
 get into C:\Users\Administrator\.ssh catalogue
 Generate public key
$ ssh-keygen
# List all local branches
git branch
# List all remote branches
git branch -r
# Create a new branch, but still stay in the current branch
git branch [branch-name]
# Create a new branch and switch to it
git checkout -b [branch]
# Merge the specified branch to the current branch
$ git merge [branch]
# Delete branch
$ git branch -d [branch-name]
# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

version control

Version control: it is convenient to view the change history of files and back up the software engineering technology of previous versions.

Common version control tools

  • Git
  • SVN(Subversion)
  • CVS(Concurrent Versions System)
  • VSS(Micorosoft Visual SourceSafe)
  • TFS(Team Foundation Server)
  • Visual Studio Online

Version control classification

**1. * * local version control

For each update of the record file, you can take a snapshot of each version, which is suitable for personal use.

**2. * * centralized version control SVN

All version data is saved on the server, and collaborative developers can update or upload their own modifications synchronously from the server

**3. * * distributed version control Git

All version information warehouses are synchronized to each local user (to ensure that data will not be lost). github is used as a transit station, and users upload and download the latest data through github

The difference between git and SVN

SVN is a centralized version control system. The version library is centrally placed on the central server. When working, you use your own computer, so you must first get the latest version from the central server, and then work. After completing the work, you need to push your work to the central server. The centralized version control system must be networked to work, and requires high network bandwidth.

Git is A distributed version control system without A central server. Everyone's computer is A complete version library. There is no need to connect to the Internet when working, because the versions are on their own computers. The method of collaboration is as follows: for example, if you change file A on the computer, others also change file A on the computer. At this time, you two only need to push your changes to each other, so that you can see each other's changes. Git can directly see which code and files have been updated!

Git is the most advanced distributed version control system in the world.

Git environment configuration

Download and install

Baidu search git, enter the official website and download directly

During installation, all options are available by default

Start git

After successful installation, there will be Git item in the start menu and 3 programs under the menu

Git Bash: Unix and Linux style command line, most used and recommended

Git CMD: Windows style command line

Git GUI: git with graphical interface. It is not recommended for beginners. Try to be familiar with common commands first

Git configuration

#View configuration
git config -l
#View system configuration
git config --system --list
#View current user configuration
git config --global --list

Git related configuration files:

1) . Git\etc\gitconfig: gitconfig under Git installation directory - system configuration file

2),C:\Users\Administrator\ .gitconfig -- only applicable to the configuration of the currently logged in user

Set user name and mailbox

(user ID, if necessary)

When you install Git, the first thing to do is to set your user name and e-mail address. This is very important because this information is used every time Git commits. It is forever embedded in your submission:

git config --global user.name "kuangshen"  #name
git config --global user.email 24736743@qq.com   #mailbox

Git Fundamentals

Work area

Git has three local work areas: Working Directory, stage / index, and Repository or git directory. If you add a remote git Repository (Remote Directory) in the, it can be divided into four work areas. The conversion relationship between these four areas is as follows:

  • Workspace: workspace is where you usually store project code
  • Index / Stage: the temporary storage area is used to temporarily store your changes. In fact, it is just a file to save the information to be submitted to the file list
  • Repository: the warehouse area (or local warehouse) is a safe place to store data, which contains the data submitted to all versions. The HEAD refers to the latest version put into the warehouse
  • Remote: remote warehouse, a server hosting code, can be simply regarded as a computer in your project team for remote data exchange

Workflow

The workflow of git is generally as follows:

1. Add and modify files in the working directory;

2. Put the files requiring version management into the temporary storage area;

3. Submit the files in the staging area to the git warehouse.

Therefore, git manages files in three states: modified, staged, and committed

Git project construction

There are two ways to create a local warehouse: one is to create a new warehouse, and the other is to clone a remote warehouse.

Local warehouse construction

1. To create a new warehouse, you need to use the root directory of the project managed by GIT:

# Create a Git code base in the current directory
$ git init

Clone remote warehouse

1. Another way is to clone the remote directory, because the warehouse on the remote server is completely mirrored to the local!

# Clone a project and its entire code history (version information)
$ git clone [url]

Git file operation

View file status

You can view the status of the file through the following command:

#View specified file status
git status [filename]
#View all file status
git status
#Add all files to staging area
git add . 
#Submit the contents of the staging area to the local warehouse - m: submit information
git commit -m "Message content"

Ignore file

Sometimes we don't want to include some files into version control, such as database files, temporary files, design files, etc

Create a ". gitignore" file in the home directory. This file has the following rules:

  1. Ignore empty lines in the file or lines starting with a pound (#) are ignored.
  2. You can use Linux wildcards. For example, an asterisk (*) represents any number of characters, a question mark (?) represents a character, square brackets ([abc]) represent optional character ranges, and braces ({string1,string2,...}) represent optional strings.
  3. If the name is preceded by an exclamation point (!), it indicates an exception rule and will not be ignored.
  4. If the name is preceded by a path separator (/), it indicates that the files to be ignored are in this directory, and the files in the subdirectory are not ignored.
  5. If the last side of the name is a path separator (/), it means that the subdirectory of the name in this directory, rather than the file, should be ignored (both the default file and directory are ignored).
*.txt        #Ignore all txt, so the upload will not be selected!
!lib.txt     #But lib Except 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    #Doc / notes. Is ignored Txt but not Doc / server / arch txt

Use code cloud

1. Set the local machine to bind SSH public key to realize password free login! (password free login is an important step. The code cloud is a remote warehouse. We usually work in a local warehouse!)

# Enter C: \ users \ administrator \ SSH directory
# Generate public key
$ ssh-keygen

2. Add the public key information public key to the code cloud account!

GIT branch

Branching is relatively difficult in GIT. Branching is the parallel universe in science fiction movies. If the two parallel universes do not interfere with each other, it will have no impact on you now. However, at a certain point in time, two parallel universes merge, and we need to deal with some problems!


Common instructions in git branch:

# List all local branches
git branch
# List all remote branches
git branch -r
# Create a new branch, but still stay in the current branch
git branch [branch-name]
# Create a new branch and switch to it
git checkout -b [branch]
# Merge the specified branch to the current branch
$ git merge [branch]
# Delete branch
$ git branch -d [branch-name]
# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

If the same file is modified when merging branches, it will cause conflicts: the solution is that we can modify the conflicting file and resubmit it! Choose whether to keep his code or your code!

The main branch of the master should be very stable and used to release new versions. Generally, it is not allowed to work on it. Generally, work on the new dev branch. After work, for example, it should be released on the, or after the dev branch code is stable, it can be merged into the main branch master.

Keywords: git

Added by John Canyon on Sun, 02 Jan 2022 08:03:47 +0200