Popular understanding of what git and github are, 30 minutes speed pass version

install git

Installing git is the same as installing software such as node and qq. Just go to the official website to download and install it. Remember to choose the correct version. The versions you need to download for mac and win are different.

configure git

Friends who are familiar with environment variables should understand the importance of this step. It doesn’t matter if you don’t have relevant experience. Configuring git can be temporarily understood as a login similar to qq. This login also has the option to remember the password and automatically log in.

Use these 2 commands to configure your identity (name and email)

  • git config --global user.name "woshishei"
  • git config --global user.email [email protected]

This command is entered in the terminal, here you need to know how to use the terminal: open your terminal and you can see C:\such a path, which is equivalent to opening this C:\folder;

When you enter a command in the terminal mkdir 新建文件夹, it will first operate under your path, that is, create a folder, its path is C:\新建文件夹, and then git initializes the warehouse, etc., and operates on the path where your terminal is located;

Notice that there is one of the above instructions --global. This instruction is to configure git globally. It will store your name and email in a certain area managed by git itself. If you don’t write it, it will --globalcontrol git under your current folder. warehouse information.

first git repository

  • git initYou can use the command to turn the current folder into a warehouse, and you can use most of the functions without networking.
  • Or find someone else's project and get the open source warehouse code through another command, for example git clone https://github.com/nesb01t/RPALite.git, this will download the code, and it is already a git warehouse.

Most of the functions of the two warehouses obtained in this way can be used locally. By now, you should have learned how to download other people's open source code.

git basic functions / local warehouse

Next, I will introduce a concept of file tracking. Git version management will track files, and define different versions by tracking subsequent operations such as modification/deletion/creation.

For example, if you write version1.0 today and version1.2 tomorrow, git will find out what changes have been made from 1.0 to 1.2? git tracks changes to these files.

  • Tracking files: git add [name]
  • Untrack: git rm [name]
  • Keep directory but not tracked: git rm --cache [name]

add Modify the tracking file and put it into the temporary storage area

Then, when you 跟踪中modify the file, the file will enter 已修改the state;

At this time, enter git add [name] again , and it will become in 已暂存state , and will be automatically put into the temporary storage area of ​​git.

If you slipped your hands, you can also enter git reset HEAD [name] to restore 已修改the status .

At this point we noticed that there are actually 4 types of files in a git repository:

  1. Untracked: files that have nothing to do with the git repository
  2. Unmodified: Files that are tracked, but not modified
  3. Modified: The file is different from the previous version, that is, it has been modified
  4. Staging: In the staging area, files waiting to be submitted to a new version

commit Submit the staging area to generate a new version

So how to submit the code in the temporary storage area? Use the command git commit to submit the temporary storage, and there are several possibilities at this time:

  • If you are using the git tool integrated with vscode, it will let you enter the submitted information directly in the box
  • If you are using git bash or a terminal, you will enter vim mode to input the information submitted this time, and focus on how to use vi/vim to submit:
    1. Press i to enter input mode
    2. Enter information for this submission
    3. Press Esc , then colon -wq
  • You can also use git commit -m "submitted information" to avoid the above two possible occurrences. For beginners, vi/vim may be awkward. Here it is recommended to use git commit -m "submitted information" directly
PS E:\CloudShip> git commit -m "Test"
[backend-dev 97a49ef] Test
 1 file changed, 1 insertion(+)

If you slipped your hands, enter git reset HEAD~ --soft to withdraw this submission

You may have doubts about what this reset is, and what is the head. For the time being, remember the functions of the previous commands, and the content involved will be discussed in depth later.

status View the modification and temporary status of the file

Enter git status and you can see which files have been modified:

  • The red file displayed means: modified but not temporarily saved
  • The green file displayed means: staged but not submitted
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:   backend/controller/auth.js

diff View the status of files that have been modified but have not entered the temporary storage area

The above diff command can see which files have changed;
and use the command git diff已修改/未进入暂存区 to see the specific changes of each line in the file.

diff --git a/backend/controller/auth.js b/backend/controller/auth.js
index 0bb29d4..71e2806 100644
--- a/backend/controller/auth.js
+++ b/backend/controller/auth.js
@@ -36,6 +36,7 @@ class AuthController {
    
    
       ctx.status = 200;
       ctx.body = "creating new account";
       return;
+      return;
     }

log View past commit logs

Enter the command git log to view the historical submission records, such as:

commit d56ba11a294d229a0993facc2591a720faa04274 (HEAD -> backend-dev)
Author: nesb01t <[email protected]>
Date:   Sat Apr 15 12:23:11 2023 +0800

    test

Here d56ba11a294d229a0993facc2591a720faa04274 behind the commit is a hash value, which is unique, and we can find our current commit through this hash value;

The following contains the author, date, and information entered at the time of submission.

Through git log --pretty=oneline, we can beautify our output to one line, and we can also customize the format, etc. Please check it yourself:

791fd226f01affae4c4e6224b8760dd5799622ac update List
43e319e0fcb412c19dccc0c832ac35352a75af3b Merge pull request #35 from Nesb01t/frontend-yjh
5911ab7f3b62dea34051803ef745c59db1fceb06 Merge pull request #34 from Nesb01t/backend-dev
bdd956e133fe0a5de6b92f374a99b78385cb5b53 Update 第一周
a5132f098fbae9a7d0888818b9eedbfc1b5d90b9 Merge pull request #33 from Nesb01t/frontend-yjh
2c983d840864dd099c49e54d029c38bf41bab01b Login and studentinfo

git remote repository

First of all, you need to prepare a github / gitee account, which belongs to github / gitee 代码仓库托管平台. The emergence of git makes the collaboration between programmers easier, because we can remotely perform different tasks on the same piece of code, and finally merge the code. This github / gitee is used to save this code, and git is the core mechanism of this platform.

Open the github platform and click new repository to create a new repository.

After filling in the necessary information, pay attention to choosing Public to open source, and then your code will be seen by everyone and shared with every user on the github platform.

After the creation is successful, your warehouse will get an exclusive link, such ashttps://github.com/nesb01t/RPALite.git

remote link remote warehouse

  • Enter git remote to see which remote warehouse links are currently available.
  • The remote warehouse link can be added by command git remote add [name] [link] , such asgit remote add origin https://github.com/nesb01t/RPALite.git
  • The name of the remote warehouse can be changed by command git remote rename [name] [newName]

push Push the code to the remote warehouse

The code can be pushed to the remote warehouse by the command git push [remoteName] [branchName]git push origin master . For example , it means to push to the warehouse named origin , and the pushed branch is master . The branch will be explained in more detail later.

[Optional] Use ssh public and private keys for remote authentication

When you connect to a remote warehouse to log in, you may encounter a situation where 账号+密码cannot be used. At this time, you can use the more convenient and safer ssh login. There is a lot of space here, so I won’t expand on it for the time being. Interested friends, please try google research by yourself ~

git branch management

The concept of branches

Every time we carry out a commit submission activity, a hash value will be generated, similar to this 39a14, 92c8awhich represents the entire object you submitted once;

A branch is a file containing this hash value, which can also be simply understood as a pointer to a commit object ;

When we initialized the warehouse, we also created a new master branch;
when we submit for the second time, the master branch will also move forward together!

Generation of new branches

When we create a new branch newBranch and modify it on the newly created branch, two different branches will be formed, one is master and the other is newBranch ;

In the branch model, the common flow model

Generally there are several branches:

  • master - master branch, eg 1.0 -> 1.0.1 -> 1.1
  • hot fixes - fix bug branch
  • release - the running branch that pushes the release forward
  • develop - the development branch that makes up the release
  • feature - Small feature update branches that make up develop

These branches are brought together layer by layer from bottom to top to form a branch model, such as:

  • From 1.0 as the starting point, the develop branch was separated to develop version 1.1, and hot fixes were separated to fix bugs in version 1.0, and then merged into the master branch to produce version 1.0.1;
  • The development branch separated from 1.0 contains multiple feature branches, and the merged development branches are finally aggregated together to form a release. After the release matures, it is merged into the master branch to produce version 1.1;

log view branch

commit ab829b0e37904181952f37cdddf9169b1697efae (HEAD -> master, origin/master)
Author: nesb01t <[email protected]>
Date:   Mon Apr 17 17:24:06 2023 +0800

    Update 4月16更新

When you enter git log , our branch is in the brackets, and we will find that we are in the master branch;

There is also an origin/master at the back to represent the master branch on the remote warehouse ;

This HEAD means that we are now on this master branch, not the origin/master branch.

status view branch

Enter git status , you can see:

On branch master
Your branch is up to date with 'origin/master'.

It means that we are now on the master branch and synchronized with the remote origin/master ;

branch view branch

It is commonly used to enter git branch / git branch --list to see which branches 查看本地are there :

  backend-dev
* master
  origin

Among them, the one with an asterisk is the branch we are currently in~

branch creates a branch

Enter git branch newBranch1 directly to 创建create a new branch called newBranch1;

  backend-dev
* master
  origin
  newBranch1

checkout switch branch

Enter git checkout newBranch1 , and you will 切换arrive at the newBranch1 branch;
at this time, enter git branch to see that the switch is successful now:

  backend-dev
  master
  origin
* newBranch1

In addition, enter git checkout -b newBranch2 , you can directly create a new branch 并切换to the newly created branch~

merge merge branches

I want to merge master and Feature1 , how to do it?

  1. switch to the master branch
  2. entergit merge Feature1

Conflicts in branches: When two branches modify a file at the same time, there may be conflicts. The general prompt is

Auto-merging <文件名>
CONFLICT (content): Merge conflict in test_file
Automatic merge failed; fix conflicts and then commit the result.

At this point, first enter git status to confirm where there is a conflict, and 手动解决冲突then commit

stash temporary storage function

When your code is halfway written, you need to go back to the master branch for operation: you cannot switch directly at this time, because there are modified files in your working directory!

You can submit the half-written code through commit , but a better way is to put stash directly in the temporary storage area

  1. When your code is half written, enter git checkout Feature1 at this time , and you will be prompted to commit or stash the code before switching branches;
  2. After entering git stash, it can be stored temporarily. At this time, if you enter git status, you will find that the directory is clean, and then checkout~
  3. When you're done, how do you recover when you come back? Enter git stash apply to restore the previously edited state.

If you have saved it several times, that is, you have entered git stash several times, you can use git stash list to look back and forth at the stash temporary storage area code submitted several times before .

git undo operation

Many times you will encounter the need to undo the operation, see below~

reset rebound

  • git reset head~ The file has been modified, but it is no longer in a temporary state
  • git reset head~ --soft staging state still exists
  • git reset head~ --hard give up the data directly, not recommended

head and tilde ~

In the command git reset head~, head refers to the latest submission, and head~ refers to the last submission of head. Similarly, head~2 can be used to represent the last submission.

rebase rebase

For example, I developed branch A, and my colleague developed branch B. We can use merge or rebase~

  1. switch to branch B
  2. Enter git rebase A
  3. At this point, the modification of the B branch will be moved to A

Use it with caution remotely. When your colleague builds a second floor above you, you rebase, and when your colleague finishes building it, you may find that your first floor is gone.

Summarize

Do more projects to get started, no one else is familiar with it

Guess you like

Origin blog.csdn.net/Littlelumos/article/details/130166844