Git Basic Operations
Git's job is to create and save a snapshot of your project and after snapshots and compare. This chapter will be about creating and submitting your project snapshots command introduction.
Get in touch with a command to create a project
Create a new directory in the Git repository with git init. You can, do any directory at any time, completely localized.
Executive git init in the directory, you can create a Git repositories. For example, we created w3big items:
$ mkdir w3big $ cd w3big/ $ git init Initialized empty Git repository in /Users/tianqixin/www/w3big/.git/ # 在 /www/w3big/.git/ 目录初始化空 Git 仓库完毕。
Now you can see the generated .git subdirectory in your project. This is your Git repository, and all data related to a snapshot of your project are stored here.
ls -a . .. .git
Use git clone a Git repository to a local copy, so that they can view the item, or modify it.
If you need a cooperation project with others, or you want to copy a project, look at the code, you can clone the project. Excuting an order:
git clone [url]
Item [url] you want to copy it.
For example, we cloned the project on Github:
$ git clone [email protected]: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 cloning is completed in the current directory will generate a directory simplegit:$ Cd simplegit / $ ls README Rakefile lib
The 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 follow the name of the URL you provided indicated items to create your local project directory. The URL is usually the last item name / after. If you want a different name, you can add the name you want after the command.
Git's job is to create and save a snapshot of your project and after snapshots and compare. This chapter will be about creating a snapshot of your project and submit the command introduction.
git add command to add 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 $
git status command is used to view the current status of the project.
Next we execute git add command to add files:
$ git add README hello.php
Now we execute git status, you can see these two documents have been added to go.
$ git status -s A README A hello.php $
The new project, add all the files in common, we can usegit add. Command to add all files in the current project.
Now we modify the README file:
$ vim README <pre> <p>在 README 添加以下内容：<b># w3big Git 测试</b>，然后保存退出。</p> <p>再执行一下 git status：</p> $ git status -s AM README A hello.php
"AM" status means that the file after we add it to the cache there are changes. After the change we execute git add command to add it to the cache:
$ git add . $ git status -s A README A hello.php
When you want your changes contained in the forthcoming report snapshot in time, we need to execute git add.
git status to see you after the last commit if there are changes.
I demonstrated this command when added -s parameter to obtain a brief result. If you do not add this parameter will be detailed output:
$ 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
Executive git diff git status to see the details of the results of execution.
git diff command and displays the write cache has been modified but not yet written to the cache of the difference changes. git diff There are two main scenarios.
- Not cachedchanges: git diff
- View cachedchanges: git diff --cached
- View cached and uncached allchanges: git diff HEAD
- Show summaries rather than the entirediff: git diff --stat
Enter the following in hello.php file:
<?php echo '本教程：www.w3big.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.w3big.com'; +?>
git status display on your last commit to change after the update or write cache changes line by line and git diff show specifically what those changes.
Next we come to see the next git diff --cached implementation of the results of:
$ 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 @@ +# w3big 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.w3big.com'; +?>
Use git add command you want to write the contents of the snapshot buffer, and execute git commit will add content to the buffer warehouse.
Git you submit each recorded your name and e-mail address, so the first step you need to configure a user name and e-mail address.
$ git config --global user.name 'w3big' $ git config --global user.email [email protected]
Next we write caching, and submit all changes to the hello.php. In the first example, we use the -m option to provide the command line to submit comments.
$ 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 that we have recorded snapshots. If we execute git status:
$ git status # On branch master nothing to commit (working directory clean)
The above output shows that we after the last submission, did not make any changes, is a "working directory clean: clean working directory."
If you do not set the -m option, Git tries to open an editor for you to fill in the information submitted. Git if you can not find the relevant information in its configuration, the default will open vim. 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 think git add submitted cache process too cumbersome, Git also allows you to use the -a option to skip this step. Command format is as follows:
git commit -a
Let's modify hello.php file as the following:
<?php echo '本教程：www.w3big.com'; echo '本教程：www.w3big.com'; ?>
Then execute the following command:
git commit -am '修改 hello.php 文件' [master 71ee2cb] 修改 hello.php 文件 1 file changed, 1 insertion(+)
git reset HEAD
git reset HEAD command to remove the cached content.
Let's change the file README file, as follows:
# w3big Git 测试 # 本教程
hello.php file amended as follows:
<?php echo '本教程：www.w3big.com'; echo '本教程：www.w3big.com'; echo '本教程：www.w3big.com'; ?>
Now after two modified files are submitted to the buffer zone, we now want to cancel one of the cache, 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 you run git commit, the changes will only be submitted README file, but hello.php is not.
$ git commit -m '修改' [master f50cfda] 修改 1 file changed, 1 insertion(+) $ git status -s M hello.php
You can see the changes hello.php file and for the submission.
Then we can use the following command to modify hello.php submit:
$ 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, do git reset HEAD to cancel before git add to add, but do not want to include in the snapshot cache in the next commit.
git rmgit rm entry will be removed from the cache. This git reset HEAD cancel the cache entries are different. "Cancel Cache", which means that the recovery will make changes to our previous way cache.
Bydefault, git rm file will be deleted from the cache files and your hard drive (the working directory).
If you want to keep the file in the working directory, you can usegit rm --cached:
As we remove hello.php files:
$ git rm hello.php rm 'hello.php' $ ls README
Does not delete files from the workspace:
$ git rm --cached README rm 'README' $ ls README
git mv command to do all the things thatgit rm --cached operation command, rename the file on disk, and then execute git add to add new files to the cache.
README we first remove just add back:
$ git add README
Then its same name:
$ git mv README README.md $ ls README.md