Geek's git common command manual
1.1 Permission Configuration
Note:
- Codeup supports these two encryption algorithms at the same time, but other platforms RSA is still the mainstream
- Jenkins seems to only support RSA, not ED25519, so be careful when using it
1.1.1 create ssh key
- Based on RSA algorithm (the most compatible)
The command to generate a key pair based on the RSA algorithm is as follows:
ssh-keygen -t rsa -C "<注释内容>"
Example:
ssh-keygen -t rsa -C "[email protected]"
- Based on ED25519 algorithm (faster and safer, some platforms are not compatible)
The command to generate a key pair based on the ED25519 algorithm is as follows:
ssh-keygen -t ed25519 -C "<注释内容>"
- Comments will appear in the .pub file, generally you can use the mailbox as the comment content
- For detailed steps, refer to: Configure SSH key
Example:ssh-keygen -t ed25519 -C "[email protected]"
1.1.2 How to automatically select the key for authentication according to the target platform when there are multiple keys locally?
When there are multiple keys locally, if no authentication rules are set, the machine will randomly select a key for authentication, which may cause authentication failure.
Therefore, in the following scenarios, you need to define the path of the authentication key yourself:
- There are multiple local keys corresponding to different accounts of Cloud Effect.
- There are multiple local keys corresponding to different code platforms (GitLab, GitHub, Yunxiao, etc.).
Define authentication key path rules
~/.ssh/config
The configuration content is as follows: (if it does not exist, please create a new one)
# Codeup 示例用户1
HostName code.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
# Codeup 示例用户2,设置别名 codeup-user-2
Host codeup-user-2
HostName codeup.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/codeup_user_2_ed25519
# GitLab 平台
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_ed25519
According to the above configuration, when using the SSH protocol to access, the SSH client will use the key specified in the file for authentication, so that access to different platforms or different accounts on the same platform use different local SSH keys for authentication.
When accessing Codeup, since the HostName is consistent, use an alias to distinguish and use different keys.
To access GitLab, different keys are used to differentiate based on HostName.
# 访问 Codeup,将使用 ~/.ssh/id_ed25519.pub 密钥
git clone [email protected]:example/repo.com
# 以 codeup-user-2 别名访问 Codeup 时,将使用 ~/.ssh/codeup_user_2_ed25519 密钥
git clone git@codeup-user-2:example/repo.com
# 访问 GitLab 平台,将使用 ~/.ssh/gitlab_ed25519 密钥
git clone [email protected]:example/repo.com
1.2 Basic Information Configuration
1.2.1 Configure username
git config --global user.name qingfeng.zhao
Verify that the configuration is correct
git config --global user.name
1.2.2 Configure user mailboxes
git config --global user.email [email protected]
Verify that the configuration is correct
git config --global user.email
1.2.3 Set file name case sensitivity
git config --global core.ignorecase false
- The default configuration of git under Windows is case-insensitive, but it is case-sensitive on the Linux server.
- true will ignore the case of the file name, so it is strongly recommended to modify this global configuration to false
1.2.4 Set the display color of the command line
git config --global color.ui auto
1.2.5 Check all git global configurations
git config --global --list
1.3 Common Operations
1.3.1 Associate a local project without version control to the created git repository
Other code platforms:
cd existing_folder
git init --initial-branch=master
git remote add origin [email protected]:geekxingyun/spring-boot-best-practices-sample.git
git add .
git commit -m "Initial commit"
git push -u origin master
github is slightly different because the default branch is main instead of master
cd existing_folder
git init --initial-branch=main
git remote add origin [email protected]:geekxingyun/spring-boot-best-practices-sample.git
git add .
git commit -m "Initial commit"
git push -u origin main
1.3.2 Download a project from the git repository
git clone -b master url
Tips:
- cd existing_folder------------------- enter the existing folder
- git init ---------------------------------- git initialization will generate hidden files.git
- git remote add origin [url]---------- git remote warehouse definition alias
- git add------------------------------ git adds all files under the current path
- git commit -m "init project"---------------------------- git commits to the local library
- git push -u origin master----------- git is submitted to the remote warehouse
1.3.3 Other basic operation usage
git command syntax description | command explanation | command example |
---|---|---|
git commit |
Save local changes to the local repository | git commit -m "init project" |
git push |
Push changes from the local repository to the repository on the server | git push |
git fetch |
It is equivalent to getting the latest version from remote to local, and will not automatically merge | git fetch |
git merge |
Merge remote warehouse with local warehouse | git merge |
git pull |
It is equivalent to getting the latest version from the remote and merging to the local, which is equivalent to executing git fetch first and then git merge | git pull |
git checkout [branch-name] |
Switch to the specified branch and update the working directory | git checkout master |
git merge [branch] |
Merge the history of the specified branch into the current branch | git merge dev |
git rm --cached [file] |
Remove the file from version control, but keep it locally | git rm --cached readme.md |