Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with Git and Github

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to get started with Git and Github. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

This article brings you a detailed introduction to Git and Github. Git is the most advanced distributed version control system in the world. Github is a website that provides Git services for users. I hope it will be helpful to you.

Overview of 01.Git

1.Git is the most advanced distributed version control system in the world.

Problem solved:

Record each modification: version, content, operator, modification time, document name, etc.

The difference between 2.Git and Github

Git is a distributed version control system, which is simply a piece of software that records changes in the contents of one or more files in order to consult the revision of a specific version.

Github is a website that provides Git services for users, which is simply a place where code can be put (and other content can be put as well). In addition to providing a web interface for managing Git, Github also provides rich functions such as subscriptions, followers, discussion groups, online editors, and so on.

Git does not have a central server like SVN.

The Git commands we use so far are executed locally if you want to share your code through Git or work with other developers. You need to put the data on a server that other developers can connect to. This example uses Github as a remote repository

Installation of Git

Local warehouse operation of 02.Git

1. Work flow

Three areas for Git local operations:

Workflow:

Temporary storage area: equivalent to the last payment for items to be purchased in a shopping cart.

two。 Local warehouse operation

What is a warehouse?

Warehouse, also known as version library, English name repository, we can simply understand as a directory, used to store code, this directory all the files can be managed by Git, each file modification, deletion and other operations Git can be tracked.

Global configuration is required for the first time after installation:

1. Click "Git Bash Here" to open the Git command line window:

$git config-- global user.name "user name" $git config-- global user.email "email address"

Set the user information, and the user name will be recorded when the project is modified.

two。 Create a local warehouse

When we need Git to manage a new / existing project, we need to create a warehouse.

It is recommended to use an empty directory to learn Git because of unnecessary errors caused by operational errors. Try to use English names for directory names.

3. Create a directory

$mkdir directory name

You can also create it in a new way directly locally.

4. Enter the project directory pre_git on the command line

$cd directory name

Initialization of 5.Git warehouse

That is, let Git know that he needs to manage the directory.

$git init

After execution, open the project directory and click the hide directory and you will find that there is an extra .git folder. It cannot be deleted or changed at will.

You can develop in it when you come here.

6.Git common instruction operation

View current working status:

$git status

Function: when we forget where the project is done, such as coming back from going to the toilet, having a meeting, etc. You can use this command to determine what to do next.

7. You can develop it in the project directory.

8. Add the workspace file to the cache:

Description: git add directive, you can add a file or multiple files at the same time. Syntax 1: $git add filename syntax 2: $git add filename 1 filename 2 filename 3. Grammar 3: $git add. [add current directory to cache]

9. Submit to version library

$git commit-m "comment content"

If you create a new file, start with add and repeat the above.

If you modify the contents of the submitted file, it would be nice to resubmit it from add.

The submission and other operations here are only submitted to the Git local repository.

Version fallback operation of Git

Version fallback is divided into two steps:

1. First check the version to determine the time point you need to go back to.

Directive:

$git log$ git log-pretty=oneline

The second instruction: simplifies information.

two。 Fallback operation:

Directive:

$git reset-- hard submission number

After the operation is performed, the files in the project directory return to the specified time.

What if I want to go back to the latest state when I go back to the past?

First: the number before the query

Directive:

$git reflog

And then execute.

$git reset-- hard submission number

Summary:

To go back to the past, first get the commit id, and then go back to the past through git reset-- hard.

Going back to the future: you need to use git reflog to query for historical operations to get the latest commit id. (serial number)

It can be found that when writing fallback instructions, commit id does not have to write all, and git will automatically recognize it. You need to write at least the top 4.

03. Remote warehouse

The operation of online warehouse, take Github as an example. (there are many websites that provide remote warehouses for Git repositories.)

1. The creation of Githubu online warehouse

two。 Use online warehouses

Add a remote warehouse (add github here)

Note: enter cd / D in git bash and note that the disk name must be uppercase. If you do not enter this statement and do not locate the git bash, the default local file location is on disk C. Then cd to a folder with the same name as the remote warehouse.

The first method is based on https protocol:

The warehouse is the catalog:

1. Create an empty directory with the name shop (optional)

two。 Copy the https corresponding address.

Then create a new shop directory in the current directory

$mkdir shop$ cd. / shop

3. Use the clone instruction to clone the online warehouse to the local.

$git clone online warehouse address

After execution, the online warehouse is brought down by clone, and if the clone comes down with an empty directory, the cloned directory has only hidden .git directories.

4. After the above operation is completed, you can do the corresponding operation in the local warehouse.

For example: (submit to temporary storage area, submit to local warehouse, submit to online warehouse (remote warehouse), pull online warehouse)

The first few operations are the same as the local warehouse operations above.

Then submit it to the online warehouse:

If it is the first time to submit, you should first obtain the permission: (otherwise an error of 403 will occur)

Because not everyone can submit content to the online warehouse.

Obtain permissions:

Modify the contents of the .git / config file:

In the url item:

Add: username: password @ before github.com

Such as:

Url=https:// user name: password @ github.com/ user name / warehouse name. Git

Everything else remains the same.

Directive:

Instructions submitted to the online warehouse

$git push

After the submission is successful, you will find that there is more of your submission in the online warehouse.

Note: if you submit successfully after work: colleagues also submit new content to the remote warehouse. When you go to work the next day, you need to pull the latest version of the online warehouse

Use the command:

$git pull

So the first thing to do every day is to pull the latest version of git pull online, and all you have to do after work is git push and submit the local code to the online repository.

The second way: based on ssh protocol (recommended)

Compared with the previous https mode, this method only affects the identity authentication mode of github for users, and there is no change to the specific operation of git.

Steps:

1. Generate client public and private key files.

two。 Upload the public key to Github.

Actual operation:

1. Create a public and private key file

The transfer between your local Git repository and GitHub repository is encrypted through SSH, so we need to configure verification information: generate SSH Key using the following command:

Open Git Bash directly here: (you can also right-click on the desktop to open it)

Then enter the command:

Ssh-keygen-t rsa-C "registered mailbox"

If you can't generate the key (you have to install OpenSSH first), then execute the above command.

Some tutorials online use commands:

$ssh-keygen-t rsa-C "your_email@youremail.com"

Note: in fact, your_email@youremail.com represents a registered mailbox, that is, the two are the same.

After that, you will be asked to confirm the path and enter the password, and we will use the default enter all the way.

two。 Find the id_rsa.pub file.

If successful, a .ssh folder will be generated under ~ /, go in, open id_rsa.pub, and copy the key inside.

Go back to github and enter Account = > Settings (account configuration).

To verify success, enter the following command:

$ssh-T git@github.com

Result: Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

3. Then create a new directory locally (to save the online project), and then enter the directory.

$mkdir directory name $cd. / directory name /

Then clone the directory to the new directory:

$git clone ssh address

The online warehouse ssh and https addresses can be viewed in clone and download

4. The follow-up operation is the same as https.

Compared with https, once this method is configured, future projects do not need to configure user names and passwords one by one, which can be said to benefit forever. So it is recommended to use this way.

Branch operation of 05.Git

What is a branch?

That is, the branch of the project (different functions):

All the branches form a project.

In the content of version fallback, each submission is recorded, and Git strands them into a timeline to form something similar to the timeline, which is a branch, which we call the master branch.

When developing, it is often teamwork and multi-person development, so having one branch alone cannot meet the needs of multiple people developing at the same time, and working on the branch will not affect the normal use of other branches, so it will be more secure. Git encourages developers to use branches to complete some development tasks.

Instructions related to the branch:

View branches: git branch create branch: git branch branch name toggle branch: git checkout branch name delete branch: git branch-d branch name merge branch: git merge merged branch

Note: you can use the "git checkout-b branch name" instruction to switch branches, and the-b option means to create and switch, which is equivalent to two operation instructions.

The "*" indicates the current branch.

You can see that we all operate on branches.

Before deleting a branch, you need to exit the branch, which cannot be deleted in use.

06. The emergence and resolution of conflicts

Case: simulated conflict

1. After I got off work, my colleagues modified the content of the online project, and the content of the local warehouse was not consistent with that of the online warehouse. The next day I forgot to do the git pull operation, but modified the local file directly.

An error is reported when a submission is made.

two。 Resolve the conflict

Start with the instruction "git pull" operation.

3. Open the conflict file and resolve the conflict.

Discuss with the user who submitted the content what to save. What to delete.

Then, after resubmitting, you can do the push operation.

Here you can see the importance of git pull before going to work.

07.Git practical function

1. Graphics management tool

Github for Desktop

It is a very convenient tool for developers who often use Github.

Source tree

Established Git GUI management tool, suitable for beginners.

TortoiseGit

For developers familiar with SVN, it is very friendly. Its predecessor, TortoiseSVN.

Built-in Git GUI management tools.

08. Ignore Fil

There are permanent directories in the project, such as css,js,images, etc., or even if there are changes, we do not want them to be submitted to remote documents, so we can use the "ignore files" mechanism to implement the requirements.

Ignoring files requires the creation of a new file called .gitignore, which declares rules for ignoring files or not ignoring files, which take effect on the current directory and its subdirectories.

Note: this file cannot be created directly under the windows directory because it does not have a file name. It can be created through the command line Git Bash.

Common rules are as follows: 1. Filter the entire folder / mtk/2. Filter all .zip files * .zip3. Filter a specific file / mtk/do.c4. But filter a specific file lindex.php

Create a new file for .gitignore

Directive:

$touch .gitignore

Configuration file. Gitignore:

Such as:

1. Ignore the / js directory

/ js/

The basic concept of 09.Github

Purpose: hosting project code with the help of github.

1. Warehouse (Repository)

The repository is your project. If you want to open source a project on Github, you must create a new Repository. If you have more open source projects, you will have multiple Repositories.

two。 Collection (star)

It means the number of people collecting items. It is convenient to check next time.

3. Copy clone project (Fork)

When browsing other people's projects, click fork, and you will create the same warehouse in your own account. The warehouse is independent, but it will show whose warehouse forked from owns.

4. Initiate a request (Pull Request)

This is based on Fork. After making improvements in his cloned project, Li Si wanted to merge his improved project into the original project, so he sent a Pull Request to the creator of the original project. If you are the creator of this project, you can receive this request, at this time you will carefully review his code, if you think it is good, you can accept his request, at this time his improvements will be found in your project.

Steps:

1.fork project

two。 Modify the project of fork

3. Create a new pull request

4. Waiting for the new project creator to merge the project

5. Follow (Watch)

If you follow a project, you will be notified of any updates to the project in the future.

6. Transaction Card (Issue)

Is that you open source a project, others look at your project and found Bug, or some places do not do well, he can give you an Issue, that is, the problem, and then you can see these problems can be improved, perfect can be its one by one Close.

That is, the code Bug is found, but there is currently no molded code that needs to be discussed.

For example, someone else submitted an issue to Itcastphpgit2

Home page concept:

1.Github home page

Display user dynamics and follow user or warehouse dynamics.

two。 Warehouse home page

3. Personal home page

Personal information

Official website: Github.com

Create a new Github account:

Note: the github server is abroad, so access is slow or inaccessible.

Validate the mailbox according to the operation and create a remote warehouse.

Operation of remote warehouse:

1. Create a new file

two。 Edit a file

Click the file name, and then click the "small stroke" pattern.

3. Upload files

Click upload file, and then add a description. (Upload files)

4. Search for warehouse files

Click search (Find file)

5. Download the project

Click Clone or download

10.github Pages sets up a website

Personal site

Visit:

Https:// username. Github.io

Building steps

1. Create a personal site-> create a new warehouse (Note: the warehouse name must be [user name .github.io]

two。 Just create a new index.html file under the warehouse.

Note: github pages only supports static web pages

two。 There can only be .html files in the warehouse.

After reading the above, do you have any further understanding of how to get started with Git and Github? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report