github study notes (2) create a repository

What is a repository? Repository is also known as warehouse, English name repository , you can simply understand it as a directory, all files in this directory can be managed by Git, and Git can track the modification and deletion of each file, so that it can be tracked at any time. History, or can be "reverted" at some point in the future.

So, creating a repository is very simple. First, choose a suitable place and create an empty directory:

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

pwdThe command is used to display the current directory. On my Mac, this repository is located at /Users/michael/learngit.

If you use a Windows system, in order to avoid encountering various inexplicable problems, please make sure that the directory name (including the parent directory) does not contain Chinese.

The second step is git initto turn this directory into a repository that Git can manage with the command:

$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/

Git builds the repository in an instant, and tells you that it is an empty Git repository. Careful readers can find that there is an additional directory under the current directory .git. This directory is used by Git to track and manage the repository, nothing to do Do not manually modify the files in this directory, otherwise the changes will be messed up and the Git repository will be destroyed.

If you don't see .gitthe directory, it's because the directory is hidden by default, and ls -ahyou can see it with the command.

 

It is also not necessary to create a Git repository in an empty directory, 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 any consequences.

Add the file to the repository

First of all, let me clarify that all version control systems can only track changes in text files, such as TXT files, web pages, all program codes, etc. Git is no exception. Version control systems can tell you every change, such as adding the word "Linux" on line 5 and deleting the word "Windows" on line 8. Although 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 together each change of the binary files, that is, only know that the picture has been changed from 100KB to 120KB. What has been changed, the version control system does not know and has no way of knowing.

Unfortunately, Microsoft's Word format is a binary format, so the version control system cannot track changes in Word files. The previous example is just for demonstration. If you want to use a version control system, you must write it in plain text. document.

Because the text is encoded, for example, Chinese has the commonly used GBK encoding, and Japanese has the Shift_JIS encoding. If there are no historical problems, it is strongly recommended to use the standard UTF-8 encoding. All languages ​​use the same encoding. supported by the platform.

Special attention should be paid to children's shoes using Windows:

Never edit any text files with Notepad that comes with Windows. The reason is that the team that developed Notepad at Microsoft used a very mentally retarded behavior to save UTF-8 encoded files, they cleverly added the character 0xefbbbf (hex) at the beginning of each file, and you'd come across a lot of weirdness For example, the first line of the web page may display a "?", a program that is obviously correct will report a syntax error as soon as it is compiled, and so on, all of which are caused by the mentally retarded behavior of Notepad. It is recommended that you download Notepad++ instead of Notepad, which is not only powerful, but also free! Remember to set the default encoding of Notepad++ to UTF-8 without BOM.

 

Closer to home, now we write a readme.txtfile with the following content:

Git is a version control system.
Git is free software.

It must be placed learngitin the directory (subdirectories are also fine), because this is a Git repository, and Git cannot find this file anywhere else.

Putting a file into a Git repository only takes two steps compared to three steps to putting an elephant in the fridge.

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

$ git add readme.txt

Execute the above command, there is no display, this is right, the philosophy of Unix is ​​"no news is good news", indicating that the addition is successful.

The second step is to tell Git with the command git committo submit the file to the repository:

$ 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

Closer to home, now we write a readme.txtfile with the following content:

Git is a version control system.
Git is free software.

It must be placed learngitin the directory (subdirectories are also fine), because this is a Git repository, and Git cannot find this file anywhere else.

Putting a file into a Git repository only takes two steps compared to three steps to putting an elephant in the fridge.

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

$ git add readme.txt

Execute the above command, there is no display, this is right, the philosophy of Unix is ​​"no news is good news", indicating that the addition is successful.

The second step is to tell Git with the command git committo submit the file to the repository:

$ 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

Closer to home, now we write a readme.txtfile with the following content:

Git is a version control system.
Git is free software.

It must be placed learngitin the directory (subdirectories are also fine), because this is a Git repository, and Git cannot find this file anywhere else.

Putting a file into a Git repository only takes two steps compared to three steps to putting an elephant in the fridge.

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

$ git add readme.txt

Execute the above command, there is no display, this is right, the philosophy of Unix is ​​"no news is good news", indicating that the addition is successful.

The second step is to tell Git with the command git committo submit the file to the repository:

$ 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

Briefly explain the git commitcommand, -mfollowed by the description of this submission, you can enter any content, of course, it is best to make sense, so that you can easily find the change records from the history.

Too troublesome and don't want to enter -m "xxx"it, okay? There is a way to do this, but it is strongly discouraged because typing the description is important for yourself and others to read. If you really don't want to enter a description, please Google it yourself, I won't tell you this parameter.

git commitAfter the command is executed successfully, it will tell you that 1 file has been changed (our newly added readme.txt file), and two lines of content have been inserted (readme.txt has two lines of content).

Why does Git need to add files add, a committotal of two steps? Since commitmany files can be committed at once, you can have multiple adddifferent files, such as:

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

summary

Now let’s summarize the two points we learned today:

To initialize a Git repository, use the git initcommand.

Add files to the Git repository in two steps:

  • The first step, use the command git add <file>, note that it can be used repeatedly to add multiple files;

  • The second step, using the command git commit, is done.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326915952&siteId=291194637