Imagine an HTML project that uses GIT to record and track this project, including the following:
1) Create a repository.
2) Add and modify files.
3) Create a new branch.
4) tag and organize the repository.
5) clone the repository.
1. Creating the repository Creating a Repository
In Git, a repository (. git directory) is placed in the same directory as the working directory tree.
In this example, to create an HTML page, name the project MySite.
First create a directory with the same name "MySite", and enter into this directory, and then enter the command git init.
[Email protected] ~]# mkdir mysite
[Email protected] ~]# CD MySite
[[email protected] mysite]# git init
Initialized Empty Git repository in/root/mysite/.git/
Create complete.
2. Code modification
Add files to the empty repository:
Create a file named Index.html, and add the text:
[Email protected] mysite]# vim index.html
Once you have created a simple HTML file (put it under the MySite path), you can start tracking the version.
To get git to keep track of this file, you need to let it know the file, two steps away:
1) First Use the git add command to add the file to the repository index;
2) then commit using the git commit command.
The file or file list can be used as a parameter to the git add command.
[[email protected] mysite]# git add index.html
The git commit command creates a commit record.
A commit record is a history that is stored in a version, each time a record is created, and the evolution of the code is marked.
Git adds the name and e-mail address of the submitter and submits the message to the submission record.
Parameter-M, tell Git that this message is "Add in the Hello World HTML".
Commit global global setting, set mailbox and username address before committing, or commit will fail
[Email protected] mysite]# git config--global user.email "[email protected]"
[[email protected] mysite]# git config--global user.name "Wangshibo"
[[email protected] mysite]# git commit-m "Add in Hello World HTML"
Run the command git log to see the information about this commit:
The first line of the output shows the commit name, which is the SHA-1 code that git automatically generates. Git uses it to keep track of commits, and using that hash code ensures that each commit's name is unique.
[[email protected] mysite]# git log
Commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author:wangshibo <[email protected]>
Date:tue Dec 20 17:16:20 2016 +0800
Add in Hello World HTML
3. In the project follow-up development, modify the HTML file as follows:
[Email protected] mysite]# vim index.html
After the modification, GIT can detect that the file has been modified.
The command git status displays the status of the working directory tree, which is the current view state.
[[email protected] mysite]# git status
# on Branch Master
# changes not staged for commit:
# (use "git add <file> ..." To update what would be committed)
# (use "Git checkout-<file> ..." to discard changes in working directory)
#
# modified:index.html
#
No changes added to commit (use "git add" and/or "Git Commit-a")
The result above shows that git has monitored the changes, but does not yet know how to handle them.
If you want to commit, you need to modify the staging (stage) to prepare the changes for submission to the repository .
Git has three places to store your code:
1) The first place is the working directory tree, which can be manipulated directly when editing a file;
2) The second one is the index, which is staging area (staging area). Staging area is a buffer between the working directory tree and the repository.
3) The third, or final, version is the repository.
Command git Add, you can temporarily save the file just made changes. It uses the same command as the previous one to add a new file, but this time it tells Git to track a new change instead of a new file.
[[email protected] mysite]# git add index.html
[[email protected] mysite]# git status
# on Branch Master
# Changes to be committed:
# (use "git reset HEAD <file> ..." to Unstage)
#
# modified:index.html
#
After you have staged the modified index.html, execute the command git status to see that the message has changed to the changes to be commited,index.html the line changed from red to green.
When using the command git commit, don't forget to use the parameter with-m and submit a message after the argument to explain the reason for the change, as follows:
Git log can quickly browse to submit a message:
[[email protected] mysite]# git log
Commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author:wangshibo <[email protected]>
Date:tue Dec 20 17:16:20 2016 +0800
Add in Hello World HTML
[[email protected] mysite]# git log-1
Commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author:wangshibo <[email protected]>
Date:tue Dec 20 17:16:20 2016 +0800
Add in Hello World HTML
Add parameters to the command:-1 to limit the number of commit entries for the command output.
4. Branch understanding and use
For example, the code for the MySite project is now almost ready to be published, but it still needs to be tested, until it is confirmed that it achieves the expected functionality and quality, and at the same time, with the help of the branch, you can start the development of the next version of the new feature.
The command to create a branch is GIT branch, which requires two parameters: a new branch name and a parent branch name.
The newly created branch is based on the parent branch that already exists.
[[email protected] mysite]# git branch rb_1.0 master
This command creates a branch called rb_1.0 from the main branch (Master branch).
Master Branch Master is the default branch of Git. The RB in the branch name represents the publishing branch (release branch). This prefix allows you to quickly identify which branches are the publishing branch.
Now let's make some new changes. These changes do not affect the code that is ready to be published.
Add the following code before </body>:
[Email protected] mysite]# vim index.html
Submit these changes with the following command:
[[email protected] mysite]# git commit-a
Aborting commit due to empty commit message.
Parameter-a tells Git to commit all modified files.
After the submission, it will pop up a text file, enter the message is to submit a message. The pop-up text file will exit normally.
Now there is the most recent modification on the main branch, and the original code on the publishing branch.
Please switch to the publishing branch and make the last modification before publishing. The command to switch branches is git checkout.
[[email protected] mysite]# git checkout rb_1.0
M index.html
Switched to branch ' rb_1.0 '
After you convert the branch, the editor of the open file you are using will remind the file that it has been modified, reload the file, and find that the changes you just made on the main branch have disappeared.
You can use the git status command to see which branch you're on:
[[email protected] mysite]# git status
# on branch rb_1.0
# Changes to be committed:
# (use "git reset HEAD <file> ..." to Unstage)
#
# modified:index.html
#
# changes not staged for commit:
# (use "git add <file> ..." To update what would be committed)
# (use "Git checkout-<file> ..." to discard changes in working directory)
#
# modified:index.html
#
Make the final changes before publishing: Add some descriptive meta tags to the [Email protected] mysite]# vim index.html
Save and modify the submission:
[[email protected] mysite]# git commit-a
Aborting commit due to empty commit message.
GIT manages project instance description-record and track items