Table of contents
Written in front - Termux
Termux is an Android terminal emulation application for setting up a complete Linux environment on an Android phone. Termux does not require root privileges to run properly. Termux basically implements many basic operations under Linux. Therefore, we can also use git to submit projects to github through termux.
The git syntax in the article is also applicable to git submission under windows and linux operating systems.
Preparation
First open termux, use the following command to install the git toolkit
pkg install git -y
After the installation is complete, use the cd command to enter the project folder.
Of course, don't forget to create a new project warehouse on github. If you don't know how to use github, you can Baidu by yourself.
text
1. Use git to upload the source code
First, after cd into the project folder, we type in termux
git init -b main
Among them, git init
the current folder will be initialized as a local warehouse, and -b main
the parameter is to modify the default branch of the local warehouse to main.
Of course, you can also not use this parameter, but after a certain update of github, the default remote branch of github’s new warehouse has changed from master to main, and the git toolkit in the termux mirror source still uses master as the default branch-b main
during initialization . Subsequent commits without modification will have many difficult problems to deal with. I still recommend using this parameter here.
After that we generally get something like this:
Depending on the operating system, the path behind will be different.
Then, type
git add .
Among them git add
is the command used to mark as a warehouse file, followed by . will mark all files in this directory. After typing, we usually get this result:
Git tells us that this is an unauthenticated and safe location. We only need to type the provided commands according to its prompts. The commands provided by different project folder paths are also different. For example, what is provided here is the following on the windows system git config --gpobal --add safe.directory /storage/....
. The path may be C:/Project/....
.
Follow the prompts to type in the command and use it again git add .
. If you don't receive a prompt, the mark is complete.
Then, we type:
git commit -m "提交说明"
git commit
You can submit the project of the local warehouse to the cache area of github. If you don’t use it, you -m "提交说明"
will use a text editor (vi on linux or termux) to create a file by default. You are required to enter the submission instructions in the file and save it after the user It is very cumbersome to start submitting, so it is recommended to use -m
parameters directly. The content of the submission instructions can be filled in by yourself, which varies from person to person.
The first time you use it, git commit
you will be prompted to configure account information. Type the following command directly
git config --global user.name "Example"
git config --global user.email [email protected]
Where
"Example"
is your username when you registered on github
[email protected]
is your email address when you registered github
then type againgit commit -m "提交说明"
The content of the prompt varies from case to case, but basically you see a prompt like this saying that the submission cache has been successful.
Next, we type
git remote add Remote名称 你的仓库的地址
The meaning of this string of commands is to add a Remote to the project. The Remote is bound to a warehouse address. The Remote name and warehouse address are determined by the individual. The warehouse address can be found on the github warehouse page. Note that the https address should be used here , there is no command prompt after typing, indicating that the remote has been added successfully.
We can use this command to add multiple warehouse addresses for a project.
Use after adding remote
git push -u 你添加的remote名称 main
After that, you will be asked to enter your user name and password, pay attention! ! The password here does not refer to the password of the github account, but the token token ! ! We need to apply for a token in the settings of github.
First, open the settings of github and find Developer settings
Then find Personal accese tokens
Choose to create a new token, and then enter the password for verification, and you can jump to the key generation page. The permissions below can be selected by Google Translate, and the time limit can also be set by yourself. Finally, use the key and follow the prompts to enter the git push -u 你的remote名称 main
user Name and key, please note that entering the password in Termux or linux system will not display the entered password.
As shown below
Indicates that the submission of github has been successful
2. Advanced - about branches
Git has a powerful branch management system. Here is a simple tutorial on modifying the default commit branch in the termux environment.
First of all, when we first use git init -b main
initialization, we can not use main as the default branch of initialization. The branches of github are divided into local branches and remote branches. On the github web page, we can delete, add, and modify our own branches. Locally, we can also delete or add branches through commands. Here we only provide how termux does not use it locally. The default branch upload method, more commands can be Baidu by yourself.
First create a new branch in the github repository, basically you can do it with Google Translate. Then remember the name of the new branch, as 1234
an example, type the following command on the command line to create a new local branch
git branch 1234
Then use the submit command again
git add .
git commit -m "提交说明"
git push -u remote名称 1234
The project can be uploaded to the new branch, and the git branch -D 1234
local branch can be deleted by using this command, but this command cannot directly delete the local default branch. The specific modification method can be found in Baidu.
Similarly, we can also modify the default branch to what we want when creating a warehouse, and git init -b main
modify the main to a custom default branch when using it.
3. Advanced - use SSH to upload github projects
Frequently entering tokens to upload files is very annoying. We can use openssh to add ssh public keys to our github in termux to realize ssh passwordless submission projects.
First install openssh in termux
pkg install openssh
After installation is complete type
ssh-keygen -t rsa
will be prompted later
openssh under termux will only recognize the .ssh folder in the installation directory, and only recognize the id_rsa
key file named , so press Enter here. You will be prompted later Enter passphrase (empty for no passphrase)
, because you want to achieve password-free transmission, so press Enter twice here. After that the ssh key is generated
Then enter according to the save directory cd, and use ls to see that there are two files, one is id_rsa
and the other is id_rsa.pub
, we use vim or vi to open id_rsa.pub
(you can use the cp command to copy the file to other available file browsers if you don’t use vim and vi Open the location, directly use the txt format to open), copy all the content in the file. Go back to the setting page of github, find SSH and GPG Keys, and create a new SSH Key.
Enter whatever you want in the title, keep the key type unchanged, fill in id_rsa.pub
the content you just copied in the key and save it.
Then use git remote add
the ssh link for your warehouse to create a remote (the ssh link can be found on the github warehouse page), and git push
replace the original https remote with ssh remote when uploading, so you don't need to enter a password for authentication.
4. Violent upload
Sometimes due to different file contents (for example, readme.md was added at the beginning of the build, but no new readme.md was created locally), the file modification time lags behind the upload file, and the submission of github often fails. At this time, violent upload can be git push -u remote名称 分支名称 -f
used document.
It is not recommended to do this, because the method of file recovery after violent upload is very complicated, and it will also cause a series of troubles when multiple people collaborate.
write at the end
The above is all my personal experience about using git in the Termux environment, thank you for reading.