Git installation & usage (version control)

1. Introduction to Git (version control)

The development of version control systems can be divided into three stages:

  • local version control system
  • Centralized version control system
  • Distributed version control system
  1. What is version control?
    You can think of a version control system (abbreviated as VCS) as a "database" that can help you save a complete snapshot of a project when needed. When you need to view a previous snapshot (called a "version"), the version control system can display the details of all changes between the current version and the previous version.

What is "version control"? Version control is a system that records changes in the content of one or several files so that you can check the revision status of a specific version in the future . In GIT CODE, we perform version control on files that store software source code, but in fact, you can perform version control on any type of file. Benefits of using version control systems in team development

  1. Version Management
    Avoid version management confusion. This is the main reason for using version management and the purpose of version management. You definitely don’t want to manually back up multiple copies locally, but in the end you don’t know which backup is the latest, what modifications were made to that backup, when was the modification date, etc. You can’t remember all the problems. Version management software can solve these problems. It has detailed logs, can remember every submission and every change you make, and can compare and view the similarities and differences between different versions, and can restore to any previous version.

  2. To improve code quality
    , before there was version management, it was often necessary to write irrelevant comments in the code, for example: someone modified a certain code on a certain day; or some code that was not sure whether to be used was retained in the form of comments, etc. Wait, these are zombie codes. Now these tasks can be done by version management tools, and these irrelevant zombie codes can be deleted from the code.
    4. Improve the efficiency of collaborative and multi-person development.
    Submitting updated code in a timely manner allows team members to learn about the latest status of the code and avoid duplication of work.
    5. Clarify the division of responsibilities.
    When and who modified the code and what content was modified, version management will record it for easy query and accountability.
    6. In addition to code, many documents and personal data, such as resumes, etc., can be versioned, which is interesting and efficient. All documents that need to be continuously modified can be versioned.
    Version control tools:
    Let’s introduce the currently common version control tools.

  3. Git

Git is currently the most advanced distributed version control system in the world. Using Git and Gitlab to build a version control environment is now the most popular version control method in Internet companies. Introduction: Git is
a
free open source distributed version control system designed to be fast and efficient. Handle everything from small to large projects efficiently.
Git is easy to learn, has a small memory footprint, and has lightning-fast performance.
Github address: https://github.com/git/git
official website: https://git-scm.com/official
documentation: https://git-scm.com/docs
Chinese documentation: https://git- scm.com/book/zh/v2
download address: https://git-scm.com/downloads

  1. SVN

Introduction:
TortoiseSVN is a very easy-to-use cross-platform version control/version control/source code control software. It is based on Apache Subversion (SVN)®; TortoiseSVN provides a simple and easy-to-use user interface for Subversion.
TortoiseSVN open source address: https://osdn.net/projects/tortoisesvn/
Subversion’s Github address: https://github.com/apache/subversion
Official website: https://tortoisesvn.net/
Official documentation: https:// tortoisesvn.net/docs/release/TortoiseSVN_en/index.html
Chinese document: https://tortoisesvn.net/docs/release/TortoiseSVN_zh_CN/index.html
Download address: https://tortoisesvn.net/downloads.html

  1. HG

Introduction:
Mercurial is a free distributed source code management tool. It can efficiently handle projects of any size and offers a simple and intuitive interface. Mercurial is a lightweight distributed version control system implemented in Python language, which is easy to learn and use and has strong scalability.
Official website: https://www.mercurial-scm.org/Official
documents: https://www.mercurial-scm.org/wiki/BeginnersGuides
Download address: https://www.mercurial-scm.org/downloads

  1. CVS

CVS is a version control system and an important part of Source Configuration Management (SCM). Using it, you can record the history of source files and documents. The old version control system is based on client/server behavior so that it can accommodate multiple users and it is also very convenient to form a network. This feature makes CVS the first choice when people in different locations work on data files (especially the source code of a program) at the same time.
Official website: http://www.nongnu.org/cvs/Official
document: https://web.archive.org/web/20140629065005/http://ximbiot.com/cvs/manual/cvs-1.11.23/ cvs.html
download address: https://ftp.gnu.org/non-gnu/cvs/

Advantage:

  1. Easier management. Traditional version control systems use centralized repositories, and some repository-related management can only be performed by the administrator alone. Due to the distributed model, there is no such problem in Mercurial. Each user manages his or her own repository, and the administrator only needs to coordinate and synchronize these repositories.
  2. More robust system. Distributed systems are more robust than centralized single-server systems. Once a server problem occurs in a single-server system, the entire system cannot run. Distributed systems are usually not affected by one or two nodes.
  3. Less dependence on the network. Because synchronization can occur at any time, Mercurial can even be managed offline and only needs to be synchronized when there is an Internet connection.

Visual client for version control tools

  1. The git kraken

Easily use repositories hosted on GitHub, GitLab, Bitbucket, Azure DevOps (VSTS). GitKraken is all about cloning, adding branches, and creating/opening pull requests.
Official website: https://www.gitkraken.com/

Features:

  1. Intuitive UI
  2. Built-in code editor: Initialize a brand new project and use the built-in code editor to add new files and folders and edit them directly in GitKraken. Save files, stage and commit changes. No more context switches!
  3. Task Tracking: Connect Glo Board to a repository in GitKraken. As you make progress on a task, you can quickly update your code on issues and get back to coding. 2.Tower

Official website: https://www.git-tower.com

Advantages of Tower

  1. Ease of use: No need to remember complex commands (and their parameters). Tower makes many advanced operations as easy as drag and drop.

  2. Undo everything: including undoing local changes, restoring old versions, reverting commits, restoring deleted commits, and undoing branch deletions

  3. Clear visualization, better understanding: Tower's clear visual interface is incredibly useful - and makes complex scenes easier to understand.

  4. Use Git elegantly: Resolve merge conflicts in a simple, intuitive way, no SSH keys, usernames, passwords, and authentication tokens required for verification

  5. Sourcetree

Introduction: Sourcetree, a free Git client for Windows and Mac,
simplifies the way you interact with Git repositories. Visualize and manage repositories through Sourcetree's simple Git GUI. Official
website: https://www.sourcetreeapp.com/

2. GIT installation and command installation

1. Installation

Since I mainly develop under windows, I will only talk about the installation of git under windows (git installation under Linux, Unix and Mac are not listed here).

1) Download from the official website: Git - Downloads
2) Double-click to run the exe and install it by default (you can also modify the installation path);
after the installation is completed, you can see it with the right mouse button, and there are several more options:
Insert image description here
Git GUI Here This is mainly for git. Graphics window (generally not used, TortoiseGit will be installed for use); Git Bash Here is mainly the command line, as follows
Insert image description here

Indicates that git installation is successful

2. Work flow

1) Clone git resources from the remote warehouse as the local warehouse;

2) Checkout the code from the local warehouse and modify it;

3) Submit the code add to the temporary storage area before submitting the code;

4) commit to submit changes (submit to the local warehouse and save each historical version of the changes);

5) After the modification is completed and the code needs to be shared, push it to the remote warehouse.

As shown below:
Insert image description here

Workspace: The directory that can be seen locally is a workspace

Staging area: In the index file (.git/index) in the .git directory, also called the index

3. Git management file version

3.1 Create a repository
A repository can be simply understood as a directory. All files in this directory can be managed by Git. Git can track the modification and deletion of each file and its history.

There is a .git folder in the git warehouse. This folder is hidden. If the hidden folder is not set to be displayed, the user cannot see the folder. The user can create the .git folder directly through the command line git init. Create or right-click "TortoiseGit creates repository here" (do not check "Create pure repository (no working directory)" in the pop-up window. If checked, the created repository will not have a working directory, that is, files cannot be added. to the local repository), the directory containing the .git folder is called the working directory.

Git will automatically create a master branch and execute a pointer HEAD of the master.

4. Order

1)init

——Initialize the git repository

git init //Initialize the git warehouse
git init project //Initialize the git warehouse under the project directory
2) add

——Add files to version control

git add readme.txt //Submit readme.txt to the warehouse
3) clone

——Copy the project from the existing git repository

git clone https//test.git test //Copy the contents of the test.git warehouse to the test directory
4) config

——Set command

git config --list //Display the current git configuration information
git config -e //Set git config -e for the current warehouse
git config -e --global //For all warehouses on the system
5) status

——View the current status of the warehouse and display the changed files

git status
git status -s //Get brief content
6) diff

——Compare the differences between files, the differences between the temporary storage area and the work area

git diff [filename] //Changes that have not been cached
git diff --cached [filename] //View cached changes
git diff --staged [filename]
git diff HEAD //View all cached and uncached changes
git diff --stat //Show summary instead of entire diff
git diff [first-branch] [second-branch] //Show the difference between 2 commits
7) commit

——Submit the staging area to the local warehouse

git commit -m [message] //Submit
git commit [file1] ... [fileN] //Submit the specified file to the warehouse area
git commit -a //No need to execute git add, submit directly
8) reset

——Return to version, you can specify to return to a certain submitted version

git reset [–soft | --mixed | --hard] [HEAD]
–mixed is the default and can be used without this parameter. It is used to reset the files in the staging area to be consistent with the last submission, and the contents of the workspace files remain the same. constant.

git reset HEAD~1 //Roll back all content to the previous version
git reset HEAD readme.txt //Roll back the version of the readme.txt file to the previous version
git reset xxx //Roll back to the specified version
–soft parameter is used Roll back to a version

git reset --soft HEAD~3 //Go back to the first 3 versions
– the hard parameter undoes all uncommitted modifications in the workspace, returns both the staging area and the workspace to the previous version, and deletes all previous ones Information submission: (Note: All information before the rollback point will be deleted)

git reset --hard HEAD~3 //Return to the first 3 versions
git reset --hard xxxx //Return to all information before the rollback point of a certain version
git reset --hard origin/master //Restore local The status falls back to the same as the remote one
9) rm

——Delete workspace files

git rm [file] //Delete files from the staging area and workspace
git rm -f [file] //To delete previously modified files that have been placed in the staging area, you need to use the forced deletion option -f
git rm -- cached [file] //Remove the file from the temporary storage area and keep it in the current working directory
git rm -r * //Recursively delete, delete all files and subdirectories in the directory
10) mv

——Move or rename workspace files

git mv [file] [newfile] //Move or rename a file, directory or soft link
git mv -f [file] [newfile] //newfile exists and needs to be forced to execute
11) log

——View historical submission records

git log //View historical commit records
git log --online //View a concise version of historical records
git log --graph //View the time when branches and merges occurred in history
git log --reverse //Display all logs in reverse
git log --author //View the log submitted by the specified user
git log --before(–since)={3.week.ago} --after(–until)={2021-07-25} //View 3 weeks ago and All commit logs after 2021-07-25
git log --no-merges //Hide merge commits
12) blame

——View the modification record of the specified file, displayed in list form

git blame [file]
13)remote

——Remote warehouse operation

git remote -v //Display all remote repositories
git remote show [remote] //Display information about a remote repository
git remote add [shortname] [url] //Add remote repository
git remote rm name //Delete remote repository
git remote rename old_name new_name //Modify the warehouse name.
If the transmission between the local git warehouse and github is encrypted through SSH, you need to configure verification information:

First generate SSH Key;

Generate the .ssh folder under ~/, open id_rsa.pub, and copy the key value;

14)fetch

——Get the code base from the remote. After executing this command, you need to execute git merge from the remote branch to the branch where it is located.

git merge //Fetch data from the remote warehouse and try to merge into the current branch
git fetch [alias] //Fetch updated data
git merge [alias]/[branch] //Merge any updates from the remote warehouse into the current branch
15) pull

——Download the remote code and merge it, which is the abbreviation of git fetch and git merge FETCH_HEAD.

git pull [remote host name] [remote branch name]:[local branch name] //Pull the remote host branch and merge it with the local branch. If it is the current local branch, it can be omitted
.

16)push

——Upload remote code and merge

git push [remote host name] [local branch name]: [remote branch name]
If the local branch name is the same as the remote branch name, the remote branch name can be omitted.

git push --force [remote host name] [local branch name]:[remote branch name] //There is a difference between the local version and the remote version, force push
git push [remote host name] --delete [remote branch name] // Delete the branch corresponding to the host
17) Branch management

git branch [branchname] //Create branch
git checkout [branchname] //Switch branch
git branch //List branches
git branch -d [branchname] //Delete branch
git merge [branchname] //Merge branchname to boil branch
18) tag

——Tag, you can delay labeling

git tag -a xxx [xxxx] //Create annotated tags
git tag -a [tagname] -m "xxx" //Specify tag information
git tag -s [tagname] -m "xxx" //PGP signature tag

The purpose of GitHubDaily is to push a batch of excellent open source projects on GitHub to developers from time to time every day, to help developers discover the hottest open source projects at the moment, so that developers can control the pulse of technology and expand their technical horizons. , and gain technical ability improvements from learning from open source projects. Portal: github.com/GitHubDaily/GitHubDaily

3. Use

4. Version control

5. Branch Management

6. Git tags

7. Minimalist Git + VsCode (Windows)

Guess you like

Origin blog.csdn.net/SDXYGZH/article/details/126541769