An article to learn Git version management

sequence

Articles are not completely connected and typeset according to strong logic. You can jump according to the directory according to your needs.
Sahua Sahua, after studying for so long, I finally finished it. To study
computer science, especially for software development, it is really recommended to learn Git first and understand the use of GitHub, otherwise you will really miss too much time to learn excellent projects. Like me, regret it.


What is Git

Git is currently the most advanced distributed version control system in the world.

Git is developed in C language.

Distributed version control system

Features:

  1. Server-based and client-side operating modes
  2. The server saves all updated versions of the file
  3. The client is a full backup of the server and doesn't just keep the latest versions of the files

advantage:

  1. Network operation, support multi-person collaborative development
  2. After the client is disconnected from the network, it supports offline local submission of version updates
  3. Backups from any client can be used to recover after server failure or corruption

Repository

What is a repository

Version library, also known as warehouse, repository. It can be simply understood as a directory. All files in this directory can be managed by Git. Git can track the modification and deletion of each file, so that the history can be tracked at any time, or it can be "restored" at some point in the future. ".

Git installation and configuration

Follow the prompts to install until the next step.

Configure user information to record personnel information for operation modification:

git config —global user.name "您的昵称"

git config —global user.email "您的邮箱"

warehouse management commands

initialization command

By git initturning catalog files into manageable repositories.

Add files to staging area

By git addadding a file to the repository, you start tracking changes to that file.

effect:

  1. You can start tracking new files with
  2. Put the tracked and modified files into the temporary storage area
  3. Mark conflicting files as resolved

other:

Add multiple files to the temporary storage area at one git add .time Add all new and modified files to the temporary storage area at one time

Submit files to local repository

By git commitsubmitting files to the repository, be sure to -madd comments.

Skip using the staging area

git commit -a -m "描述信息"Submit the files in the workspace directly to the Git repository

View file status

By git statusviewing the status of files in the current warehouse.

Show file status in a compact way:git status -s 或者 git status -- short

?? Indicates untracked state, untracked

A means newly added to the status of the staging area

M indicates the status of files that have been modified and not put into the temporary storage area

M indicates the status of files that have been modified and placed in the temporary storage area

View the similarities and differences between the file in the current workspace and the local warehouse

By git diffviewing the difference between the before and after files, that is, the changed content.

View historical version

By git logviewing the historical version of the file in the warehouse, add –pretty=onelinea line to display.

unstaged file

git reset HEAD 要移除的文件名称

Fall back to the specified version

git reset --hard commit_idRoll back to the state of the repository where the version number resides via .

commit_id:

  1. The version number, you can use HEAD to represent the current version, the previous version is HEAD , the previous version is HEAD ^, and the previous 100 versions can be expressed as HEAD~100
  2. Can also be a numeric number expressed in hexadecimal

remove file

  1. Remove the corresponding files from the Git repository and workspace at the same timegit rm -f index.js
  2. Only remove the specified files from the Git repository, but keep the corresponding files in the workspacegit rm --cached index.css

View commit history

View git reflogthe content of the version notes submitted by the history.

The difference between git log and git reflogis: the former is the detailed information of the historical submission, including the version number, author information, date, and comments at the time of submission, and the latter is to directly view the version comments.

Undo workspace changes

git restore to discard changes in working directory

git checkoutto discard changes in working directory

The above two commands are to undo the modification of the workspace. git checkoutIn fact, it is to replace the version in the workspace with the version in the repository . No matter whether the workspace is modified or deleted, it can be restored with one click.

remote warehouse

Both Gitee and GitHub can provide Git warehouse hosting services. After registering the relevant account, you have a remote warehouse.

The transmission between the local warehouse and the remote warehouse is encrypted through SSH. The configuration steps are as follows:

  1. Create SSH keys. In the directory, check whether there is a .ssh directory, and C:\Users\Administratorwhether there are two files in this directory . If there is, you can skip it directly. If not, execute the following command to create an SSH Key:id_rsaid_rsa.pubGit Bashssh-keygen -t rsa -C "[email protected]"

  2. Log in to the account of the remote warehouse, find the word SSH under the settings, and paste id_rsa.pubthe contents of the file in the Key text box
    insert image description here
    insert image description here

  3. added complete

Add remote library

Find the Create a new repository page, click the Create a repository button to create a new repository named learngit.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-oftTXrhk-1655044110365)(https://secure2.wostatic.cn/static/kTacVsGLm5WjJmcqy4Vwq2/image.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-pMMSQrdr-1655044110365) (https://secure2.wostatic.cn/static/hu1GHsqj2o323WHfL7JBvH/image.png)]

Associated remote library

git remote add origin [email protected]:ahcici/learngit.git

Replace .com the following ahcici with your own account name, the name of the remote library is origin, and it can also be changed to other.

Push local library content

git push -u origin main

In addition, -u it will associate the local branch with the remote branch, and only use it when pushing for the first time

Can simplify future pull/push commands

Subsequent push commands can begit push origin main

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-J4TGRUTQ-1655044110366)(https://secure2.wostatic.cn/static/2sbPiMELqBYNYBodTVhxWC/image.png)]

The branch name after origin depends on whether you have changed it yourself, and the content prompted after the path shall prevail.

origin is an alias for the remote repository.

delete remote repository

It's not really deleting the remote library, it's just canceling the connection between the local warehouse and the remote warehouse.

By git remote -vviewing the remote library information.

By git remote rm origin<远程仓库名称>unbinding the local and remote warehouses.

Clone the remote repository

If I accidentally delete my local learngit directory, because I have already pushed the remote library, then I can copy a copy from the remote library to the local, also known as cloning.

First of all, you have to know the address of the warehouse to be cloned first, which is the same as the address of the warehouse we linked before, which is in this format: [email protected]:ahcici/learngit.git

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-nWxVhj2z-1655044110367)(https://secure2.wostatic.cn/static/sGnemBZ4CzMkn5peuCh8o8/image.png)]

Operate according to the figure, and finally use git clone [email protected]:ahcici/learngit.git<你第四步复制的地址>the command to clone to the local.

branch management

create branch

git checkout -b dev(分支名)Adding -bparameters means creating and switching is equivalent to git branch dev[Create Branch] + git checkout dev[Switch Branch]

view branch list

git branch View the current branch list The * in front of the branch name indicates the current branch

View the list of branches in the remote repositorygit remote show origin

merge branch

git merge dev(分支名) Merge the specified branch into the current branch

When merging branches, use --no-ffparameters to disable fast forward and use normal merging.

delete branch

git checkout -d dev(分支名) Delete the specified branch

delete remote branch

git push 远程仓库名称 --delete 远程分支名称

switch branch

git switch -c devCreate and switch to new dev branch

resolve conflict

In two different branches, different modifications are made to the same file, that is, git cannot perform "quick merge", and can only try to merge the respective modifications, so there will be conflicts, which need to be manually modified and then added and submitted .

branch merge diagram

git log --graph You can view the branch merge graph.

git log --graph --pretty=oneline —abbrev-commitOne line showing the merge graph and commit message abbreviation.

Related concepts

Open Source License Agreement

BSD

Apache License 2.0

GPL: An infectious open source agreement that does not allow modified and derived code to be released and sold as closed source commercial software

The most famous software projects with GPL are: Linux

LGPL

MIT: It is currently the least restrictive agreement, the only condition: in the modified code or distribution package, the original author's license information must be included

Software projects using MIT include jquery, Node.js

Open source project hosting platform

A website dedicated to storing the source code of open source projects for free

GitHub

Gitlab

Gitee

Two access methods for remote warehouses

  1. HTTPS: zero configuration; but every time you access the warehouse, you need to repeatedly enter the GitHub account and password to access successfully
  2. SSH: Additional configuration is required; however, after the configuration is successful, you do not need to repeatedly enter the github account and password every time you access the warehouse

In actual development, it is recommended to use SSH to access the remote warehouse.

SSH Key

Function: Realize login-free encrypted data transmission between local warehouse and github.

Workspace and staging area

The workspace is the file directory we operate. In addition to the workspace, there is also a temporary storage area, which is the git addlocation where the files will be stored in the future, and git committhe files will be submitted to the master branch in the future.

In layman's terms, it's like eating in a restaurant. Several of us ordered first, but we would like to change a certain dish or add a few more dishes on the way. When we are full at the end, we have to pay, then The actual amount paid is the value of all menu items after the last modification. Of course, this example may not be appropriate, but it is enough to show that the contents of the work area, temporary storage area and the final branch are different.

Note that Git tracks and manages changes, not files.

master main branch: used to save and record the completed function code of the entire project

Feature branch: a branch dedicated to developing new features

common mistakes

Q:fatal: not a git repository (or any of the parent directories): .git

A: The current directory folder is not a Git warehouse, that is, it is not .gitmanaged, and the operation is invalid

Q:fatal: pathspec 'readme.txt' did not match any files

A: The file cannot be matched readme.txt, that is, the file does not exist or the file name is incorrect and cannot be found

Guess you like

Origin blog.csdn.net/weixin_51229662/article/details/125114907
Recommended