Git
- sequence
- What is Git
- Repository
-
- What is a repository
- Git installation and configuration
- warehouse management commands
-
- initialization command
- Add files to staging area
- Submit files to local repository
- Skip using the staging area
- View file status
- View the similarities and differences between the file in the current workspace and the local warehouse
- View historical version
- unstaged file
- Fall back to the specified version
- remove file
- View commit history
- Undo workspace changes
- remote warehouse
- Related concepts
- common mistakes
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:
- Server-based and client-side operating modes
- The server saves all updated versions of the file
- The client is a full backup of the server and doesn't just keep the latest versions of the files
advantage:
- Network operation, support multi-person collaborative development
- After the client is disconnected from the network, it supports offline local submission of version updates
- 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:
- You can start tracking new files with
- Put the tracked and modified files into the temporary storage area
- 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 -m
add 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:
- 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
- Can also be a numeric number expressed in hexadecimal
remove file
- Remove the corresponding files from the Git repository and workspace at the same time
git rm -f index.js
- Only remove the specified files from the Git repository, but keep the corresponding files in the workspace
git 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:
-
Create SSH keys. In the directory, check whether there is a .ssh directory, and
C:\Users\Administrator
whether 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_rsa
id_rsa.pub
Git Bash
ssh-keygen -t rsa -C "[email protected]"
-
Log in to the account of the remote warehouse, find the word SSH under the settings, and paste
id_rsa.pub
the contents of the file in the Key text box
-
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.
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
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
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 -b
parameters 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-ff
parameters 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 dev
Create 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-commit
One 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
- HTTPS: zero configuration; but every time you access the warehouse, you need to repeatedly enter the GitHub account and password to access successfully
- 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 .git
managed, 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