This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

| author: jiangxia

| CSDN:blog.csdn.net/qq_41153943

| the nuggets: juejin. Cn/user / 651387…

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

This article is about 6,309 words and takes 21 minutes to read

Bs part

Why write this article? Some time ago, the company needed to use Git for a new project. The leader asked me if I would use Git. I said no, and was severely despised by the leader. It’s true that a lot of companies are using Git for version control, and if you can’t use Git or even know it, you’re going to be ridiculed in the company. Therefore, I made up for the knowledge related to Git, and recorded some relevant knowledge, hoping to help people who need it like me, so as not to be despised by others.

The body part

First, the concept

Before introducing Git, let’s talk about the concept of version control.

1, 1 What is version control

I don’t know if you remember writing big papers when you were in school, but we have a folder on our computer with big papers 1, big papers 2, big papers 3, big papers 4…….. . Sometimes you need to open all the files to find a version of a paper, which is very troublesome because version control is not used.

Revision control is a software engineering technique used during development to manage the history of changes made to files, directories, or projects. It can be easily viewed and backed 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

It is easy to think of version control as a technique for managing collaborative development projects. Just like the aforementioned write big paper, if there is no version control or lack of the right process management, version control itself can introduce a lot of problems in the software development process, such as the consistency of software code, software redundancy, software process the content of the sex, the concurrency in the process of software development, software source code security, and software integration, etc.

At present, the mainstream version control software is as follows:

Git

SVN (Subversion)

CVS (Concurrent Versions System)

VSS (Micorosoft Visual SourceSafe)

TFS (Team Foundation Server)

Git and SVN are the most influential and widely used.

SVN is a centralized version control system, while Git is a distributed version control system.

What is Git

With version control out of the way, Git is the focus of this article.

Git is the most advanced distributed version control system in the world, and it is very efficient and superior in handling various projects.

The SVN is a centralized version control system, and the version library is centralized on the central server. When the SVN works, all users use their own computers. Therefore, they need to obtain the latest version from the central server and then work. For example, we have A group to write an article, but there is only one notebook, so A needs to finish writing and update his content to the notebook, then B can start to write, B finished and then update the content to the notebook, then C can write…. It is conceivable that this is more troublesome. And centralized version control systems must be networked to work.

Git, on the other hand, is a distributed version control system. There is no central server, and everyone’s computer is a complete version library. For example, if the article is distributed, everyone can write directly, and it will be merged automatically when submitting. Therefore, Git has certain advantages over SVN.

3. How do I install Git

Install Git on Linux:

First, you can type git on the command line to see if your system has git installed:

$ git
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git
Copy the code

On Linux, you can install Git with the following command:

sudo apt-get install git
Copy the code

Windows installation:

Go to the Git official website to download the Installation program for Windows and use the default installation options.

After the installation is complete, open Git bash software, and a command-line window similar to CMD is displayed, indicating that the installation is successful.

Once the installation is complete and you need to set it up, type the following code on the command line to set your name and Email address:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
Copy the code

You can also use the following command to view the user name and password:

$ git config user.name
$ git config user.email
Copy the code

If no errors are reported, git is successfully installed. Git config -l

Git config --local --list git config --local --listCopy the code

Two, the operation part

Now that we have introduced some basic concepts related to Git, we will start to use Git in detail.

Git local has three Working areas: Working Directory, Stage/Index, Repository or Git Directory.

If you add a Remote Directory to your Git repository, you can divide it into four working areas.

Workspace: That’s where we usually store the project code

Index/Stage: staging area, used to temporarily store your changes, is actually just a file that holds the list of files to be submitted

Repository: A secure place to store data that you have submitted to all versions of. Where HEAD points to the latest version put into the repository

Remote: a server that hosts code. It can simply be considered a computer in a project team for Remote data exchange, or github as we often use it.

The relationship between the four work areas looks like this (from the web) :

So git works like this:

1. Add and modify files in the workspace;

2. Put the files that need to be versioned into the temporary storage area;

3. Commit the files in the staging area to git repository. Thus, Git manages files in three states: Modified, staged, and Committed. After introducing the above content, we will start to practice Git operation. Only by doing it can we deepen our understanding and memory.

2. 1 Create a version library

To create a repository, use the following command to create an empty directory in a local location:

$ mkdir gitspace
Copy the code

Then go to the directory and run the following command to make it a Git repository:

$ git init
Copy the code

Git is the repository that git uses to keep track of and manage repositories.

Note: some people may not be able to see the.git file, that may be because the file is a hidden file, so you need to set the hidden file to show.

Create a new text file in this directory and type something like this:

Git add and git commit

Then use the git add command to add the text file to the repository:

git add demo1.txt
Copy the code

Commit to repository with git commit

Git commit -m "Commit demo1.txt to repository"Copy the code

Note: the input after -m is the description of this submission, that is, the annotation, you can input freely. Generally, the development is to annotate the content and description of this modification, etc. At this time, we have submitted a version of the file to the warehouse, so if we need to modify the content of the file at this time.

To check the current status of the warehouse, run the following command:

git status
Copy the code

There are currently no changes to commit, and the working directory is clean. Then let’s modify the demo1.txt file:

Let’s take a look at the state of the warehouse:

2, 3 Version information

At this point, you can see that Demo1 has been modified, but the changes have not been committed. You can also use git diff to view the changes:

git diff demo1.txt
Copy the code

We then commit the modified file using git add and Git commit above.

At this point, we have committed two versions of the file to the repository, you can use the git log command to view the history information:

git log
Copy the code

The user’s information is displayed: name and email, and when and what the modification was made, with the earliest submission recorded below.

Version 2 and version 4 are rolled back

What if the hello World content we just added is wrong? Then you need to roll back the file to the previous version, using the git reset command.

git reset --hard HEAD^
Copy the code

The version has been rolled back to the previous version (the version that committed Demo1 to the repository). Note: HEAD indicates the current version, HEAD^ indicates the previous version, so the previous version is HEAD^^.

Run the cat command to check that the file has been rolled back to the previous version.

What if we later discover that the hello Word we just added is the right one, and we want to go back to that version? You can add it directly to the text and then submit it. Of course, that’s not the idea here. You can still use the git reset command to roll back to the latest version. The command used here is:

git reset --hard commit_id
Copy the code

Commit_id is mandatory because the latest version has been deleted before the return, and commitid can be obtained by using the git log command mentioned above with –pretty=oneline:

git log --pretty=oneline
Copy the code

So rolling back to the latest version is:

git reset --hard 75b036995f2ba5a6ebabaad22ce0a3d916c1b393
Copy the code

What if there is a file in the repository that we don’t want? Create a new demo2.txt file and commit it to your Git repository.

You can use the rm command to delete unwanted files.

rm demo2.txt
Copy the code

If you want to delete a file from the repository, run the git rm command to delete it, and then run the git commit command to commit it.

git rm demo2.txt
Copy the code

If you delete a workspace file and later find out that you deleted it incorrectly, you can use git checkout to restore it with a repository file:

$ git checkout -- demo2.txt
Copy the code

2, 5 remote library

Above is through Git in the local repository for version control, we know that Git also has a remote repository, here we use Github to make the local repository and remote repository repository synchronization, so that the remote repository can not only carry out file backup, but also with others for synchronization cooperation development.

To register, you need to create an account on Github and enter your username, password and email address.

The SSH KEY needs to be configured because the local and remote warehouses need to use SSH secret keys for encrypted transmission synchronization.

After registering with Github, go to Settings and find Add SSH and GPG Keys.

Git git git git git git git git git git git

Ssh-keygen -t rsa -c "[email protected] email account"Copy the code

After creating the key, you can find the.ssh folder in the user directory, similar to.git

Then click “New SSH Key” to create an SSH Key, fill in Title (optional), copy id_RSA. pub to the Key text box, and click Add SSH Key. To view the key, run the following command: cat ~/.ssh/id_rsa.pub

With all the preparation done, we can create a repository on Github. Find the New Repository button in the upper right corner to create a New repository

Then enter the name of the repository and click Create Repository to leave everything else as default.

After creation it looks like this:

You can then push the local repository to the remote repository by using git remote add Origin and SSH:

Git remote add origin SSH keyCopy the code

Files from the local library have not yet been pushed to the remote library. Git push git push git push git push git push git push

git push -u origin master
Copy the code

Git will not only push the contents of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch. The command can be simplified later when pushing or pulling.

At this point we can see that the remote library already has the local library file that was just pushed up:

After that, when the local library commits, you can push the latest changes from the local master branch to GitHub with the following command:

git push origin master
Copy the code

When you’re working with a team on a project it’s often the case that the source code is hosted in a remote repository, so if someone new comes in and wants to get the source code for the project or someone else updates the remote code and you need to get the latest version, then you need to clone the code from the remote repository. This example uses Github as a remote repository to illustrate how to clone code from a remote repository.

Git clone: git clone: git clone: git clone

Git clone SSH remote library or the HTTP address or git clone [email protected]: JangYt/Vue. GitCopy the code

2. 6 Merge branches

We can see that there is a master at the top, which in Git is called the master branch, and often in a project we have not only a master branch, but also a dev branch and so on, for the sake of the project. So how do you create new branches and merge them?

First we create a new dev branch and switch to it using the command:

Git branch dev git checkout dev git branch devCopy the code

Switched to a new branch ‘dev’: Switched to a new branch. You can also view branches by using the git branch command, which lists all branches, with the current branch preceded by an asterisk.

At this point we add something new to the demo1.txt file: create a new dev branch and commit it to the local library using git add and git commit commands.

Then run the cat command to view the contents of the demo1.txt file:

The garbled code here is not encoded, so I don’t care about it here, as long as I know that the added content is there.

Then switch to the master branch and run the cat command to check the contents of demo1.txt:

At this point we can see that the demo1.txt file on the main branch has no new content added. Can I merge content from the dev branch into the Master branch? Git merge dev git merge dev

At this point we can see that the contents of the dev branch have been merged into the Master branch.

What if we don’t need the Dev branch anymore, but we’re afraid someone will upload code to that branch? The dev branch can be removed using the following command:

git brach -d dev
Copy the code

conclusion

These are some common commands for how to use Git.

Git also has some graphical software, such as Sourcetree, which is also very convenient to use and easy to operate. If you need it, you can search on the Internet to find out how to use it.

Phase to recommend

  • Here’s what you need to know about databases

  • Spring is worth your collection!!

  • What do we know about atomic operations?

  • The execution order of SQL statements

  • A syntax explanation of stored procedures

  • Why don’t you say you don’t know what stored procedures and stored functions are

  • How much do you know about database triggers