Author: Peter Editor: JackTian Source: public account “Jie Ge’S IT Journey” ID: Jake_Internet reprint please contact authorization (wechat ID: Hc220088)

Quick start on GitHub

GitHub is a household name in the field of application development. Now almost the entire Internet developers use GitHub as the first choice for version control. Even non-developers like me, a person who deals with data every day, also use GitHub.

  • Search materials: GitHub has a lot of data mining, machine learning materials and codes, which I can fork or clone directly

  • Technical exchange: Technical exchange can be achieved by issuing issues to open source projects

  • Personal note: In today’s world of the Internet, if a programmer has a good open source project on GitHub, it will be noticed. So GitHub is actually a great place to showcase your strengths and maybe get you in the recruiter’s good light

It can be said that the official GitHub has made social full staff programming a reality.

Since the benefits are quite many, as a member of the Internet community, there is no reason not to learn GitHub😄 this article will introduce GitHub related basic operations in detail, take you to quickly start GitHub

Version management

Versioning is managing a history of updates. Before the advent of Git, Subversion (SVN for short) was primarily used as a version control tool.

SVN

The SVN is a centralized version management system that stores repositories in a centralized manner on the server. Therefore, there is only one repository. Centralized will have all the characteristics of easy management, but if the developer’s environment is not connected to the Internet, it will not be able to obtain the latest source code, and therefore cannot develop work.

Git

Git is a distributed version management system. As you can see from the figure below, GitHub forks the repository to each user. Fork’s repository and the original repository are two different repositories that developers can edit at will.

The process of forking is simply copying the repository into your own account

What is making

GitHub is a Git-based code hosting platform. If the paying users can set up their own private warehouse, ordinary users can only set up a public warehouse, that is to say, the code of the warehouse must be public. What’s the difference between Git and GitHub?

In Git, developers store source code in a repository called Git repository for easy use. GitHub is a service that provides Git repositories on the web

The source code available on GitHub is managed through Git.

Install and configure Git

The installation

The MacOS I’m using now comes with Git. For installation under Windows, please refer to the following articles, which are very detailed.

Git installation for Windows

www.cnblogs.com/xueweisuoyo…

configuration

First, you need to set the name and email address for Git. The name should be in English

Git config --global user.email "[email protected] [user] name = Firstname Lastname email = [email protected] is displayed in ~/. GitconfigCopy the code

Note that:

  1. The name and email address of the GitHub repository will be published together, so please do not use private information that is inconvenient to publish

  2. Programmers from all over the world, please use English, do not use Chinese characters; If you don’t want to use your real name, use your website’s nickname

How to improve the readability of command output?

Git config --global color. UI autoCopy the code

Create account

The page for creating a user is displayed: github.com/join

Fill in the following information and click Create an Accout

Configure SSH

GitHub uses SSH public key authentication to connect to an existing repository. Now we need to create the SSH Key required for the public Key and add it to GitHub.

Ssh-keygen -t rsa -c # Create SSH KeyCopy the code

The password is then entered twice, resulting in two files:

  • Id_rsa: private key

  • Id_rsa. pub: public key

Next we need to add a public key to GitHub so that we can use the private key for authentication in the future. Click the account Settings button in the upper right:

After entering Settings, add a new SSH Key

The Title and Key fields appear, with the appropriate Key name in Title and the Key section copied from the id_RSA.pub file above

'cat ~/.ssh/id_rsa.pub #Copy the code

After the public key is added, we will receive an email indicating “public key is added” in our mailbox. Just confirm. This completes the entire private key in hand and GitHub authentication and communication problem, verify:

ssh -T [email protected] 
Copy the code

Enter the password and yes on the following page to complete the verification.

Establish warehouse

First we must understand that there are two kinds of warehouses

  • Remote repository on GitHub: Remote repository

  • In your own local warehouse: local warehouse

This paper takes MacOS system as an example, based on Linux; If you want to learn to create a warehouse under Windows, please refer to the following, explain very detailed:

Git use (10 minutes to get started) :

www.jianshu.com/p/09f243768…

Remote warehouse

1. To build a remote warehouse, we need to log in our GitHub account first and then build a warehouse.

2. We create a git_start repository

  • Name of warehouse

  • A brief description of the warehouse

  • Do not add readme.md files to remote repositories. We use a manual push to add

3. Warehouse explanation

Open the repository we created above, and you will see something like this (comment first, explain later)

Git commit -m "first commit" # Git branch -m main # git remote add origin [email protected]:pidada/git_start Origin main # pushCopy the code

Local repository

1. Establish local warehouses

A local repository is the repository of your own computer client. Similarly, I set up a local repository locally with the same name, git_start, which is actually a folder

The local repository should be aligned with the remote repository

Mkdir git_start # create folder, i.e. Storehouse CD git_start # change the contents of the storehouse to ls #Copy the code

2. Initialize operations

Git init = git git init = git git git git git git git git git git git git git git git git git git git git gitCopy the code

After initialization, a.git file is automatically generated in the current directory. This file stores the warehouse data needed to manage the contents of the current directory

3. Review the submission document

View the readme.md document by ls

Git status # check the document you want to commitCopy the code

4. Submit documents

We submitted the above readme. md document along with the generated.git document

Git commmit -m "2021-1-1" # commit Git commit -am "2021-1-1"Copy the code

Note: With the add operation, the file has not yet been uploaded to the Git remote repository, just committed to the cache area

Git commit -m “2021-1-1” is the actual commit with a note: commit the file from cache to remote

5. Establish remote warehouse connection and push

Git remote add origin [email protected]: Peter /git_startCopy the code

6, check the

Now we can refresh the remote repository page and find that the page is updated and has the content:

7. View the commit log

Git log --pretty=short -- git log readme. md Only the log changes of the specified directory and file are displayedCopy the code

Next to the COMMIT column is the hash that points to the commit. Git’s other commands use this hash value when pointing to commit

Next to the Author bar are the user name and email address Git sets

The Date field shows the execution Date and time

At the bottom is the commit information, after the -m parameter

8. Modify the readme. md file

Vim editor to modify the content as follows:

The Markdown syntax is used above, and we will repeat the above command:

Git add. # commit to cache git commit -m "modify readme. md" # git remote add origin [email protected]: Peter /git_start.git #Copy the code

To review the process:

  1. Setting up remote warehouse

  2. Set up local warehouse

  3. Initialize the local repository

  4. Documents are submitted to the cache

  5. The cache is pushed to the remote repository

Important command

Let’s summarize a few important commands from the above operation:

1, the git status

View the status of files in the repository. If there is a new file or the original file has been modified, it will appear in red

2, git add

Add content to the cache, which is a temporary area (Stage or Index) before submission

Git commit [-m]

Save files in the staging area to the warehouse’s history; Git commit -m “first commit”

4, the git log

View previous commit logs: who committed or merged when, and what was the difference before and after

Git log --pretty=short -- git log readme. md The -p parameter displays only the submitted changesCopy the code

5, git diff

View the differences between the working tree, staging area, and latest commit.

Git diff HEAD git diff HEAD git diff HEAD HEAD a pointer to the last commit in the current branchCopy the code

Git diff HEAD before git commit to see the difference between this commit and your last commit. HEAD a pointer to the last commit in the current branch.

6. Warehouse operation

-u Function: When you run the git pull command to fetch content from the remote repository, the local repository can directly fetch content from the master branch of origin without adding other parameters

Git remote add origin [email protected]: making/Peter/git_start git # add remote warehouse git push # pushed to the remote warehouse git push -u origin master # Git git git git git git git git git git git git git git git git git git gitCopy the code

branch

The master branch

The master branch is the default branch that Git creates, and all other branches are built on top of this branch.

  • The different branches do their own work

  • The branch is merged with the Master branch after the job is done

After doing the job merge:

Branch related commands

Git branch- Displays a branch

Display branch list: display the branch list and confirm the current branch. An asterisk * indicates the current branch

Git branch -aCopy the code

Git branch feature- Create a branch

git branch feature
Copy the code

Git Checkout feature

Git checkout - # switch to the previous branchCopy the code

Merge the two commands above to create a new branch and switch to the new branch:

Git checkout -b feature # switch to the new branch createdCopy the code

Git merge

If a branch has completed its job and needs to merge with the master branch, use the following statement:

Git checkout master git merge --no-off feature -- ACopy the code

Git log — graph-graph

View submissions in the form of charts

git log --graph
Copy the code

Version back

Since it is a version control system, the management of different versions must be critical. Another feature of GitHub is the flexibility to manipulate different versions of history. With the advantage of decentralized repositories, historical versions can be manipulated without affecting other repositories.

1. Go back to the specified state

Hash values need to be entered at least 4 digits to be executed

Git reset --hard [hash] # add a hash value that represents the state at a point in timeCopy the code

2. View all operation logs of the current warehouse

Record every command we operate

Git reflog # git log #Copy the code

Git reflog to check the hash value, and git reset — hard to get back to a state

3. Modify the previous submission information

Use git commit –amend

git commit --amend
Copy the code

4. Compress history

Before merging feature branches, if you find errors such as spelling in the content you have committed, you can commit a change and then include that change in the previous commit to compress it into a history

Git rebase -i git rebase -i HEAD~2 #Copy the code

5. Add the submission once

Git commit -am "Add and commit at the same time"Copy the code

Git reset,

The command

A detailed explanation of the version traceback command. The git reset command is used to rollback to a specific historical version.

Git reset command syntax is as follows:

git reset [--soft | --mixed | --hard] [HEAD]
Copy the code

— Mixed is the default and can be left out to reset the file in the staging area to the last commit, while the workspace file content remains unchanged

soft

The soft parameter is used to roll back to a version

Git reset --soft HEAD~3Copy the code

Hard ⚠ ️

!!!!!!!!! ⚠️ Use caution with the –hard parameter, which deletes all information up to the fallback point

Git reset --hard origin/master git reset --hard origin/masterCopy the code

HEAD

Git reset HEAD^ # git reset HEAD^ # git reset HEAD^ # Requires at least the first 4 bits of the hash value; Git reflog first looks at the hash of the version number you want to roll backCopy the code

Git reset HEAD also cancels cached content. When we have modified the contents of a file and have done git add, we want to cancel the contents of the cache using the following command:

git reset HEAD [filename]
Copy the code

About HEAD:

  • HEAD indicates the current version

  • HEAD^ Previous version

  • HEAD^^ Previous version

  • HEAD^^^ the previous version

  • And so on…

The value can be a ~ number

  • HEAD to 0 indicates the current version

  • HEAD~1 Previous version

  • The previous version of HEAD^2

  • HEAD^3 before the previous version

  • And so on…

conclusion

  • The version that HEAD points to is the current version, so Git allows us to travel through the history of versions using the command Git reset –hard commit_id.

  • Before shuttling, you can view the commit history with git log to determine which version you want to fall back to.

  • To go back to the future, look at the command history with Git reflog to determine which version you want to go back to in the future.

Original link: super detailed! Hand on hand to get you started quickly on GitHub!