Git version control

Source: Internet
Author: User

What is Git?

Git is currently the most advanced Distributed version control system in the world.

What is the main difference between SVN and git?

SVN is a centralized version control system, the repository is centrally placed in the central server, and work, with their own computers, so the first to get from the central server where the latest version, and then work, after the completion of the job, you need to put the work done to the central server. Centralized version control system must be networked to work, if the LAN can also, the bandwidth is large enough, fast enough, if under the Internet, if the speed is slow, I wonder.

Git is a distributed version control system, then it does not have a central server, everyone's computer is a full version of the library, so that the work of the time will not need to network, because the version is on their own computer. Since everyone's computer has a full repository, how do people collaborate? For example, they changed the file a on the computer, the other people also on the computer changed the file a, at this time, you two only need to put their changes to each other, you can see each other's changes.

How do I install git on Windows?

Need to download one from the official website to install, my installation in the D Drive, directory: D:\Git, configure environment variables: In the system variable path plus (depending on your installation path): D:\Git\cmd, click Confirm, confirm, confirm

Win+r, input cmd , Click Confirm , enter git , and then Enter , the following shows that the installation was successful

After the installation is complete, find "git–> git bash" in the Start menu and click Git Bash to open git

After you open git, you need the final step, and enter the following at the command line:

git config--global user.name "Jacksile"

git config--global user.email [email protected]

Because Git is a distributed version control system, you need to fill in the user name and mailbox as an identity.

Note:the git config–global parameter, with this parameter, means that all of your git repositories on this machine will use this configuration, and of course you can specify a different user name and mailbox for a warehouse.

How does it work? Create a repository.

What is a repository? Repository also known as the Warehouse, English name repository, you can easily understand a directory, all the files in this directory can be managed by git, file additions, modifications, deletions, git can track, so that any time can track history, or at some point in the future can also "restore the file ”。

So the creation of a repository is also very simple, as follows I am the e-disk gittest directory to create a new version of the Testgit repository.

The PWD command is used to display the current directory.

1. Use the command git init to turn this directory into a repository that git can manage, as follows:

At this time your current E:\GitTest\testgit directory will be more than a. Git directory, this directory is git to track the management version of, nothing to manually change the files in this directory, otherwise, the GIT repository will be destroyed. As follows:

In some cases, this directory is hidden by default and needs to be displayed as follows

2. Add the file to the repository.

First to be clear, all version control system, can only track changes in text files, such as txt files, Web pages, all the code of the program, Git is not listed outside, the version control system can tell you each time the changes, but pictures, videos of these binaries, although can also be managed by the version control system, But can not track the file changes, only the binary files each time the change string up, that is to know the picture from 1kb to 2KB, but what changed, version control also do not know.

The following code demonstrates

Add File

I create a new Notepad file in the Repository Testgit directory test.txt The content as follows: first time Add content

Use the command git add test.txt to go inside the staging area. As follows:

If the same as above, without any hint, the description has been added successfully.

Use the command git status to view the current status and proceed to the third step after confirming the error

Use the command git commit to tell git to submit the file to the repository.

Now that we have submitted a Test.txt file, we can see if there are any files that have not yet been submitted under the command git status below:

The display file has not been submitted.

Note:

    1. Adding an empty folder does not require management
    2. There are 2 ways to add a folder containing files:

1, the files in the folder according to the above file processing, such as the Add Test folder, there are testin.txt files

2, the folder as a file processing, the method above (suitable for a lot of files in the folder), such as Add Test2 folder, there are testin2.txt files

deleting files

Create a new file Test2.txt and add it to the warehouse in the above way

command to delete a file that already exists in the warehouse

Manually delete, create file Test3.txt

The Git add–a above. Here-A is a shorthand for--all, so it can also be written as Git add–all.

Manual removal of folders and manual deletion of files one principle

Accidental deletion of recovery

As long as there is no commit, what if I want to recover this file in the repository?

You can use the following command git checkout--test.txt, as follows:

File modification

I'm going to go ahead and change the test.txt content, like I'm adding a second row below, and continue using GIT status to see the results, as follows:

The above command tells us that the Readme.txt file has been modified but has not been committed.

Next I want to see what the Readme.txt file changed, how to view it? You can use the following command:

Git diff test.txt is as follows:

The red is the content before the change, the green is the modified content

Knowing what changes have been made to the Test.txt file, we can safely submit to the warehouse, submit the changes and submit the document is the same 4 steps

Version fallback

As above, we have learned to modify the file, and now I continue to modify the Test.txt file, and then add a line

Content is: The third addition of the content continues to execute the command as follows:

Now that I have made three changes to the Test.txt file, I now want to check the history, how to check it? We can now use the command git log demo as shown below:

The git log command displays logs from the most recent to the farthest, and we can see the last three commits, the most recent being a file that was modified 2 times. Last time: Commit the modified first file, first: Delete the Test2 folder, if too much information is displayed above, we can use the command The Git log--pretty=oneline demo is as follows:

Now I want to use the version fallback operation, I want to fallback the current version to the previous version, what command to use? You can use the following 2 kinds of commands, the first is: git reset--hard head^ then if you want to fall back to the first version just change head^ to head^^ and so on. If you want to fall back to the first 100 versions, using the above method is certainly inconvenient, we can use the following simple command operation: git reset--hard head~100. The Test.txt content before fallback is as follows:

Check out the following Readme.txt content: View by command cat Test.txt

As you can see, the content has been rolled back to the previous version. We can continue to use git log to view the following history information, as follows:

We see a file that was submitted with a modified content that we did not see, but now I want to go back to the latest version, such as: How to recover the content of the third addition? We can go back with the version number and use the command method as follows:

Git reset–hard version number, but now if I've turned off the command line or the version number of the 333 content, I don't know. How do you know the version number of the third added content? You can get to the version number by using the following command: Git Reflog, and then back to the version number 1115FFB of a file after the commit modification 2 times to the content containing the third addition.

You can see that the latest version is now available.

Understand the difference between a workspace and a staging area?

Workspace: The directory you see on your computer, such as the files in Testgit (except the. git hidden directory repository). or later, you need to create a new directory file, and so on is the workspace category.

Version Library (Repository) : The workspace has a hidden directory. Git, this does not belong to the workspace, this is the repository. The repository contains a lot of things, the most important of which is the stage (staging area), and Git automatically created the first branch master for us, and a pointer to master head.

As we said earlier, using Git to submit a file to the repository has two steps:

The first step is to add the file using git add, which is actually adding the file to the staging area.

The second step: Commit the change using git commit, which is actually committing all the contents of staging area to the current branch.

We continue to use demo to demonstrate the following:

We add a line in the Test.txt: the fourth addition of content, and then in the directory to create a new file for the Test4.txt content of test4, we first use the command git status to view the status, as follows:

Now let's use the git add command to add 2 files to staging area, and then use GIT status to see the next state, as follows:

Then we can use git commit to commit to the branch one at a time, as follows:

Undo Changes

For example, I now add a line in the Test.txt file: The sixth time add the content, we first through the command to see the following:

Before I submit, I found that the addition of the sixth time added content is wrong, should be the fifth time content, so I have to restore the previous version immediately, now I can have the following methods can be modified:

First: If I know what to delete, just manually change the files that need to be removed, then add to staging area, and finally commit.

Second: I can revert to the previous version directly in the previous way. Using Git reset–hard head^

But now I do not want to use the 2 methods above, I would like to directly use the Undo command how to do? First, before we undo, we can look at the current state with Git status. As shown below:

As you can see, Git will tell you that git checkout-file can discard changes to the workspace, such as the following command:

Git checkout--test.txt, as follows:

We'll find out that the sixth time Plus is gone.

Command git checkout--test.txt means to undo all changes made to the Test.txt file in the workspace, here are 2 cases, as follows:

    1. Test.txt automatically modified, has not been placed in the staging area, using undo changes back to the same state as the repository.
    2. Another is that the test.txt has been put into the staging area, and then modified to undo the changes back to the state after the addition of staging area.

For the second case, I think we continue to do the demo to see, if now I add a line to Test.txt: The fifth time added content, my git add to staging area, then add content: The seventh Add the content, I want to undo the command to return to staging area after the state. As shown below:

Note: the command git checkout--readme.txt--is important, if not--then the command becomes a branch of creation.

Git version control

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.