In the process of software development is an important part of the output code, software coding process is usually performed by a team, it is a parallel activity, in order to ensure that the code in the multiplayer development would be able to complete, we need to use the code for unified storage code version control tool, and track the history of each code in order to trace changes of code, Other version control tools also provide advanced features such as conflict merging to coordinate multiple people making changes to the same code file. Therefore, version control tools can be regarded as the foundation of the entire coding effort. Without version control to unify the code base, it is difficult to complete complex and large number of projects, and without a uniform code base, such as continuous integration, publishing, and so on will not be able to carry out.

This article will introduce the version control tool and Git usage from the following aspects:

  • Introduction to common version control tools

  • Use Git to manage source code

    • Introduction of Git
    • Install Git
    • Create a local repository
  • Git common operations

    • Document tracking, modification and submission
    • File comparison
    • View file status and history
    • Undo operation
    • The label
    • The alias
  • Commit the local repository to remote

  • summary

Introduction to common version control tools

CVS:

Concurrent Versions System (CVS) is an open source version control System. Based on C/S mode, CVS stores code on the server and obtains and submits code and other operations through the client. SVN: SVN(Subversion), like CVS, is an open source version control system in C/S mode. TFS/VSTS: TFS(Team Foundation Server)/VSTS(Visual Studio Team Services) is a software development tool for Team collaboration launched by Microsoft. Version control is only one of the functions. In addition, it also provides agile development support, continuous integration and other advanced functions. TFS has the same functionality as VSTS, which can be considered the cloud version of TFS. TFS is licensed by server, VSTS installation account, and VSTS offers a free trial version (note: VSTS supports Git as a version control tool). Git: Git is a distributed version control tool. Compared with other version control tools, Git has some unique characteristics such as: distributed/decentralized (each client keeps a complete code repository), strong branching ability, very fast (because the code base is basically local operation). Because of its characteristics, Git is very suitable for code management of open source projects, so it is widely used.

Use Git to manage source code

Introduction of Git

Git is a distributed version control tool based on file snapshot. For file snapshot, it makes a snapshot of all files when submitting an update. In the snapshot, the same files are linked to the previously stored files:

  

Each Version in the figure is a snapshot of all the files, with the dotted boxes representing unchanged files, linked to previous files. And distributed can be understood as a decentralized, there is no specific server, each node has all of the content, and each Git installed computer can be seen as a node, so Git almost all of the operations are carried out on the local (can be created in the local warehouse, add/modify/submit files, etc.). Git has three important workspaces for files:

  • Git repository: Stores all Commited files submitted by Git. It is equivalent to a file database managed by a server in a centralized version management tool. The data repository files are located in the. Git directory
  • Working directory: The working directory is the area used for development. The content of the working directory is checked out from the data warehouse for modification. After modification, the modified content can be submitted to the staging area.
  • Temporary storage area: The temporary storage area stores the files submitted by the working area. “Temporary storage” means that it only temporarily stores the files that need to be submitted to the data warehouse after modification by users. Finally, the temporary storage content can be submitted to the data warehouse at a time.

They work as follows (think of them as normal file directories, and their operations as files between directories to understand how Git works, where the Git repository is special and saves every copy (commit) content, while the other two directories will overwrite the same content) :

    

Install Git

When it comes to Git, some people may wonder why Git can be used in Visual Studio, and GitHub also provides its own client, but there are such hosting platforms as Code Cloud (Gitee) in China, so what is the tool in the end? How do YOU use it? Git has a variety of manifestations, one is the native command line tool, other tools such as VS, GitHub client are actually the command encapsulation to simplify the user operation GUI tools, in other words, no matter how the tool of Git changes, its core is the native command line tool. It also shows that as long as you have a command line tool, you can use different Git-based code hosting platforms such as GitHub and Gitee. Install the Git command line tool on Windows: git-scm.com/download/wi… Install Git: Select components:

  

Note: If you check GitBash Here and Git GUI Here, you can open GitBash in the current directory by right-clicking it in the window explorer. You can also configure the git CMD directory into an environment variable using the Windows command line tool to execute git commands) or a simple built-in GUI program in the new version:

  

Installation and running results:

  

Git Bash and Windows CMD Note: Since the new version of VS will install the built-in Git tool, if the following information is displayed, check whether there is an environment variable such as “Microsoft Visual Studio 14.0\Web\External\ Git”, delete it:

   

Create a local repository

This article uses.net Core-based My Blog application code as an example. The directory structure of My Blog is as follows:

  

The SRC directory contains all the code:

  

1. Open Git Bash in My Blog root directory and create a local repository with Git init:

  

At the same time, a. Git hidden directory will be generated under this directory. Name “Selim” git config user.email “[email protected]

  

Note: The user information here is only valid for the current warehouse. To configure global user information, add the –global option: git config –global user.name “Selim” git config –global user.email “[email protected]

3. Finally add ignore file configuration to the repository (only manage code, files such as compilation results should be filtered) :

  

Source: github.com/github/giti… Note: Windows can not directly create a. Gitignore file, you can use the editor to save the file name, and the gitignore file format can refer to the document: git-scm.com/docs/gitign… .

4. Add all files to the staging area:

  

Change the level of change (for example, change the level of change at which the user interacts) by using git config — global core. Change the level of change (for example, change the level of change at which the user interacts) Git status:

  

You can see that all the necessary files have been added to the staging area. Git commit -m “create a repository”

  

7. Check log :(git log)

  

8. When VS2017 is used to open the solution, the team Explorer will automatically recognize the local Git repository:

  

Git common operations for repositories

Now that you’ve created your repository, what are some common Git operations?

Document tracking, modification and submission

Adding a new file to Git involves the following operations:

  • Keep track of new files :(git add filename or git add -a which adds all files except ignored)

  

  • Git commit -m

  

  • File modification: If a file has been committed and is modified again, you still need to use git add or use git commit -a to skip the staging area. Git commit -a commits only the modified files that are already managed by Git.

  

Skip staging commit:

  

  • Manually delete workspace files (that is, directly delete files in the directory) :

   

  • To delete files from git :(git rm 1.md or git rm 1.md -force)

  

File comparison

  • Git diff:

  

  • Compare staging changes :(git diff –cached or git diff –staged)

  

Note: the results will only be available if there is content in the temporary storage area.

  • Use the built-in vimdiff comparator to view changes :(git difftool)

  

Windows can use the winmerge to replace the default compare/merge tool: winmerge download address: winmerge.org/ configuration method reference: blog.csdn.net/guoxinian/a… Stackoverflow.com/questions/1…

View file status and history

  • Git status:

  

  • Check the commit history:

  

  • Git status -s

  

The status consists of two characters. The first character represents the staging state, the second character represents the workspace state, and the second character represents the staging state. Represents files that Git is not tracking.

Undo operation

  • Undo workspace changes :(git checkout — readme.md)

  

  • Git reset HEAD readme.md

  

  • Commit supplement (multiple commits combined into one subsequent commit overwrite the previous one) :(git commit –amend)

  

The file READ. Md has been modified and committed twice, but only the last commit is seen in the log:

  

The label

  • Git tag -a v0.1 -m “version 0.1”

   

  • Git tag-a v0.01-m “Created Repository”

  

  • Git push Origin v0.1

  

Tags on GitHub:

  

  • Git checkout-b version0 v0.01

  

Note: This command creates a new branch version0 and gets the v0.01tag code, which is the original version of the repository.

The alias

Git allows you to set command aliases to simplify commands:

  

The aliases in the figure above simplify commands like checkout to co, br, ci, st.

For more commands and how to use them, see git-scm.com/docs git-scm.com/book/en/v2

Commit the local repository to remote

The local Git repository has implemented code version control, but the local repository does not allow for team cooperation, and the code base is not distributed locally. So you can submit your local code repository to a remote hosting platform like GitHub, Gitee, and so on. To create a new repository on GitHub (note: you need to register a GitHub account first) :

   

GitHub will give you tips on how to use the library, such as the process for creating a new library, the process for importing existing libraries, and the process for importing code:

   

Here we need a second method:

  

Note: If you push your code to Github through Git for the first time, you will get the following login prompt:

  

 

   

Git credential Manager provides the git credential Manager function to record the login information. For details, see github.com/Microsoft/G… This will enable the code to be successfully submitted to GitHub:

  

More on using GitHub hosted code to follow.

summary

This article mainly introduced the commonly used code version control tool, version control tools can be divided into centralized and distributed, including centralized commonly used have SVN and distributed have Git, along with the development of the software development way of Git more accord with modern concepts such as agile, remote collaboration, so this article chose the Git as code version control tool. In addition, this paper mainly introduces the use of Git command line tools and uses GitHub as a remote code hosting platform for code hosting. There are many GUI tools in Git, but all GUI tools are based on the principle of the Git command encapsulation, and then provide some convenient functions, so it is very necessary to understand Git commands. Once you understand Git’s basic commands, you’ll feel more comfortable using GUI tools. Git commands have a lot of options, so you can refer to Git documentation or books for more information: git-scm.com/docs git-scm.com/book/en/v2 The next article will introduce Git’s core feature branch and pull request.

Reference: git-scm.com/docs git-scm.com/book/en/v2 github.com/Microsoft/G… Blog.csdn.net/guoxinian/a… Stackoverflow.com/questions/1… Github.com/github/giti…

This paper links: www.cnblogs.com/selimsong/p…

Good code is tube out — talk about it. Net Core code management approach and implementation