Git enterprise development control theory and practical operation - from entry to in-depth (2)|Basic operation of Git

前言 

那么这里博主先安利一些干货满满的专栏了!

首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助。

高质量博客汇总https://blog.csdn.net/yu_cblog/category_12379430.html

Then there is the column "Git Enterprise Development Control Theory and Practical Operations", which is the blogger's most informative column recently. I hope you pay more attention!
Git enterprise development control theory and practice https://blog.csdn.net/yu_cblog/category_12419275.html?spm=1001.2014.3001.5482

Blogger's Github home page

There are some projects made by bloggers themselves, I hope it will be helpful to everyone.

Yufccode (Fengcheng Yu) · GitHubfocus on backend development. Yufccode has 12 repositories available. Follow their code on GitHub.https://github.com/Yufccode


Basic operations of Git

Create a local warehouse

Order:git init

After it is created, there will be an additional .gitdirectory. We don’t care about what is in it, and we will talk about it later.

 Do not manually modify anything inside!

Configure local warehouse

After entering, the first two things to be configured must be configured.

  • name
  • email

If it is not configured, there will be a series of problems in the follow-up.

Configure using git configthe command

git config user.name "xxx"
git config user.email "xxx"

Check if it is configured:

git config -l # 检查config中的所有配置

Delete configuration:

like delete name

git config --unset user.name

Global Settings:

Indicates that it can take effect in all warehouses under the current machine.

git config --global user.name "xxx" # 全局设置
git confit --global --unset user.name # 全局重置

Know the workspace, temporary storage area, version library

Now, can this git repository manage this please README.md?

The answer is not yet. Because it is essentially .gita warehouse, not the outside.

But .gitmanual modification is absolutely not allowed.

In fact, .gitthe inside is called the repository (warehouse), and the outside is called the workspace.

So how can it be README.mdmanaged? Do something.

 

The working area of ​​this picture is where README.md is located.

Stage can be called temporary storage area/index, ie/index

HEAD is a pointer, just want to master, we will understand this concept later.

add files

git add

The essence is to add to the stage!

Order:

git add # 后面跟上文件名
git add . # 添加当前所有问题
git add file.log test.txt # 添加指定文件

git commit

git commit -m "add my first file" # 这里不要乱写,这里写的是提交的细节,对后续维护很重要

git log

git log

Can print all commit records from nearest to farthest.

This is calculated by hashing, a very large number.

git log --pretty=oneline # 这样可以打印的漂亮一点

Learn to understand the files in the .git directory

We can look at the directory structure after a series of operations, as shown below.

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/gitcode]$ tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           └── master
├── objects
│ ├── 0d
│ │ └── f05318450b3455603cd0b0fc2cb4f4c8faa366
│   ├── 86
│ │ └── 5a7036c778b72e201a13e65b37bc35cbd83ade
│ ├── 8d
│ │ └── 9ba68bb8b2dfea545ec559b43f9f08d4d362c0
│ ├── d3
│ │ └── b98d1ce30bb03f3140dee28718bbdf08579543
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── f4
│ │ └── 2df147c60543792b9b56e1118f11409d55e87a
│   ├── info
│   └── pack
└── refs
  ├── heads
  │   └── master
  └── tags
18 directories, 24 files

We just said that after adding, things will be put into the temporary storage area:/index

Temporary storage area/index

Then we can find this index in the directory structure.

The content after add will be put into the index!

HEAD pointer

We also need to look at HEAD. We just said that HEAD is a pointer to the master.

It is pointing to the master

master

It can be found that master is the latest submissioncommit id 

As we just said, all objects are stored in the master object, which can be commit idunderstood as an object

object object

So how to view the specific content in the object object?

First of all, the above should commit idbe divided into two parts.

The first two digits f4represent the name of the folder, and the latter represent the file name, as shown in the figure.

But now you can't check catit directly, you need to use a special command

git cat-file -p [commit id]

The description information of the latest submission is written here.

Having learned this, we can output a conclusion: Git tracking management is actually modification, not files!

modify file

git statusOrder

First introduce a command to view the status of the warehouse.

git status

I just added a new line in my README.md, and then git statuestold me:

Changes not staged for commit: There is no content to modify in the temporary storage area

README.mdThe modified content is in the workspace

git diffOrder

git diff README.md

That is: Find the difference between the files in the workspace and the staging area README.md. The print format is Unixthe commonly used diff format.

 

after modification

Let's try againgit add . after a whilegit status

 

Submit to see.

version rollback

Rollback operation

Key commands:

git reset [--soft | --mixed | --hard] [HEAD]

Go straight to the example.

Our README.md actually has two versions, one is the version at the beginning, and the other is the version after adding a line.

git resetThe essence is to roll back the content in the repository.

--soft --mixed --hard
Only the content in the version library is rolled back, and the work area and temporary storage area remain unchanged The version library + temporary storage area is rolled back, and the work area remains unchanged Roll back content in all regions

option is used by default --mixed.

--hardUse it with caution, because if I am developing in the workspace, but call --hardthe fallback, the content of the workspace will be lost directly.

Look at the log first in the operation line.

 

transfergit reset

After the rollback, add my first fileall modifications after this modification will be rolled back directly.

If you need to undo the modification later: also execute git resetthe command.

If your bash has not been cleared, you can still see the previouscommit id

That direct call git resetcan also be undone.

What if your screen clearis off? It can also be undone.

git reflog # 它可以看到本地每一次的提交命令

Those short numbers and letters in front are actually commit idpart of . It is also possible to roll back with this part.

fallback essence

The essence is just a pointer moving around.

Undo changes

If we continue to develop on the basis of a version in the workspace, but the more we write, the more we feel that our code is rubbish, and we can’t write any more. We want to go back to the version before modification.

First, there are three scenarios:

  • Only changed in the workspace

  • added but no commit

  • committed

Case 1: Only modified in the workspace

git checkout -- README.md

This command can undo the file to the state of the last add.

Note: the command --is very important, if not added --, the command has another meaning.

Situation 2: add but no commit

Do the preparatory work first, and make the work area and the temporary storage area consistent first.

git resetjust use it

git reset --hard ... # 这个命令也可以回退到和当前版本库内容一致的版本下,即版本库啥样就回退到啥样
git reset --hard HEAD # 表示回退到当前版本
git reset --hard HEAD^ # 表示回退到上一个版本
git reset --hard HEAD^^ # 表示回退到上两个版本,以此类推
# 当然这里面reset的选项可以自己选

Situation 3: Already committed

git resetCommands are also available .

Then we know that HEADit is the current version and HEAD^the previous version, so the third case is also easy to solve.

git reset --hard HEAD^ # 三个区都回退到上个版本

Note: Case 3 has a big prerequisite, namely: no pushoperation after commit.

As for what is pushthe operation, it will be explained later.

Why do we have this premise? It’s not that our code cannot be changed after pushing, but that in an enterprise, the code that runs is the code in the remote warehouse, not my local code.

The purpose of revocation: it does not affect the code of the remote warehouse.

Delete Files

method one

After the workspace rm, addand commiton the line.

Method Two

Use gitthe command provided.

git rm file1

This command can do two things for us:

  • delete the workspacefile1

  • delete the staging areafile1

So we git commitcan complete the deletion in one click.

Guess you like

Origin blog.csdn.net/Yu_Cblog/article/details/132449419