Git Study Notes <local version Library Management> (2)

Source: Internet
Author: User

Today, I will record my notes on creating and managing version libraries.

 

What is the version library?

A version library can be understood as a repository (a directory that can be managed by GIT). Modifications and deletions to files in the repository can be tracked, in addition, each version is saved for restoration (only the modified or deleted files are saved). Therefore, it can be considered that different versions are saved, so it is called a version library.

 

How to manage it?

Git provides you with various commands to manage version libraries.

 

1.Create:

Select a place to create a new folder and enter the folder (do not see Chinese path in Windows)

$ Mkdir learner// Create a folder in the current directory

$ CD learner// Enter this folder

If you do not know where the current directory is, enter $ PWD to display the complete path.

Next$ Git initThis folder can be managed by git. Of course, setting a directory for an existing file is also possible.

 

2.Add files to the version Library:

As described in Chapter 1, the local client has a workspace and a version library.

All files created manually in the folder actually exist in the workspace. You must upload the files to the temporary storage area of the version library and submit them to the official version library. the temporary storage area allows you to submit multiple files at a time, so that the version library code can be run at any time to maintain integrity.

Another problem is that git can only track changes to TXT files, web pages, and code (including the changes in the first line), images, videos, and so on. Although they can be managed, they do not know where to change. And,Word documents cannot track changes..

SuggestionsUse UTF-8Encoding to save the file. In this caseDo not use Windows notepadTo edit the text, because when notepad saves the UTF-8, a 0 xefbbbf (hexadecimal) character is added at the beginning. There may be some unexpected errors. ShouldDownload another text editor, such as PS3..

 

Next, add the file.

Create a hello.txt File

/*Hello, I‘m Hanson Green.*//*Wish You Can Learn Something From Here.*/

 

Saved in the directory managed by git (sub-directories can also be used ).

① Input in git bash$ Git add hello.txt(If not displayed, the file is successfully saved in the temporary storage area.

② Continue Input$ Git commit-M "add hello.txt"Then the file is submitted to the repository. The following-M "content" is used to save the remarks for this submission. Be sure to fill it out, which is necessary for work.

1 file changed, 2 insertions (+) tells you to change a file and add two lines.

 

In this way, the file is submitted. You can add multiple files at a time for one-time submission.

 

3.View the workspace status:

Modify hello.txt.

/*Hello, I‘m Hanson Chan.*//*Wish You Can Learn Something From Here.*/

Then enter $ git status to view the status)

Shown as follows:

?
$ git status On branch master Changes not staged for commit:   (use "git add <file>..." to update what will be committed)   (use "git checkout -- <file>..." to discard changes in working directory)           modified:   hello.txt   no changes added to commit (use "git add" and/or "git commit -a")

It tells you that there are changes not submitted. Two solutions are prompted: Use git add <File> to add the file to be submitted; Use git checkout -- <File> to discard changes (this method is mentioned below ).

Then the system prompts that the modified file is hello.txt. Is it user-friendly? What should I do next when I want to view the changes.

Input$ Git diff

$ git diffdiff --git a/hello.txt b/hello.txtindex faa367f..761d254 100644--- a/hello.txt+++ b/hello.txt@@ -1,2 +1,2 @@-/*Hello, I‘m Hanson Green.*/+/*Hello, I‘m Hanson Chan.*/ /*Wish You Can Learn Something From Here.*/\ No newline at end of file

You can view the changes.

Input$ Git add hello.txt

Then check the status $ git status

$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)        modified:   hello.txt

On branch master is in the branch master, which will be discussed later.

Then$ Git commit-M "change my name"Submitted

After entering $ git status, you will be prompted that the workspace is clean and there is no need to submit.

 

4.Version rollback and Management:

First, add a row to the file.

/* I want to change this file */

Then add and commit-M "Some Change" (note required)

How do we know the records submitted previously? Of course there is a solution.

Enter $ git log (-3 can be added after git log,-3 means only two commit will be displayed. If you want to display five,-5 will be displayed. If this parameter is not specified, the GIT log will be displayed from the commit .)

 

$ Git log


Commit b160e923d62a6568233d4278551893bd00c3e1c0
Author: Big Data <[email protected]>
Date: Wed Aug 6 11:37:08 2014 + 0800

 

Some change

Commit quota: [email protected]> date: Wed Aug 6 11:11:47 2014 + 0800 change my namecommit quota: [email protected]> date: wed Aug 6 10:53:18 2014 + 0800 add hello.txt

All information is recorded. Commit is the corresponding version number. If you think too much,Enter $ git log -- pretty = oneline to display only commit and remarks.

 

Then you find that this change has an error but has already been submitted to the local database. What should I do if I want to roll back to the previous version?

Method ①:

Input$ Git reset -- hard head ^ 

Head is the current Branch (you can also directly write the branch name, such as the current master. This concept will be discussed later .)

The following ^ indicates the previous version. ^ Indicates the first two, which is equivalent ~ 2 is$ Git reset -- hard head ~ 2


So what does the hard parameter mean? There are actually -- soft and -- mixed options.

-- Mixed: This is the default option. For example, git reset [-- mixed] master ^. It only serves to reset the branch status to master ^, but does not change the content of any working file. That is, all file changes from master ^ to Master are retained, but all commit logs between master ^ and master are cleared, the changed file content is not identified by git Add. If you want to re-commit the file, you also need to perform git add on the changed file. In this way, a very clean submission record is obtained after the commit operation. (Content in the temporary storage area and warehouse is rolled back)
-- Soft is equivalent to git reset-mixed, and then git add for the changed file. If this option is used, you can directly commit it. (The contents in the repository are rolled back)
-- Hard:This leads to the rollback of all information, including the file content.. It is generally used only when the discarded code is reset. After execution, the file content cannot be restored. (The contents in the working directory, temporary storage area, and warehouse are rolled back)

If you know the version number, for example, change my name and the commit_id is f5842ba282aec1_f1cebe5899ec413ce74fc681.

Run$ Git reset f5842ba2You can directly roll back to this version. You do not have to enter the commit from the first few letters.

In addition, in the above reference, there is an exception when hard cannot be recovered. That is, when you have not disabled the command line, you can go up to the version number and execute $ git reset commit_id (the version number you found.

In addition, you can use $ git reflog to find all your operation records or the original commit _ id.

Method 2:

Input$ Git revert commit_id


This can also be returned to the original version, but at this time it is to form a new record, rather than back.

-------------------------------

The difference between the two methods is that the reset is backward, and the records after that version will disappear from the repository (depending on whether the selected mode exists in the workspace or temporary storage area ). The revert is equivalent to a new version, but it is the same as the file with the specified version number.

------------------------------

5.Undo your modifications in the workspace or temporary storage area:

Do you still remember to check the status after modification in Workspace 3?

That's right.$ Git checkout -- FileYou can undo the workspace modification to the last git add or commit. (File is the file name)

Do you still remember to view the status after adding and modifying the file?

Is to use$ Git reset head FileThen the files in the temporary storage area will be rolled back to the work area. Then execute the code to discard the workspace modification (in the above two lines ).

 

6.Delete an object:

Use$ RM fileYou can delete files in the workspace.$ Git checkout -- File), And then enter$ Git RM file and $ git commit-M "Remove File"It will be deleted from the version library.

Git Study Notes <local version Library Management> (2)

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.