The basic operation of Git tutorial-related skills

Source: Internet
Author: User
Tags commit diff git clone

Git Basic Operations

Git's job is to create and save a snapshot of your project and compare it to a subsequent snapshot. This chapter describes the commands for creating and submitting your project snapshots.

Get and create a project command

Git init

Create a new Git repository in the directory with Git init. You can do so at any time, in any directory, completely localized.
By executing git init in the directory, you can create a git repository. For example, we create Runoob projects:

$ mkdir runoob
$ cd runoob/
$ git init
initialized empty git repository in/users/tianqixin/www/runoob/.git/< c7/># in/www/runoob/.git/directory initialization empty git warehouse complete

Now you can see that you have generated a. Git subdirectory in your project. This is your Git warehouse, and all the snapshot data about your project is stored here.

Ls-a
. .. . git

git clone

Use git clone to copy a git warehouse to the local, so that you can view the project, or make changes.

If you need to work with someone for a project, or if you want to copy a project and look at the code, you can clone that project. To execute a command:

git clone [url]

[url] For the item you want to copy, it's OK.

For example, we cloned the items on Github:

$ git clone git@github.com:schacon/simplegit.git
cloning into ' simplegit ' ...
Remote:counting objects:13, done.
Remote:total (Delta 0), reused 0 (Delta 0), pack-reused
receiving objects:100% (13/13), done.
Resolving deltas:100% (2/2), done.
Checking connectivity ... done.

After the clone completes, a simplegit directory is generated under the current directory:

$ cd simplegit/$ ls README rakefile Lib

The above operation copies all records of 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 according to the name of the item that is indicated by the URL you provide. This is usually the name of the project that is the last/after the URL. If you want a different name, you can add the name you want after the command.

Basic snapshots

Git's job is to create and save a snapshot of your project and compare it to a subsequent snapshot. This chapter describes the commands for creating and submitting snapshots of your project.

git add

The git add command adds the file to the cache, as we add the following two files:

$ touch the Readme
$ touch hello.php
$ ls
README hello.php
$ git status-s
?? README
?? hello.php
$ 

The git status command is used to view the current status of the project.

Next, we execute the git add command for adding files:

$ git add README hello.php

Now that we're done with git status, we can see that these two files have been added.

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

In the new project, adding all the files is common and we can use git Add. command to add all the files for the current project.
Now we modify the README file:

$ vim Readme
<pre>
<p> Add the following content to the README: <b># runoob Git Test </b>, and then save exit. </p>
<p> then execute git status:</p>
$ git status-s
AM README
A hello.php

The "AM" state means that the file was changed after we added it to the cache. We added the git add command to the cache after the change:

$ git Add.
$ git status-s
a README
a hello.php

When you want to include your changes in the upcoming snapshots, you need to execute git Add.

git status

git status to see if any changes have been made since your last submission.

When I demonstrated the command, I added the-s parameter to get a short result output. If this parameter is not added, the contents are printed in detail:

$ git status on
branch master

Initial commit

Changes to is committed:
 (use "git rm--cached <file> . "To Unstage"

 new file:  README
 new file:  hello.php

Git diff

Execute git diff to see the details of the results of executing git status.

The git diff command shows the difference between the write cache and changes that have not been written to the cache. Git diff has two major application scenarios.

    1. Changes that have not been cached: Git diff
    2. To view cached changes: Git diff--cached
    3. View all cached and not cached changes: Git diff head
    4. Display a summary instead of the entire Diff:git diff--stat

Enter the following in the hello.php file:

<?php
Echo ' rookie tutorial: Www.runoob.com ';
? >
$ git status-s
A README
AM hello.php
$ git diff
diff--git a/hello.php b/hello.php
index e69de29. 69b5711 100644
---a/hello.php
+++ b/hello.php @@ -0,0
+1,3 @@ -0,0 +echo
' Rookie tutorial: Www.runoob.com ';
+?>

Git status Displays the changes you have made since you last submitted the update, or the changes you write to the cache, and git shows exactly what the changes are in line.

Let's look at the execution effect of git diff--cached:

$ git add hello.php 
$ git status-s
a README
a hello.php
$ git diff--cached
diff--git A/readme B/rea DME
New file Mode 100644
index 0000000..8f87495
---/dev/null
+++
b/readme @@ -0,0 +1 @@ -0,0
Runoob Git Test
diff--git a/hello.php b/hello.php
new file Mode 100644
index 0000000..69b5711
---/ Dev/null
+++
b/hello.php @@ -0,0 +1,3 @@ -0,0
' rookie tutorial: +echo ';
+?>

Git commit

Use the git add command to write the contents of the desired snapshot to the cache, and Git commit to add the cache contents to the warehouse.

Git records your name and email address for each of your submissions, so the first step is to configure the username and email address.

$ git config--global user.name ' Runoob '
$ git config--global user.email test@runoob.com

Next we write to the cache and submit all changes to the hello.php. In the first example, we use the-M option to provide a submit comment on the command line.

$ git add hello.php
$ git status-s
a README
a hello.php
$ $ git commit-m ' first version commit '
[Master (root-com MIT) d32cf1f] first version submitted
 2 files changed, 4 insertions (+)
 Create mode 100644 README
 create mode 100644 hello.php

Now we have recorded the snapshot. If we execute git status again:

$ git status
# on Branch Master
Nothing to commit (working directory clean)

The above output indicates that we have not made any changes after the most recent submission, and that this is a "working directory clean: Clear working directory".
If you do not set the-M option, Git will try to open an editor for you to fill in the submission information. If Git doesn't find the information in your configuration, it will open vim by default. The screen will look like this:

# Please enter the ' commit ' for your changes. Lines starting
# with ' # ' would be ignored, and a empty message aborts the commit.
# on Branch Master
# Changes to is committed:
#  (use "Git the Reset head <file> ..." to unstage)
#
   # modified:  hello.php
#
~
~
"git/commit_editmsg" 9L, 257C

If you think the git add commit cache process is too cumbersome, Git allows you to skip this step with the-a option. The command format is as follows:

Git commit-a

We first modify the hello.php file to read as follows:

<?php
Echo ' rookie tutorial: Www.runoob.com ';
Echo ' Rookie tutorial: Www.runoob.com ';
? >

Then execute the following command:

git commit-am ' modify hello.php file '
[master 71EE2CB] Modify hello.php files
 1 file changed, 1 insertion (+)

git Reset head

The git reset Head command is used to cancel cached content.

We first change the document Readme file, the contents are as follows:

# Runoob Git Test
# Rookie Tutorial

The hello.php file is modified to:

<?php
Echo ' rookie tutorial: Www.runoob.com ';
Echo ' Rookie tutorial: Www.runoob.com ';
Echo ' Rookie tutorial: Www.runoob.com ';
? >

Now that two files have been modified and submitted to the buffer, we now want to cancel one of the caches, as follows:

$ 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 Hell o.php
$ git status-s
m README
 m hello.php

Now that you execute git commit, only the changes to the README file are submitted, and hello.php is not.

$ git commit-m ' modify '
[master F50CFDA] Modify
 1 file changed, 1 insertion (+)
$ git status-s
 m hello.php

You can see the modification of the hello.php file and submit it.

At this point we can submit the hello.php modifications using the following command:

$ git commit-am ' modify hello.php file '
[master 760f74d] Modify hello.php files
 1 file changed, 1 insertion (+)
$ git st ATUs
on Branch Master no
to commit, working directory clean

In short, execute git reset head to cancel the previous git add, but do not want to include the cache in the next commit snapshot.

git rm

git rm will remove entries from the cache. This differs from the git reset head to remove entries from the cache. "Canceling caching" means restoring the cache to the way it was before we made the change.

By default, git rm file deletes files from the cache and from your hard disk (working directory).

If you want to keep the file in your working directory, you can use git rm--cached:

If we delete the hello.php file:

$ git rm hello.php 
rm ' hello.php '
$ ls
README

Do not delete files from the workspace:

$ git rm--cached Readme 
RM ' Readme '
$ ls
Readme

Git MV

The git MV command does all the work of git rm--cached commands, renaming files on disk, and then executing git add to cache new files.

Let's add the newly removed README back:

$ git Add README

And then duplicate it with the name:

$ git mv README readme.md
$ ls
readme.md

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.