@[TOC]


Introduction: When did I begin to understand and contact Github? What is Github for?

Now I am a sophomore, it has been more than four years, almost five years since I typed the first line HelloWorld (I bought A copy of C Primer Plus in junior grade, and borrowed the money from my brother, but I am still a chicken now). When I wrote the code put TXT inside, is like this:I’m going to save them down, but I found there is unreliable, I remember very clearly, I also don’t have a computer at that time, the programming has always been in my aunt’s learning with a computer in the home, why say it is unlikely, not I a person to use computer, so I can’t control, literally a reshipment system, my product is doomed. But soon I found a new storage, is the following QQ space log inside.Later, WHEN I was in senior one, my family bought a laptop for me to learn programming. At that time, it cost more than 5000, so I gave all the money to others. I don’t know if I made the right decision. I also registered my CSDN account in October, nineteen nineteen, which was just the beginning of the university. I often use Baidu to solve programming problems, so CSDN must be one of the websites with the highest hits, but that’s it. After using CSDN for more than two years, I have not even registered an account!! This time, I moved the code to CSDN again. I saved and published the code as an article and packaged the code as resources. Within a year of application, I applied for blog expert and successfully passed it in September this year. Then there is Github, the protagonist of our article. I registered and used Github for the first time on January 15th this year. Before that, I had only known about Github and knew that it was used to save codes, but I didn’t feel its power at that time, although everyone said it was powerful.It took me a while to configure GitHub. When I sent the local code to the remote repository, I couldn’t help sending a message to my friends: “GitHub smells great! You may think I said a lot of nonsense, but I think it is a record of my growth, a process, like the sentence in my introduction: == Executable files are not your wealth, the experience of the modification process is. When you start from the beginning, you can see the beauty of it. At this stage, when you think something is not important, I think it is not important, except for learning.

Github is a Git server provider to help us share code (managed code) and also an open source platform, which can be used to download the source code of many well-known open source projects. When we modify the project, we can create a branch, and merge it into the main branch after modification for version management.

Similar version (code) hosting service platform: Code Cloud (gitee.com) : It is a fast, free and stable online code hosting platform based on Git launched by the Open Source Chinese community team, without limiting the number of private libraries and public libraries.

Coding(Coding.net) : Is a one-stop development platform under Coding, providing git/ SVN code hosting, free support for private (limited) and public libraries

And we have to mention CSDN’s new Codechina

I’ll tell you in plain English what it can do:


Set up a GitHub account

This is theGithubThe home page, before the experience, also ask you to download Google browser, I was using other browsers before the pit miserably!Let’s create an account first and click Sign Up to register the account. ! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210185…= 500 x) belowEmail perferencesThe option is checked by default, meaning github can occasionally send you updates via email, etc. ! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210190…GitHub will send you an email to verify your email and ask you to log in using the password you just set. ! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210191…=400x) ! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210191… =400x)

! [insert picture description here] (img – blog. Csdnimg. Cn / 20201210190… =500x) welcome page, you can set some attributes, such as whether you are a student or a teacher, whether you have programming experience, etc.


Create a warehouse and delete the warehouse

1. Create a warehouse

! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210200… =800x)! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210200… =900x)


2. Delete the warehouse

Click on Settings and drag it down to the bottom.

Enter the warehouse name to confirm, you can delete the warehouse.


Git configuration and connection

How do you get code on your computer to sync to a repository created on Github? You need to download Git,Let me download, select the corresponding system and version. ! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201210193…=400x) above is the official download address, the download is still unsatisfactory, 11KB per second…. Got us a domestic one(Click download)Then all the way to Next, waiting for the installation completed, no accident right mouse button will be displayedGit GUI HereGit Bash HereGUI is the graphical version, Bash is the command line version. If you don’t like the command line, you can use GUI. If you like simplicity and mystery, you can use Bash. I still recommend the command line.If you feel that the Bash black box is small, you can press CTRL + mouse wheel to zoom in and out.

1. Set the user name and email address

Git config –global user.name “XXXXXXX” XXXXX represents your username

Git config –global user.email “XXXXXXXX” XXXXX


2. Generate an SSH Key.

Ssh-keygen -t rsa -c “XXXXXXX” XXXXX represents your email address

Pub is the Key pair of the SSH Key. Id_rsa is the private Key and cannot be disclosed. Id_rsa. pub is the public Key and can be disclosed.

Next, go to the top right corner of GitHub, open the “Account Settings” — “SSH Keys” page, click “Add SSH Key”, fill in the Title (feel free), and paste the entire contents of the id_RSA. pub file in the Key text box.


3. Check whether the verification is successful

$ ssh -T [email protected]

If this is your first setup, a yes/no option will appear, so simply select Yes.

If hi XXXX appears, you have succeeded.


4. Associate the local repository with Github

To use Bash, select a location where your code is to be stored, go to the repository you just created and click Code, copy the SSH below, and type the following command:

git clone [email protected]:FdogMain/fdogtest.git

! [insert picture description here] (img – blog. Csdnimg. Cn / 20201211121… =700x) ! [insert picture description here] (img – blog. Csdnimg. Cn / 20201211122… =700x), the path you specified will generate a folder with the same name as your repository, which contains a.git folder and the readme.md file you just created.

At this point, Git and GitHub are successfully connected.


4. Command operation

1. Change master to main

Let’s write some simple code, put it in the fdogtest folder, and show you how to upload our code at bash. There are some changes before that, if you’re interested.

Starting October 1, 2020, Github will change the name of all “master branches” to “Main branches.” In June, Petr Baudis, the developer who originally wrote the word “master” in Git, took a stand on a social networking site, saying that he should not have used the word “master” that might cause harm to others. He stated that he wished several times that “master” could be changed to “main” (and “upstream”). But only now is GitHub leading the replacement effort. If it is a new build, the default description has been changed to Main, so don’t worry. Before that, it’s still showing the master. The diagram below:

So how do I migrate the GitHub project’s default branch from Master to Main?

Use the following command to move the master branch to main:

git branch -m master main

Push the newly named main branch to GitHub(assuming it’s your remote repository) with the following command:

git push origin main

Git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

Now you need to set “Main” as the default branch for your project on GitHub. For this, lot of log in to your account, open the project repository, click “Settings” | > “branch”. In the left sidebar, click “Branch,” then select “Main” as the default from the drop-down list. Click “Update,” and when prompted, click “I understand.” Now, > go back to the terminal and execute the following command:

git push origin --delete master

That should do it.


2. Upload the code

Git has four local working areas: workspace, staging area, release area (also known as the warehouse area), and remote repository.

Git status This command is used to check whether the file has been modified again since your last commit. All the code you write is in your workspace, or folder.

Git add-a commits all changes to files that are modified and deleted, excluding new files. After executing the above command, the code enters the staging area.

Git commit -m ‘comment’ to add staging contents to local repository

Git push Origin Mian pushes the local version library to the remote server


Code submission process:

  • Git status looks at the difference between workspace code and staging area
  • Git add. Add all code changed in the current directory from the workspace to the staging area. Represents the current directory
  • Git commit -m ‘comment’ adds the cache contents to the local repository
  • Git pull Origin main Synchronizes information from the remote repository main to the local repository main
  • Git push Origin Mian pushes a local version library to a remote server,
  • Origin is a remote host, and main is a short form of the same name as the main branch on the remote server and the local branch. The branch name can be changed.

Download someone else’s code

The first is to download it from GitHub.The second is to use SHH and type in your Bash:

git clone [email protected]:FdogMain/FdogMusicPlayer.git

Can be downloaded. Note: HTTP is slower than SSH ~~~


4. How do I view historical versions

For example, if I make two separate updates to the code, I want to send it to a remote repository, and commit is the second and third commit respectively, how do I see my historical version?

Git log To view historical versions

Git log-x displays the latest x version information

Git log -x filename Displays the latest x versions of a file named filename (go to the directory where the file resides).

Git log –pretty=oneline View all historical version information, including only the version number and record description


5. How do I roll back to the historical version

Git reset –hard HEAD^ Rollback to previous version

Git reset –hard HEAD^~2 rollback to the previous two versions

Git reset –hard XXX (version number or the first few digits of the version number) rolls back to the specified version number. If it is the first few digits of the version number, Git automatically looks for a matching version number

Git reset –hard XXX (version number or first digits of version number) filename Rollback a file to the specified version number (you need to go to the directory where the file was stored)

When you use git log again, the third commit is gone.


6. How do I roll back to the historical version

Git log -g Displays the commit information of the previous operation

Once you have found the version you want to roll back to, press Q to exit and then use the following command to roll back to the version you want to roll back to.

Git reset – hard 46 f3ecbdfd18153d3b575c768f84c5d8d16c764c commitID is a string of behind.


7. How do I check my code volume

1. Count the number of codes added/deleted by individuals

git log --author="_Your_Name_Here_" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
Copy the code

2. Count the amount of code added/deleted by everyone

git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
Copy the code

3. Code statistics within a period

git log  --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat:  --since ==20192 -- 12 --until=20192 -- 15 --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
Copy the code

4. Count the code quantity of the whole project

git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
Copy the code

Five. Branch details

The role of the branch for the role of the team is too big, when many people for the development of A project, the development process, the inevitable for collaboration, when A, B two people were arranged for different tasks, development in different branches, will not affect each other, when A, B two people, after the completion of the task of the test is correct, then merge into the main branch. In most cases, development is not done directly on the main branch mian (formerly matser), but a new branch is created. The code in the main branch is usually stable and can be released or forked.

1. Use a new branch

View the current branch

git branch

Create a new branch

git branch XX

Switch branch

git checkout XX

! [insert picture description here] (img – blog. Csdnimg. Cn / 20201212211… =700x)

Then we update our code under the new branch, uploading our G1 branch code to the remote repository. ! [insert picture description here] (img – blog. Csdnimg. Cn / 20201212212… =700x)

Git push git push git push

fatal: The current branch G1 has no upstream branch. To push the current branch and set the remote as upstream, Use git push –set-upstream origin G1 To push the branch and set the remote to upstream, use git branch –set-upstream origin G1. Git branch –set-upstream origin G1 if the connection is still not established, run git push -u origin G1 to establish the connection.

Next we open Up Github and click on branches. ! [insert picture description here] (img – blog. Csdnimg. Cn / 20201212214… =700x) we can see that the branch we just created, G1, and the code we updated is only in G1, the main branch mian exists. ! [insert picture description here] (img – blog. Csdnimg. Cn / 20201212214… =700x)

Git log can be used to view all commits that are related to the current branch. If you are currently in G1, you can only view all commits that are related to the current branch. Git log –graph –all


2. Merge new branches

Click on New Pull Request! [Insert picture description here]Img – blog. Csdnimg. Cn / 20201212215…=900x) is submitted on the new page, showing the merge from G1 to main, with the green check box indicating that the merge is possible without conflicts, and the content added to the branch is shown below.You can see our request just now in our pull request.Click on theAdd code to the branch The translation

We can look at the content of the branch and decide if we agree to merge the branch. One is to click the button directly, the other is to use the command line. This is my merging of my own code. If you want to commit a branch to someone else’s project, you have to fork the project and then create a branch with the same name, which is equivalent to the branch, where you can modify as much as you want, and then follow the above method. You can click here to experience the above process, which is a warehouse I built, or you can directly search fdogtest in the search bar to experience.


Common commands