In ancient times, not only the world’s talent, but also the indomitable ambition – Su Shi

Say first concept

Git is currently the most popular version control software, before we learn about Git let’s first understand the concept of software version control.

What is software version control

Version control software provides complete version management functions. It is used to store and track the modification history of directories (folders) and files. It is an essential tool for software developers and the infrastructure of software companies. The ultimate goal of version control software is to support the software company’s configuration management activities, track the development and maintenance activities of multiple releases, and release the software in a timely manner. Its main functions are:

  • Centralized file management, secure authorization mechanism: Files are centrally stored on the server and are authorized by the system administrator to each user. By the usercheck incheck outIs used to access files on the server. Unauthorized users cannot access files on the server.
  • Software version upgrade management: every time you log in, a new version will be generated on the server, and any version can be checked out at any time.
  • Lock function: protect the file when the file is updated to avoid conflicts when different users change the same file.
  • Provides comparisons between different versions of source programs.

Version control systems fall into three categories: local version control systems, centralized version control systems, and distributed version control systems

Local version control system

Local version control, where versions of files are stored on disk, has solved the problem of manual copy and paste to a certain extent, but not the problem of multi-party collaboration.

Centralized version control system

The repository of a centralized version control system is centrally located in a central server. Developers need to get the latest version of the content from the version control central server first, and then submit the local content to the version control central server after the development work is completed. The biggest problem with centralized version control systems is that they must be networked to work. If it is a LAN, it is good that the bandwidth is big enough and the speed is fast enough. But if it is the Internet, it is limited by bandwidth. Distributed version control

Distributed version control system

A distributed version control system is much more secure than a centralized version control system, because each developer has a complete version library on his or her computer, and it doesn’t matter if one developer’s computer breaks, just copy it from another developer. In a centralized version control system, if the central server fails, no developer can work.

Summary of the Git

Git is the best software version control system in the world. Git is an open source distributed version control system that can handle project version management from very small to very large projects efficiently and quickly.

Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development. Torvalds started developing Git as an interim alternative to BitKeeper, which had been the primary source code tool used by Linux kernel developers around the world.

Although Git was originally developed to aid in the Linux kernel development process, it has been found to be used in many other free software projects.

Git official

Git features:

  • Branching is faster and easier.
  • Support offline work; Local submissions can be submitted to the server later.
  • Git commits are atomic and project-wide, not per-file like in CVS.
  • Each working tree in Git contains a repository with a complete project history.
  • No Git repository is inherently more important than another.

Git configuration

The Git client needs to do some configuration after installation. The configuration commands are as follows:

Git config --global user.email git config --global user.email
#For example,
git config --global user.name "is_sweet"
git config --global user.email "[email protected]"
Copy the code

Because Git is a distributed version control system, each computer registers with user information (name and Email address).

Note that the –global parameter of the git config command indicates that all git repositories on the current computer will use this configuration, but you can also specify different user names and Email addresses for each repository.

Git operation

Workspaces, staging areas, and version libraries

  • Workspace: the directory you see on your computer.
  • The staging area: Stage or index. General storage.gitGit /index, so we sometimes call the staging area an index.
  • repositoryThe workspace has a hidden directory.gitThis is not a workspace, but a Git repository.

The following figure shows the relationship among the three:

Create a warehouse

Create a warehouse is divided into two ways, respectively is to create a new warehouse, the other way is to clone an existing warehouse.

git init

The Git init life is used to create a new Git repository in your directory. Many Git commands need to be run in Git repositories, so Git init is the first command to use Git.

The sample code looks like this:

is_sweet@Sweet MINGW64 /b/Practice code
$ mkdir is_sweet	Create folder

is_sweet@Sweet MINGW64 /b/Practice code
$ cd is_sweet/		# change directory

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet
$ git init			Create Git repository
Initialized empty Git repository in B:/Practice code/is_sweet/.git/

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ ls -a				# check whether the creation is successful. .gitCopy the code

After the git init command is executed, the git repository generates a.git directory that contains all metadata for the resource, leaving the other project directories unchanged.

Now that we have created the Git repository, we also need to create a repository on the code hosting platform. GitHub is used as an example to create a repository.

Clicking the New Repository button brings up the following screen

Here is the information needed to create the repository, as shown below

Click Create Repository to Create the repository. After creating the repository, we can get the repository’s address in two ways

HTTPS

https://github.com/Is-Sweet/test.git
Copy the code

SSH

[email protected]:Is-Sweet/test.git
Copy the code

Let’s create a new readme. md file on our local and say 123321

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ touch README.md	Create a new file
is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ vi README.md		# Edit file
Copy the code

Now we will push our new repository to the GitHub repository as follows:

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git add README.md		# 1. Add files to Git's buffer
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git commit -m "This is the first file."	# 2. Commit the version to the repository from the staging area of Git, and the parameter -m indicates the comment information of the current commit[master (root-commit) e1246ac] 1 insertion(+) create mode 100644 README.md is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)$ git remote add test https://github.com/Is-Sweet/test.git	# 3. Add remote version libraries	

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git push -u test master	# 4. Upload the local Git repository information to the master branch of the server, where the following interface may pop up, enter your account password
Logon failed, use ctrl+c to cancel basic credential prompt.
#5. Enter the account passwordUsername for 'https://github.com': Is-Sweet Password for 'https://[email protected]': Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 248.00 100% (3/3), 248 bytes | KiB/s, done. Total 3 (delta 0), reused zero (0) delta, pack-reused 0 To https://github.com/Is-Sweet/test.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'test'.Copy the code

git clone

Git Clone can be used to copy projects from an existing Git repository.

The syntax structure is as follows:

git clone <repo> <directory>
Copy the code

Parameter Description:

  • Repo :** Git repository.
  • Directory: indicates a local directory.

Here we’ll clone our new GitHub repository locally with the following code:

is_sweet@Sweet MINGW64 /b/Practice code
$ git clone https://github.com/Is-Sweet/test.git	Git repository will also let you log in as a user
Cloning into 'test'...
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': Is-Sweet
Password for 'https://[email protected]':
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 228 bytes | 1024 bytes/s, done.

is_sweet@Sweet MINGW64 /b/Practice code
$ ls
is_sweet   test

is_sweet@Sweet MINGW64 /b/Practice code
$ cd test

is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ ls
README.md

is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ cat README.md
# 123321
Copy the code

The two ways of creating a repository are covered here.

Git basic operations

Submission and Modification

Git submission and modification are mainly accomplished through the following commands, as shown in the following table

The command instructions
git add Adds one or more files to the staging area
git status View the current status of the warehouse to display the changed files.
git diff Compare the differences between files, that is, the staging area and the workspace.
git commit Submit staging area to local warehouse.
git reset Roll back the version.
git rm Delete the workspace file.
git mv Move or rename workspace files.

The sample code looks like this:

is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$touch file{1.. 10}.txt# create file

is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ ls		Verify that the creation is successful
README.md  file10.txt  file3.txt  file5.txt  file7.txt  file9.txt
file1.txt  file2.txt   file4.txt  file6.txt  file8.txt

#Git add syntax:
#Git add [file1] [file2]
is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ git add .	

#See if you have changed the file again since your last submission.
is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ 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:   file1.txt
        ...

#Use the -s argument to get short output:
is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ git status -s
A  file1.txt
....

#Adds staging area contents to the local repository
#Git commit [file1] [file2]... -m [message]
#-a Parameter Settings After modifying the file, you do not need to run the git add command to directly submit the file
is_sweet@Sweet MINGW64 /b/Practice code/test (master)
$ git commit -m 'submit'
Copy the code

Remote operation

Common instructions for remotely operating a warehouse are as follows:

The command instructions
git remote Remote warehouse operation
git pull Download the remote code and merge
git push Upload and merge the remote code

Git remote command

Git remote add [shortname] git remote add [url] Git remote rm name git remote rm nameCopy the code

Git push command

Git push < remote hostname > < local branch name >#If the local branch name is the same as the remote branch name, the colon can be omitted:Git push < remote host name >#For example,
git push origin master:master
Copy the code

The git pull command

Git pull < remote host name > < remote branch name >#The instance
is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git pull test master	# If the remote branch is merged with the current branch, the part after the colon can be omitted.
Copy the code

Git Branch Management

Almost every version control system supports branching in some form. Using branches means you can detach from the main line of development and continue working without affecting the main line.

Some call Git’s branching model a killer feature that sets Git apart from the version control system family.

So far, we have only one branch in Git repository. This branch is also called the master branch in Git.

View, create, switch branches, and update content

View branch commands

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git branch
* master
Copy the code

Create branch command

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git branch myMaster	# git branch (branchname)
Copy the code

Branch switch command

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git checkout myMaster
Already on 'myMaster'	# git checkout (branchname)
Copy the code

Modify a file and push it

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ vi test1.txt

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git add test1.txt			Commit to the staging area
warning: LF will be replaced by CRLF in test1.txt.
The file will have its original line endings in your working directory

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git status				# View the contents of the temporary storage area
On branch myMaster
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test1.txt


is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git commit -m 'test'		Commit to local repository
[myMaster 6bdd4fc] test
 1 file changed, 1 insertion(+)

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git push test myMaster	# push to serverLogon failed, use ctrl+c to cancel basic credential prompt. Username for 'https://github.com': Is-Sweet Password for 'https://[email protected]': Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 257.00 100% (3/3), 257 bytes | KiB/s, done. Total 3 (1) delta, reused zero (0) delta, pack - reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: remote: Create a pull request for 'myMaster' on GitHub by visiting: remote: https://github.com/Is-Sweet/test/pull/new/myMaster remote: To https://github.com/Is-Sweet/test.git * [new branch] myMaster -> myMasterCopy the code

Merging branches

The Git merge command is used to merge branches in Git.

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (myMaster)
$ git checkout master	# Switch branches
Switched to branch 'master'
Your branch is up to date with 'test/master'.

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git merge myMaster # merge myMaster into current branchUpdating b682387.. 6bdd4fc Fast-forward test1.txt | 1 + 1 file changed, 1 insertion(+) is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)$ git branch -d myMaster	# delete branch
Deleted branch myMaster (was 6bdd4fc).

is_sweet@Sweet MINGW64 /b/Practice code/is_sweet (master)
$ git branch			# check the result
* master
Copy the code

Some commands about branching

#Lists all local branches
$ git branch

#List all remote branches
$ git branch -r

#Lists all local and remote branches
$ git branch -a

#Create a new branch, but remain in the current branch
$ git branch [branch-name]

#Create a new branch and switch to it
$ git checkout -b [branch]

#Create a new branch that points to the commit
$ git branch [branch] [commit]

#Create a new branch and establish a trace relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]

#Switch to the specified branch and update the workspace
$ git checkout [branch-name]

#Switch to the previous branch
$ git checkout -

#Establish a trace relationship between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

#Merges the specified branch into the current branch
$ git merge [branch]

#Select a COMMIT and merge it into the current branch
$ git cherry-pick [commit]

#Delete the branch
$ git branch -d [branch-name]

#Deleting a Remote Branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
Copy the code

Write in the last

This blog introduces the basic use of Git, Git is a necessary function in our future development, this blog is only the introduction to Git.