git study notes
Table of contents
5. Rollback version (understand)
Eight, .gitignore file (understand)
Nine, github ssh-key configuration
Ten, local warehouse and remote warehouse content push
1. What is git
Git is a distributed mainstream version management control system.
Combined with the GitHub management hosting platform,
a database called warehouse is used to control the changes of files. Each file in the warehouse has a complete version history record, and you can see when the submitter was What content of which files have been modified, and the historical version can be rolled back if necessary
2. Create a warehouse
Method 1: Create a warehouse directly on your computer locally
Create a folder mkdir folder name as the project repository
Then enter the directory and enter the command: git init
Or initialize directly in an existing local project folder
git init
The above echo statement appears, indicating that the creation is successful, and there will be an additional hidden folder.git
ls cannot see hidden folders, you need ls -a to see all folders under the directory
Of course, you can directly initialize a warehouse, directly add the warehouse name after the command, and the corresponding folder will be generated directly
git init my-repo
Method 2: Clone an existing warehouse from a remote server
git clone github/gitee项目地址
例如:git clone https://github.com/jjw-DL/OpenPCDet-Noted.git
3. Work area and file status
Workspace : the directory that can actually be seen on the computer
Temporary storage area : also called index, used to temporarily store uncommitted content, generally in the index under the .git directory
Local warehouse : Git's local version library, warehouse information is stored in the hidden directory .git
Remote warehouse : a warehouse hosted on a remote server, such as github\gitlab\gitee
file status :
Modified : The file has been modified, but not saved to the temporary storage area
Temporary : Put the modified file into the temporary repository
Submitted : Submit the files in the temporary storage area to the local warehouse
Mainly the above three, the next two are less mentioned
(untracked): newly created files that have not been managed by git
(unmodified): has been managed by git, the content has not been modified
The figure below shows some conversion relations
4. Add and submit files
git init //创建仓库
git status //查看仓库状态
git add //添加到暂存区
git add *.后缀名 //添加一类文件到暂存区
git add . //把当前文件夹下所有文件添到暂存区
git commit //提交
git commit -m "message" //提交所有暂存区的⽂件到本地仓库
git commit -am "message" //提交所有已修改的⽂件到本地仓库
git log //查看提交记录信息
git log --oneline //查看提交历史,--oneline表示简介模式
ps:
git diff //查看未暂存的⽂件更新了哪些部分
git diff <commit-id> <commit-id> //查看两个提交之间的差异
git ls-files //查看暂存区的文件
For example to view status:
This is the submitted status
Untracked files just created are shown in red
git add can use wildcards to add multiple files with the same suffix
Example: There are four red untracked files, both txt and sh
Add files ending with txt to the temporary storage area, use the command
git add *.txt
Check the status after running the command . Green means it is in the temporary storage area, and red means it has not been added to the temporary storage area in the workspace
git commit will only submit the files in the temporary storage area, not the files in the work area
5. Rollback version (understand)
git reset
git reset --soft //工作区和暂存区都不清空
git reset --hard //工作区清空 暂存区清空
git reset --mixed //工作区保留 暂存区清空 --默认此场景
For example, the following three submissions:
The following is the version number of the git log query
Make three copies to familiarize yourself with the three parameter operations
Example: try soft fallback:
View commit history:
back to the second commit
The workspace file is still there, the content has not changed, and the temporary storage area file is still there
Check the status and return to the second submission version. At this time, file3 has not been submitted yet, and it is still in the temporary storage area, showing green
The hard parameter is rolled back to the previous version: file3 in the temporary storage area of the workspace is gone
The mixed parameter is rolled back to the previous version: the workspace is still there, but the temporary storage area is gone
Six, view the difference
7. Delete files
rm <file> ;git add <file> //本地工作区删除文件,然后暂存删
git rm <file> //从暂存区和工作区同时删除
git rm --cached <file> //暂存区删除,保留在工作区中
git rm -r * //递归删除某个目录下所有子目录和文件,删后提交下
ps:
git mv <file> <new-file> //移动⼀个⽂件到新的位置
About adding after rm , you can see that after simply rm , the temporary storage area is not deleted, so there is the command git rm file , just once,
But still remember to git commit to submit, otherwise, the deleted file still exists in the repository
Eight, .gitignore file (understand)
The function is to let us ignore some files that should not be added to the version library, making the warehouse smaller and cleaner
Nine, github ssh-key configuration
After github builds the warehouse, there are two remote warehouse addresses: https and ssh.
The way at the beginning of https is to verify the account and password when we need to push the local code to the remote warehouse
The ssh protocol at the beginning of git does not need to verify the user name and password when pushing, but it needs to add the ssh public key configuration on github, which is more secure
github configuration ssh key:
Directly git clone the address on github, and found that the connection could not be established
type:
ssh-keygen
Echo the generated location of the key file, press Enter directly, jump out to set the secondary verification password, it is not necessary, press Enter directly, and then press Enter to confirm the secondary verification password, as shown in the figure below, success
Notepad opens the public key.pub file, selects all content, and copies
Open the github page, click on the avatar, enter the settings ,
Find ssh and gpg keys on the left column , enter, click add keys
As shown in the picture, you can take a name
It may jump out to verify the account password of github , just enter it
To test the connection, enter the following command
ssh -T [email protected]
test passed
Summarize:
Ten, local warehouse and remote warehouse content push
Clone the github repository we created again, the clone is successful
Review adding commands and pushing to the platform
Enter the warehouse, create a small file, write hello
git add is added to the temporary storage area, and git commit is added to the local warehouse
At this point, github still can't see our hello.txt
Because there is no push from the local warehouse to the remote platform warehouse
So, execute the git push command
At this point, refresh the github warehouse page and find that the push has been successful
10. Associate local warehouses with remote warehouses
git remote add origin ....github仓库地址....
----------------------------
git branch -M mian //指定分支名称为main
----------------------------
git push -u origin main
//把本地的main分支和远程的origin别名的仓库的main分支关联起来
//其实,完整语句应该是:
//git push -u origin main:main
//如果本地分支和远程分支名字相同的话,就写一个main就行啦
----------------------------
git pull origin main
//远程仓库上在线修改了文件的话,或者其他人的本地仓修改了推上去,
//而我们在本地仓库要同步的话,需要拉取同步,这时要使用pull
//作用是把远程仓库的指定分支拉取到本地进行合并
Take the example just now to illustrate the steps
It can be seen that the corresponding alias of the remote warehouse is origin, followed by the corresponding address
There are specific examples of push in the previous section
Demonstrate the pull operation
Add a readme file online on github
If you do not enter the alias branch name, the default warehouse alias is origin and the branch is main
The local warehouse has added a modified new readme file
Check the content, consistent
Note that after the pull is executed, the merge operation will be executed automatically. If there is no conflict between the modified content of the remote warehouse and the modified content of the local warehouse, the merge operation will succeed, otherwise the conflict will fail and need to be resolved manually
The fetch command can also obtain the modification of the remote warehouse, but it will not be automatically merged. The specific conflict problem will be recorded when learning the branch
Section summary:
2023.8.7