Preface: Recently, I need to open source my code to my own Github warehouse, but found that there is no option to upload a folder with one click, so I refer to the existing tutorial for practice, and it succeeds✌️The following are the detailed steps.
Tips: After the article is accepted, small partners can choose to open source their code, so that peers can carry out academic research more conveniently.
step
Preparations: Github side
1. Register a Github account
Register an account at https://github.com/join , students who already have an account skip this step.
2. Create a new Github warehouse
Step 1: Log in to the Github website, open the URL https://github.com/ , and follow the steps below:
Step 2: Come to the following interface, fill in the corresponding information, and finally click Create Repository
to complete the creation of the new Github warehouse.
At this point, the preparations on the Github side are ready! Next we enter the local operation.
Preparations: local side
1. Install Git
Note: Students who have installed Git ignore this step.
Download Git at https://git-scm.com/downloads , Mac OS version needs to be installed.
Here I use the first method, which is Homebrew
to install with help Git
, so the first step is to install Homebrew
.
Step 1 : Install homebrew. Reference URL: https://brew.sh/
Specific operation: Open the Mac terminal and run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Successful installation interface:
Step 2 : Install Git. Reference URL: https://git-scm.com/download/mac
Specific operation: Mac terminal continues to run the following command:
brew install git
After installation, use git --version
the command to view the Git version, and the corresponding version output indicates that the installation is successful. The corresponding interface is as follows:
2. Create a local project repository
Step 1: Open the terminal under the path where the local project is located, or use cd pj_path
the command to switch the terminal to the path pj_path
where the local project is located, where is the path where the local project is located, where the absolute path where my local project is located /Users/meilinger/Desktop/ICB
is ICB
the name of my local project.
Step 2: Use git init
the command to initialize the local warehouse. After this step is executed, a .git
hidden folder named " will appear in the root directory of the local project. (PS: Use the shortcut key Command + Shift + .
three keys to view hidden files)
Heavy! Joint operation of local end & Github end
In the previous operations, both the local end and the Github end are preparatory work, and there is no priority between the two. The following is the highlight~
1. Configure SSH and git config
Overview: First generate locally SSH key
(step 1 ➡️ step 3), and then SSH key
add it to SSH key
the list on the Github official website (step 4).
Step 1 : Enter ssh-keygen -t rsa -C email_address
the command in the terminal, where is email_address
the email address used when registering Github. ( Note ⚠️: If Github is bound to multiple mailboxes, fill in the main mailbox here .) Press Enter to enter the next step.
Step 2 : Enter the generation path of the SSH key. Here I SSH key
set the save path to /Users/meilinger/.ssh/id_rsa_ICB
, as you can see, for easy memory, add the local project name ICB
to the end of the path ~ Note: If you press Enter directly, SSH key
it will be saved to the default path provided by the system (here my default build path address is /Users/meilinger/.ssh/id_rsa
). If the key with the same name already exists in the current path, you can choose to rewrite or not. Enter, enter password + enter + confirm password + enter. Just press Enter without a password.
So far, the SSH key has been generated~
Step 3 : Executing the command in the terminal pbcopy < ~/.ssh/id_rsa_demo.pub
will SSH public key
copy it to the clipboard, and the interface shown in the figure below indicates that the operation is successful.
Step 4 : Go back to the Github official website, click the account avatar , Settings , SSH and GPG keys , and New SSH key in turn, as shown in the figure.
After clicking New SSH key, the system will automatically jump to the following interface. In this interface, fill in the Title
and Key
fields respectively. Here Title
, I fill in the local project name, and the field is what we just copied Key
using the command in step 3. Then click to complete the adding operation.pbcopy < ~/.ssh/id_rsa_demo.pub
SSH public key
Add SSH key
Next, go back to the terminal and use the command ssh -T [email protected]
to verify whether the addition is successful. If the addition is successful, the following interface will appear:
If the terminal returns
Permission denied (publickey)
the field, it means that the above SSH key has not been added successfully. Solution: Execute in the terminal命令 ssh-add ~/.ssh/id_rsa_ICB
, remember to replace it with your ownSSH key
path.
Step 5 : Configuration git config
. As shown in the figure below, in the terminal, enter the commands in sequence:
git config --global user.name github_name
git config --global user.email github_email
Among them, github_name
is the name corresponding to the Github account, and github_email
is the main mailbox in the Github account, which is the mailbox used in the preceding steps.
2. Upload code
First: Make sure that the current path of the terminal is the path of the local project, that is, the .git
parent directory of the hidden folder.
Step 1 : Execute the command git add .
to add all the files in the local project to git, .
the meaning of the command is "all files".
Step 2 : Execute the command git commit -m "description"
, add the description text of the project, description
the custom description text is in the command.
Step 3 : Execute the command git remote add origin pj_ssh_path
, which pj_ssh_path
can be copied and pasted through the following picture, for example, here pj_ssh_path
is [email protected]:meiling-fdu/ICB.git
.
⚠️ PS: This step may report an error " 错误:远程 origin 已经存在。
", the solution is as shown in the figure:
First execute the command git remote rm origin
to delete the existing remote configuration, and then execute the above command git remote add origin pj_ssh_path
.
Step 4 : Execute the command git push -f origin master
to upload the file of the local project to the official Github website. The error is found as follows:
According to the content of this blog , I found that it is a problem related to the mailbox setting on the Github official website, that is, we have checked the two options on the mailbox setting page , as shown in the following figure:Keep my email addresses private
Block command line pushes that expose my email
So the first solution is to cancel the above two ticks ☑️. But if we don't want to cancel the above settings, we can solve it by the following method:
-
Get Github's recommended email account. It can be viewed in the purple underlined position shown in the figure above on the mailbox settings page. You can see that my recommended email account is
[email protected]
-
Execute the command
git config --global user.email “推荐E-mail”
to reset the global user E-mail, here is“推荐E-mail”
the recommended email account above.
Execute the command on the terminal git config --global user.email
to check whether the global email account has been successfully modified. As can be seen from the figure below, the global email account has been successfully modified.
- Execute the command
git commit --amend --reset-author
to reset the author information. Enter the command and press Enter to enter the vi editing mode, save and exit under the English input method:wq
. (As shown below)
PS: If the following error occurs, according to this blog , the solution is: execute the command git stash
, as shown in the figure below:
After all the above errors are checked, execute the command again git push -f origin master
, and the upload will be successful!
At this point, refresh the GitHub page, and you can see that the project has been uploaded successfully! (According to this blog, pay attention to switch to master
the branch~)
Postscript: I used to feel that code hosting was very cumbersome, and I always wanted to learn it, but I put it on the shelf because I didn't just need it. It's not that difficult to use it today! I have mastered another skill, and it really is "everything is difficult at the beginning". Wow, today's harvest: Just do it!
References
- Step by step to teach you how to upload your own project on GitHub_github upload project_Xia Yuwei'an's Blog-CSDN Blog
- [Git] Mac uploads existing projects to GitHub - Short Book (jianshu.com)
- Solution to git push origin master error & common git commands (to be updated)_Wang Xiaoxi ww's Blog-CSDN Blog
- git using --rebase_ cannot rebase pull: You have unstaged changes. _Three fish blog-CSDN blog
- [git error –>! remote rejected master -> master (push declined due to email privacy restrictions)
- After GitHub submits Push, it is found that the code is in the Master branch. The default main branch has no code.