Git Repository
Git Repository (git存储库)
What is a Git Repository?
What is a Git Repository? (什么是Git仓库?)
A Git Repository is a storage of your project files, which makes it possible to save code versions and have access to them. (Git存储库是项目文件的存储,可以保存代码版本并访问它们。)
Git Init
First of all, you need git init command to create a new repository. This command is only used once, while initialising a new repository.
git init
Consequently, a new subdirectory and a new master branch will be created:
git init <directory>
This creates an empty Git Repository in the specified directory. Running this command will create a new folder which contains the .git subdirectory.
Git clone for Cloning an Existing Repository
Git clone for Cloning an Existing Repository (用于克隆现有存储库的Git克隆)
The git clone command is used for creating a local clone of an already existing repository. It is a one-time operation, too.
git clone <repo url>
Git add and Git Commit for Saving Changes to the Repository
Git add and Git Commit for Saving Changes to the Repository (Git add和Git Commit用于保存对存储库的更改)
Using git add and git commit commands, you can save the file version changes to your repository. Here is an example of the usage of these two commands:
change directories to /path/of/project (-将目录更改为/path/of/project)
create a new file GitCommit.txt with contents “commit example for git repo” (-创建一个新文件GitCommit.txt ,内容为“commit example for git repo”)
git add GitCommit.txt to the repository staging area (- git将GitCommit.txt添加到存储库暂存区)
create a new commit message describing the work done (-创建描述已完成工作的新提交消息)
cd /path/of/project
echo "commit example for git repo" >> GitCommit.txt
git add GitCommit.txt
git commit -m "added GitCommit.txt to the repo"
Git Push for Repo-to-Repo Interaction
Git Push for Repo-to-Repo Interaction (Git推送Repo-to-Repo交互)
If you executed git clone command, it means that you already have a remote repository, so you can run git push command to push your changes to that repository. But if you used git init, you have no remote repository. In this case, you can use a hosted Git service, like Github or Bitbucket, and create your repo there, which will give a URL that you can add to your local repository and git push to the hosted repository.
Git Config for Configuration and Set up
Git Config for Configuration and Set up (用于配置和设置的Git配置)
A remote repository is added to the local git config with the help of git remote command:
git remote add <remote_name> <remote_repo_url>
After adding the remote repo, you can push local branches to it:
git push -u <remote_name> <local_branch_name>
When you use the -u option, Git not only pushes your changes to the remote repository but also sets the remote branch as the default for future git pull and git push commands without specifying the remote branch explicitly.
You may also need to set global Git configuration options such as username, or email. With the help of the git config command, you can configure your Git installation from the command line. This command defines everything (user info, preferences, the behavior of a repository). Below you will find some configuration options.
Use the –global flag to set configuration options for the current user. Define the name of the author of all the commits in the current repository. (使用–global标志为当前用户设置配置选项。定义当前存储库中所有提交的作者的名称。)
git config --global user.name <name>
Adding the –local option or not using a configuration option at all, will define the user.name for the current local repository. Define the e-mail of the author of all the commits by the current user. (添加–local选项或根本不使用配置选项,将为当前本地存储库定义user.name。定义当前用户所有提交的作者的电子邮件。)
git config --local user.email <email>
Use the –system option to set the configuration for the whole system, that is to say all users and repos on a machine. (使用–system选项设置整个系统的配置,即机器上的所有用户和存储库。)
git config --system core.editor <editor>