May 25, 2021 Git
Git's job is to create and save snapshots of your project and compare them to subsequent snapshots. T his chapter describes the commands for creating and submitting snapshots of your project.
Create a new Git repository in the catalog with git init. Y ou can do this at any time, in any directory, completely localized.
By executing git init in the directory, you can create a Git repository. F or example, we create a w3cschoolcc project:
$ mkdir w3cschoolcc $ cd w3cschoolcc $ git init Initialized empty Git repository in /www/w3cschoolcc/.git/ # 在 /www/w3cschoolcc/.git/ 目录初始化空 Git 仓库完毕。
Now you can see that there is a subdirecte of .git in your project directory. This is your Git repository, where all the snapshot data about your project is stored.
ls -a . .. .git
Use git clone to copy a Git repository locally so that you can view the item or make changes.
If you need to work on a project with someone else, or if you want to copy a project and look at the code, you can clone that project. T o execute a command:
git clone [url]
For the project you want to copy, that's it.
For example, we clone a project on Github:
$ git clone git://github.com/schacon/simplegit.git Initialized empty Git repository in /private/tmp/simplegit/.git/ remote: Counting objects: 100, done. remote: Compressing objects: 100% (86/86), done. remote: Total 100 (delta 35), reused 0 (delta 0) Receiving objects: 100% (100/100), 9.51 KiB, done. Resolving deltas: 100% (35/35), done. $ cd simplegit/ $ ls README Rakefile lib
This will copy all records for the project.
$ ls -a . .. .git README Rakefile lib $ cd .git $ ls HEAD description info packed-refs branches hooks logs refs config index objects
By default, Git creates your local project directory based on the name of the item indicated by the URL you provided. T his is usually the name of the project after the last / / url of the URL. I f you want a different name, you can add the name you want after the command.
Git's job is to create and save snapshots of your project and compare them to subsequent snapshots. This chapter describes the commands for creating and submitting snapshots of your project.
The git add command adds the file to the cache, as we add the following two files:
$ touch README $ touch hello.php $ ls README hello.php $ git status -s ?? README ?? hello.php $
The git status command is used to view the current state of the project.
Next we execute the git add command to add the file:
$ git add README hello.php
Now that we're doing git status, we can see that the two files have been added.
$ git status -s A README A hello.php $
In the new project, it is common to add all files to execute commands in the current working directory: git add ..
Now let's change the file and do the git status:
$ vim README $ git status -s AM README A hello.php
The "AM" state means that the file has changed since we added it to the cache. After the change, we execute the git add command and add it to the cache:
$ git add . $ git status -s A README A hello.php
When you want to include your changes in the snapshot you are about to submit, you need to perform git add.
git status to see if there have been changes since your last commit.
When I demonstrated the command, I added the -s parameter to get a short result output. If this parameter is not added, the output is detailed:
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README new file: hello.php
Execute git diff to see the details of the results of executing git status.
The git diff command shows the difference between changes that have been written to the cache and changes that have been modified but have not yet been written to the cache. git diff has two main scenarios.
Enter the .php in the hello file:
<?php echo 'www.w3cschool.cn'; ?>
$ git status -s A README AM hello.php $ git diff diff --git a/hello.php b/hello.php index e69de29..d1a9166 100644 --- a/hello.php +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo 'www.w3cschool.cn'; +?>
git status shows changes that you changed or written to the cache after you last committed an update, and git diff shows, line by line, exactly what those changes are.
Let's take a look at how git diff --cached works:
$ git add hello.php $ git status -s A README A hello.php $ git diff --cached diff --git a/README b/README new file mode 100644 index 0000000..704cce7 --- /dev/null +++ b/README @@ -0,0 +1 @@ +w3cschool.cn diff --git a/hello.php b/hello.php new file mode 100644 index 0000000..d1a9166 --- /dev/null +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo 'www.w3cschool.cn'; +?>
Use the git add command to write the contents of the desired snapshot to the cache, and execute the snapshot of the git commit record cache.
Git records your name and email address for each of your submissions, so the first step is to configure your userna name and email address.
$ git config --global user.name 'w3cschool' $ git config --global user.email [email protected]
Next we write to the cache and commit all the changes .php the hello. I n the first example, we use the -m option to provide a commit comment on the command line.
$ git add hello.php $ git status -s A README A hello.php $ git commit -m 'test comment from w3cschool.cn' [master (root-commit) 85fc7e7] test comment from w3cschool.cn 2 files changed, 4 insertions(+) create mode 100644 README create mode 100644 hello.php
Now we've taken a snapshot. If we do git status again:
$ git status # On branch master nothing to commit (working directory clean)
The above output indicates that we have made no changes since our last commit and are a "clean working directory".
If you don't set the -m option, Git will try to open an editor for you to fill in the submission information. I f Git couldn't find information in your configuration of it, vim opens by default. T he screen will look something like this:
# Please enter the commit message for your changes. L ines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Changes to be committed: # (use "git reset HEAD ..." t o unstage) # # modified: hello.php # ~ ~ ".git/COMMIT_EDITMSG" 9L, 257CGit also allows you to skip this step with the -a option if you find the process of submitting a cache for git add too cumbersome. The command format is as follows:
git commit -a
Such as:
$ git commit -am 'changes to hello file' [master 78b2670] changes to hello file 1 files changed, 2 insertions(+), 1 deletions(-)
The git reset HEAD command is used to un cache cached content.
Here we have two documents that have been recently submitted and changed. W e'll cache both and un cache one of them.
$ git status -s M README M hello.php $ git add . $ git status -s M README M hello.pp $ git reset HEAD -- hello.php Unstaged changes after reset: M hello.php $ git status -s M README M hello.php
Now that you perform git commit, you will only record changes to the README file and will not include hello.rb, which is not currently in the cache.
git rm removes the file from the cache.
If we delete a hello .php file:
$ git rm hello.php rm 'hello.php' $ ls README
By default, git rm file removes files from the cache and from your hard drive (working directory). If you want to keep the file in the working directory, you can use the command:
git rm --cached。
All the git mv command does is git rm--cached, rename the files on the disk, and then execute the git add to add the new files to the cache. So while there is a git mv command, it's a bit redundant.