Article Directory
First, what is Git
1.Git defined
Git is a distributed the version control software .
- Software
like QQ, office and other tools installed on your computer or other electronic device to use. - Version control
is similar thesis, writing copy, video clips, etc., need to repeatedly modify and retain the original historical data . - distributed
2. The development of distributed version control
File (folder) copy
Simply copy the very beginning, and then modified on the basis of the copy, save all versions, will not only occupy a lot of memory, and difficult to manage and synchronize shared with others, as follows:
Local Version Control
Only later developed into a file, save multiple versions of the file in the software, but still not conducive to sharing between different developers synchronization, as follows:
Centralized version control
Further development, to a centralized version control, such as SVN, there is a control center save all versions, but due to local non-version, such as the central problem, the local no full version, the whole project may be lost or can not be normal, as follows:
Distributed Version Control
Now and then distributed version control such as Git, the version will be submitted to the local, and then synchronized to the center, so as to enhance fault tolerance, as it follows:
Second, the reasons for and install Git version control
1. The reason version control
To preserve all of the previous versions, in order to roll back and modify.
For the following reasons:
- Written procedures specific process is an iterative editing processes taken the trouble to edit, modify when you do not want to destroy the state before the modification, the best is to modify the period of each event, be able to save a state, as a snapshot of a similar automatic system, when behind something goes wrong, you can choose a snapshot state prior to recovery;
- Finally, when the release of software often have multiple versions of the software source code generated is often only one, but when used in different parts of the last compiled, most of the code is shared, so often it requires version control several versions copy a few folders out;
- With version control, you can browse all the history of the development, grasp the progress of the development team, and any changes are no longer afraid, because you can easily recover before returning to the normal version, the function can also be done through branches and labels different versions of the software release.
Development is always a process, rather than results. Version control is the process of tracking the process of recording, the achievements reached in the process.
2. Install Git
You need to download the installation file, you can click https://git-scm.com/download choose the right system and version to download, if download speed is too slow can be downloaded through the mirror.
Can also directly click https://download.csdn.net/download/CUFEECR/12275488 download.
After downloading click to install, choose the installation directory, and complete the installation.
After the installation is complete, the right mouse button to see the Git GUI Here and Git Bash Here the installation is successful.
The entire installation process is to install Git to the local .
Right can choose Git Bash Here , a command window will pop up, you can enter git --vsesion
to view the git version, enter git --help
help documentation, examples are as follows:
Lenovo@LAPTOP-61GNF3CH MINGW64 ~/Desktop
$ git --version
git version 2.25.0.windows.1
Lenovo@LAPTOP-61GNF3CH MINGW64 ~/Desktop
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
restore Restore working tree files
rm Remove files from the working tree and from the index
sparse-checkout Initialize and modify the sparse-checkout
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.
Three, Git file management
- Into the folder you want to manage
- Right choice Git Bash Here
- initialization
git init
- Check file status in the directory
git status
-
File Management
- Management specified file
git add xxx
- All file management
git add .
-
Personal information (username, email) to configure
the first operation using Git. sho
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
- Generation version
git commit -m '描述信息'
- View version
git log
Tests are as follows:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest
$ git init
Initialized empty Git repository in E:/projecttest/.git/
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# 创建新文件index.html后
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html # 红色
nothing added to commit but untracked files present (use "git add" to track)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html # 绿色
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'v1'
# 首次使用会提示配置个人信息
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'Lenovo@LAPTOP-61GNF3CH.(none)')
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git config --global user.email "[email protected]"
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git config --global user.name "Corley"
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'v1'
[master (root-commit) 1e8ddbf] v1
1 file changed, 15 insertions(+)
create mode 100644 index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
# 创建新文件
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ touch readme.txt
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)
# 修改index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'v1.1'
[master 5f6b74b] v1.1
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1 (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git submit -m 'v1.15'
git: 'submit' is not a git command. See 'git --help'.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'v1.15'
[master 2ec3695] v1.15
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78 (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
nothing to commit, working tree clean
Four, Git's three regions
Git's three regions include:
- Workspace
- It has control file
- Changes and new files (red logo)
- Staging area (green logo)
- Repository
Illustrated as follows:
Code improvements, add new functionality, the test:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'v2'
[master 742d05c] v2
1 file changed, 6 insertions(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
nothing to commit, working tree clean
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git git commit -m '-v3'
git: 'git' is not a git command. See 'git --help'.
The most similar command is
init
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m '-v3'
[master f12719f] -v3
1 file changed, 6 insertions(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit f12719f22469243dd4e95bc3de7da2f524627938 (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:17:09 2020 +0800
-v3
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
...skipping...
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
Fifth, the version rollback and file recovery
1. Rollback
- Roll back to the previous version
git log
git reset --hard 版本号
- After the roll back to version
git reflog
git reset --hard 版本号
Where log
command to view the version before the rollback, reflog
check the version after the rollback.
Rollback forward test:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git reset --hard 742d05cf56bd15331d6c762a2f6f86b56c14a45b
HEAD is now at 742d05c v2
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b (HEAD -> master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
At this time you will find the code back to the time of the V2 version.
Rollback next test:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git reflog
742d05c (HEAD -> master) HEAD@{0}: reset: moving to 742d05cf56bd15331d6c762a2f6f86b56c14a45b
f12719f HEAD@{1}: commit: -v3
742d05c (HEAD -> master) HEAD@{2}: commit: v2
2ec3695 HEAD@{3}: commit: v1.15
5f6b74b HEAD@{4}: commit: v1.1
1e8ddbf HEAD@{5}: commit (initial): v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git reset --hard f12719f
HEAD is now at f12719f -v3
2. File Recovery
- Recovery of modified files
git checkout -- xxx
- Return to the workspace from scratch
even if the file name of the color from green to red
git reset HEAD xxx
Test example: