Original link : https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d599b3aed534aaab22a0db6c4e07fd0000
What is a repository? Repository, 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. Git can track the modification and deletion of each file, so that it can be tracked at any time. History, or it can be "restored" at some point in the future.
Therefore, creating a repository is very simple. First, choose a suitable place and create an empty directory:
$mkdir learngit
$cd learngit
$pwd
/Users/whe/learngit
pwd
The command is used to display the current directory. On my Mac, this repository is located /Users/michael/learngit
.
If you use a Windows system, in order 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 git init
to turn this directory into a warehouse that can be managed by Git through the command:
$ git init
Initialized empty Git repository in /Users/whe/learngit/.git/
Git instantly builds the warehouse, and tells you that it is an empty Git repository. Careful readers can find that there is one more directory under the current .git
directory. This directory is used by Git to track and manage the repository. It's okay. Do not manually modify the files in this directory, otherwise the changes will mess up and the Git repository will be destroyed.
If you don't see the .git
directory, it is because the directory is hidden by default and ls -ah
you can see it with the command.
It is not necessary to create a Git repository in an empty directory. It is also possible to choose a directory that already has things. 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 repository
First of all, let me clarify that all version control systems can only track changes to text files, such as TXT files, web pages, all program codes, 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 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 is changed from 100KB to 120KB, but in the end What has been changed, the version control system does not know, nor can it know.
Unfortunately, Microsoft’s Word format is a binary format. Therefore, the version control system cannot track changes to Word files. The previous example is just for demonstration. If you want to use the version control system, you must write it in plain text. file.
Because the text is encoded, for example, Chinese has commonly used GBK encoding, and Japanese has Shift_JIS encoding. If there are no historical problems, it is strongly recommended to use standard UTF-8 encoding. All languages use the same encoding. There is no conflict and it is all Supported by the platform.
Now we write a readme.txt
file with the following content:
$ vi readme.txt
Git is a version control system.
Git is free software.
Be sure to put it in the learngit
directory (subdirectories are fine too), because this is a Git repository, and no matter how powerful Git is in other places, you can’t find this file.
Compared to the three steps required to put the elephant in the refrigerator, it only takes two steps to put a file in the Git repository.
The first step is to git add
tell Git with a command to add the file to the warehouse:
$ git add readme.txt
Execute the above command, there is no display, that's right, the philosophy of Unix is "No news is good news", indicating that the addition is successful.
The second step is to git commit
tell Git with a command to submit the file to the warehouse:
$ git commit -m "wrote a readme file"
[master (root-commit) 2e89d97] wrote a readme file
1 file changed, 10 insertions(+)
create mode 100644 readme.txt
Briefly explain the git commit
command. The -m
following input is the description of this submission. You can enter any content. Of course, it is better to be meaningful, so that you can easily find the change record from the history record.
Is it -m "xxx"
okay not to enter in trouble ? There is indeed a way to do this, but it is strongly not recommended that you do this, because the input description is very important for yourself and others to read. If you really don’t want to enter the description, please Google by yourself. I won’t tell you this parameter.
git commit
After the command is executed successfully, it will tell you 1 file changed
:: 1 file has been changed (the readme.txt file we added newly) 2 insertions
;: Two lines of content have been inserted (readme.txt has two lines of content).
Why add files Git needs add
, commit
a total of two steps it? Because commit
you can submit many files at once, you can have add
different files multiple times , such as:
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
summary
Now summarize the two points learned today:
To initialize a Git repository, use the git init
command.
Add files to the Git repository in two steps:
- Use the command
git add <file>
, note that it can be used repeatedly to add multiple files; - Use the command
git commit -m <message>
to finish.