Git tutorial | (3) Create Repository

Original Address

table of Contents

1. Create a repository

2. The file is added to the repository

3. Summary


1. Create a repository

What is the repository of it? Warehouse repository, also known as the English name Repository , you can simply understood as a directory, all files in this directory can be inside Git to manage, modify each file, delete, Git can be tracked so that any moment can be traced history, or you can "restore" at some point in the future.

So, create a repository is very simple, first of all, choose a suitable place, create an empty directory:

$ mkdir Learngit
$ cd Learngit
$ pwd
/Users/apple/Learngit

pwdCommand is used to display the current directory. On my Mac, the warehouse is located /Users/apple/Learngit.

The second step, through the git initcommand to manage this directory can become a Git repository:

$ git init
Initialized empty Git repository in /Users/apple/Learngit/.git/

Git instantly put the warehouse built, and to tell you is an empty warehouse (empty Git repository), careful readers can find more than a directory under the current .gitdirectory, the directory is to track and manage Git repository, nothing one thousand million will not manually modify the file or folder, or change the mess, put the Git repository to destroy.

If you do not see the .gitdirectory, it is because this directory is hidden by default, use ls -ahthe command you can see.

$ ls -ah
.	..	.git

Does not necessarily have to create an empty Git repository in the directory, choose a directory already have something also possible. However, we do not recommend you use your own company is developing a project to learn Git, otherwise all the consequences will not be responsible.

 

2. The file is added to the repository

First look again clear here, all the version control system, in fact, can only track changes in text files, such as TXT files, web pages, and so all the code, Git is no exception. Version control system can tell you every time changes, such as on line 5 plus the word "Linux", in line 8 to delete the word "Windows". And pictures, videos binaries, although it can be managed by the version control system, but can not track file changes, only the binary string together each change, which is the only known picture from 100KB changed to 120KB, but in the end what changed, version control system does not know, I could not tell.

Unfortunately, Microsoft Word format is a binary format, and therefore, the version control system is unable to track the changes to Word files, in front of our example just to demonstrate, if you want to really use version control system, it is necessary to write in plain text file.

Since the text is coded, such as Chinese have frequently used GBK coding, Japanese have Shift_JIS encoding, if there is no problem left over from history, it is strongly recommended to use standard UTF-8 encoding, all languages ​​use the same encoding, neither the conflict, they were all platform support.

Use Windows paying particular attention to children's shoes:

Do not use the built-in Windows Notepad to edit any text file. The reason is that Microsoft Notepad development team used a very mentally behavior to save UTF-8 encoded files, they try to be smart in the beginning of each file added 0xefbbbf (hexadecimal) characters, you will meet many incredible the problem, for example, the first line of the page may show a "?", obviously the correct procedures to compile a report syntax errors, etc., are caused by the mentally retarded behavior of Notepad. I suggest you download Notepad ++ instead of Notepad, not only powerful, it's free! Remember to Notepad ++ default encoding to UTF-8 without BOM to:

Closer to home, now we have prepared a readme.txtdocument, which reads as follows:

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

Be sure to put learngitthe directory (subdirectories too), because this is a Git repository, Git worse then put the rest can not find the file.

And the elephant into the refrigerator takes three steps compared to a file in the Git repository requires only two steps.

The first step, use the command git addtells Git, to add files to the repository:

$ git add readme.txt 

Implementation of the above command, no display, that's right, Unix philosophy is "no news is good news", indicating successfully added.

The second step, use the command git committells Git, the submission of documents to the repository:

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

Briefly explain git commitcommand, -mentered followed by explanation of this submission, you can enter any of the content, of course, the best is meaningful, so you can from the history list easy to find change records.

I could not be bothered enter -m "xxx"okay? Indeed there are ways to do this, but it is strongly recommended that you do not do it, because the input is very important to explain myself to read to others. Shoes really do not want to enter your own description of Google, I do not tell you this parameter.

git commitAfter the successful implementation of the command will tell you 1 file changed: a file is changed (we readme.txt file newly added); : 2 insertionsinsert the two lines (readme.txt have two lines).

Why add files Git needs add, commita total of two steps it? Because commitit can submit many files at once, so you can multiple adddifferent files, such as:

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

 

3. Summary

Initialize a Git repository, use the git initcommand.

Add files to the Git repository, in two steps:

1) using the command git add <file>, pay attention, it can be used repeatedly to add multiple files;

2) Use the command git commit -m <message>to complete.

 

Published 353 original articles · won praise 700 · views 130 000 +

Guess you like

Origin blog.csdn.net/sdu_hao/article/details/104068651