"Git through time and space" to create a version library and common command operations

Git is an open source distributed version control system that can effectively and quickly handle version management for small to very large projects. Git's powerful branch management far exceeds SVN. How does git create a repository? How to perform some common operations? For the answer, please see the breakdown below.
Insert picture description here
When I was young, I watched a very impressive play called "Love Through Time and Space". Today, I was also cheeky and directed a "Git Through Time and Space".

what is a repository? No matter how often we use git, but friends in the IT industry must often hear someone saying, which project of which cattle X is in which warehouse, and which warehouse of the project has been maliciously added to the issue (I have heard before Apollo's planned code warehouse is occupied by many unrelated Chinese comments ~ Hey, our test engineers must not do this and do our best for this industry);

Having said so much nonsense, in fact, the warehouse we usually talk about is the repository. The repository is also called a repository, and the English name is repository. I believe many buddies who use GitHub often see this word;

I will simply understand it as a directory folder, where you can put various files, and you can delete and modify it as usual. However, Git's "folder" is more powerful, it has a good memory-Git can " Remember "the history of the entire management, but also" archive ".

Note: All version control systems can only track changes in text files, such as TXT files, web pages, all program codes, etc., and Git is no exception.

How to create a repository is very simple:

1) Choose a path (you like just ok!) To create an empty directory
Insert picture description here
2) The warehouse address is created, it is now a normal folder, and then use the git init command to turn it into a "Git folder", which is Turn this directory into a repository
Insert picture description here
that Git can manage. It will prompt that an empty Git repository has been created in your directory, and there is an additional .git in the directory, at this time, even if the creation is complete.
Insert picture description here
2.1 Submit documents

Before looking at the commands, let's use a picture to understand several concepts, which can facilitate understanding and memory:

Working Directory (Working Directory)
temporary warehouse (staging area, Staging Area)
Git warehouse (Repository)
Insert picture description here
we are first in the work area to edit files; then add to the temporary warehouse, you can add multiple; finally submit together to submit In the warehouse.
Then formally enter into practical operation:

1) Under the path of mygit, create the file readme.txt and enter the following:

I want to study Git

Insert picture description here
2) Use the git add command to submit the file to the temporary warehouse (no message will be output if the operation is successful, don't panic if you don't see the output ~)
Insert picture description here
3) Then use the git commit command to submit the file from the temporary storage area to the warehouse.
Insert picture description here
Command explanation:
Insert picture description here
2.2 Find the difference (diff)

1) We first use git status to view the status of the current warehouse.
Insert picture description here
Now it means that there is nothing to commit on a master branch, and the working directory is currently clean.

2) Modify the readme.txt file:
Insert picture description here
3) Then use git status to view At
Insert picture description here
this time, you can see that the file has been modified, but it has not been added and committed;

4) Then use the git diff (different) command to see what is the difference between the current operation and the previous operation, and where has been modified:
Insert picture description here
you can see that I added very much in the second line

2.3 Back to the past (withdrawal back)

1) We first submit the modified file to the warehouse:
Insert picture description here
2) We then use the git log command to see what we have done before:
Insert picture description here
after the git log command, we can see the history of the previous commit commit, every Each submission is assigned a unique commit id. This id is the key to our return to the past, which is quite the same as a certain period in the science fiction movie.

3.1) HEAD-Now, if I want to go back to the "period" of study git, I can use git reset --hard HEAD ^ At
Insert picture description here
this time we can see that I have successfully returned to the "period" of study git, very much It disappears.
Command explanation:
If you do n’t understand it in the past, it can also be understood as an archive. When I was a kid, I like to play a computer game called Red Alert. When I play a
task battle, it will be archived before each level, which is convenient. After "dead", you can go back to the designated level and come back to
represent the last archive. ^ Represents the last archive. If you want to reflect up to 100 versions, you can use HEAD ~ 100 directly to return to the past
3.2) commit id-now Then modify the file, add I can fly in the first line, and then add, commit:

Insert picture description here
git log View the modification commit log.
Insert picture description here
It says that you can use the commit id to go back to the past. Now let's try it; use the git reset --hard 64f5ce ... command:
Insert picture description here
from the above results, we can see that we have successfully returned through the commit id In the past, I went back to the time when there was no "I can fly"; one thing I can notice is that when I wrote the commit id, I didn't write it all, just wrote the previous part, and git can find it;

amount. . . Certainly someone has to ask how many are specific, to be honest ~ I do n’t know 0.0, nor have I specifically studied it, the top four or five? Six or seven? Seven or eight? Just meet the usage requirements, you can guarantee the unique id, just copy a few more. . .

3.3) Back to the era before add-only the add file arrived in the temporary storage area, and there was no commit; it felt as if it had entered another space of add, and he could not be found in the future. You can use git reset HEAD to drop the file

Now the content of my readme.txt file is as follows, and it has been added to the temporary storage area:
Insert picture description here
use the git reset HEAD readme.txt command to undo the modification back from add
Insert picture description here
3.4) At this time there is another question, I just readreadme.txt from add The temporary storage area has been revoked, and the file has not changed; to revoke the file, you must use the git checkout – command:
Insert picture description here
supplement: a file has been submitted to the repository, sometimes we are in the work area Deleted a file by mistake (rm or manual), this file still exists in the repository at this time, you can use the git checkout-command to "recover";

If you really want to delete files from the repository, you must use the git rm and git commit commands

2.4 Return to the future

1) Yes, in the movies that are generally traversed, you will always find a way to return to the future, and git is no exception. In addition to returning to the past (version back), you can also return to the future (back to the new version);

Here we still have to do it with the help of commit id, but we ca n’t see the positioning coordinates in the future by using git log after retreating (commit id):
Insert picture description here
2) At this time, we will use the git reflog command, record your previous The way
Insert picture description here
I walked: I can see the commit id of the previous I can fly; here by the way, it is said that the commit id does not need to write all, write the previous part, but not sure how many to write, see git here The log only records the top seven, so let's just count the seven. . .

With the commit id, you get the coordinate information and method to return to the future, and the movie is almost at the end, this article is about to end, here is the end:
Insert picture description here
(The article comes from Hogwarts Test Academy)

Published 10 original articles · Likes0 · Visits10

Guess you like

Origin blog.csdn.net/weixin_46635091/article/details/105432781