Creating a Git Repository
In this section, we will guide you through creating 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 require running within a Git repository, so git init is the first command you use with Git.
After executing the git init command, a .git directory is created, which contains all the metadata for the repository, while the rest of the project directory remains unchanged.
Usage
To use the current directory as a Git repository, simply initialize it:
git init
This command will create a .git directory in the current directory.
To use a specified directory as a Git repository:
git init newrepo
After initialization, a .git directory will appear in the newrepo directory, where all Git data and resources are stored.
If there are 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 'initial project version'
The above commands will submit the .c files and the README file to the repository.
>
Note: On Linux systems, commit messages use single quotes '
, while on Windows systems, commit messages use double quotes "
.
So in git bash, git commit -m 'commit message'
works, but in the Windows command line, you should use double quotes git commit -m "commit message"
.
git clone
We use git clone to copy a project from an existing Git repository (similar to svn checkout).
The command format for cloning a repository is:
git clone <repo>
If you need to clone into a specified directory, you can use the following command format:
git clone <repo> <directory>
Parameter Descriptions:
- repo: Git repository.
- directory: Local directory.
For example, to clone the Ruby language's Git code repository Grit, 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, containing a .git directory to store all the downloaded version history.
$ git clone git://github.com/schacon/grit.git mygrit
Configuration
Git settings are managed using the git config
command.
$ git config --list
credential.helper=osxkeychain
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
$ git config -e # For the current repository
Or:
$ git config -e --global # For all repositories on the system
To set user information when committing code:
$ git config --global user.name "tutorialpro"
$ git config --global user.email [email protected]
Removing the --global parameter makes the settings only apply to the current repository.