Git 3 and git 3 _ PHP Tutorial

Source: Internet
Author: User
Git 3 and git 3. Details are as follows: echoGitisadistributed git 3 and git 3

1. status and diff commands

Previously, we successfully submitted a readme.txt file and modified readme.txt as follows:

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

Run the git status command to check the result:

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

The git statuscommand allows us to grasp the current status of the warehouse. the command output shows that readme.txt has been modified, but it is not ready to be submitted.

Git diff command:

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

Git diff, as its name implies, is to view difference. the displayed format is the common diff format of Unix. from the command output above, we can see that we added the word "distributed" in the first line.

After modifying readme.txt, submit it to the repository. The two steps are the same: git add and git commit:

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

Note:

  • You can use the git status command to understand the status of the workspace at any time.
  • 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, exercise again and modify the readme.txt file as follows:

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

Let's submit another example: readme.txt.

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

We have already submitted files many times. what are there? The version control system certainly has a command that tells us the history. in Git, we can use the git log command to view:

$ git logcommit 3628164fb26d48395383f8f31179f24e0882e1e0Date: Tue Aug 25 15:11:49 2015 +0000append GPLcommit ea34578d5496d7dd233c827ed32a8cd576c5ee85Date: Tue Aug 25 14:53:12 2015 +0000add distributedcommit cb926e7ea50ad11b8f9e909c05226233bf755030Date: Mon Aug 24 17:51:55 2015 +0000wrote a readme file

The git log command displays the most recent and farthest commit logs. we can see three commits, the most recent one is append GPL, and the last one is add distributed, the first time is wrote a readme file. Commit 36281 ** 2e1e0 is the commit id (version number ). You can use$ Git log -- pretty = onelineIn this case, a large string similar to 3628164... 882e1e0 is the commit id (version number ).

Each time you submit a new version, Git will automatically concatenate them into a timeline. Now, the Slave node rolls readme.txt back to the previous version, that is, the version of "add distributed". how can this problem be solved?

First, Git must know which version the current version is. in Git, use HEAD to indicate the current version, that is, the latest commit 3628164... 882e1e0 (note that my submission ID is definitely different from yours). The previous version is HEAD ^, and the previous version is HEAD ^, of course it is easier to write 100 ^ in 100 versions.HEAD ~ 100.

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

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

3. restore to the new version

Next, we can roll back the previous version to the previous version of wrote a readme file. but now let's look at the status git log of the version Library:

$ git log

The latest version of append GPL is no longer available! For example, you have been on a shuttle from the 21st century to the 19th century. if you want to go back, you cannot go back. is it swollen?

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 to return to a future version:

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

The version number does not need to be fully written. only the first few digits can be written. Git will automatically find the version number. Of course, you cannot write only the first one or two digits, because Git may find multiple versions, and you cannot determine which one is.

You can upload readme.txt content $ cat readme.txt.

Git's version rollback speed is very fast, because Git internally has a HEAD pointer pointing to the current version. when you roll back the version, git only changes the HEAD from pointing to append GPL to pointing to add distributed.

4. git reflog command

Now, you have rolled back to a specific version. what if you want to restore to the new version? What if the new version of commit id cannot be found?

InGitYou can rest assured. When you use$ Git reset -- hard HEAD ^When you roll back to the add distributed version and restore to append GPL, you must find the commit id of append GPL.GitA git reflog command is provided to record each of your commands:

$ git reflogea34578 HEAD@{0}: reset: moving to HEAD^3628164 HEAD@{1}: commit: append GPLea34578 HEAD@{2}: commit: add distributedcb926e7 HEAD@{3}: commit (initial): wrote a readme file

In this way, the second line shows that the commit id of append GPL is 3628164, so that we can find it again.

Note:

  • The version pointed to by HEAD is the current version. Therefore, Git allows us to shuttle between versions and use the command git reset -- hard commit_id.
  • You can use git log to view the submission history to determine the version to be rolled back.
  • To return to the future, use git reflog to view the command history to determine which version to return.

5. Basic concepts

Work Zone: The Directory you can see on your computer. the learngit folder is a work area, such as the current directory in our environment.

Version Library: There is a hidden directory in the workspace. git, which is not the workspace, but the Git version Library.

Temporary storage area: Stage or index. It is generally stored in the index file (. git/index) under the git directory, so we call the temporary partition index ).

The Git version library stores a lot of things, the most important of which is the stage (or index) temporary storage zone, and the first branch master that Git automatically creates for us, and a pointer to the master is HEAD.

When we add files to the Git repository, we perform the following two steps:

  • The first step is to use git add to add the file. In fact, it is to add the file modification to the temporary storage area;
  • The second step is to use git commit to submit changes. In fact, it is to submit all the content in the temporary storage area to the current branch.

When we create a Git version Library, Git automatically creates a unique master branch for us, so now git commit commits changes to the master branch.

You can simply put the file changes to be submitted in the temporary storage area, and then submit all changes to the temporary storage area at a time.

Practice

Now let's modify readme.txt, such as appending a row of content:

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

Then, add a LICENSE text file in the workspace.

echo "LICENSE is a new file." > LICENSE

The git status.txt status indicates that readme.txt has been modified and the LICENSE has never been added. Therefore, the status of readme.txt is Untracked.

Now, run the git addcommand twice to add both readme.txt and LICENSE, and check it again with git status. The figure can be understood:

Therefore, the git add command puts all the modifications to be submitted in the Stage. then, execute git commit to submit all the modifications in the temporary storage area to the branch at a time.

$ git commit -m "understand how stage works"

Once submitted, if you haven't made any changes to the workspace, you can use git status to check that there is no content. now the version Library is changed to this, and there is no content in the temporary storage zone:

Prepare 1. before the status command and diff command, we have successfully submitted a readme.txt file. modify readme.txt as follows: echo "Git is a distributed...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.