Git Learning (i)

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

git is an open source distributed version control system for efficient, high-speed processing of project versioning from very small to very large, and is now widely used in the enterprise. Git is a distributed version control system, unlike the previous SVN,SVN is only connected to the network to work, even if you modify a very small part of the code, you must continue to upload the file to the SVN server, even if it is a local network, it is very time-consuming, plus, if the SVN server hangs off, So programmers are not working, and if the SVN server data is lost, oh my god! everything has to start from scratch, but git is not so, he is a distributed version control system, that is, each of our local host is a complete library, Although we also need to change the files on the uploaded server, but there is a lot of security. Git was developed by Linux and the core founders for a week.

First you need to install GIT, first look at the installation of Linux under the mode:

Under the Linux command line, we can try to enter git, see if there is no git installed, if the system is not prompted to install, then at the command line: sudo apt-get install git, so that you can complete the installation of Git. If this does not install, it should be the version of Linux is older. You can try entering the sudo apt-get install Git-core to complete the installation.

Under Windows, there is a one-click installation of the Git tool, called msysgit, can be downloaded, and then basically a one-click installation. git download, after completion of the installation, there will be a Git-core shortcut at the table, if not, then we open the GIT installation directory, manually open also can, open the shortcut, will appear as the interface:


You can see the git version that I'm currently using by typing git versions. This also indicates that the installation of Git was successful. After the installation is complete, we need to set up our git and set it up with the following command:

git config--global user.name "user name"

git config global user.email "email"

Here my configuration is as follows:


After we've configured Git, we'll need to create our repository. What is a repository? Repository is actually a directory, git can use it to manage our files, including the addition of modifications, and so on.

Here I create a new directory under F: Disk to learn git, "F:/gitlearn", then git-core the terminal, enter Git init, such an empty repository, created. Such as:


You can see that there is more than one. git file in the directory, this file is hidden from the file, it doesn't matter if you don't see it, and you can't delete the file. For git, he can manage the tracking of text files, program code, and other changes to the file, and can know what the file has changed and what has been added. But for binary files, it is not so accurate, git can only know that we have changed the file, but changes in those places, there is nothing to do.

Next, we write a gitfirst.txt text file with the following content:
Git is a good version control.
I like to learn it.

Note If we want to add the file to the Git repository, you must put the file in the directory that owns the. git file, or in his root directory, so git can find the file.

The next step is to add Gitfirst.txt to the repository, which is divided into two steps:

The first step:

git add gitfirst.txt

Step Two:

Git commit-m "Add a Gitfirst file"

This completes the gitfirst.txt file to be added to the local repository work:


Here I explain: git commit-m "", here's the meaning of-m parameter is added description. It is important to note that adding a file to the Git repository requires that git add be executed first, then git commit, and git add simply adds the file to the temporary intermediate directory, git Commit is the real repository where the files for this temporary directory are actually added. We can add multiple files at a time and then commit through git commit once.

Summarize:

Initialize a repository with Git init, and then, in the. git directory or its subdirectories, enter the change or add file, then execute the git add file name and execute git commit-m "description" to add the file to the local repository.

Next I modify the Gitfirst.txt file, and then add it to the repository, here I added a sentence, as follows:

Git is a good version control.

I like to learn it.
Add a string git is free.

As you can see, I added "add a string git is free" compared to the previous file. Such a word. First, with Git status, you can see that the files under the current Git repository have been modified, as well as the details of the changes in Git diff, including which line has been modified, what has been added, what has been deleted, and so on. Finally, just like before, add it to the repository by using the git add file name and git commit-m "add description".


You can see the detailed modification information through git diff.

The next question is, then, if I add the phrase "add a string git is free." Found adding errors, I need to fall back to the previous version, what to do, don't worry, it is possible. To verify this, I'll start with three different additions:

First time:

git haha

Second time:

Git test

Third time:

git gaga

After these three additions are complete, you can check my previous commit record by using git log.


you can see the detailed commit record, here the "three", "the" "One", "one" is the description that I am using git commit-m "instructions" here. If we suspect that there is too much information to be displayed through Git log, it can be shown by the following command: "git log--pretty=oneline", as follows:


Notice that every commit git automatically adds a bunch of characters that we don't know, which is our commit id,git to record every commit we make, and the last time I add a "git Gaga", such a string, if I need to roll it back to the previous version, can do so.

git reset--hard head^

Here, I explain: HEAD represents the current version, head^ represents the previous version, of course head^^ represents the previous version, so if you need to fall back to the first 20 versions of what to do?? Can this jiangzi: HEAD ~100

By looking at Gitfirst.txt can see our files back to the previous version, where I found in the back of the window under the Git-bash seemingly can not copy, in fact, can be, such as:


So you can copy and paste, hey, and Ubuntu under, or a little bit of trouble, to the point, we again through the Git log--pretty=oneline view the current commit record:

8aa4dec5b57c334da8a81361312deab1512a7265 One
1dbd2f7e16112559fe5c5004fb277fa01748ee58 Add a String
579b28a5c26b83931f601469e71a2cb3c110b271 Add a Gitfirst file

The discovery did fall back, because I operated the git reset--hard head^ two times, so I went straight back to the "one" commit record. If I have regrets, I would like to return from the current version to the latest version of what to do. We can use this command to view our git records: Git reflog


Then use the command git reset--hard commit_id to go back to the specified version. For example: I want to go back to the latest version of "Three", how to do it??? "Git reset--hard 2d223ec", so after that, you can go back to the latest version. Viewing the file content discovery did return to the three version.

Summarize:

With git reset--hard commit_id can go back to any version

Git log can be used to see the commit record of git, as well as a git--pretty=oneline to see a short record.


So the problem is, if I have not submitted to find my modified file is wrong, this time what to do??? Here I add such a sentence to "gitfirst.txt": "Oh my god I am wrong",,, the git status discovery has changed at this time, and git will give me a hint as to how to fallback to the previous version


Can see here I through "git checkout-gitfirst.txt", back and forth my changes, look at the file, just add the line "Oh my god I am wrong", does not exist, it means that we have successfully returned. Note that I am not adding this file through git add to staging area in this case. So what if I already have git add gitfirst.txt file?? See:


You can see that when I add the file to staging area, through git status, I'm prompted to reset HEAD <file> via git, I'll use git reset had file name, and then use git checkout--the file name to go back to No Modified version.


So what if I accidentally delete a file in git? Here I first create a "tobedelete.txt" text file in the repository, and then add it to the repository via git add and git commit, so what if I accidentally delete the file? Look first:


You can see that when I delete the file and continue to view the status through GIT status, we are prompted to restore the previous deletion in the form of git checkout-file. This time, I'll execute the command first:

Git checkout--tobedelete.txt

The deleted files are then restored. Check the local file to find out that there is indeed a recovery. Isn't it amazing?

There is another case of deletion here, if I do need to remove the file from the local and repository, what to do: In fact, Git has given us an obvious hint, such as:


The deletion was done through several commands above, including deleting from the local library and recovering the deleted files to the local library.


Well, the first stage of Git learning is here. I hope you enjoy it.

Git Learning (i)

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.