Git command for obtaining and creating projects, git command for obtaining Projects
Obtain and create project commands
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. If we create a test project:
$ Mkdir runoob $ cd runoob/$ git initInitialized empty Git repository in/Users/tianqixin/www/runoob /. git/# In/www/runoob /. git/directory Initialization is complete.
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
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 13Receiving 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/$ lsREADME Rakefile lib
The above operations will copy all records of the project.
$ ls -a. .. .git README Rakefile lib$ cd .git$ lsHEAD description info packed-refsbranches hooks logs refsconfig 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 in the command.
Basic Snapshot
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$ lsREADMEhello.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 -sA READMEA hello.php$
It is common to add all files in a new project. You can use the git add. Command to add all files in the current project.
Now let's modify the README file:
$ vim README
Add the following content in README: # Test Git Test, save and exit.
Run git status again:
$ git status -sAM READMEA 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 -sA READMEA hello.php
When you want to include your modifications in the snapshot to be submitted, You need to execute git add.
Git status
Git status to check whether any changes have been made after your last submission.
We added the-s parameter to demonstrate this command to get a brief result output. If this parameter is not added, the output details are as follows:
$ git statusOn branch masterInitial commitChanges to be committed: (use "git rm --cached
..." to unstage)new file: READMEnew file: hello.php
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.
· Unbuffered changes: git diff
· View cached changes: git diff -- cached
· View All 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. cpp file:
#include
int mian(int argc, char * argv[]){printf("Hello, Word!");return 0;}
D:\test>git status -sA READMEAM hello.cppD:\test>git diffdiff --git a/hello.cpp b/hello.cppindex e69de29..8684883 100644--- a/hello.cpp+++ b/hello.cpp@@ -0,0 +1,7 @@+#include
++int mian(int argc, char * argv[])+{+ printf("Hello, Word!");+ return 0;+}\ No newline at end of file
Git status displays the changes you made after the last update commit or the changes you made to the write cache, while git diff displays the changes one by one.
Next, let's check the execution result of git diff -- cached:
D:\test>git add hello.cppD:\test>git status -sA READMEA hello.cppD:\test>git diff --cacheddiff --git a/README b/READMEnew file mode 100644index 0000000..eb29eb6--- /dev/null+++ b/README@@ -0,0 +1 @@+#Test Git
<8B>
<95>\ No newline at end of filediff --git a/hello.cpp b/hello.cppnew file mode 100644index 0000000..8684883--- /dev/null+++ b/hello.cpp@@ -0,0 +1,7 @@+#include
++int mian(int argc, char * argv[])+{+ printf("Hello, Word!");+ return 0;+}\ No newline at end of file
Git commit
Use the git add command to write the content of the desired snapshot to the buffer zone, and execute git commit to add the buffer content 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 'syk'$ git config --global user.email han1102131288@163.com
Next, we write data to the cache and submit all changes to hello. cpp. In the first example, we use the-m option to provide comments for submission in the command line.
$ Git add hello. php $ git status-sA READMEA 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 masternothing to commit (working directory clean)
The above output indicates that we did not make any changes after the last submission. 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 with submitted 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
..." 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 -a
First, modify the hello. cpp file as follows:
#include
int mian(int argc, char * argv[]){printf("Hello, Word!");printf("Hello, Word!");return 0;}
Run the following command:
Git commit-am 'modify the hello. cpp file '[master 71ee2cb] modify the hello. cpp file 1 file changed, 1 insertion (+)
Git reset HEAD
The git reset HEAD command is used to cancel cached content.
First, modify the README file. The content is as follows:
# Test Git Test
Modify the hello. cpp file:
#include
int mian(int argc, char * argv[]){printf("Hello, Word!");printf("Hello, Word!");printf("Hello, Word!");printf("Hello, Word!");printf("Hello, Word!");printf("Hello, Word!");return 0;}
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:
D:\test>git status -sM READMEM hello.cppD:\test>git add .D:\test>git status -sM READMEM hello.cppD:\test>git reset HEAD -- hello.cppUnstaged changes after reset:M hello.cppD:\test>git status -sM READMEM hello.cpp
Now you execute git commit and only commit the changes to the README file, but hello. cpp does not.
$ Git commit-m' modify '[master f50cfda] modify 1 file changed, 1 insertion (+) $ git status-s m hello. cpp
The hello. cpp file is modified and submitted.
In this case, we can use the following command to submit the modification of hello. cpp:
$ Git commit-am' modify hello. cpp File '[master 760f74d] modify hello. cpp file 1 file changed, 1 insertion (+) $ git statusOn branch masternothing 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.
Git rm
If you just manually delete the file from the working directory, you will be prompted at Changes not staged for commit when running git status.
To remove a file from Git, you must remove it from the list of Tracked files and submit the file. Run the following command to complete this task:
git rm
If you have deleted a file that has been modified and put in the temporary storage area, you must force delete option-f
git rm -f
If the file is removed from the temporary storage, but you still want to keep it in the current working directory, in other words, you only need to delete it from the tracking list and use the -- cached option.
git rm --cached
For example, delete the hello. cpp file:
$ git rm hello.cpprm 'hello.cpp'$ lsREADME
Do not delete files from the Workspace:
$ git rm --cached README rm 'README'$ lsREADME
It can be recursively deleted, that is, if a directory is followed by a parameter, all subdirectories and files in the entire directory will be recursively deleted:
git rm –r *
Go to a directory and execute this statement to delete all files and subdirectories in the directory.
Git mv
The git mv command is used to move or rename a file, directory, and soft connection.
Add the removed README first:
$ git add README
Then name it again:
$ git mv README README.md$ lsREADME.md