Git three, Git's three _php tutorials

Source: Internet
Author: User
Tags using git version control system

Git three, git in the third


1. Status Command and diff command

Before we have successfully added and submitted a Readme.txt file, modify the Readme.txt as follows:

echo "Git is a distributed version control system." > Readme.txtecho "git was 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 state of the repository, which shows that Readme.txt has been modified, but not yet ready to commit the changes.

git diff This command to see:

$ 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 looking at difference, which is the UNIX generic diff format, and can be seen from the command output above, and we've added a "distributed" word to the first line.

Readme.txt made a change, and then submit it to the warehouse, commit to modify and submit the new file is 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 your workspace, use the git status command.
    • If Git status tells you that a file has been modified, use Git diff to see what's changed.

2. Version fallback

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

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

We re-submit once again Readme.txt

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

We have now submitted multiple documents, would like to see those? The version control system must have a command that tells us the history, and in Git we use the git log command to see:

$ git logcommit 3628164fb26d48395383f8f31179f24e0882e1e0date:tue 15:11:49 +0000append GPLcommit ea34578d5496 D7dd233c827ed32a8cd576c5ee85date:tue 14:53:12 +0000add Distributedcommit Cb926e7ea50ad11b8f9e909c05226233bf755030date:mon 17:51:55 +0000wrote a Readme file

The git log command shows the commit log from the most recent to the farthest, and we can see 3 commits, the most recent being the Append GPL, the last time the add distributed, and the oldest was wrote a Readme file. Commit 36281**2E1E0 is the commit ID (version number). If too much output information, you can use the git log--pretty=oneline, at this time you see a large string similar to 3628164 ... 882E1E0 is the commit ID (version number).

Each time a new version is submitted, git actually automatically strings them into a timeline. Now you are ready to roll back the Readme.txt to the previous version, the "Add distributed" version, how to do it?

First, git must know which version of the current version, in Git, the current version with Head, that is, the latest commit 3628164 ... 882E1E0 (Note that my submission ID and your affirmation is not the same), the previous version is head^, the last version is head^^, of course, to 100 versions write 100 ^ more easy to count, so written head~100.

Now, we're going to roll back the current version of "Append GPL" to the previous version of "Add distributed", and you can use the git reset command:

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

3. Restore to new version

Then the previous version fallback, you can continue to fall back to the previous version wrote a Readme file, but we now look at the repository state git log:

$ git log

The latest version of the Append GPL is out of sight! Like you from 21st century to sit time shuttle machine came to 19th century, want to go back already can't go back, swollen?

As long as the right environment is still there, you can find the append GPL's commit ID is 3628164 ..., so you can specify a future version:

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

The version number is not necessary to write the whole, the first few can be, git will automatically go to find. Of course, you can't just write the top one or two bits, because git might find multiple version numbers, and there's no way to determine which one.

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

Git's version fallback is very fast because git has a head pointer to the current version inside, and when you roll back the version, Git simply changes the head from pointing to the append GPL to the add distributed.

4. Git reflog command

Now that you've rolled back to a version, what if you want to revert to the new version? What if I can't find a new version of the commit ID?

You can rest assured in Git . When you return to the Add distributed version with git reset--hard head^ , you will have to find the append GPL's commit ID when you want to revert to the append GPL. git provides a command git reflog used to record every command you make:

$ git reflogea34578 head@{0}: Reset:moving to head^3628164 head@{1}: Commit:append GPLea34578 head@{2}: Commit:add Dist Ributedcb926e7 head@{3}: Commit (initial): wrote a readme file

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

Note that we can learn from these two sections:

    • The version that head points to is the current version, so git allows us to navigate between versions of history, using the command git reset--hard commit_id.
    • Before the shuttle, use git log to view the commit history to determine which version to fallback to.
    • To return to the future, use Git reflog to view the command history to determine which version to go back to in the future.

5. Basic Concepts

Workspace : The directory that you can see on your computer, the Learngit folder is a workspace, such as the current directory in our environment.

repository : The workspace has a hidden directory. Git doesn't count as a workspace, it's a git repository.

Staging Area : English is called stage, or index. It is generally stored in the index file (. git/index) in the Git directory, so we also call the staging area index.

Git has a lot of stuff in its repository, the most important of which is the staging area called stage (or index), and the first branch master that git automatically creates for us, and a pointer to master called head.

When we add the file to the Git repository, it's done in two steps:

    • The first step is to use git add to add the file, in fact, the file is modified to add to the staging area;
    • The second step is to commit the change with git commit, which in effect commits all of the staging area content to the current branch.

Because git automatically created the only master branch for us when we created the Git repository, git commit now commits the changes to the Master branch.

You can simply understand that the file changes that need to be submitted are all put to staging area, and then all changes to staging area are submitted at once.

Practical Understanding Staging Area

Now let's make a change to the Readme.txt, such as appending a line of content:

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

Then, add a license text file to the workspace

echo "LICENSE is a new file." > LICENSE

With git status, git displays the results, Readme.txt has been modified, and license has never been added, so its status is untracked.

Now, using git add with two commands, add both Readme.txt and license, and then look at git status again, which can be understood as:

So, the git add command actually puts all the changes you want to commit to staging area (Stage), and then executes a git commit to commit all the staging area changes to the branch at once.

$ git commit-m "understand how stage works"

Once submitted, if you do not make any changes to the workspace, use git status to view, no content, and now the repository becomes so, staging area has no content:

http://www.bkjia.com/PHPjc/1111691.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111691.html techarticle git three, git 1, the Status command and the diff command before we have successfully added and submitted a Readme.txt file, modified 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.