search
HomeBackend DevelopmentPHP Tutorialgit part 3, git detailed explanation part 3_PHP tutorial

git part 3, git detailed explanation part 3_PHP tutorial

Jul 12, 2016 am 08:56 AM
diffgitandpayOrderlandsuccessusAdd toDetailed explanation

Git three, git detailed explanation

1. status command and diff command


We have successfully added and submitted a readme.txt file earlier. Modify the readme.txt as follows:

echo "Git is a distributed version control system. " > readme.txt
echo "Git is free software." >> readme.txt

Run the git status command to see the results:

$ git status
...
no changes added to commit (use "git add" and/or "git commit -a")

The git status command allows us to keep track of the current status of the warehouse. The above shows that readme.txt has been modified, but there are no modifications ready to be submitted.

Take a look at this git diff command:

$ git diff readme.txt
...
-Git is version control system.
+Git is a distributed version control system.
Git is free software

As the name suggests, git diff is to view differences. The format displayed is the universal diff format of Unix. As you can see from the command output above, we added a "distributed" word in the first line.

After making any changes to readme.txt, submit it to the warehouse. Submitting changes and submitting new files are the same two steps, git add and git commit:

$ git add readme.txt
$ git commit -m "add distributed"

Attention

  • To keep track of the status of the workspace, use the git status command.
  • If git status tells you that a file has been modified, you can use git diff to view the modification content.

2. Version rollback


Now, practice again and modify the readme.txt file as follows:

echo "Git is a distributed version control system." > readme.txt
echo "Git is free software distributed under the GPL." >> readme.txt

We submit readme.txt again

$ git add readme.txt
$ git commit -m "append GPL"

We have submitted multiple documents now, would you like to see which ones are there? There must be a command in the version control system that can tell us the history. In Git, we use the git log command to view:

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Date: Tue Aug 25 15:11:49 2015 +0000
append GPL
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Date: Tue Aug 25 14:53:12 2015 +0000
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Date: Mon Aug 24 17:51:55 2015 +0000
wrote a readme file

The git log command displays the commit log from the most recent to the farthest. We can see 3 commits. The most recent one is append GPL, the last one is add distributed, and the earliest one is write a readme file. Commit 36281**2e1e0 is the commit id (version number). If you think the output information is too much, you can use $ git log --pretty=oneline. At this time, you will see a large list of commit ids similar to 3628164...882e1e0. Number).

Every time a new version is submitted, Git will actually automatically string them together into a timeline. Now I am going to roll back readme.txt to the previous version, which is the version of "add distributed". How to do it?

First, Git must know which version the current version is. In Git, HEAD is used to represent the current version, which is the latest commit 3628164...882e1e0 (note that my commit ID is definitely different from yours) , the previous version is HEAD^, and the previous version is HEAD^^. Of course, it is easier to count the previous 100 versions by writing 100^, so it is written as HEAD~100 .

Now, if we want to roll back the current version "append GPL" to the previous version "add distributed", we can use the git reset command:

$ git reset --hard HEAD^
HEAD is now at ea34578 add distributed

3. Restore to the new version


Following the version rollback in the previous section, you can continue to roll back to the previous version write a readme file, but now let’s look at the status of the repository git log:

$ git log

The latest version append GPL is no longer visible! It's like you took a time shuttle from the 21st century to the 19th century, and if you want to go back you can't go back. What should you do?

As long as the environment on the right is still there, you can find that the commit id of the append GPL is 3628164..., so you can specify a version back to the future:

$ git reset --hard 3628164
HEAD is now at 3628164 append GPL

There is no need to write the complete version number, just the first few digits, Git will automatically find it. Of course, you can't just write the first one or two, because Git may find multiple version numbers and it won't be able to determine which one it is.

You can view the contents of readme.txt $ cat readme.txt .

Git’s version rollback is very fast because Git has an internal HEAD pointer pointing to the current version. When you roll back the version, Git just changes the HEAD from pointing to append GPL to pointing to add distributed .

4. git reflog command


Now, you have rolled back to a certain version. What should you do if you want to restore to a new version? What should I do if I can’t find the commit id of the new version?

You can rest assured in Git. When you use $ git reset --hard HEAD^ to roll back to the add distributed version, and then want to restore to append GPL, you must find the commit id of append GPL. Git provides a command git reflog to record each of your commands:

$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file

As you can see, the second line shows that the commit id of append GPL is 3628164, so we can find it again.

Note, we can learn from these two sections:

  • HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。
  • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。
  • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

 

5、基本概念


工作区:就是你在电脑里能看到的目录,learngit文件夹就是一个工作区,比如我们环境中当前的目录。

版本库:工作区有一个隐藏目录.git 这个不算工作区,而是Git的版本库。

暂存区:英文叫stage,或index。一般存放在git 目录下的index文件(.git/index)中,所以我们把暂存区时也叫作索引(index).

Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

我们把文件往Git版本库里添加的时候,是分两步执行的:

  • 第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
  • 第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以现在git commit就是往master分支上提交更改。

你可以简单理解为,需要提交的文件修改通通放到暂存区,然后一次性提交暂存区的所有修改。

 

实践理解暂存区


现在我们对readme.txt做个修改,比如追加一行内容:

echo "Git has a mutable index called stage." >> readme.txt

然后,在工作区新增一个LICENSE文本文件

echo "LICENSE is a new file." > LICENSE

用git status查看一下状态,Git显示结果,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked。

现在,使用两次命令git add,把readme.txt和LICENSE都添加后,用git status再查看一下,通过图可以理解为:

所以,git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。

$ git commit -m "understand how stage works"

一旦提交后,如果你又没有对工作区做任何修改,用git status查看下,没有任何内容,现在版本库变成了这样,暂存区就没有任何内容了:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1111691.htmlTechArticlegit之三,git详解之三 1、status命令与diff命令 前面我们已经成功地添加并提交了一个readme.txt文件,修改readme.txt如下: echo "Git is a distributed...
Statement
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.