Git Diagram - Common Command Operations

Table of contents

I. Introduction

2. Initialize the warehouse

3. Add files

Four, Git process panorama

Five, Git workflow

6. Work area and temporary storage area

7. View file status

Eight, view the submission log

Nine, view the difference

10. Version rollback

11. Management modification

12. Revocation of modification

13. Delete files

Fourteen, branch management

15. Project branch operation

16. File Conflicts

17. Turn to video version


I. Introduction

Continue with the previous article: Git Diagram - Why Git? How to pretend? Next, let's see what are the commonly used commands of Git.

2. Initialize the warehouse

Before Git operation, the warehouse needs to be initialized to store the project code of version management. Currently, there are two types of Git warehouses:

  • Local warehouse: a warehouse on the developer's own computer

  • Remote warehouse: It is a warehouse on a remote server (shared with other team members, not mentioned here for the time being)

Configure your own name and email account. In the company, it is usually the pinyin of your name and the company's work email

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

 

Initialize the local warehouse

git init

 

.gitAfter the command is executed, there will be an additional directory under the current directory . This directory is Gita local warehouse, which is used to track and manage code (files). Don’t manually modify the files in this directory . It is easy to change and cause Gitthe warehouse to jump up.  

 It should be noted here that some friends' computers do not check the hidden project option, and they will not see the .git directory

3. Add files

The warehouse is initialized, how to add files to the warehouse and manage them?

Step 1: Create a plain text file

 Step 2: Add the file to the staging area

git add readme.txt

Step 3: Add files to repository

git commit -m "添加了readme.txt文件"

 Briefly explain git committhe command. -mThe following input is the description of this submission. You can enter any content, of course, it should be meaningful , so that you can easily find the change record from the history. git commitAfter the command is successfully executed, it will tell you that 1 file has been changed (our newly added readme.txt file)

expand

If you add more files later, you can use the following command

git add file1.txt
git add file2.txt file3.txt
git add .   当前文件夹下所有文件
git commit -m "add 3 files."

Four, Git process panorama

Five, Git workflow

 

6. Work area and temporary storage area

When performing crud operations in Git, you need to perform the operation of git add file. The underlying operation will add the operation file to a cache in an area called the cache area. After the operation is completed, use the git commit operation to perform a unified submission, and the edited file will be unified and synchronized. middle

 

7. View file status

Question: How to check the current status of the project? I wrote the code in front of the computer for a while, managed it with Git, went to the bathroom halfway, then went to eat an apple, and continued to come back to work. I don’t remember what I did with Git before?

git status # 查看当前git版本库的状态(查看缓存区中的文件内容)

Eight, view the submission log

In actual work, how can we remember what has been changed in a file with thousands of lines every time in our minds, otherwise what would we need a version control system for? The version control system must have a command that can tell us the history . In Git, we use git logthe command to view

git log

 git logThe command displays the commit log from the nearest to the farthest. If you think the output information is too much and you are dazzled, you can try to add --pretty=onelineparameters:

git log --pretty=oneline

 The long yellow string is the commit id of this submission, which is a unique identifier generated Gitby an algorithm and can be guaranteed to be globally unique .SHA-1

Nine, view the difference

If a file is known to have been modified, it would be better if you can see what was modified.
For example, when you come back from a two-week vacation abroad, when you go to work on the first day, you can’t remember how you modified it last time` readme.txt`, so you need to use the command `git diff` to see:

git diff # 查看不同版本之间的文件差异

10. Version rollback

We are constantly modifying files and submitting files to the repository. Just like RPGwhen playing a game, every time you pass a level, the game state will be automatically saved. If a certain level has not passed, you can also choose to read the state of the previous level. GitThe same is true, whenever you feel that the file has been modified to a certain extent, you can "save a snapshot" , this snapshot Gitis called in commit. Once you mess up the file, or delete the file by mistake, you can commitrestore from the most recent one, and then continue to work, instead of losing all the work of several months.

What should I do if I want to go back to the previous version?

GitYou must know which version the current version is. In Git, use HEADmeans the current version. The previous version is HEAD^, and the previous version is HEAD^^. Of course, writing 100 ^ to the previous 100 versions is easier than counting, so it is written as HEAD~100.

git reset --hard HEAD^

 back to the specified version

git reset --hard <commit id>

 Expanding requirements: How to roll back to the latest version

11. Management modification

Using Git to modify files, there is a problem that needs to be discussed: secondary modification

Operation mode 1:

第一次修改 -> git add -> 第二次修改 -> git commit`

Operation method 2: recommended

第一次修改 -> git add -> 第二次修改 -> git add -> git commit

Note: It is recommended to check whether any files have not been added before each commit

12. Revocation of modification

git checkout -- filename` can discard changes in the workspace: -- followed by a space

git checkout -- readme.txtThe meaning of the command is readme.txtto cancel all the modifications of the file in the workspace. There are two situations here: 1. It readme.txthas not been placed in the temporary storage area ( git add) since the modification. Now, the modification will return to the same state as the repository; Two: readme.txtAfter it has been added to the temporary storage area, it has been modified. Now, if you undo the modification, it will return to the state after adding it to the temporary storage area.

In short, it is to return this file git committo git addthe state of the last or time.

 Note: git checkout -- file The in the command is --very important, without it --, it becomes a command of "switching to another branch" , we will encounter git checkoutthe command

13. Delete files

Under normal circumstances, you usually delete useless files directly in the file manager, or rmdelete them with the command

git rm test.txt

At this time, Gityou know that you have deleted the file, so the workspace and the version library are inconsistent, and git statusthe command will immediately tell you which files have been deleted:

Needed after deletion is completecommit

If you delete and want to recover, you can use resetversion recovery

Step 1: Delete useless files locally (check status)

 Step 2: first add the following (check the status and compare with step 1)

 Step 3: Submit the file for deletion

Fourteen, branch management

Branch management is the soul of Git. Basic operations are essential for development and must be mastered.

Why do branches exist? Because the finished product of the project goes through these processes: development, testing, launch, bug modification, multi-version release, etc. Different versions of the same project are developed, tested, and launched at the same time. How to ensure that the project can be independent and related to each other in such a complex situation? The solution given by Git is branch management . Each stage is a branch, which can be independent of each other and merged with each other.

view branch

git branch

create branch

git branch <name>

switch branch

git checkout <name>

create + switch branch

git checkout -b <name>

merge branch

Merge a branch into the current branch

git merge <name>

delete branch

git branch -d <name>

15. Project branch operation

Simplified version

full version

master branch : used for version updates. When a relatively large function is developed or updated, there will be a collective release, and all the code will be merged into the master (some companies will also use the release branch to release the version, the principle is the same) The same one);

develop branch : Generally, it is a development and test branch. Before the project is released and launched, it will be tested on the dev branch uniformly to ensure that the functions meet the standards and there are no bugs before pushing to the master branch;

feature branch : used for sub-module function development, it is recommended to name it feature-xxx, after the module is completed, it will be merged into the dev branch;

Hotfix/fixbug branch : It is a branch used for online emergency bug fixes, and it is recommended to be named hotfix-xxx. When there is a problem with a certain version online, the code of the corresponding version will be checked out, and a Hotfix branch will be created. After the problem is fixed, it will be merged back into dev and master. Note here that when merging into master, it is generally necessary to label the version after the repair.

Extended reading: An elegant Git branch practice_git branch management best practice_Langfei yes's Blog-CSDN Blog

16. File Conflicts

There is a file in branch 1 that is the same as other branch files. If it is modified at the same time and merged, there will be a file conflict.

 

 

At this point, this article is over. If you want to know what will happen next, please listen to the next chapter to break it down~

17. Turn to video version

If you are not addicted to reading text, you can switch to the video version: 4 hours for you to get started directly with Git operations

Guess you like

Origin blog.csdn.net/langfeiyes/article/details/129367051