Getting started with GitHub (detailed)


foreword

Dark Horse Programmer GitHub Course Simple Notes


1. Concepts related to open source and GitHub

1. Open source thinking

The core idea of ​​open source is "I am for everyone, and everyone is for me". People are more and more fond of open source for the following three reasons: ① Open source gives users more control

Open source makes learning easier
③ Only open source can provide real security
. Open source is a major trend in the field of software development. Embracing open source is like standing on the shoulders of giants. You don’t have to reinvent the wheel yourself, making development easier and easier.

2. Open source project hosting platform

A website dedicated to storing the source code of open source projects for free is called an open source project hosting platform. Currently, there are three well-known open source project hosting platforms in the world:
GitHub (the best open source project hosting platform in the world, none of them)
Gitlab (good support for code privacy, so there are many enterprise users)
Gitee (also known as code cloud, is a domestic open source project hosting platform. Fast access, pure Chinese interface, friendly to use) Note
: The above three open source project hosting platforms can only host project source code managed by Git, therefore, they All names start with Git.

3. What is GitHub

GitHub is the world's largest open source project hosting platform. Because it only supports Git as the only version control tool, it is named GitHub. In GitHub, you can:
① Follow your favorite open source projects, like and call them
② Contribute to your favorite open source projects ( Pull Request )
③ Discuss bugs and raise requirements with the authors of open source projects ( Issues )
④ Put Copy a copy of your favorite project and modify it as your own project (Fork)
⑤ Create your own open source project
⑥ etc...
So, GitHub ≠ Git

2. Use GitHub

1. Register a GitHub account

① Visit the homepage of GitHub’s official website https://github.com/
② Click the “Signup” button to jump to the registration page
③ Fill in the available user name, email, and password
④ Click the arrow to align the verification picture
⑤ Click “ Create account" button to register a new user
⑥ Log in to the mailbox filled in the third step, click the activation link to complete the registration
Note: the Internet is all in English, for students who are not good at English, it is recommended to use Google Chrome, Google Chrome can translate web pages It is very convenient to use in Chinese.

2. Create a new blank remote warehouse

insert image description here


3. Two access methods for remote warehouses

The remote warehouse on Github has two access methods, namely HTTPS and SSH. Their differences are:
① HTTPS: Zero configuration, but every time you access the warehouse, you need to repeatedly enter the Github account and password to access successfully
② SSH: Additional configuration is required; but after the configuration is successful, every time you access the warehouse, you will not need to Need to repeatedly enter the Github account and password
Note: In actual development, it is recommended to use SSH to access the remote warehouse.

  • Upload the local warehouse to GitHub based on HTTPS
    ① First use Git to create a local warehouse
    insert image description here
    ② If there is an existing Git warehouse, enter the following command code:
git remote add origin https://github.com/linjingbei/project_01.git
git branch -M main 
git push -u origin main
  • ③ After the code runs successfully, a login box will pop up. After successful login, just refresh the GitHub webpage

4. Push and upload the second time after modifying the file

  • After modification, use Git's git add . command to add the file to the temporary storage area, and then submit the file in the temporary storage area to the Git warehouse (command: git commit -m "commit message')
  • Then enter the command git push (the above two or three lines of commands are uploaded and submitted for the first time, and then use git push), the GitHub file is updated successfully

5.SSH key

  • SSH concepts

    • The role of SSH key: to realize the encrypted data transmission between the local warehouse and GitHub without login.
    • The benefits of SSH key: login-free identity authentication, encrypted data transmission.
    • SSH key consists of two parts, namely:
      ① id_ rsa (private key file, which can be stored in the client computer)
      ② id_ rsa.pub (public key file, which needs to be configured in GitHub)
  • Generate SSH key
    ① Open Git Bash
    ② Paste the command below, and replace [email protected] with the email address you filled in when you registered your Github account:
    ③ Press Enter 3 times in a row to enter the C:\Users\username folder Two files id_rsa and id_rsa.pub are generated in the .ssh directory

ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • Configure SSH key
    ① Use Notepad to open the id_ rsa.pub file and copy the text content inside
    ② Log in to GitHub in the browser, click on the avatar -> Settings -> SSH and GPG Keys -> New SSH key
    ③ Copy the id_ rsa.pub file Paste the content in the Key to the corresponding text box
    ④ Fill in a name in the Title text box to identify where the Key comes from

  • Detect whether GitHub’s SSH key is successfully configured
    ① Open Git Bash, enter the following command and press Enter to execute:
    ssh -T [email protected]
    ② After the above command is executed successfully, you may see the following prompt message:
    The authenticity of host ' github.com (IP ADDRESS)' can't be establish ised.
    RSA key fingerprint is SHA256:nThbg6kXUp JWGl7E 1 IGOCspRomT xdCARLviKw6E5SY8.*
    Are you sure you want to continue connecting (yes/no)?

    ③ After entering yes, If you can see a message similar to the following, it proves that the SSH key has been configured successfully:
    Hi username! You've successfully authenticated, but GitHub does not
    provide shell access.

  • Upload local repository to GitHub based on SSH key
    insert image description here

git remote add origin [email protected]:linjingbei/project_2.git
git branch -M main
git push -u origin main

After the code runs successfully, a login box will pop up. After the login is successful, refresh the GitHub webpage.


6. Clone the remote warehouse to the local

Open Git Bash, enter the following command and press Enter to execute:

git clone 远程仓库的地址

The address of the remote warehouse (it is recommended to copy the address of the warehouse of SSH as much as possible):
insert image description here
-- then all the files of this warehouse have been successfully cloned locally


7. Git branch - local branch operation

  • The concept of branches
    • A branch is a parallel universe in a sci-fi movie. When you are trying to learn Git in front of the computer, another you are trying to learn SVN in another parallel universe. If the two parallel universes do not interfere with each other, it will have no effect on you now. At some point, though, the two parallel universes merged, and as a result, you learned both Git and SVN!
  • The role of branches in actual development
    • When conducting multi-person collaborative development, in order to prevent mutual interference and improve the experience of collaborative development, it is recommended that each developer develop project functions based on branches.
  • master main branch
    • When initializing the local Git warehouse, Git has created a branch named main for us by default. Usually we call this main branch the main branch.
      In actual work, the role of the main main branch is to save and record the completed function code of the entire project.
      Therefore, programmers are not allowed to modify the code directly on the main branch, because the risk of doing so is too high, and it is easy to cause the entire project to crash.
  • feature branch
    • Since programmers cannot directly develop functions on the main branch, there is a concept of function branches. The function branch refers to the branch specially used to develop new functions. It is temporarily forked from the main main branch. When the new function is developed and tested, it needs to be merged into the main main branch, as shown in the figure ( The current version of the master main branch in the figure has been replaced by the main main branch):
      insert image description here

  • view branch list
    • Use the following command to view a list of all branches in the current Git repository:
      git branch
    • The result of the operation is as follows:
      insert image description here
      Note: The * sign in front of the branch name indicates the current branch.
  • create a new branch
    • Use the following command to create a new branch based on the current branch. At this time, the code in the new branch is exactly the same as the current branch:
      git branch branch name
    • The diagram is as follows (the current version of the master branch in the figure has been replaced by the main branch):
      insert image description here
      Note: After executing the command to create a branch, the user is still in the main branch:
      insert image description here

  • Use the following command to switch to the specified branch for development:
    git checkout login
    • The diagram is as follows (the previous version of the master main branch in the picture has become the main main branch):
      insert image description here
      the current location is login:
      insert image description here

  • Quick creation and switching of branches
    • Use the following command to create a new branch with a specified name and immediately switch to the new branch:
      // -b means to create a new branch
      // checkout means to switch to the newly created branch
      git checkout -b branch name
    • The illustration is as follows (the current version of the master main branch in the picture has been replaced by the main main branch): the
      insert image description here
      current position is the newly created branch reg:
      insert image description here

  • merge branch
    • After the code development and testing of the function branch is completed, the following command can be used to merge the completed code into the main main branch: // 1. Switch to the
      main branch
      git checkout main
      // 2. Run the git merge command on the main branch , Merge the code of the login branch into the main branch
      git merge login
    • The diagram is as follows (the current version of the master main branch in the picture has been replaced by the main main branch):
      insert image description here
      Points to note when merging branches : first switch to the C branch, and modify the corresponding local file code on the C branch, assuming that the C branch The code is merged into the A branch, you must first switch to the A branch, and then run the git merge command to merge the C branch!

  • delete branch

    • After merging the code of the functional branch into the main main branch, you can use the following command to delete the corresponding functional branch:
      git branch -d branch name
    • The diagram is as follows (the current version of the master main branch in the picture has been replaced by the main main branch):
      insert image description here

  • Merging branches when encountering conflicts

    • If different changes are made to the same file in two different branches, Git cannot merge them cleanly. At this point, we need to open these conflicting files and manually resolve the conflicts.
      // Hypothesis: Both reg and main have modified the login.html file. During the merging of the reg branch into the main branch, the code conflicts // first switch to the main branch, and then
      merge the reg branch
      git checkout main
      git merge reg

    Check out the conflicting files as follows:
    insert image description here
    the header of the file will display: adopt current changes | adopt incoming changes | keep both changes | compare changes, select according to your needs, and then execute the following commands.

    // Open the file containing the conflict, manually resolve the conflict, and then execute the following command
    git add
    gitcommit -m "Solved the problem of branch merge conflict"
    and the execution is successful as shown in the figure:
    insert image description here


8. Git branch - remote branch operation

  • Push the local branch to the remote repository
    • If it is the first time to push the local branch to the remote warehouse, you need to run the following command:
# -u 表示把本地分支和远程分支进行关联,只在第一次推送的时候需要带 -u 参数
git push -u 远程仓库的别名 本地分支名称:远程分支名称

# 实际案例:
git push -u origin reg:regiter

#如果希望远程分支的名称和本地分支名称保持一致,可以对命令进行简化:
git push -u origin reg

Note: The -u parameter is required to push the branch for the first time, and then you can directly use git push to push the code to the remote branch.
The successful result is shown in the figure:
insert image description here

insert image description here

  • View the list of all branches in the remote warehouse
    You can view the information of all the branch lists in the remote warehouse through the following command:
git remote show 远程仓库名称

As shown in the picture:
insert image description here

  • track branch
    • Tracking the branch refers to downloading the branch of the remote warehouse to the local warehouse from the remote warehouse. The commands that need to be run are as follows:
 1.不重命名的方法:
# 从远程仓库中,把对应的远程分支下载到本地仓库,保持本地分支和远程分支名称相同
git checkout 远程分支的名称
# 示例:
git checkout regiter

As shown in the picture:
insert image description here

2.重命名的方法:
# 从远程仓库中,把对应的远程分支下载到本地仓库,并把下载的本地分支进行重命名
git checkout -b 本地分支名称 远程仓库名称/远程分支名称
#示例:
git checkout -b reg origin/regiter

As shown in the picture:
insert image description here


  • Pull the latest code from the remote branch
    • You can use the following command to download the latest code from the remote branch to the corresponding local branch:
# 从远程仓库,拉取当前分支最新的代码,保持当前分支的代码和远程分支代码致
git pull

example:

  1. Modify the code on the regiter branch of the remote warehouse
    insert image description here
  2. Let the files of the local corresponding branch also be updated and downloaded to the corresponding branch (switch to the current modified branch, use the git pull command) and a
    screenshot of the successful result:
    insert image description here
  • delete remote branch
    • You can use the following command to delete the specified branch in the remote warehouse:
# 删除远程仓库中,指定名称的远程分支
git push 远程仓库名称 --delete 远程分支名称
#示例:
git push origin --delete pay

Example: Delete the regiter branch of the remote warehouse.
Screenshot of the successful result:
insert image description here
Note: The deletion of the local branch is:

git branch -d reg

At this point, the command will pop up:
insert image description here
Note: -d means that if the branch has not been merged, an error message will appear (as shown above)

If the remote branch is deleted, if you delete the local branch again, there will be no backup, and the branch will be completely lost. Therefore, if it is detected that the branch has not been merged with the main branch when deleting -d, an error message will be prompted, asking if you are really To delete this branch, if you are sure to delete this branch, rewrite it to -D

-D means forced deletion, regardless of whether the branch is merged or not, the branch will be deleted


Summarize

     git init
     git add .
     git commit -m "提交消息"
     git status 和 git status -s
  • Ability to create and maintain remote repositories using GitHub
    能够配置 GitHub 的 SSH 访问
    能够将本地仓库上传到 Github
  • Be able to master the basic use of Git branches
    git checkout -b 新分支名称
    git push -u origin 新分支名称
    git checkout 分支名称
    git. branch

The above is what I will talk about today. This article only briefly introduces the basic operation and use of GitHub.

  • I wish: peace and success

Guess you like

Origin blog.csdn.net/m0_61874034/article/details/125098545