Basic Git Operations
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.
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 in the directory
init, you can create a Git repository. For example, we create the runoob project:
$ mkdir runoob $ cd runoob/ $ git init Initialized empty Git repository in /Users/tianqixin/www/runoob/.git/ # 在 /www/runoob/.git/ 目录初始化空 Git 仓库完毕<BR>
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 and take a look at the code, you can clone that project. Execute the command:
git clone [url]
[url] for the project you want to copy, 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. Usually the last / of the URL
The project name after. If you want a different name, you can add the name you want after the command.
Basic Snapshots
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 you 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 class="brush:php;toolbar:false"> <p>在 README 添加以下内容:<b># Runoob 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 making the changes, 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 changes in the snapshot 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 get 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.
1. Changes that have not been cached: git diff
2. View cached changes: git diff --cached
3. View all cached and uncached changes: git diff HEAD
4. Display Summary instead of the entire diff: git diff --stat
Enter the following in the hello.php file:
<?php echo '菜鸟教程: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 @@ +<?php +echo '菜鸟教程:www.runoob.com'; +?>
git status shows changes since your last commit update Or write cached changes, 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 @@ +# Runoob 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 '菜鸟教程:www.runoob.com'; +?>
git commit
Use the git add command to write the content of the snapshot you want into the cache, and executing git commit will The cache contents are added to the warehouse.
Git records your name and email address for every submission you make, so the first step is to configure your 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 commit all changes to hello.php. In the 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)
以上输出说明我们在最近一次提交之后,没有做任何改动,是一个"working directory clean:干净的工作目录"。
如果你没有设置 -m
选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim。屏幕会像这样:
# 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
如果你觉得 git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式如下:
git commit -a
我们先修改 hello.php 文件为以下内容:
<?php echo '菜鸟教程:www.runoob.com'; echo '菜鸟教程:www.runoob.com'; ?>
再执行以下命令:
git commit -am '修改 hello.php 文件' [master 71ee2cb] 修改 hello.php 文件 1 file changed, 1 insertion(+)
git reset HEAD
git reset HEAD 命令用于取消已缓存的内容。
我们先改动文件 README 文件,内容如下:
# Runoob Git 测试
# 菜鸟教程
hello.php 文件修改为:
<?php echo '菜鸟教程:www.runoob.com'; echo '菜鸟教程:www.runoob.com'; echo '菜鸟教程:www.runoob.com'; ?>
现在两个文件修改后,都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下
$ 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
现在你执行 git commit,只会将 README 文件的改动提交,而 hello.php 是没有的。
$ git commit -m '修改' [master f50cfda] 修改 1 file changed, 1 insertion(+) $ git status -s M hello.php
可以看到 hello.php 文件的修改并为提交。
这时我们可以使用以下命令将 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
简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。
git rm
git rm 会将条目从缓存区中移除。这与 git reset HEAD 将条目取消缓存是有区别的。
"取消缓存"的意思就是将缓存区恢复为我们做出修改之前的样子。
默认情况下,git rm file 会将文件从缓存区和你的硬盘中(工作目录)删除。
如果你要在工作目录中留着该文件,可以使用 git rm --cached:
如我们删除 hello.php文件:
$ git rm hello.php rm 'hello.php' $ ls README
不从工作区中删除文件:
$ git rm --cached README rm 'README' $ ls README
git mv
git mv 命令做得所有事情就是 git rm --cached 命令的操作, 重命名磁盘上的文件,然后再执行 git add
把新文件添加到缓存区。
我们先把刚移除的 README 添加回来:
$ git add README
然后对其重名:
$ git mv README README.md $ ls README.md
以上就是Git 教程之基本操作详解的内容,更多相关文章请关注PHP中文网(www.php.cn)!