Table of Contents
#1. Set SSH Key
$ git clone git@github.com:hirocastest/Hello-World.git
Copy after login
" >
$ git clone git@github.com:hirocastest/Hello-World.git
Copy after login
$ git log --graph
Copy after login
" >
$ git log --graph
Copy after login
5 推送至远程仓库
5.1 git remote add 添加远程仓库
5.2 git push 推送至远程仓库
6 从远程仓库中获取
6.1 git clone 获取远程仓库
Home Development Tools git How to understand the basic use of git in 20 minutes

How to understand the basic use of git in 20 minutes

Jan 05, 2022 pm 05:36 PM
git

This article brings you basic knowledge about the use of git, including basic operations of git, branch operations, change submission operations, etc. I hope it will be helpful to everyone.

How to understand the basic use of git in 20 minutes

#1. Set SSH Key

Set SSH Key so that the device can have permission to access the code warehouse in the account

$ ssh-keygen -t rsa -C "your_email@example.com"
Copy after login
  • "your_email@example.com" Set it as the registered email address of your GitHub account
  • The id_rsa file is the private key, and id_rsa.pub is the public key.
$ cat ~/.ssh/id_rsa.pub
Copy after login

ssh-rsa The content of the public key your_email@example.com

  • Then copy the public key and add it to the account. Pay attention to the # in front. ##ssh-rsa Also copy it
  • Avatar》Settings》SSH Key》new SSH Key

Next verify it, if the word "successfully" appears, it means success

$ ssh -T git@github.com
Enter passphrase for key '/c/Users/MYPC/.ssh/id_rsa':
Hi abc! You've successfully authenticated, but GitHub does not provide shell access.
Copy after login

2. Basic git operations

2.1 git clone existing warehouse

$ git clone git@github.com:hirocastest/Hello-World.git
Copy after login

You will be asked to enter the open key set on GitHub. Password, after successful authentication, the warehouse will be cloned into the current directory.

2.2 git add adds the file to the staging area

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

$ git add 文件夹/文件
Copy after login
2.3 git commit Save the history of the warehouse

The git commit command can actually save the files in the current staging area to the history of the warehouse. Through these records, we can restore files in the working tree.

$ git commit -m "记录一行提交信息"
Copy after login
    m indicates an overview of this submission. If you want to record detailed information, remove -m
2.4 After git push

, just Execute the

push command, and the warehouse on GitHub will be updated

$ git push
Copy after login
2.5 git init initializes the warehouse

clone method to create a warehouse, no execution is required init Operation. If you want to set a local file as a warehouse, you need to perform an init operation

$ mkdir git-tutorial

$ cd git-tutorial

$ git init
Copy after login
2.6 git status to view the warehouse status

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Copy after login

The result is that we are under the master branch and there is nothing to submit Content

2.7 git log View submission log

git log command can view the logs submitted in previous warehouses. Including who made the commit or merge at what time

$ git log
commit 5dbbff6e009abb8a6cc44187c93b694f94fbf82a (HEAD -> main, origin/main, origin/HEAD)
Author: ywm <ywm_up@qq.com>
Date:   Sun Feb 28 17:17:00 2021 +0800
Copy after login
If you only want to display the first line of the commit information, you can add

-- pretty=short after the git log command

$ git log --pretty=short
Copy after login
2.8 git diff Check the difference before and after the change

Execute

git diff Check the difference between the current working book and the staging area

$ git diff
Copy after login

3. Branch operation

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

3.1 git branch displays the branch list

There is an “*” (asterisk) on the left side of the master branch, indicating that this is the branch we are currently on.

$ git branch
* master
Copy after login
3.2 git checkout creates and switches branches

Create and switch to branch feature-A

$ git checkout -b feature-A
Copy after login
In fact, the above command is equivalent to the following two commands

$ git branch feature-A

$ git checkout feature-A
Copy after login
Switch back to branch main

$ git checkout main
Copy after login
Switch back to the previous branch

$ git checkout -
Copy after login
    Do this: create a new branch feature, and modify the README on the new branch. md, and add and commit
It can be proved through actual 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 branches

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

$ git merge --no-ff feature
Copy after login
The editor will then start to enter the merge submission information

3.4 git log --graph View branches as icons

$ git log --graph
Copy after login

4. Change commit operations

4.1 git reset Backtrack historical versions

$ git reset --hard 目标时间点的hash值
Copy after login

View the operation log of the current warehouse through

git reflog to find the hash value before the backtracking history. As long as git's GC (garbage collection) is not performed, the recent historical status can be retrieved at will through the log. Even if the developer performs a git operation by mistake, he can basically use the git reflog command to restore to the original state.

$ git reflog
Copy after login
The most recent operation is printed above, and the oldest operation is printed below.

4.2 Eliminate conflicts

    When merging operations, conflicts are prone to occur. At this time, you need to
  • open the editor to resolve the conflict
  • In actual development, one of them often needs to be deleted, so be sure to
  • carefully analyze the content of the conflicting part before making modifications.
  • After resolving the conflict, perform add and commit operations again
If you are not satisfied with the previous submission information, you can use the amend parameter to modify it

$ git commit --amend
Copy after login
4.3 git rebase -i compress history

Before merging branches, if you find some spelling errors in the submitted content, you may wish to submit a modification, and then include the modification into the previous submission and compress it into a history Record.

git rebase -i HEAD~2
Copy after login
You can select the two latest historical records including HEAD in the branch as objects for the period, and open them in the editor

5 推送至远程仓库

5.1 git remote add 添加远程仓库

在创建新仓库的时候,建议不要勾选 README.md 文件,这样会使本地仓库和远程仓库失去整合性。虽然到时候可以强制覆盖,但防止这一情况发生,还是不要勾选,就创建一个空仓库就好。

git remote 在先写代码,后创建仓库的情况下能较好的使用

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

对于一般先创仓库,后写代码的,需要先 pull 下来仓库,再对文件进行修改

5.2 git push 推送至远程仓库

推送至 master 分支

$ git push -u origin master
Copy after login

-u 参数可以在推送的同时,将 origin 仓库的 master 分支设置为本地仓库当前分支的 upstream(上游),添加这个参数,将来运行 git pull 命令从远程仓库获取内容的时候,本地仓库的这个分支就可以直接从 origin 的 masteer 分支获取内容,省去了另外添加参数的麻烦

除了 master 分支之外,还可以推送到其他分支

$ git checkout -b feature-D

$ git push -u origin feature-D
Copy after login

6 从远程仓库中获取

6.1 git clone 获取远程仓库

$ git clone git仓库地址
Copy after login

将本地的 feature-D 分支更新到最新状态

$ git pull origin feature-D
Copy after login
  • 多名开发者在同一个分支中进行作业时,为减少冲突情况的发生,建议更频繁的进行 push 和 pull 操作

推荐学习:《Git教程

The above is the detailed content of How to understand the basic use of git in 20 minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

DeepSeekAI Tool User Guide and FAQ DeepSeek is a powerful AI intelligent tool. This article will answer some common usage questions to help you get started quickly. FAQ: The difference between different access methods: There is no difference in function between web version, App version and API calls, and App is just a wrapper for web version. The local deployment uses a distillation model, which is slightly inferior to the full version of DeepSeek-R1, but the 32-bit model theoretically has 90% full version capability. What is a tavern? SillyTavern is a front-end interface that requires calling the AI ​​model through API or Ollama. What is breaking limit

What are the AI ​​tools? What are the AI ​​tools? Nov 29, 2024 am 11:11 AM

AI tools include: Doubao, ChatGPT, Gemini, BlenderBot, etc.

What are the Grayscale Encryption Trust Funds? Common Grayscale Encryption Trust Funds Inventory What are the Grayscale Encryption Trust Funds? Common Grayscale Encryption Trust Funds Inventory Mar 05, 2025 pm 12:33 PM

Grayscale Investment: The channel for institutional investors to enter the cryptocurrency market. Grayscale Investment Company provides digital currency investment services to institutions and investors. It allows investors to indirectly participate in cryptocurrency investment through the form of trust funds. The company has launched several crypto trusts, which has attracted widespread market attention, but the impact of these funds on token prices varies significantly. This article will introduce in detail some of Grayscale's major crypto trust funds. Grayscale Major Crypto Trust Funds Available at a glance Grayscale Investment (founded by DigitalCurrencyGroup in 2013) manages a variety of crypto asset trust funds, providing institutional investors and high-net-worth individuals with compliant investment channels. Its main funds include: Zcash (ZEC), SOL,

As top market makers enter the crypto market, what impact will Castle Securities have on the industry? As top market makers enter the crypto market, what impact will Castle Securities have on the industry? Mar 04, 2025 pm 08:03 PM

The entry of top market maker Castle Securities into Bitcoin market maker is a symbol of the maturity of the Bitcoin market and a key step for traditional financial forces to compete for future asset pricing power. At the same time, for retail investors, it may mean the gradual weakening of their voice. On February 25, according to Bloomberg, Citadel Securities is seeking to become a liquidity provider for cryptocurrencies. The company aims to join the list of market makers on various exchanges, including exchanges operated by CoinbaseGlobal, BinanceHoldings and Crypto.com, people familiar with the matter said. Once approved by the exchange, the company initially planned to set up a market maker team outside the United States. This move is not only a sign

Delphi Digital: How to change the new AI economy by parsing the new ElizaOS v2 architecture? Delphi Digital: How to change the new AI economy by parsing the new ElizaOS v2 architecture? Mar 04, 2025 pm 07:00 PM

ElizaOSv2: Empowering AI and leading the new economy of Web3. AI is evolving from auxiliary tools to independent entities. ElizaOSv2 plays a key role in it, which gives AI the ability to manage funds and operate Web3 businesses. This article will dive into the key innovations of ElizaOSv2 and how it shapes an AI-driven future economy. AI Automation: Going to independently operate ElizaOS was originally an AI framework focusing on Web3 automation. v1 version allows AI to interact with smart contracts and blockchain data, while v2 version achieves significant performance improvements. Instead of just executing simple instructions, AI can independently manage workflows, operate business and develop financial strategies. Architecture upgrade: Enhanced A

Significantly surpassing SFT, the secret behind o1/DeepSeek-R1 can also be used in multimodal large models Significantly surpassing SFT, the secret behind o1/DeepSeek-R1 can also be used in multimodal large models Mar 12, 2025 pm 01:03 PM

Researchers from Shanghai Jiaotong University, Shanghai AILab and the Chinese University of Hong Kong have launched the Visual-RFT (Visual Enhancement Fine Tuning) open source project, which requires only a small amount of data to significantly improve the performance of visual language big model (LVLM). Visual-RFT cleverly combines DeepSeek-R1's rule-based reinforcement learning approach with OpenAI's reinforcement fine-tuning (RFT) paradigm, successfully extending this approach from the text field to the visual field. By designing corresponding rule rewards for tasks such as visual subcategorization and object detection, Visual-RFT overcomes the limitations of the DeepSeek-R1 method being limited to text, mathematical reasoning and other fields, providing a new way for LVLM training. Vis

Bitwise: Businesses Buy Bitcoin A Neglected Big Trend Bitwise: Businesses Buy Bitcoin A Neglected Big Trend Mar 05, 2025 pm 02:42 PM

Weekly Observation: Businesses Hoarding Bitcoin – A Brewing Change I often point out some overlooked market trends in weekly memos. MicroStrategy's move is a stark example. Many people may say, "MicroStrategy and MichaelSaylor are already well-known, what are you going to pay attention to?" This is true, but many investors regard it as a special case and ignore the deeper market forces behind it. This view is one-sided. In-depth research on the adoption of Bitcoin as a reserve asset in recent months shows that this is not an isolated case, but a major trend that is emerging. I predict that in the next 12-18 months, hundreds of companies will follow suit and buy large quantities of Bitcoin

See all articles