Git Create Repository

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

You can use an existing directory as a Git repository.


git init

Git Usagegit initThe command to initialize a Git repository. Many Git commands need to be run in a Git repository, sogit initThis is the first command for using Git.

On completion of executiongit initAfter the command, the Git repository will generate a .git directory, which contains all the metadata of the resources, while the other project directories remain unchanged.

UsageMethods

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

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 is executed, a .git directory will be generated in the current directory.

Use the directory we specify as the Git repository.

git init newrepo

After initialization, a directory named .git will appear under the newrepo directory, and all the data and resources required by Git are stored in this directory.

If there are 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 will commit all files ending with .c and the README file in the directory to the repository.

Note:On Linux systems, use single quotes for commit messages.', Windows system, commit message uses double quotation marks"。

So in git bashgit commit -m 'commit message'This is fine, but in the Windows command line you should use double quotes.git commit -m "commit message"。


git clone

We usegit cloneCopy a project from an existing Git repository (similar tosvn checkout)。

The command format for cloning a repository is:

git clone <repo>

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

git clone <repo> <directory>

Parameter description:

  • repo:Git 仓Library.
  • directory:Local directory.

For example, to clone the Git code repository Grit for 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 saving all the version records downloaded.

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

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

Configuration

Git settings usagegit configcommand.

Display 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 git config file:
$ git config -e    # 针对当前仓库 

Or:

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

Set the user information when submitting code:

$ git config --global user.name "example"
$ git config --global user.email test@example.com

ifgo掉--globalThe parameter is only valid for the current repository.


other extensions