Install and use Git standalone version
Introduction:
Git is an open-source distributed version control system. It is one of the most popular and advanced version control software today.
Git is a distributed version control system. On the contrary, SVN is a centralized version control system. Each time SVN changes a file, it must be submitted to the server for storage, and Git has a complete local version library.
1. Install Git
1. Linux (CentOS)
Shell> yum-y install git
# Yes, the installation is complete
2. Windows (WIN7)
: Https://github-windows.s3.amazonaws.com/GitHubSetup.exe
> Double-click the icon and select install (the package is automatically downloaded)
> Select skip. If you have a github user, you can log on to it.
> Click "+" in the upper left corner to add your own version library, enter the version library name, select the version library path, and click Create
> Then you can add the file to the version library. You can use the Git Shell command line to operate it.
Ii. Git (CentOS)
1. Create a version Library
Shell> git config -- global user. name "bkjia"
Shell> git config -- global user. email "linux@bkjia.com"
Shell> git config -- global color. ui true
# Article 1 and Article 2 set your username, email address, and distributed version control system to prove that you are the only one. Because it is a global configuration, all local version libraries use this name. You can also set it for a specific version Library (this is required)
# The third item is intelligent color display. The output result is color-differentiated. It looks convenient and significantly tall.
Shell> mkdir-p/git/gitdb; cd/git/gitdb
Shell> git init
Initialized empty Git repository in/git/gitdb/. git/
Shell> ls. git/
Branches config description HEAD hooks info objects refs
# Use git init to initialize the version Library (under which directory to execute and which directory is the version library directory)
# In this way, a gitdb version library is created, and all files in the version library are saved as hidden directories. in git, do not manually modify the content. Otherwise, the version library will break down.
2. Add files to the version Library
# For Git operations, the Operation directories are/git/gitdb/here without special instructions
Shell> head-5/etc/passwd | tee passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin
# Redirect the first five lines of data in the/etc/passwd file to the/git/gitdb/passwd file. The content is as follows:
Shell> git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (Use "git add <file>..." to include in what will be committed)
#
# Passwd
Nothing added to commit but untracked files present (use "git add" to track)
# In this case, the git status command shows that there is a file named passwd In the workspace that has not been submitted. (You must use the git add <file> command to add the file to the staging area before submission)
# Workspace: The version library directory is the workspace (create a file in the version library directory, that is, create a file in the workspace)
# Temporary storage zone: When the git add command is used, the file is actually added from the workspace to the temporary storage zone.
# Branch: When you create a version library, git automatically creates a master branch. When you use the git commit command, it actually submits all the files in the temporary storage area to the master branch.
# So the relationship between the three is: workspace --> temporary storage zone --> branch
# Git add can be added multiple times or multiple files can be added to the temporary storage area at a time. git commit will submit all the modifications in the temporary storage area to the branch at a time.
Shell> git add passwd
Shell> git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (Use "git rm -- cached <file>..." to unstage)
#
# New file: passwd
#
# After the file is added, it is found that a file named passwd has not been submitted in the temporary storage area (you can use the git rm -- cached <file> command to delete the file in the temporary storage area. Note: is not a workspace file)
Shell> git rm -- cached passwd
Rm 'passwd'
# After deletion, you will find that the passwd file in the workspace is still running. When you use the git status Command, the status changes back to the status when no git add is available.
Shell> git add passwd
Shell> git commit passwd-m "one"
Git commit-m "one"
[Master (root-commit) 7af25e2] one
1 files changed, 5 insertions (+), 0 deletions (-)
Create mode 100644 passwd
# When submitting a request, the-m parameter is used for marking. You can mark interesting things, such as what changes you have made...
Shell> git status
# On branch master
Nothing to commit (working directory clean)
# Use the git status Command to find that there is nothing in the workspace (submitted)
3. restore to the previous version or a specific version.
Shell> sed-I's/nologin/login/G' passwd
Shell> git status
# On branch master
# Changed but not updated:
# (Use "git add <file>..." to update what will be committed)
# (Use "git checkout -- <file>..." to discard changes in working directory)
#
# Modified: passwd
#
No changes added to commit (use "git add" and/or "git commit-")
# Because the nologin field in the file is changed to login, git status finds that the passwd file has been modified but has not been updated (git checkout -- <file> will be said later)
Shell> git diff HEAD -- passwd
Diff -- git a/passwd B/passwd
Index 03dab68 .. 96635c1 100644
--- A/passwd
++ B/passwd
@-+ @@
Root: x: 0: 0: root:/bin/bash
-Bin: x: 1: 1: bin:/sbin/nologin
-Daemon: x: 2: 2: daemon:/sbin/nologin
-Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
-Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin
+ Bin: x: 1: 1: bin:/sbin/login
+ Daemon: x: 2: 2: daemon:/sbin/login
+ Adm: x: 3: 4: adm:/var/adm:/sbin/login
+ Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/login
# You can use the git diff command to view the differences between the files (CHANGES) of the workspace and the version Library (branches) (HEAD indicates the current version of the branch (version Library), that is, the latest version)
Shell> git add passwd
Shell> git commit-m "two"
[Master 6fd5803] two
1 files changed, 4 insertions (+), 4 deletions (-)
Shell> git status
# On branch master
Nothing to commit (working directory clean)
# Submit the modified file to the version library and mark it as two.
# After the submission is complete, there will be nothing in the Workspace
Shell> sed-I's/bash/python/'passwd
Shell> git add passwd
Shell> git commit-m "three"
[Master 4cba143] three
1 files changed, 1 insertions (+), 1 deletions (-)
Shell> git status
# On branch master
Nothing to commit (working directory clean)
Shell> git diff HEAD -- passwd
# After the third submission is completed, use the git diff command to find that the latest version in the version library is exactly the same as the workspace File
Shell> git log
Git log
Commit 4cba14306c482080f442043b33f1dd640352a3f9
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:51:30 2015 + 0200
Three
Commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:43:17 2015 + 0200
Two
Commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 18:54:41 2015 + 0200
One
# The git log command clearly shows that there are three commits in total, marked as one, two, and three. What should I do if I want to return to the previous two version?
Shell> git reset -- hard HEAD ^
HEAD is now at 6fd5803 two
Shell> cat passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/login
Daemon: x: 2: 2: daemon:/sbin/login
Adm: x: 3: 4: adm:/var/adm:/sbin/login
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/login
# Use the git reset command. HEAD ^ indicates the previous version, and HEAD ^ indicates the previous two versions, now the two version has been returned (three version bash has been changed to python and has been restored to bash)
Shell> git log
Commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:43:17 2015 + 0200
Two
Commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 18:54:41 2015 + 0200
One
# Now there are two versions left. The top two version is the latest version, that is, the current version.
# Restore to a specific version. If there are many versions, use HEAD ^ or HEAD ~ The [number] method is very inconvenient (all numbers are required)
# Since the HEAD represents the version number, you can simply enter the version number when restoring it? Very convenient. The commit field (7af25e23a89ba16e5ff14a8da305542bc9cc6062) output by the git log command is the version number.
Shell> git reset -- hard 7af25e2
HEAD is now at 7af25e2 one
Shell> cat passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin
# When using the git reset command, you only need to output the first few digits of the version number. You can see that the version has been restored to the first version, and the file content has changed back to the initial status.
Shell> git log
Commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 18:54:41 2015 + 0200
One
# When I use the git log command to find that only the version submitted for the first time is used, what should I do if I want to return to two or three? (Version no more)
Shell> git reflog
7af25e2 HEAD @ {0}: 7af25e2: updating HEAD
6fd5803 HEAD @ {1}: HEAD ^: updating HEAD
4cba143 HEAD @ {2}: commit: three
6fd5803 HEAD @ {3}: commit: two
Shell> git reset -- hard 4cba143
HEAD is now at 4cba143 three
# Use the git reflog command to view the operation records. The first column is the desired version number. Use the git reset command again to restore the record.
Shell> cat passwd
Root: x: 0: 0: root:/bin/python
Bin: x: 1: 1: bin:/sbin/login
Daemon: x: 2: 2: daemon:/sbin/login
Adm: x: 3: 4: adm:/var/adm:/sbin/login
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/login
Shell> git log
Commit 4cba14306c482080f442043b33f1dd640352a3f9
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:51:30 2015 + 0200
Three
Commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:43:17 2015 + 0200
Two
Commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 18:54:41 2015 + 0200
One
# The file content and version information are all returned, and the current version is restored to the three status
4. git checkout -- <file>: Do you still remember this?
Shell> sed-I '1d 'passwd
Shell> git status
# On branch master
# Changed but not updated:
# (Use "git add <file>..." to update what will be committed)
# (Use "git checkout -- <file>..." to discard changes in working directory)
#
# Modified: passwd
#
No changes added to commit (use "git add" and/or "git commit-")
# After the workspace file content is modified, you can see this prompt using the git status Command (git checkout -- <file> can undo the modification of the workspace file)
Shell> cat passwd
Bin: x: 1: 1: bin:/sbin/login
Daemon: x: 2: 2: daemon:/sbin/login
Adm: x: 3: 4: adm:/var/adm:/sbin/login
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/login
Shell> git checkout -- passwd
# Use the git checkout command to undo the changes to the workspace file (restore to the same status as the version library, that is, all the changes just now are recalled)
Shell> sed-I '1d 'passwd
Shell> git add passwd
Shell> sed-I '1d 'passwd
Shell> git status
Shell> git status
# On branch master
# Changes to be committed:
# (Use "git reset HEAD <file>..." to unstage)
#
# Modified: passwd
#
# Changed but not updated:
# (Use "git add <file>..." to update what will be committed)
# (Use "git checkout -- <file>..." to discard changes in working directory)
#
# Modified: passwd
#
# What is the current situation? First, the first line of the file is deleted, and the git add command is executed (the file modification is added to the temporary storage area). Then, the command is deleted again, so what is the result of executing the git checkout command file?
Shell> git checkout -- passwd
# Practice: the file is restored to the state of the temporary storage area, that is, deleted once.
Shell> git status
# On branch master
# Changes to be committed:
# (Use "git reset HEAD <file>..." to unstage)
#
# Modified: passwd
#
# This is the status of the temporary storage area. How to restore the status of the temporary storage area, that is, to cancel the first deletion (Note: This is already added by git)
Shell> git reset HEAD passwd
Unstaged changes after reset:
M passwd
# Do not remember the git reset command. Yes, it is used to restore the file to the previous version. It can also be used to roll back the changes in the temporary storage area to the workspace.
Shell> git status
# On branch master
# Changed but not updated:
# (Use "git add <file>..." to update what will be committed)
# (Use "git checkout -- <file>..." to discard changes in working directory)
#
# Modified: passwd
#
No changes added to commit (use "git add" and/or "git commit-")
# Check the status again and prompt that the workspace has changed. You can use git checkout -- <file> to undo the workspace change.
# Git reset: roll back the file to the previous version/roll back the changes in the temporary storage area to the Workspace
# Git checkout undo workspace changes/Roll Back workspace changes to the temporary Workspace
5. delete an object
Shell> git rm passwd
Rm 'passwd'
Shell> git commit-m "delete passwd"
[Master 3be8d7a] delete passwd
1 files changed, 0 insertions (+), 5 deletions (-)
Delete mode 100644 passwd
# Use the git rm command to delete the files in the version library and then submit and delete the files (the files are deleted, and the files are not in the version library directory)
Shell> git log
Commit 3be8d7a3ade6f8947135843832bb6cfa075638a4
Author: bkjia <linux@bkjia.com>
Date: Wed Jul 22 13:18:23 2015 + 0200
Delete passwd
Commit 4cba14306c482080f442043b33f1dd640352a3f9
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:51:30 2015 + 0200
Three
Commit 6fd5803a3fc2d3039ceaf6fc0b9c4fb8f0dfc67a
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 19:43:17 2015 + 0200
Two
Commit 7af25e23a89ba16e5ff14a8da305542bc9cc6062
Author: bkjia <linux@bkjia.com>
Date: Tue Jul 21 18:54:41 2015 + 0200
One
Shell> git reset -- hard 4cba14
HEAD is now at 4cba143 three
##... It's hard to delete the file, and the result is restored again ^_^. Don't be too happy. If you delete the file after modifying the file, if you have not submitted the file, the modification is actually lost ..
Shell> rm-rf passwd
Shell> git reset -- hard 4cba14
HEAD is now at 4cba143 three
# Do you no longer have to worry about accidentally deleting the file? ^_^ (in single-host scenarios, you should stop the physical disk from exploding, so there is no such thing)
Git Tutorials:
GitHub tutorials
Git tag management details
Git branch management
Git remote repository details
Git local Repository (Repository) Details
Git server setup and Client installation
Git Overview
Share practical GitHub tutorials
How to Build and use Git servers in Ubuntu
Git details: click here
Git: click here
This article permanently updates the link address: