Quick Start Git...login
Quick Start Git Tutorial
author:php.cn  update time:2022-04-11 13:44:34

Git basic operations


Git’s job is to create and save snapshots of your project and compare them to subsequent snapshots. This chapter introduces the commands for creating and submitting snapshots of your project.


Get and create project commands

git init

Use git init to create a new Git repository in the directory. You can do this at any time, in any directory, and it's completely localized.

Execute git init in the directory to create a Git warehouse. For example, if we create a php project:

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

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

ls -a
.	..	.git

git clone

Use git clone to copy a Git repository locally so that you can view the project or make modifications.

If you need to collaborate on a project with others, or want to make a copy of a project to see the code, you can clone that project. Execute the command:

 git clone [url]

[url] for the project you want to copy, and that’s it.

For example, if we clone the project on Github:

$ git clone git@github.com:schacon/simplegit.git
Cloning into 'simplegit'...
remote: Counting objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.
Checking connectivity... done.

After the cloning is completed, a simplegit directory will be generated in the current directory:

$ cd simplegit/ $ls README Rakefile lib

The above operation will copy 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 will create your local project directory with the name of the project indicated by the URL you provide. This is usually the project name after the last / in the URL. If you want a different name, you can add the name you want after the command.


Basic Snapshot

The job of Git is to create and save snapshots of your project and compare them with subsequent snapshots. This chapter introduces the commands for creating and submitting snapshots of your project.

git add

The git add command can add the file to the cache, for example, 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 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
$

In new projects, 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:

$ vim README
<pre>
<p>在 README 添加以下内容:<b># php Git 测试</b>,然后保存退出。</p>
<p>再执行一下 git status:</p>
$ git status -s
AM README
A  hello.php

The "AM" status means that this file has been changed after we added it to the cache. After the change, we execute the git add command to add it to the cache:

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

When you want to include your modifications in the snapshot that is about to be submitted, you need to execute git add.

git status

git status to see if there have been changes since your last commit.

I added the -s parameter when demonstrating this command to obtain a brief result output. If this parameter is not added, the detailed output will be:

$ 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

git diff

Execute git diff to view the detailed information of the result 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 not yet written to the cache. There are two main application scenarios for git diff.

  • Not yet cached changes: git diff

  • View cached changes: git diff -- cached

  • View all cached and uncached changes: git diff HEAD

  • Show summary Instead of the entire diff: git diff --stat

Enter the following in the hello.php file:

<?php
echo 'php中文网:www.php.cn';
?>
$ 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 @@
+<?php
+echo 'php中文网:www.php.cn';
+?>

git status shows you Submit updated changes or changes written to the cache, and git diff shows what these changes are line by line.

Next let’s check 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/README
new file mode 100644
index 0000000..8f87495
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+# php Git 测试
diff --git a/hello.php b/hello.php
new file mode 100644
index 0000000..69b5711
--- /dev/null
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo 'php中文网:www.php.cn';
+?>

git commit

Use the git add command to write the content of the desired snapshot into the cache area , Execute git commit to add the contents of the cache area to the warehouse.

Git records your name and email address for every commit you make, so the first step is to configure your username and email address.

$ git config --global user.name 'php'
$ git config --global user.email test@php.cn

Next we write to the cache and commit all changes to hello.php. In this first example, we use the -m option to provide commit comments on the command line.

$ git add hello.php
$ git status -s
A  README
A  hello.php
$ $ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
 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:

$ git status
# On branch master
nothing to commit (working directory clean)

, the above output shows that we have not made any changes since the latest submission, and it is a "working directory clean: clean working directory".

If you do not set the -m option, Git will try to open an editor for you to fill in the commit information. If Git can't find the relevant information in your configuration for it, it will open vim by default. The screen will look like this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

If you feel that the process of git add commit caching is too cumbersome, Git also 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 the following content:

<?php
echo 'php中文网:www.php.cn';
echo 'php中文网:www.php.cn';
?>

Then execute the following command:

git commit -am '修改 hello.php 文件'
[master 71ee2cb] 修改 hello.php 文件
 1 file changed, 1 insertion(+)

git reset HEAD

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

We first change the file README file, the content is as follows:

# php Git 测试
# php中文网

hello.php file is modified to:

<?php
echo 'php中文网:www.php.cn';
echo 'php中文网:www.php.cn';
echo 'php中文网:www.php.cn';
?>

Now after the two files are modified, they are submitted to the cache area. We now need to cancel the cache of one of them. The operation is 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	hello.php
$ git status -s
M  README
 M hello.php

Now if you execute git commit, only the changes to the README file will be submitted, but not hello.php.

$ git commit -m '修改'
[master f50cfda] 修改
 1 file changed, 1 insertion(+)
$ git status -s
 M hello.php

You can see the modifications to the hello.php file and submit it.

At this time we can use the following command to submit the modifications to hello.php:

$ git commit -am '修改 hello.php 文件'
[master 760f74d] 修改 hello.php 文件
 1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean

In short, execute git reset HEAD to cancel the previous git add addition, but do not want to include it in the next Commit the cache in the snapshot.

git rm

git rm will remove the entry from the cache. This is different from git reset HEAD which uncaches the entry. "Uncache" means to restore the cache area to the way it was before we made the modification.


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

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

For example, 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

All the git mv command does is the operation of the git rm --cached command, rename the disk file, and then execute git add to add the new file to the cache area.

Let’s first add back the README we just removed:

$ git add README

and then rename it:

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

php.cn