First, origin

Code is the most valuable asset of a software company. What is the most efficient way to keep your code?

If we were to go our own way without any tools, there would be at least the following problems:

  • If a team member’s code is lost due to disk damage or resignation, the team member’s code is lost
  • Team members can’t get another member’s code. You need to regularly spend time merging your code with other members (in pairs) to get the complete code, which can be a lot of work once the project is complex
  • If in the process of project development, it is found that the coding idea is wrong and the code needs to be returned to the state three days ago, it needs to be completely rewritten
  • After the project went live, I found a low-level bug and needed to confirm who wrote the bug (and then…).

This is all about code management. And that’s just a very, very small part of the real world code management problem.

The specialized tool for solving these problems is today’s code management tool, or VCS Version Control System. Each time a code change in VCS is committed to VCS management, a Version is generated so that changes to individual files can be tracked. A typical VCS system has a server that commits local code to a remote server and supports automatic code merging, so team members can easily share code and the cost of merging code is greatly reduced.

There are a lot of VCS software on the market, but the days of the herd are over. Git is the most popular and powerful version management system available today. His author is Linus Torvalds, the famous author of the Linux kernel. Linux kernel is a version management tool used by the Linux community to manage the Linux kernel code.

2. Git installation

windows

Visit git-scm.com/ to download the installation package and click Next to install. Once installed, Git provides a Git-bash, which is a Linux terminal emulated under Windows that can use basic Linux commands.

linux

Download and install directly using package manager. Yum install git

mac

Homebrew install git is recommended

Basic concepts of Git

Let’s use Git as an example to explain some common VCS concepts.

Git – Server and Git Remote

Git is a C/S architecture, which means that Git also has a server, a local client. A single Git server (Git-server) can host multiple Git code repositories (Git-repository). This means that you can create one repository for project A and one repository for project B on the same Git server. The two repositories are managed independently of each other. Each repository on the server is called a remote in comparison to a local repository, and each local client can have more than one remote. That is, you can push code for the same project to more than one remote. For example, push to github abroad and push to Gitee in China.

Git servers can be built on their own (commonly used tools gitlab, Gogs), or they can use hosting services. The most famous is Github.com, but slower for well-known reasons. China’s main code cloud (gitee.com), Ali Code and so on.

Git-client and local code repository

Our code is on our own computer, and to interact with a Git server, we obviously need a client called git-client. Git was originally a Linux command line program (after all, it was written by the father of Linux), so git-client is a Git command in Linux. As the technology has evolved, Windows versions are now available as well as GUI tools (most notably SourceTree).

Git local repository is a local copy of a Git remote repository. The usual representation is a folder called.git. It supports local submission and synchronization with remote repositories.

Git creates a local version of the file for each commit, and Git assigns a unique commit ID that contains changes to the file, commit information, author information, and so on. Git can browse commit records, including local commits and remote commits that have been synchronized, without networking. You can also view the modification information of a file. Git support also supports rolling back to a specific version, making it easy to undo code changes. These commit records are also committed when the commit is pushed to the remote end.

Git’s file management needs to be added proactively. Once added, Git automatically monitors file changes and updates the file status depending on whether it is committed or not.

Git branch

One of the most important aspects of Git is its ability to support parallel development. For example, A and B independently develop two unrelated functions, which can be developed at the same time, and merge the code at the appropriate time. This supported feature is called Git branching and is a very important feature of Git. We will discuss this separately in other articles.

4. Use Git

4.1 Creating a Remote Repository

Git repository git repository git repository git repository It is recommended to use Gitee instead of self-building.

  1. First visit gitee.com/signup?from… Register for a Gitee account
  2. Then click the + sign next to your avatar to create the repository

  1. For multi-user collaboration, you can add personnel on the warehouse management page as follows

Send the link or qr code to the person invited.

4.2 Local Warehouse Operations

Let’s take a look at some of the basic operations of the local warehouse

  • Clone: The operation of downloading a repository from a remote location, which is called a Clone clone operation in Git. This operation not only downloads the repository files, but also includes the commit records, branches, and other affiliated information. After cloning is complete, the local result is a complete repository
  • Add: add to add a file to git management scope.
  • Commit: Commit, which is a local commit. A commit is created in the local repository, a version is generated, and the commit record is added. No networking required.
  • Push: push the local code repository submission information and code changes to the remote repository, need to be connected
  • Pull: Pull downloads code changes and commit information from a remote repository, requires networking

Specific operation steps:

  1. To open a Git-bash terminal, first configure the environment
$git config --global user.email $git config --global user.email $git config --global user.emailCopy the code
  1. Clone a remote repository using the git clone command
$ git clone https://gitee.com/somename/somerepo.git Cloning into 'somerepo'... remote: Enumerating objects: 273, done. remote: Counting objects: 100% (273/273), done. remote: Compressing objects: 100% (270/270), done. remote: Total 273 (delta 102), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (273/273) and 117.85 MiB | 3.75 MiB/s, done. Resolving deltas: 100% (102/102), done. Updating files: 100% (79/79), done.Copy the code

The warehouse address is available on the Gitee Warehouse page

  1. Switch to the clone project directory, add the files to the folder, and use git add to add the files to git management
$vi somecode.txt = $vi somecode.txt = $vi somecode.txt = $vi somecode.txt = $vi somecode.txt Untracked files $git status On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) somecode.txt nothing added to commit but untracked files present (use "git add" to $git add somecode.txt Warning: LF will be replaced by CRLF in somecode.txt. The file will have its original line endings in your working directory # New file $git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed:  (use "git restore --staged <file>..." to unstage) new file: somecode.txtCopy the code
  1. Git status Displays the workspace status
  2. Git commit commit to the local code repositoryGit commit -m
1 file changed, 1 insertion(+) Create mode 100644 somecode.txtCopy the code
  1. Git push to remote, this step may pop up the verification box, you need to fill in the user name, password, according to the user name and password when registering Gitee
$ git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 157.00 100% (3/3), 314 bytes | KiB/s, done. Total 3 (1) delta, reused zero (0) delta, pack - reused 0 remote: Powered by GITEE.COM [GNK 6.0] To https://gitee.com/somename/somerepo.git afbb494.. bb5a735 master -> masterCopy the code
  1. Git log You can view the change records of a file by following the file path
$ git log commit bb5a7350d504b5e26fc6d1932e2165cc31aedbff (HEAD -> master, origin/master, origin/HEAD) Author: XXX <[email protected]> Date: Thu Jul 22 16:54:45 2021 +0800 Added important codeCopy the code
  1. In the Gitee warehouse management page, directly add a file, casually fill in some content, easy to simulate pull

  1. Git pull pulls remote changes
$ git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 15.00 100% (3/3), 268 bytes | KiB/s, done. The From https://gitee.com/somename/somerepo.git bb5a735.. fd0f532 master -> origin/master Updating bb5a735.. fd0f532 Fast-forward abc.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 abc.txtCopy the code

4.3 Conflict Resolution

The conflict occurs because two clients change the same location of the same file at the same time. Git cannot merge automatically, so the conflict occurs.

Conflict resolution principle: first discuss, then operate, is strictly prohibited to directly overwrite others’ code without discussing

Emulation: Modify the same line of somecode.txt on both the local and Gitee warehouse management pages

1) Modify somecode.txt in gitee repository and submit it for editing (no need to push, it is immediately applied to code repository)

  • Click File to enter the file interface and select Edit File

  • Change the content to java123

2) Submit local code and push, will directly report an error

$git add somecode.txt warning: LF will be replaced by CRLF in somecode.txt. The file will have its original line of Endings in your working directory # submit $git commit -m create conflict [master 61fca79] 𫔄 drop €犲啿 1 file changed, 1 insertion(+), 1 deletion(-) Asked To pull (pull) $git push To https://gitee.com/maizdotme/class56phrase3.git! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://gitee.com/maizdotme/class56phrase3.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ... ') before pushing again. hint: $git pull remote: Enumerating Objects See the 'Note about fast-forward 'in 'git push --help' for details. 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 16.00 100% (3/3), 261 bytes | KiB/s, done. The From https://gitee.com/maizdotme/class56phrase3 e9d1c58.. 9ceecd7 master -> origin/master Auto-merging somecode.txt CONFLICT (content): Merge conflict in somecode.txt Automatic merge failed; fix conflicts and then commit the result.Copy the code

3) Conflict resolution

The content of the post-conflict file is changed to:

<<<<<<< HEAD
java456
=======
java123
>>>>>>> 9ceecd77ce954380e3f4a75e0aa4a9df6b67ce4f
Copy the code

The content above ======= is local, and the content below is on the remote server

Manually modify the file contents, then add, commit, and push

Java123456 $git add somecode. TXT $git commit -m merge conflict [master 0415e1e] merge conflict #  Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 8 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 552 bytes | 184.00 KiB/s, done. Total 6 (2) the delta, reused zero (0) delta, pack - reused 0 remote: Powered by GITEE.COM [GNK 6.0] To https://gitee.com/maizdotme/class56phrase3.git ceecd7. 9. 0415e1e master -> masterCopy the code

5. IDE integration

The above describes the basic operations of Git. But usually our development takes place in an IDE. How do YOU use GIT in your IDE?

This section uses IDEA as an example to illustrate the use of integration in general ides.

Idea automatically recognizes Git projects, so when you import a Git project into IDEA, IDEA automatically adds Git support.

Based on personal experience, if you want to use Git to manage projects in IDEA. The following steps are recommended:

  1. Create a Git repository in the cloud
  2. Clone to the local
  3. Put the project code into the repository or point the project code location to the repository when creating the project
  4. Import an item into IDEA, and IDEA automatically identifies it

Common operations:

clone

Choose the menu bar VCS > Get from Version Control

Fill in the Git repository address and select the folder path

After the end, a dialog box will pop up asking whether to open the new project

add

Idea will ask if you want to join git after creating a file. You can also check the remember option

The.gitignore file can be placed in the root directory of your project and lists all files that git should ignore, such as the Target folder of your Maven project, the.idea folder, the.iml file of your idea, etc. Git automatically ignores these files every commit. It is recommended to use.

commit

Use the shortcut key CTRL + K

push

Use the shortcut keys CTRL + Shift + K

pull

Use the shortcut key CTRL + T

log

Idea provides a page for viewing Git logs after identifying git projects

Resolve the conflict

When there is a conflict, click the conflict file to enter the conflict resolution interface below. Click “④” and “⑤” to add to the result or cancel

other

In addition, there are two other git-related places where all git-related functions can be done

One is to right-click on the project and select the Git menu

The other is under the VCS menu at the top

Six, summarized

This article mainly introduces the concept of VCS and the basic use of Git. The content is relatively superficial, aiming to help you quickly understand the basic concept and basic use of Git. If the details are not clear, you can discuss them.

No matter you have any problems in your study, Chongqing Snail College welcomes you to come to consult, contact QQ: 296799112

Resources:

  1. Git official open source book Pro Git online Chinese version git-scm.com/book/zh/v2
  2. Liao Xuefeng GIT tutorial www.liaoxuefeng.com/wiki/896043…
  3. Backlog interesting GIT tutorials backlog.com/git-tutoria…