GIT basic operations. git usage tutorial
GitThe job is to create and save the snapshots of your project and compare them with the subsequent snapshots. In short, Git is the source code management tool. The following are simple Git commands that are frequently used in the work. If you have any shortcomings, I hope to provide them. Thank you.1. Obtain and create project commands1. git init
Use git init to create a Git repository in the directory. You can do this at any time and in any directory, and it is completely localized.
Execute git init in the directory to create a Git repository. For example, we create a xxxxxx project:
$ Mkdir xxxxxx
$ Cd xxxxxx/
$ Git initInitialized empty Git repository in/Users/tianqixin/www/runoob/. git/# Initialize an empty git repository in the/www/runoob/. Git/directory.
Now you can see that the. git subdirectory is generated in your project. This is your Git repository. All snapshot data about your project is stored here.
Ls-a... git
2. git clone
Use git clone to copy a Git repository to your local device so that you can view the project or modify it.
If you need to work with another project, or want to copy a project and check the code, you can clone the project. Run the following command:
Git clone [url]
[Url] the project you want to copy.
For example, we clone a Github project:
$ Git clone git@github.com: schacon/simplegit. gitCloning into 'simplegit '...
Remote: Counting objects: 13, done.
Remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13 sorting ing objects: 100% (13/13), done. resolving deltas: 100% (2/2), done. checking connectivity... done.
After cloning, a simplegit directory is generated in the current directory:
$ Cd simplegit/$ ls README Rakefile lib
The above operations 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 according to the name of the project indicated by the URL you provided. It is usually the name of the project after the last/of the URL. If you want a different name, you can add the name you want after the command.
Ii. Basic snapshots
Git's job is to create and save the snapshot of your project and compare it with the subsequent snapshot. This chapter describes how to create and submit a snapshot for your project.
1. 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, run the git add command to add files:
$ Git add README hello. php
Now we can execute git status to see that these two files have been added.
$ Git status-s
A README
A hello. php
$
It is common to add all files in a new project.Git add.Command to add all files of the current project.
Now let's modify the README file:
$ Vim README <pre> <p> Add the following content to README: <B> # Runoob Git test </B>, save and exit. </P> <p> RUN git status again: </p>
$ Git status-s
AM README
A hello. php
The "AM" status indicates that this file is changed after we add it to the cache. After the modification, we will 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 to be submitted, You need to execute git add.
2. git status
Git status to check whether any changes have been made after your last submission.
When I demonstrated this command, I added the-s parameter to get a brief result output. If this parameter is not added, the output details are as follows:
$ Git statusOn branch master
Initial commit
Changes to be committed:
(Use "git rm -- cached <file>..." to unstage)
New file: README
New file: hello. php
3. git diff
Execute git diff to view details about the results of running git status.
The git diff command shows the differences between the changes that have been written to the cache and those that have been modified but not yet written to the cache. Git diff has two main application scenarios.
- Changes that have not been cached:Git diff
- View cached changes:Git diff -- cached
- View cached and Uncached changes:Git diff HEAD
- Display the abstract instead of the entire diff:Git diff -- stat
Enter the following content in the hello. php file:
<? Php
Echo 'tianshu-blog: www.xxxxxx.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 + 1, 3 + <? Php + echo 'tianshu-blog: www.xxxxxx.com '; ++?>
Git status displays the changes you made after the last update commit or the changes written to the cache, while git diff displays the changes one by one.
Next, let's check the execution result of git diff -- cached:
$ Git add hello. php
$ Git status-s
A README
A hello. php
$ Git diff -- cached
Diff -- git a/README B/READMEnew file mode 100644
Index 0000000 .. 8f87495 ---/dev/null ++ B/README-+ 1 + # Runoob Git Test
Diff -- git a/hello. php B/hello. phpnew file mode 100644
Index 0000000 .. 69b5711 ---/dev/null ++ B/hello. php @-+ 1, 3 + <? Php + echo 'tianshu-blog: www.xxxxxx.com '; ++?>
4. git commit
Use the git add command to write the content of the desired snapshot to the cache, and execute git commit to add the content of the cache to the repository.
Git records your name and email address for each commit, so you need to configure the user name and email address for the first step.
$ Git config -- global user. name 'xxxx'
$ Git config -- global user. email test@xxxxxx.com
Next, we write data to the cache and submit all changes to hello. php. In the first example, we use the-m option to provide comments for submission in the command line.
$ Git add hello. php
$ Git status-s
A README
A hello. php
$ Git commit-m' first version commit '[master (root-commit) d32cf1f] first version commit
2 files changed, 4 insertions (+)
Create mode 100644 README
Create mode 100644 hello. php
Now we have recorded snapshots. If we execute git status again:
$ Git status # On branch master
Nothing to commit (working directory clean)
The above output indicates that we did not make any changes after the last commit. 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 submission information. If Git cannot find relevant information in its configuration, vim is enabled 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 think that the process of submitting the cache by git add is too cumbersome, Git also allows you to skip this step using the-a option. The command format is as follows:
Git commit-
First, modify the hello. php file as follows:
<? Php
Echo 'tianshu-blog: www.xxxxxx.com ';
Echo 'tianshu-blog: www.xxxxxx.com ';?>
Run the following command:
Git commit-am 'modify the hello. php file' [master 71ee2cb] modify the hello. php file
1 file changed, 1 insertion (+)
5. git reset HEAD
The git reset HEAD command is used to cancel cached content.
First, modify the README file. The content is as follows:
# Runoob Git test # tianshu-blog
Modify the hello. php file:
<? Php
Echo 'tianshu-blog: www.xxxxxx.com ';
Echo 'tianshu-blog: www.xxxxx.com ';
Echo 'tianshu-blog: www.xxxxx.com ';?>
After the two files are modified, they are all submitted to the cache. Now we want to cancel the cache of one of them, as shown below:
$ 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 execute git commit and only commit the changes to the README file, but hello. php does not.
$ Git commit-m' modify '[master f50cfda] modify
1 file changed, 1 insertion (+)
$ Git status-s
M hello. php
The hello. php file is modified and submitted.
In this case, we can use the following command to submit modifications to hello. php:
$ Git commit-am 'modify the hello. php file' [master 760f74d] modify the hello. php file
1 file changed, 1 insertion (+)
$ Git statusOn 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 the cache in the next submitted snapshot.
6. git rm
Git rm removes entries from the cache. This is different from how git reset HEAD removes the entry from the cache. "Cancel cache" means to restore the cache area to what we looked like before making changes.
By default,Git rm fileFiles will be deleted from the cache and your hard disk (working directory.
If you want to keep the file in the working directory, you can useGit rm -- cached:
For example, 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
7. git mv
The git mv command does everything.Git rm -- cachedCommand operation, rename the files on the disk, and then execute git add to add the new files to the cache area.
Add the removed README first:
$ Git add README
Then name it again:
$ Git mv README. md
$ Ls
README. md