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

What is the basic usage of git

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

Share

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

This article mainly explains "what is the basic use of git". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the basic use of git"?

1. Set up SSH Key

Set SSH Key so that the device has access to the code repository in the account

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

"your_email@example.com" is set to the registered mailbox of your GitHub account

The id_rsa file is a private key and id_rsa.pub is a public key.

$cat ~ / .ssh/id_rsa.pub

Content of ssh-rsa public key your_email@example.com

Then copy the public key and add it to the account. Note that the previous ssh-rsa is also copied.

Profile picture "Settings" SSH Key "new SSH Key"

Next, verify that the appearance of the word successfully is a success.

$ssh-T git@github.comEnter passphrase for key'/ c/Users/MYPC/.ssh/id_rsa':Hi abc! You've successfully authenticated, but GitHub does not provide shell access.2. Git basic Operation 2.1git clone already has a warehouse $git clone git@github.com:hirocastest/Hello-World.git

Here, you will be asked to enter the password of the empty open key set on the GitHub. After the authentication is successful, the repository will be clone to the current directory.

2.2 git add adds files to the staging area

After the code has been written, add the code to the system's staging area

$git add folder / file 2.3git commit holds the history of the warehouse

The git commit command actually saves the files in the current staging area to the history of the warehouse. With these records, we can recover the files in the working tree.

$git commit-m "record one line of submission information"

M represents an overview of the submission, and if you want to record details, remove-m

2.4 git push

After that, as long as the push command is executed, the warehouse on GitHub will be updated.

$git push2.5 git init initializes the warehouse

The clone method builds the warehouse and does not need to perform init operations. If you want to set the local file as a repository, you need to init

$mkdir git-tutorial$ cd git-tutorial$ git init2.6 git status to view warehouse status $git status# On branch master## Initial commit#nothing to commit (create/copy files and use "git add" to track)

As it turns out, we are under the masteer branch and have nothing to submit.

2.7 git log View submission Log

The git log command can view logs previously submitted in the warehouse. Including who submitted or merged at what time

$git logcommit 5dbbff6e009abb8a6cc44187c93b694f94fbf82a (HEAD-> main, origin/main, origin/HEAD) Author: ywm Date: Sun Feb 28 17:17:00 2021 + 0800

Just want to realistically submit the first line of information, you can add-- pretty=short after the git log command

$git log-- pretty=short2.8 git diff to see the difference before and after the change

Execute git diff to see the difference between the current working book and the temporary storage area

$git diff3. Branch operation

You can create multiple branches and perform completely different jobs at the same time. Wait for the branch job to complete before merging with the master branch. Through the flexible use of branches, multiple people can carry out concurrent development at the same time and efficiently.

3.1 git branch displays a list of branches

There is a "*" (asterisk) on the left side of the master branch, indicating that this is the branch we are currently in.

$git branch* master3.2 git checkout create and switch branches

Create and switch to a branch feature-A

$git checkout-b feature-A

In fact, the above command is equivalent to the following two commands.

$git branch feature-A$ git checkout feature-A

Switch back to branch mian

$git checkout main

Switch back to the previous branch

$git checkout-

Do something: create a new branch feature, modify README.md on the new branch, and add, commit

It can be proved by practical operation that as long as multiple branches are created, multiple functions can be developed at the same time without affecting each other.

3.3 git merge merge Branch

Add the-- no-ff parameter when merging to save the previous branch history

$git merge-no-ff feature

The editor then launches to enter the information submitted by the merge

3.4 git log-- graph looks at the branch $git log-- graph4 as an icon. Change committed operation 4.1 git reset rewind historical version $git reset-- hard target time point hash value

By viewing the operation log of the current warehouse through git reflog, you can find the hash value before the history is traced back. As long as there is no GC (garbage collection) of git, you can call up the recent historical status at will through the log. Even if the developer performs the git operation incorrectly, it can basically be restored to the original state with the git reflog command.

$git reflog

The most recent operation is printed above, and the oldest operation is printed below.

4.2 Resolution of conflicts

Conflicts are easy to occur during the merge operation, so you need to open the editor to resolve the conflicts.

In actual development, it is often necessary to delete one of them, so be sure to carefully analyze the contents of the conflict part before modifying it.

After the conflict is resolved, add and commit operations are performed

If you are not satisfied with the previous submission information, you can modify it using the amend parameter.

$git commit-- amend4.3 git rebase-I compression history

Before merging branches, if you find some spelling mistakes in the submitted content, you might as well submit a change, then include the change in the previous submission and compress it into a history.

Git rebase-I HEAD~2

You can select the two latest history records in the branch, including HEAD, as objects and open them in the editor.

5 push to remote warehouse 5.1 git remote add add remote warehouse

When creating a new warehouse, it is recommended that you do not check the README.md file, which will make the local warehouse and remote warehouse lose integration. Although it can be forcibly overwritten at that time, to prevent this from happening, do not check it, just create an empty warehouse.

Git remote can be used well when the code is written first and then the warehouse is created.

$git remote add origin git@github.com:github-book/git-tutorial.git

For those who create the warehouse first and then write the code, you need to pull the warehouse first, and then modify the file.

5.2 git push push to remote warehouse

Push to master branch

$git push-u origin master

The-u parameter can set the master branch of the origin warehouse to the upstream (upstream) of the current branch of the local warehouse while pushing, and add this parameter. When you run the git pull command to obtain content from the remote warehouse in the future, this branch of the local warehouse can obtain the content directly from the masteer branch of origin, saving the trouble of adding additional parameters.

In addition to master branches, you can also push to other branches

$git checkout-b feature-D$ git push-u origin feature-D6 acquires 6.1 git clone from the remote warehouse $git clone git warehouse address

Update the local feature-D branch to the latest status

$git pull origin feature-D

When multiple developers are working in the same branch, in order to reduce the occurrence of conflicts, it is recommended to perform push and pull operations more frequently.

Thank you for your reading, the above is the content of "what is the basic use of git". After the study of this article, I believe you have a deeper understanding of what is the basic use of git, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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