The use of the version controller Git.

Table of contents

1. Distributed version tools

1. Basic introduction

2. Git installation and configuration

3. Local warehouse and basic instructions

Two, branch

1. Branch overview

2. Resolving conflicts

3. Git remote warehouse 

1. Common hosting services [remote warehouse]  

2. Configure SSH public key

3. Operate the remote warehouse

4. Resolve merge conflicts

3. Using Git in IDEA

 1. Configure Git in Idea

 2. IDEA commonly used GIT operation entry

 3. Supplementary questions


1. Distributed version tools

1. Basic introduction

▶ Version control tools

1. Centralized version control tool
   Centralized version control tool. The version library is stored in the central server. Everyone in the team downloads the code from the central server when they work . It must be connected to the Internet to work, LAN or Internet. Individual modifications are then submitted to the central repository. Example: SVN and CVS
 2. Distributed version control tool
    There is no " central server " in the distributed version control system. Everyone's computer has a complete version library. When working in this way, there is no need to connect to the Internet, because the version library is on your own computer. Multi-person collaboration only needs to push their own modifications to each other, and they can see each other's modifications. Example: Git

▶ Git overview

  Git is distributed, Git does not need a central server, and each of our computers has the same things. We use Git and have a central server, just to facilitate the exchange of everyone's modifications, but the status of this server is the same as each of our PCs. We can think of it as a developer's pc, just for easy communication of codes without shutting down. Everyone can work without it, but the " exchange " modification is inconvenient.
   Git is an open source distributed version control system that can effectively and quickly handle version management from small to very large projects. Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.

2. Git installation and configuration

▶ Download and install

Download address: https://git-scm.com/download

 After the download is complete, you can get the following installation files:

Double-click the downloaded installation file to install Git . After the installation is complete, right-click on the computer desktop (or other directories). If you can see the following two menus, it means that Git is installed successfully.

 

Notice:
 ● Git GUI: Graphical interface tool provided by Git
 ● Git Bash: the command line tool provided by Git

▶ Basic configuration

1. Open Git Bash
2. Set user information
git confifig --global user.name "用户名"

git confifig --global user.email "邮件地址"
3. View the configuration letter
//查看用户名
git confifig --global user.name

//查看地址
git confifig --global user.email

▶  Configure aliases for common commands (optional)

Some commonly used commands have a lot of parameters, and a lot of parameters need to be input every time, so we can use aliases. (Configure first, will introduce in detail later)
1. Open the user directory and create a .bashrc file. Some windows systems do not allow users to create files starting with dots. You can open gitBash and execute touch ~/.bashrc

2. Enter the following content in the .bashrc file :

#用于输出git提交日志 
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit' 

#用于输出当前目录所有文件及基本信息 
alias ll='ls -al'

 3. Open gitBash and execute source ~/.bashrc

▶  Solve the problem of GitBash garbled characters

1. Open GitBash and execute the following command
git config --global core.quotepath false

2. Add the following two lines at the end of the ${git_home}/etc/bash.bashrc file

export LANG="zh_CN.UTF-8" 
export LC_ALL="zh_CN.UTF-8"

3. Local warehouse and basic instructions

▶ Get local warehouse

To use Git to version control our code, we first need to obtain a local warehouse
1 ) Create an empty directory (such as test ) anywhere on the computer as our local Git repository
2 ) Enter this directory, right-click to open the Git bash window
3 ) Execute the command git init
4 ) If the creation is successful, you can see the hidden .git directory under the folder .

▶  Basic operation instructions

There will be several states for file modification ( addition, deletion, update ) in the   Git working directory, and the state of these modifications will change as we execute Git commands.

 ●  Working area --> Temporary storage area: (This instruction will be introduced below)

git add
 ● Temporary storage area --> local warehouse: (this instruction will be introduced below)
git commit 

▶  View the status of the modification ( status )

 ● Role: view the status of the modification (temporary storage area, work area)
 ● Command format:
git status

▶  Add workspace to temporary storage area (add)

 ● Role: Add the modification of one or more files in the workspace to the temporary storage area
 ● Command format:
git add 单个文件名|通配符

 ● Add all changes to the staging area:

git add .

▶  Submit the temporary storage area to the local warehouse (commit)

 ● Role: Submit the contents of the temporary storage area to the current branch of the local warehouse
 ● Command format:
git commit -m '注释内容'

▶  View the submission log (log)

 ● Role:  View submission records
 ● Command format:
git log [option]

options:

  ▷ all : display all branches
  ▷ pretty=oneline : display commit information as one line
  ▷ abbrev-commit: make the output commitId shorter
  ▷ graph: display in the form of a graph
 ● The previously configured alias git - log contains these parameters  , the previously configured alias:
#用于输出git提交日志 
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'

  So use  git - log directly in the future (under the premise of configuring the alias).

▶  Version rollback

 ● Function: version switching (every time a submission is made locally, a version will be generated)
 ● Command format:
git reset --hard commitID

commitID: You can use the git - log or git log command to view

● How to view deleted records?
git reflflog

 This command can see the commit records that have been deleted

▶  Add files to ignore list

   Generally, we will always have some files that do not need to be included in Git 's management, and we don't want them to always appear in the list of untracked files. These are usually automatically generated files, such as log files, or temporary files created during compilation. In this case, we can create a file called .gitignore in the working directory (the file name is fixed), listing the file patterns to ignore. Here is an example:
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

Two, branch

1. Branch overview

  Almost all version control systems support branching in some form. Using branches means that you can separate your work from the main line of development to make major bug fixes and develop new features without affecting the main line of development.
▶ View local branch
  Order:
git branch

▶ Create a local branch

  Order:
git branch 分支名

▶ switch branch (checkout)

  Order:
git checkout 分支名

We can also switch directly to a branch that doesn't exist (create and switch)

  Order:
git checkout -b 分支名
▶ Merge branches (merge)
 Commits on one branch can be merged into another branch
  Command: is to merge other branches into the current branch
git merge 分支名称

▶ Delete branch: the current branch cannot be deleted, only other branches can be deleted

//删除分支时,需要做各种检查
git branch -d b1 
//不做任何检查,强制删除
git branch -D b1 

2. Resolving conflicts

  When the modification of files on two branches may conflict, for example, the same line of the same file is modified at the same time, then the conflict needs to be resolved manually. The conflict resolution steps are as follows:
 1. Deal with the conflicts in the file
 2. Add the conflict-resolved files to the temporary storage area (add)
 3. Submit to the warehouse (commit)
The content handling of the conflicting part is as follows:

▶  Principles and procedures for using branches during development
  Almost all version control systems support branching in some form. Using branches means that you can separate your work from the main line of development to make major bug fixes and develop new features without affecting the main line of development.
In development, there are generally the following principles and procedures for using branches:
  • master (production) branch : online branch, main branch, small and medium-scale projects as the branch corresponding to the application running online;
  • develop (development) branch : a branch created from master , generally used as the main development branch of the development department, if there are no other requirements for parallel development to go online in different periods , it can be developed in this version, after the stage development is completed, it needs to be merged into master branch , ready to go live.
  • feature/xxxx branch : The branch created from develop is generally developed in parallel at the same time, but the branch created when it goes online at different times, and merged into the develop branch after the R&D tasks on the branch are completed.
  • hotfifix/xxxx branch : a branch derived from master , generally used for online bug fixes, and needs to be merged into master , test , and develop branches after the fix is ​​completed.
  • There are also some other branches, such as test branch (for code testing), pre branch (pre-launch branch) and so on .

3. Git remote warehouse 

1. Common hosting services [ remote warehouse ] 

▶ hosting platform

  We already know that there are two types of warehouses in Git , namely local warehouses and remote warehouses. So how do we build a Git remote warehouse? We can use some code hosting services provided on the Internet to achieve this, among which GitHub , Code Cloud, GitLab , etc. are more commonly used .
  • gitHub (address: https://github.com/ ) is a hosting platform for open source and private software projects, because it only supports Git as the only repository format for hosting, hence the name gitHub
  • Code Cloud (address: https://gitee.com/ ) is a code hosting platform in China. Since the server is in China, Code Cloud is faster than GitHub .
  • GitLab (address: https://about.gitlab.com/ ) is an open source project for warehouse management systems, using Git as a code management tool, and web services built on this basis , generally used in enterprises , schools and other internal networks to build git private servers.

▶ Create a remote warehouse

 First go to the official website to log in and register an account, and then create a warehouse. The steps to create a warehouse are as follows:

After the warehouse is created, you can see the warehouse address, as shown in the following figure :

2. Configure SSH public key

▶ Generate SSH public key

 Gitee provides a Git service based on the SSH protocol. Before using the SSH protocol to access the warehouse, you need to configure the SSH public key of the account/warehouse.

 1. Open git Bash and enter the command line

ssh-keygen -t rsa
 2. Then press Enter three times. If the public key already exists, it will be overwritten automatically
 3. Enter the following command line to obtain the public key
cat ~/.ssh/id_rsa.pub

▶  Gitee sets account public key: copy and paste the obtained public key

 ▶  Verify whether the configuration is successful: enter the following command

ssh -T [email protected]

3. Operate the remote warehouse

▶  Add remote warehouse

  Order:
git remote add <远端名称> <仓库路径>

git remote add origin <仓库路径>

 ●  Remote name, the default is origin , it depends on the remote server settings

 ● Warehouse path, get this URL from the remote server, as follows:

  例如: git remote add origin [email protected]:czbk_zhang_meng/git_test.git

▶  View remote warehouse

  The command is as follows:
git remote

▶  Push to remote warehouse

  ● Command:
git push [-f] [--set-upstream] [远端名称 [本地分支名][:远端分支名] ]

  If the remote branch name is the same as the local branch name, you can only write the local branch

git push origin master

 ● -f   : Indicates mandatory overwriting

 ● --set - upstream  : Push to the remote and establish an association with the remote branch.
git push --set-upstream origin master

 ●  If the current branch has been associated with the remote branch , the branch name and remote name can be omitted.

//将master分支推送到已关联的远端分支。
git push 

▶  Relationship between local branches and remote branches

  ● To view the relationship we can use the command:
git branch -vv

▶  Clone from a remote repository

  If there is already a remote warehouse, we can directly clone to the local.
  Command: The local directory can be omitted, and a directory will be automatically generated
git clone <仓库路径> [本地目录]

▶  Grab and pull from remote repositories

  ▷ The remote branch is the same as the local branch, we can perform the merge operation, but we need to download all the updates in the remote warehouse to the local before proceeding.
  Grab command:
git fetch [remote name] [branch name]

The grab command is to grab all the updates in the warehouse locally without merging

 ▷ If you do not specify the remote name and branch name, fetch all and update the current branch.
Pull command:
git pull [remote name] [branch name]

The pull command is to pull the modification of the remote warehouse to the local and automatically merge it, which is equivalent to fetch+merge

4. Resolve merge conflicts

In a period of time, users A and B modify the same file and modify the code at the same line position, and a merge conflict will occur at this time. After user A modifies the code locally, it is first pushed to the remote warehouse. At this time, user B revises the code locally. After submitting it to the local warehouse, it also needs to be pushed to the remote warehouse. At this time, user B is later than user A , so the remote needs to be pulled first. The submission of the warehouse can only be pushed to the remote branch , as shown in the figure below.
When user B pulls the code, because users A and B have modified the code at the same location of the same file at the same time, a merge conflict will occur.
Remote branches are also branches, so the resolution of conflicts during merging is the same as resolving local branch conflicts .

3. Using Git in IDEA

 1. Configure Git in Idea

▶ configure

After installing IntelliJ IDEA , if Git is installed in the default path, idea will automatically find the location of git . If you change the installation location of Git , you need to manually configure the path of Git. Select File Settings to open the settings window and find the git option under Version Control :

▶  Operate Git in Idea

1. To create a project remote warehouse, refer to the previous creation method.

2. Initialize the local warehouse

 3. Set up a remote warehouse

4. Submit to the local warehouse

5. Push to the remote warehouse

6. Clone the remote warehouse to the local 

▶  Create a branch

 ●  The most conventional way

 ●  The most powerful way

▶  Switch branches and other branch-related operations

▶  resolve conflicts

1. When performing merge or pull operations, conflicts may occur
2. Join the staging area after the conflict is resolved
3. Submit to the local warehouse
4. Push to the remote warehouse

2. IDEA commonly used GIT operation entry

▶ Operation entry

1. The shortcut entry on the first picture can basically meet the needs of development.

2. The second picture is more entry to operate git in IDEA .

3. Supplementary questions

▶  A few iron orders

1. Submit local modifications before switching branches
2. The code is submitted in time, and it will not be lost after submission
3. Do not delete the file directory first if you encounter any problems, you must keep it first, otherwise the data will not be retrieved if it is gone.

▶  Unable to create .ignore|.bashrc files under windows

Here is an example of creating a .ignore file:
  ● Open gitbash in the git directory
  ● Execute the command touch .gitignore

▶  Hidden files ( .bashrc , .gitignore ) cannot be seen under windows

▶  IDEA integrates GitBash as Terminal

Guess you like

Origin blog.csdn.net/yzh2776680982/article/details/126995613