Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Create Repository

Git Create Repository

In this chapter we will introduce how to create a Git repository.

You can use an existing directory as a Git repository.


git init

Git uses the git init command to initialize a Git repository. Many Git commands need to be run in a Git repository, so git init is the first command to use Git.

After executing the git init command, the Git repository generates a .git directory, which contains all the metadata of the resources, while other project directories remain unchanged.

Usage

Enter the directory where you want to create the repository, or first create a new directory:



mkdir my-project

cd my-project

To use the current directory as a Git repository, we just need to initialize it.



git init

After this command executes, a .git directory will be generated in the current directory.

Use our specified directory as the Git repository.



git init newrepo

After initialization, a directory named .git will appear in the newrepo directory, where all the data and resources Git needs are stored.

If you have several files in the current directory that you want to put under version control, you need to first use the git add command to tell Git to start tracking these files, and then commit:



$ git add *.c

$ git add README

$ git commit -m '初始化项目版本'

The above command commits files ending in .c and the README file in the directory to the repository.

Note: In Linux systems, commit messages use single quotes ', while in Windows systems, commit messages use double quotes "。

So in git bash git commit -m 'commit message' this works; in the Windows command line you need to use double quotes git commit -m "commit message"。


git clone

We use git clone Copy a project from an existing Git repository (similar to svn checkout)。

The command format for cloning a repository is:

git clone 

If we need to clone to a specified directory, we can use the following command format:



git clone  

Parameter Description:

For example, to clone the Git code repository Grit of the Ruby language, you can use the following command:



$ git clone git://github.com/schacon/grit.git

After executing this command, a directory named grit will be created in the current directory, which contains a .git directory for storing all downloaded version records.

If you want to define the name of the new project directory yourself, you can specify a new name at the end of the above command:



$ git clone git://github.com/schacon/grit.git mygrit

Configure

git settings use the git config command.

Display the current git configuration information:

$ git config --list

credential.helper=osxkeychain

core.repositoryformatversion=0

core.filemode=true

core.bare=false

core.logallrefupdates=true

core.ignorecase=true

core.precomposeunicode=true

Edit the git configuration file:

$ git config -e    # 针对当前仓库 

or:

$ git config -e --global   # 针对系统上所有仓库

Set user information when committing code:

$ git config --global user.name "runoob"

$ git config --global user.email test@runoob.com

If you remove the --global parameter, it is only valid for the current repository.