Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Git creates version Library _ Power Node Java College collation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is a version library? Version library, also known as warehouse, English name repository, you can simply understand as a directory, this directory all the files can be managed by Git, each file modified, deleted, Git can track, so that at any time you can track history, or at some point in the future can be "restored".

So, it's very simple to create a version library. First, choose an appropriate place and create an empty directory:

$mkdir learngit$ cd learngit$ pwd/Users/michael/learngit

The pwd command displays the current directory. On my Mac, this warehouse is located in / Users/michael/learngit.

If you are using the Windows system, to avoid all kinds of inexplicable problems, please make sure that the directory name (including the parent directory) does not contain Chinese.

The second step is to change this directory into a warehouse that Git can manage through the git init command:

$git initInitialized empty Git repository in / Users/michael/learngit/.git/

Git built the warehouse in an instant, and told you that it is an empty warehouse (empty Git repository). Careful readers can find that there is a .git directory under the current directory, this directory is Git to track and manage the version library, do not manually modify the files in this directory, otherwise change the chaos, it will destroy the Git warehouse.

If you don't see the .git directory, it's because the directory is hidden by default and can be seen with the ls-ah command.

It is not necessary to create an Git repository under an empty directory, but it is also possible to choose a directory that already has something. However, it is not recommended that you use the company project you are developing to learn Git, otherwise you will not be responsible for all the consequences.

Add files to the version library

First of all, it is clear here that all version control systems can only track changes to text files, such as TXT files, web pages, all program code, etc., and Git is no exception. The version control system can tell you every change, such as adding a word "Linux" in line 5 and deleting a word "Windows" in line 8. Although the binary files such as pictures and videos can also be managed by the version control system, they cannot track the changes of the files. They can only string up the binary files every time they are changed, that is, they only know what the picture has changed from 100KB to 120KB, but the version control system does not know and cannot know what has changed.

Unfortunately, Microsoft's Word format is binary, so the version control system cannot track changes to Word files, and the previous example is just to demonstrate that if you really want to use a version control system, you have to write the file in plain text.

Because the text is encoded, for example, there is a commonly used GBK encoding in Chinese and Shift_JIS encoding in Japanese. If there are no problems left over from history, it is strongly recommended to use the standard UTF-8 encoding. All languages use the same encoding, which is not conflicted and is supported by all platforms.

Children's shoes that use Windows should pay special attention to:

Never use notepad that comes with Windows to edit any text files. The reason is that the Microsoft notepad team used a very retarded behavior to save UTF-8-encoded files. They cleverly added 0xefbbbf (hexadecimal) characters at the beginning of each file, and you will encounter a lot of incredible problems, for example, the first line of the web page may show a "?", the correct program will report syntax errors as soon as it is compiled, and so on, which are all brought about by the mentally retarded behavior of notepad. It is recommended that you download Notepad++ instead of notepad, not only powerful, but also free! Remember to set the default encoding of Notepad++ to UTF-8 without BOM:

Back to the point, now let's write a readme.txt file with the following contents:

Git is a version control system.

Git is free software.

Be sure to put it in the learngit directory (or a subdirectory), because this is a Git repository, and Git can't find this file anywhere else.

Compared with three steps to put an elephant in the refrigerator, it takes only two steps to put a file in the Git warehouse.

The first step is to tell Git with the command git add to add the file to the repository:

$git add readme.txt

Execute the above command without any display, which is right. Unix's philosophy is "no news is good news", indicating that the addition is successful.

The second step is to tell Git with the command git commit to submit the file to the warehouse:

$git commit-m "wrote a readme file" [master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions (+) create mode 100644 readme.txt

A brief explanation of the git commit command,-m is followed by a description of this submission, you can enter anything, preferably meaningful, so that you can easily find the change record from the history.

Is it all right if you don't want to enter-m "xxx" after too much trouble? There are ways to do this, but you are strongly discouraged because input instructions are important to yourself and to others. Really do not want to enter the description of children's shoes, please Google, I will not tell you this parameter.

The successful execution of the git commit command will tell you that 1 file has been changed (our newly added readme.txt file) and two lines have been inserted (readme.txt has two lines).

Why does Git need add,commit to add files in a total of two steps? Because commit can submit many files at a time, you can add different files multiple times, such as:

$git add file1.txt$ git add file2.txt file3.txt$ git commit-m "add 3 files."

We have successfully added and submitted a readme.txt file, and now it is time to continue to work, so we continue to modify the readme.txt file to read as follows:

Git is a distributed version control system.

Git is free software.

Now, run the git status command to see the results:

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

The git status command allows us to keep abreast of the current state of the warehouse, and the above command tells us that the readme.txt has been modified, but not ready to commit the changes.

Although Git tells us that readme.txt has been modified, it would be nice to see what has been changed. For example, when you come back from a two-week vacation, on your first day at work, you can't remember how to modify readme.txt last time, so you need to use the git diff command to see:

$git diff readme.txt diff-- git a/readme.txt b/readme.txtindex 46d49bf..9247db6 100644 Murray-a Git is a version control system.+Git is a distributed version control system README.txt.com @-1 Git is a version control system.+Git is a distributed version control system 2 + 1 Magi 2 @ @. Git is free software.

Git diff, as its name implies, is to view difference, which is displayed in the diff format commonly used by Unix. As you can see from the command output above, we have added the word "distributed" on the first line.

Once you know what changes have been made to readme.txt, you can rest assured to submit it to the repository. Submitting changes is the same as submitting a new file. The first step is git add:

$git add readme.txt

There is also no output. Before performing the second step git commit, let's run git status to see the current status of the warehouse:

$git status# On branch master# Changes to be committed:# (use "git reset HEAD..." To unstage) # # modified: readme.txt#

Git status tells us that the changes to be committed include readme.txt, and the next step is to safely commit:

$git commit-m "add distributed" [master ea34578] add distributed 1 file changed, 1 insertion (+), 1 deletion (-)

After the submission, we use the git status command to look at the current state of the warehouse:

$git status# On branch masternothing to commit (working directory clean)

Git tells us that there are currently no changes that need to be committed, and that the working directory is working directory clean.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report