"Detailed Tutorial" Use git to upload local projects to Github warehouse (MacOS as an example)

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.


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:

insert image description here

Step 2: Come to the following interface, fill in the corresponding information, and finally click Create Repositoryto complete the creation of the new Github warehouse.

insert image description here

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.

insert image description here

Here I use the first method, which is Homebrewto 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:
insert image description here
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 --versionthe command to view the Git version, and the corresponding version output indicates that the installation is successful. The corresponding interface is as follows:
insert image description here


2. Create a local project repository

Step 1: Open the terminal under the path where the local project is located, or use cd pj_paththe command to switch the terminal to the path pj_pathwhere 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/ICBis ICBthe name of my local project.

Step 2: Use git initthe command to initialize the local warehouse. After this step is executed, a .githidden 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)
insert image description here


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 keyadd it to SSH keythe list on the Github official website (step 4).

Step 1 : Enter ssh-keygen -t rsa -C email_addressthe command in the terminal, where is email_addressthe 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 keyset the save path to /Users/meilinger/.ssh/id_rsa_ICB, as you can see, for easy memory, add the local project name ICBto the end of the path ~ Note: If you press Enter directly, SSH keyit 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~

insert image description here

Step 3 : Executing the command in the terminal pbcopy < ~/.ssh/id_rsa_demo.pubwill SSH public keycopy it to the clipboard, and the interface shown in the figure below indicates that the operation is successful.

insert image description here

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.
insert image description here

After clicking New SSH key, the system will automatically jump to the following interface. In this interface, fill in the Titleand Keyfields respectively. Here Title, I fill in the local project name, and the field is what we just copied Keyusing the command in step 3. Then click to complete the adding operation.pbcopy < ~/.ssh/id_rsa_demo.pubSSH public keyAdd SSH key

insert image description here

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:

insert image description here

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 own SSH keypath.

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_nameis the name corresponding to the Github account, and github_emailis the main mailbox in the Github account, which is the mailbox used in the preceding steps.

insert image description here


2. Upload code

First: Make sure that the current path of the terminal is the path of the local project, that is, the .gitparent 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, descriptionthe custom description text is in the command.
Step 3 : Execute the command git remote add origin pj_ssh_path, which pj_ssh_pathcan be copied and pasted through the following picture, for example, here pj_ssh_pathis [email protected]:meiling-fdu/ICB.git.

insert image description here

⚠️ PS: This step may report an error " 错误:远程 origin 已经存在。", the solution is as shown in the figure:

First execute the command git remote rm originto delete the existing remote configuration, and then execute the above command git remote add origin pj_ssh_path.
insert image description here

Step 4 : Execute the command git push -f origin masterto upload the file of the local project to the official Github website. The error is found as follows:

insert image description here

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 privateBlock command line pushes that expose my email
insert image description here

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:

  1. 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]

  2. 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.emailto 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.
insert image description here

  1. Execute the command git commit --amend --reset-authorto 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)
    insert image description here

PS: If the following error occurs, according to this blog , the solution is: execute the command git stash, as shown in the figure below:

insert image description here

After all the above errors are checked, execute the command again git push -f origin master, and the upload will be successful!

insert image description here

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 masterthe 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

  1. Step by step to teach you how to upload your own project on GitHub_github upload project_Xia Yuwei'an's Blog-CSDN Blog
  2. [Git] Mac uploads existing projects to GitHub - Short Book (jianshu.com)
  3. Solution to git push origin master error & common git commands (to be updated)_Wang Xiaoxi ww's Blog-CSDN Blog
  4. git using --rebase_ cannot rebase pull: You have unstaged changes. _Three fish blog-CSDN blog
  5. [git error –>! remote rejected master -> master (push declined due to email privacy restrictions)
  6. After GitHub submits Push, it is found that the code is in the Master branch. The default main branch has no code.

Guess you like

Origin blog.csdn.net/qq_36332660/article/details/131024361