git learning 2

1. Git creates a warehouse

  • git init

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

After executing the git init command, the Git warehouse will generate a .git directory, which contains all the metadata of the resource, and other project directories remain unchanged .

  • Instructions

Using the current directory as the Git repository, we just need to initialize it.

git init
  • After the 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 data and resources required by Git are stored in this directory.

    If there are several files in the current directory that you want to include in version control, you need to use the git add command to tell Git to start tracking these files, and then submit :

$ git add *.c
$ git add README
$ git commit -m '初始化项目版本'
  • The above command submits the README file ending with .c in the directory to the warehouse .

    Note: In the Linux system, the commit information uses single quotes ', and in the Windows system, the commit information uses double quotes ".

    So in git bash, git commit -m 'commit description' is ok, but in the Windows command line, use double quotes git commit -m 'commit description'.

  • git clone

We use git clone to copy projects from existing Git repositories (similar to svn checkout).

The command format for cloning a repository is:

git clone <repo>

如果我们需要克隆到指定的目录,可以使用以下命令格式:

git clone <repo> <directory>

Parameter Description:

repo:Git 仓库。
directory:本地目录。

For example, to clone the Git code repository Grit in 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 under the current directory, which contains a .git directory for saving all downloaded version records.
If you want to define the name of the project directory to be created yourself, you can specify a new name at the end of the above command:

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

Two, Git basic operation 1

Git's job is to create and save snapshots of your project and compare them to future snapshots.

This chapter describes the commands for creating and committing snapshots of your project.

Git commonly used the following six commands: git clone, git push, git add, git commit, git checkout, git pull

insert image description here
illustrate:

workspace:工作区
staging area:暂存区/缓存区
local repository:版本库或本地仓库
remote repository:远程仓库

A simple operation steps:

$ git init    
$ git add .    
$ git commit  
 git init - 初始化仓库。
git add . - 添加文件到暂存区。
git commit - 将暂存区内容添加到仓库中。 
  • Create warehouse command

The following table lists the commands for git to create a warehouse:

命令 	说明
git init 	初始化仓库
git clone 	拷贝一份远程仓库,也就是下载一个项目。
  • git init command

The git init command is used to create a new Git repository in a directory.
Execute git init in the directory to create a Git repository.
For example, we create a project named runoob in the current directory:

$ mkdir runoob
$ cd runoob/
$ git init
Initialized empty Git repository in /Users/tianqixin/www/runoob/.git/
# 初始化空 Git 仓库完毕。

Now you can see that the .git subdirectory is generated in your project. This is your Git repository, and all the snapshot data about your project are stored here.

.git is hidden by default and can be viewed with the ls -a command:

  • git clone command

git clone copies a Git repository locally so that you can view the project or modify it.
The format of the copy project command is as follows:

 git clone [url]

[url] is the item you want to copy.

For example, we copy the project on Github:

$ git clone https://github.com/tianqixin/runoob-git-test
Cloning into 'runoob-git-test'...
remote: Enumerating objects: 12, done.
remote: Total 12 (delta 0), reused 0 (delta 0), pack-reused 12
Unpacking objects: 100% (12/12), done.

insert image description hereinsert image description here

  • Submit and modify

Git's job is to create and save snapshots of your project and compare them to future snapshots.
The following table lists the commands for creating and committing snapshots of your project:

命令 	说明
git add 	添加文件到暂存区
git status 	查看仓库当前的状态,显示有变更的文件。
git diff 	比较文件的不同,即暂存区和工作区的差异。
git commit 	提交暂存区到本地仓库。
git reset 	回退版本。
git rm 	将文件从暂存区和工作区中删除。
git mv 	移动或重命名工作区文件。

git add command

The git add command adds the file to the staging area.

Add one or more files to the staging area:

git add [file1] [file2] ...

Add the specified directory to the staging area, including subdirectories:

git add [dir]

Add all files in the current directory to the temporary storage area:

git add .

In the following example we add two files:

$ touch README                # 创建文件
$ touch hello.php             # 创建文件
$ ls
README        hello.php
$ git status -s
?? README
?? hello.php
$ 

insert image description hereThe git status command is used to view the current status of the project.

Next we execute the git add command to add files:

$ git add README hello.php 

Now we execute git status again, and we can see that these two files have been added.

$ git status -s
A  README
A  hello.php
$ 

insert image description here

  • In a new project, it is common to add all files, we can use the git add . command to add all files of the current project.

    Now we modify the README file:
    insert image description here
    learn from "https://www.runoob.com/git/git-add.html"

Summarize

Learning is a process of continuous summary and practice.

Guess you like

Origin blog.csdn.net/weixin_51884452/article/details/130564646