You need to know about GIT

Time:2021-6-22

This is a concise guide to getting started with GIT. If you want to learn GitHub quickly, this article is a good choice.

Personal summary, if there is anything wrong, please point out.

catalogue

The creation of version Library

  • Git installation
  • The creation of version Library

Operation of version Library

  • File operation
  • Version rollback
  • Modification and revocation

Remote library connection

  • Adding and removing remote database
  • Clone from remote library

Branch Management

  • Create and merge branches
  • Multi person collaboration

Label management

  • create label
  • Operation label

The creation of version Library


Git installation

Using apt package management tool to install on Ubuntu Linux

sudo apt-get install git

Enter directly on WindowsGit websiteDownload the installation program, and then install according to the default options

The creation of version Library

Select an appropriate location, use MKDIR instruction to create an empty directory, and then enter the input instruction

git init

As a result, the directory becomes a warehouse that git can manage. Careful readers can find that there is one more in the current directory.gitThis directory is used by git to track and manage the version library. If you have nothing to do, don’t modify the files in this directory manually, or the GIT warehouse will be destroyed if you change it disorderly.

Operation of version Library


File operation

Before the file operation, first clear a few concepts

work area

It’s the directory that you can see directly in the computer

You need to know about GIT

Version Library

There is a hidden directory. Git in the workspace directory. This is git’s version library. The version library is mainly composed of two parts, stage buffer and branch area.

You need to know about GIT

There are three steps to add a file to the GIT Repository:

firstCreate a file read.txt in the workspace

vim/typora Read.txt

Add the file modification to the temporary storage area. Before adding to the temporary storage area, let’s first look at the status of GIT

~/CS61A$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    Read.txt

nothing added to commit but untracked files present (use "git add" to track)

Git clearly told us that a file appeared, andRead.txtIt has never been added, so its state isUntracked

thenWe create a file and add it to the staging area

git add Read.txt

If you add a folder, you can use the

git add vs/*

If you want to delete a file, you can use

git rm Read.txt

Check the status of GIT again

~/CS61A$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   Read.txt

Changes to be committedWe found that git has identified this file, so it can be seen that this file has been added to the staging area.

lastWe submit the file creation operation to the master branch of the warehouse

git commit Read.txt -m "commit"

If you want to submit all the files, you can use the

git commit -m "commit"

The – M parameter here is a text description of this submission

Check the status of GIT again

~/CS61A$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

It can be seen that the temporary storage area is empty and the modification has been submitted to the warehouse

The process of creating a file and submitting it to the warehouse can be represented by a flowchart

graph LR
A [create file in workspace] = = = > b [add modify / create / delete operation to staging] = = = = > C [submit operation to warehouse]

It is estimated that many people have doubts about why the operation is written here instead of adding files to the staging area, because git manages modification instead of files. Let’s illustrate this problem with an example.

First, we add a paragraph of text to read. TXThello meAnd then add this operation to the staging area. Let’s see the status at this time

it status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   Read.txt

It can be seen that this modification has been added to the staging area, and then we will modify the file and add a linehello you, do not add this modification to the staging area.

git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   Read.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   Read.txt

We execute the submit instruction, submit the modification to the warehouse, and then check the GIT status

git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   Read.txt

We found that changes that were not added to the staging area were not submitted to the warehouse. Let’s see the difference between the workspace and the latest version of the library

~/CS61A$ git diff -- Read.txt
diff --git a/Read.txt b/Read.txt
index 1f8eec4..12ee40e 100644
--- a/Read.txt
+++ b/Read.txt
@@ -1,2 +1,3 @@
 hello world
 hello me
+hello you

If git manages the file itself, then what the file is like when it is submitted is what the submitted file is like. However, after comparison, it is found that the files in the version library are different from those in the workspace. What we add to the staging area each time is the operation steps of the file, such as creating and deleting the file, adding a few lines and deleting a few lineshello youThe modification operation of this line of text is not added to the staging area. When submitting, only the operations in the staging area are submitted, so the operation is not submitted to the warehouse.

Version rollback

As I mentioned above, GIT manages modifications. If we accidentally delete the last line during the modification submission processhello meWhat should we do if we need this line?

At this time, you need to use the version rollback function.

In git, we can view the record of each operation

~/CS61A$ git log --pretty=oneline
aa03a474011bcb97e9f596d3b5c9a1b517726d05 (HEAD -> master) delete a line
05491fda20edff579daf14f24b7afed61d591842 add a line
a58d42838b1284034c4a9d52f0d2780ad251bda2 a
f67db85d1deeb3d7ca0020a332ac4e07bccf12b0 all
b96112ab41f972a9949e213347d7403dd2f8a03b 1

We’re going to go back to the versiondelete a lineBefore operation, so we useresetinstructions

~/CS61A$ git reset --hard HEAD^
HEAD is now at 05491fd add a line

Here, head ^ represents the previous version, if the last two versions are ^ ^, and so on.

But the problem is, if we want to go back nowdelete a lineWhat about the version? The version has been rolled back.

Git has a command that records every command you make

~/CS61A$ git reflog
05491fd (HEAD -> master) [email protected]{0}: reset: moving to HEAD^
aa03a47 [email protected]{1}: commit: delete a line
05491fd (HEAD -> master) [email protected]{2}: commit: add a line

So we can use itaa03a47This ID goes back to the original version.

git reset --hard aa03a47
HEAD is now at aa03a47 delete a line

So the version came back.

Modification and revocation

Sometimes we write wrong code because of shaking hands. For example, we add a line “RM – RF” to the read. TXT (. SH) file. If we know something about Linux, we all know that this is a fatal operation.

If you have not submitted this file modification to the staging area, then it’s very simple. Just modify it directly, or you can use the instruction

git checkout -- Read.txt

The modification of the workspace is discarded, and the content of the workspace file is the same as that of the version library file.

If you have added file changes to the staging areaThen we need to use another instruction

git reset HEAD Read.txt

The reset instruction can not only backtrack the version, but also remove the modification operation of the temporary storage area when the head is not availablecheckoutInstruction synchronizes the version library and workspace contents.

If you have submitted your changes to the repositoryThen you can follow the version rollback method

git reset --hard HEAD^

If you have already pushed the contents of the version library to a remote library, such as GitHub, it’s up to you. Whoever runs will suffer.

Remote library connection


Remote library add / remove

First, log in to GitHub, and then find the “create a new repo” button in the upper right corner to create a new warehouse

(Liao Xuefeng borrowed from the picture, big L.):

You need to know about GIT

Fill in the repository nameReadTo create a new git repository successfully, keep the default settings and click the “create repository” button

You need to know about GIT

Then we run the command locally

 git remote add origin [email protected]:xxxx/Read.git

XXX for your own GitHub account name, you can connect the local version library and remote library.

Push all the contents of the local library to the master branch of the GitHub remote library.

git push (-u) origin master

Added-uParameter, GIT will not onlymasterA new remote method for branch content pushmasterBranch, but also the localmasterBranch and remotemasterBranch Association, in the future push or pull can simplify the command, do not need to add – U parameter.

SSH warning

When you first use git’scloneperhapspushWhen the command connects to GitHub, you get a warning:

The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?

This is because git uses SSH connection. When SSH connection verifies the key of GitHub server for the first time, you need to confirm whether the fingerprint information of GitHub key really comes from GitHub server. EnteryesJust enter.

Git will output a warning that GitHub’s key has been added to a local trust list

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

This warning will only appear once, and there will be no warning for subsequent operations.

If you’re really worried about someone posing as a GitHub server, typeyesIt can be compared with beforeFingerprint information of GitHub’s RSA keyWhether it is consistent with that given by SSH connection.

Disassociate remote library from local library

If we need to disassociate the local library from the remote library, we can use the command

git remote rm origin

Remote library clone

Sometimes we need to clone the remote library from GitHub. In order to learn from the code or do distributed work, we use the following instructions to clone the remote library to the local

git clone [email protected]:xxx/Read.git

Branch Management


Create and merge branches

stayVersion rollbackAs you already know, GIT strings them into a timeline for each submission, which is a branch. So far, there is only one timeline. In git, this branch is called the main branchmasterBranches.HEADStrictly speaking, it’s not a submission, it’s a submissionmastermasterIt’s pointing to submission, so,HEADIt points to the current branch.

You need to know about GIT

When we create new branches, such asdevGit creates a new pointer calleddev, pointing tomasterThe same submission, and then theHEADpointdev, which means that the current branch is indevupper

You need to know about GIT

We set up a new branchdevAnd switch to the new branch

git checkout -b dev    /   git switch -c dev

If you switch to an existing branch, then

git checkout dev    /    git switch dev

If you just create branches

git branch dev

From now on, the modification and submission of the workspace is aimed atdevFor example, after a new submission,devThe pointer moves one step forward, andmasterPointer unchanged:

You need to know about GIT

If we’re heredevWhen you have finished your work, you can finish itdevmerge tomasterIt’s on. How does git merge? The simplest way is to directlymasterpointdevTo complete the merge:

You need to know about GIT

We switch to the main branch and merge the dev branch into the main branch master

git checkout master
git merge dev

Then we can keep the branch, or we can delete the branch, if we delete the branch

git branch -d dev

Multi person collaboration

In the actual development, we should carry out branch management according to several basic principles

first,masterThe branch should be very stable, that is, it is only used to release new versions, and can’t work on it at ordinary times;

Where do you work? It’s all workdevOn the branch, that is to say,devThe branch is unstable. When the 1.0 version is released, for example, it will be changeddevBranch merge tomasterOn, onmasterBranch release version 1.0;

You and your friends, everyone is heredevEvery one has his own branch and goes to work from time to timedevJust merge the branches.

So the branch of teamwork looks like this:

You need to know about GIT

Remote library clone

In the process of team cooperation, the remote library should be cloned to the local first. For the specific method, see the section of remote library cloning above

After cloning, GIT automatically matches the local master branch with the remote master branch, and the default name of the remote warehouse is origin

View remote library information

~/CS61A$ git remote -v
origin    [email protected]:x/CS61A.git (fetch)
origin    [email protected]:x/CS61A.git (push)

It shows you how to grab and pushoriginYour address. If you don’t have push permission, you can’t see the push address.

Branch push

There are many modes for pushing branches. Here we only introduce the simplest mode. Suppose that we have cloned the master branch of the remote library locally, and then we create a new branch, dev, to work in the new branch. After the work is finished, we merge the dev branch into the master branch and use it

git push origin master

The branch is pushed to the remote library

Grabbing of branches

There may be some problems in multi person collaboration. For example, one day, your little partner has pushed his submission to the origin / Master branch, and you happen to modify the same file and try to push it. At this time, you will find that git prompts that the push fails because there is a conflict between your little partner’s latest submission and your submission.

Therefore, we need to catch the latest situation of master remotely before pushing, and then merge and resolve conflicts locally.

The solution to the conflict is as follows

We know that when we grab the remote master branch, there is a conflict between our local dev branch and the remote pull master branch

You need to know about GIT

In this case, GIT can’t perform “quick merge” and can only try to merge their own modifications, but there will be conflicts in this merge. Let’s check the status of GIT

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   Read.txt

no changes added to commit (use "git add" and/or "git commit -a")

Git tells us that there is a conflict in the read.txt file, so we can directly view the content of the file

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> dev

For Git<<<<<<<=======>>>>>>>Mark the contents of different branches. We modify them as follows and save them

Creating a new branch is quick and simple.
Creating a new branch is quick and simple.

Then submit the modification to the master branch

$ git add readme.txt 
$ git commit -m "conflict fixed"
[master cf810e4] conflict fixed

Git tells us that the conflict has been resolved, so we merge the dev branch into the master and then push

git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:x/Read.git
   7a5e5dd..57c53ab  master -> master

success.

Label management

Why do you need labels?

Git has commit, why introduce tag?

“Please package and release last Monday’s version. The commit number is 6a5819e…”

“It’s hard to find a bunch of messy numbers!”

Another way:

“Please package and release last Monday’s version, version number is v1.2”

“OK, just follow tag v1.2 to find commit!”

Therefore, tag is a meaningful name that is easy to remember. It is tied to a commit.

create label

Tagging in Git is very simple. First, switch to the branch to be tagged, and then click tag to tag

git tag v1.0

utilizegit tagView all tags

$ git tag
v1.0
v2.0
v2.1

If you want to tag the historical commit, you can find the commit number through git log

git tag v0.9 f287d34

View the information for each tag

$ git show v0.9
commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9)
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:56:54 2018 +0800

    add merge

diff --git a/readme.txt b/readme.txt
...

Then use the method in the version fallback sectionresetThe instruction can be returned to the corresponding version and packaged.

Note that the tag is always tied to a commit. If the commit appears in both the master and dev branches, you can see the tag on both branches.

Operation label

If the label is wrongly typed, it can also be deleted:

$ git tag -d v0.1
Deleted tag 'v0.1' (was f15b0dd)

Because the created tags are only stored locally, they will not be automatically pushed to the remote. Therefore, the wrong label can be safely deleted locally. If you want to push a label to remote, use the commandgit push origin <tagname>, or usegit push origin --tagsPush all tags.

If you want to delete a remote tag, you first delete the tag locally, and then synchronize the deletion instruction to the remote

$ git push origin :refs/tags/v0.9
To github.com:x/Read.gi0t
 - [deleted]         v0.9

reference resources

1.Rookie tutorial Git

2.Liao Xuefeng’s git course

3.Git official website

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]