Use the git more advanced
Background
Since I have started my graduate student life, I found there are some new needs to be proficiency with git. New needs as followed:
- How to connect differenct remote repository : Both company and lab have requirements for code security and privacy. A lot of times, their codes is not allowed to open source. For this purpose, they build their own repository of code management. So the new demand for me is to how to push or pull my local git code to the differenct repository.
- Checkout/Merge : Thanks to HIT-SZ and Tuling-Robot Company, they assined me a new computer when I worked there. When I am in the company and lab, I would like to use the desktop computer to code some code. While I am in other place, like dormitory and classroom, I prefer to use my laptop computer. So I need a synchronization tool to synchronize my code which might be coded in different computer. Because of its’ own characteristic, git looks perfect. But only when I am on top of some advanced operation, like checkout and merge, I can use git to satisfy my new demand.
- How to use VSCode to git : VSCode is a really powerful software. Because of the lightweight, VSCode becomes my most commonly used software. And VSCode can extend git functionality by adding plug-in, it’s convenient.
How to connect differenct remote repository ?
References
- Git同时连接Github和Gitee(码云)
- 如何将代码同时提交到Github和码云Gitee上
- git 本地仓库关联到远程仓库
- git push error: src refspec main does not match any on linux
- 解决unable to access ‘https://github.com/xxx/xxx.git/‘: OpenSSL SSL_read: Connection was reset, errno
Clear Mind
When people push code from local repository to remote repository or pull code from remote repository to local, they would say “I git my code”. In a way, we can think of git process as a data transfer between localhost and remote server. Essentially, git is a process of network communication. The communcation there is more like https rather than http, because https has a network layer called SSL
, where it implements encryption algorithm.
Encryption algorithm can be divied into 2 classes, which is symmetic encrytion and asymmetic encrytion. For symmetic encryption
, the same key is used for both encrytion and decryption. For asymmetic encryption
, it uses public-private key encryption mechanism, which means the sender uses the private key for encryption while receiver uses the public key to decrypt.
Git communication process uses public-private key encryption mechanism. So when our local repository need to connect to different remote repository(etc. like n), we only need to generate n different public-private key pair.Meanwhile, the local repository need a ID to identify itself. In fact, the “ID” is user.name
and user.email
.
Practice-01 – SSH with code-hoster
First : Config your local ID
If you have configed the local ID, you can skip this step
1 | # git config [option] field value |
the change you make will save in the
C:\user\<user_name>
,you can modify the file to achieve the same purpose as the command line operation.
Second: Generate differenct public key for different remote repository
1 | # Suppose the 2 repositories are github and gitee |
Third: Add public key to the website interface of the remote repository
In github, for example:
In gitee, for example:
Forth: Edit config file
The config file will guide the localhost choose a correct private key to connect with the remote repository.
file path:
~/.ssh/config
1 | # gitee |
validation effect:
1 | ssh -T git@github.com |
Practice-02: Host a project to a different repository
Suppose I have a local repository named TestGit with the following directories:
Step1: Create remote repositories on different hosting platforms
Step2: Create local repository and link in to remote repository
1 | # 0.precondition: cd the project direction |
Step3-01: Push local code to remote repository
1 | # 0. If there are some files in the remote repository, the first thing is "pull" |
Checkout/Merge
Only a short while ago, I don’t think I will need to use git commands like checkout
and merge
someday.Therefore, I have been lack of motivation to study relevant knowledges. But today, I need.
There is a Git Cheat Book for you to find out how each command is used.
Despite the title of the section is checkout and merge, but we are not just talking about these 2 operations.
References
Advanced git add
1 | # Ususally use |
Checkout(Branch)
1 | # Create a local branch |
Merge
1 | # merge <branch_name> to current branch |
Reback
1 | # change to the newest code |
Log
1 | # log changes made by someone, -l will log the commit ID |