掌握创建版本库后,本篇咱们将着重讨论Git版本穿梭。版本穿梭,包含版本追踪回退、工作区与暂存区 、管理修改与撤销修改、删除文件四个内容。大家可能有所疑问,版本穿梭?什么是版本穿梭?为什么叫版本穿梭?版本穿梭,顾名思义,使用git add <文件名>
、git commit -m “提交说明”
、git reset —-hard commit_id
等有效命令,跟踪并管理文件夹目录工作区以及.git
版本库历史版本的修改,而非文件。
版本追踪回退
HEAD
指向是当前版本,git log
查看提交版本历史,git reflog
查看命令历史,git reset —-hard commit_id
切换版本历史。
git status
查看工作区当前状态
localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
git diff
查看工作区修改内容
localhost:learngit admin$ git diff
diff --git a/readme.txt b/readme.txt
index 2231bd8..2b9b14a 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
Now we write a readme file, the file must be put in learngit directory or subdirectory.
-To keep track the status of work area, use the git status command.
\ No newline at end of file
+To keep track the status of work area, use the git status command.^MIf tell you git status files have been modified, use the git diff can view the changes.
\ No newline at end of file
git log
查看提交到仓库历史记录
localhost:learngit admin$ git add readme.txt
localhost:learngit admin$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
localhost:learngit admin$ git commit -m "add git diff explain sec"
[master eefc07f] add git diff explain sec
1 file changed, 1 insertion(+), 1 deletion(-)
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ git diff
localhost:learngit admin$ git log
commit eefc07feb2351b50afb8728e3f028e36ad0f7029
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 11:02:09 2017 +0800
add git diff explain sec
commit ec56b0739ebfdc70aabf410364e6e21dec246237
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 10:59:08 2017 +0800
add git diff explan
commit 73f71b3dd5beed2196900d164636c7523e11bd70
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:37:07 2017 +0800
submit a lot of files at a time
commit f04d8865cf9826ef1214790716ddec64458fcf6c
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:30:01 2017 +0800
write a readme file
git log --pretty=oneline
查看提交到仓库历史记录
localhost:learngit admin$ git log --pretty=oneline
eefc07feb2351b50afb8728e3f028e36ad0f7029 add git diff explain sec
ec56b0739ebfdc70aabf410364e6e21dec246237 add git diff explan
73f71b3dd5beed2196900d164636c7523e11bd70 submit a lot of files at a time
f04d8865cf9826ef1214790716ddec64458fcf6c write a readme file
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
git reset --hard HEAD^
回退到上个版本
localhost:learngit admin$ git reset --hard HEAD^
HEAD is now at ec56b07 add git diff explan
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
git reset --hard HEAD^^
回退到上上个版本
localhost:learngit admin$ git reset --hard HEAD^^
HEAD is now at f04d886 write a readme file
localhost:learngit admin$ cat readme.txt
localhost:learngit admin$ cat readme.txt
localhost:learngit admin$ , the file must be put in learngit directory or subdirectory.
localhost:learngit admin$ git log
commit f04d8865cf9826ef1214790716ddec64458fcf6c
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:30:01 2017 +0800
write a readme file
git reset --hard eefc07feb23
回到某个历史版本
localhost:learngit admin$ git log --pretty=oneline
f04d8865cf9826ef1214790716ddec64458fcf6c write a readme file
localhost:learngit admin$ git reset --hard eefc07feb23
HEAD is now at eefc07f add git diff explain sec
localhost:learngit admin$ git log
commit eefc07feb2351b50afb8728e3f028e36ad0f7029
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 11:02:09 2017 +0800
add git diff explain sec
commit ec56b0739ebfdc70aabf410364e6e21dec246237
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 10:59:08 2017 +0800
add git diff explan
commit 73f71b3dd5beed2196900d164636c7523e11bd70
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:37:07 2017 +0800
submit a lot of files at a time
commit f04d8865cf9826ef1214790716ddec64458fcf6c
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:30:01 2017 +0800
write a readme file
localhost:learngit admin$ git log --pretty=oneline
eefc07feb2351b50afb8728e3f028e36ad0f7029 add git diff explain sec
ec56b0739ebfdc70aabf410364e6e21dec246237 add git diff explan
73f71b3dd5beed2196900d164636c7523e11bd70 submit a lot of files at a time
f04d8865cf9826ef1214790716ddec64458fcf6c write a readme file
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
git reset --hard HEAD~n
回退到往上第n个版本
localhost:learngit admin$ git reset --hard HEAD~3
HEAD is now at f04d886 write a readme file
localhost:learngit admin$ git reflog
f04d886 HEAD@{0}: reset: moving to HEAD~3
eefc07f HEAD@{1}: reset: moving to eefc07feb23
f04d886 HEAD@{2}: reset: moving to HEAD^^
ec56b07 HEAD@{3}: reset: moving to HEAD^
eefc07f HEAD@{4}: commit: add git diff explain sec
ec56b07 HEAD@{5}: commit: add git diff explan
73f71b3 HEAD@{6}: commit: submit a lot of files at a time
f04d886 HEAD@{7}: commit (initial): write a readme file
git reflog
查看命令历史
localhost:learngit admin$ git reflog
f04d886 HEAD@{0}: reset: moving to HEAD~3
eefc07f HEAD@{1}: reset: moving to eefc07feb23
f04d886 HEAD@{2}: reset: moving to HEAD^^
ec56b07 HEAD@{3}: reset: moving to HEAD^
eefc07f HEAD@{4}: commit: add git diff explain sec
ec56b07 HEAD@{5}: commit: add git diff explan
73f71b3 HEAD@{6}: commit: submit a lot of files at a time
f04d886 HEAD@{7}: commit (initial): write a readme file
localhost:learngit admin$ git reset --hard eefc07f
HEAD is now at eefc07f add git diff explain sec
工作区和暂存区
工作区:本地创建的文件夹目录,就是一个工作区。工作区,用于修改文件。
版本库:工作区隐藏目录.git
,就是Git的版本库。版本库轻易不能动,不然就把这个目录下的仓库破坏掉了。版本库,包含暂存区以及HEAD
指向的分支(当前分支)。
暂存区:版本库中存有暂存区,git add <文件名>
就是把工作区文件的修改添加到暂存区。而git commit -m “提交说明”
把暂存区所有内容添加到HEAD
指向的分支(当前分支)。
管理与撤销修改
管理修改
Git跟踪修改时,多次使用git add <文件名>
,分别将多次工作区的修改添加到暂存区,或者git add .
一次性将工作区所有的修改添加到暂存区。否则,git commit -m “一次性提交所有内容”
不会将未添加到暂存区的修改提交到当前分支。
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———localhost:learngit admin$ git add readme,txt
fatal: pathspec 'readme,txt' did not match any files
localhost:learngit admin$ git add readme.txt
localhost:learngit admin$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———localhost:learngit admin$ fix readme.txt
-bash: fix: command not found
localhost:learngit admin$ git commit -m "git tracks changes"
[master b8134c7] git tracks changes
1 file changed, 2 insertions(+), 1 deletion(-)
localhost:learngit admin$ git commit -m "git tracks changes"
On branch master
Changes not staged for commit:
modified: readme.txt
no changes added to commit
localhost:learngit admin$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index aaa3b0a..ccbe40b 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@
Now we write a readme file, the file must be put in learngit directory or subdirectory.
To keep track the status of work area, use the git status command.^MIf tell you git status files have been modified, use the git diff can view the changes.
-—— fix readme txt ———
\ No newline at end of file
+—— fix readme txt ———
+Git tracks changes of files.
\ No newline at end of file
localhost:learngit admin$ git add readme.txt
localhost:learngit admin$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
localhost:learngit admin$ git commit -m "git tracks changes 2"
[master 9cf24f9] git tracks changes 2
1 file changed, 2 insertions(+), 1 deletion(-)
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
撤销修改
撤销修改,撤销对工作区的修改。
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.
git checkout -- filelocalhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git checkout --readme.txt
error: unknown option `readme.txt'
usage: git checkout [<options>] <branch>
or: git checkout [<options>] [<branch>] -- <file>...
-q, --quiet suppress progress reporting
-b <branch> create and checkout a new branch
-B <branch> create/reset and checkout a branch
-l create reflog for new branch
--detach detach HEAD at named commit
-t, --track set upstream info for new branch
--orphan <new-branch>
new unparented branch
-2, --ours checkout our version for unmerged files
-3, --theirs checkout their version for unmerged files
-f, --force force checkout (throw away local modifications)
-m, --merge perform a 3-way merge with the new branch
--overwrite-ignore update ignored files (default)
--conflict <style> conflict style (merge or diff3)
-p, --patch select hunks interactively
--ignore-skip-worktree-bits
do not limit pathspecs to sparse entries only
--ignore-other-worktrees
do not check if another worktree is holding the given ref
--progress force progress reporting
localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git checkout -- readme.txt
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
如果git add .
到暂存区,那么回退到git add .
到暂存区前的一个版本,然后再对工作区撤销修改。
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.
git checkout -- filelocalhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git add readme.txt
localhost:learngit admin$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.
git checkout -- filelocalhost:learngit admin$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.
git checkout -- filelocalhost:learngit admin$ git checkout --readme.txt
error: unknown option `readme.txt'
usage: git checkout [<options>] <branch>
or: git checkout [<options>] [<branch>] -- <file>...
-q, --quiet suppress progress reporting
-b <branch> create and checkout a new branch
-B <branch> create/reset and checkout a branch
-l create reflog for new branch
--detach detach HEAD at named commit
-t, --track set upstream info for new branch
--orphan <new-branch>
new unparented branch
-2, --ours checkout our version for unmerged files
-3, --theirs checkout their version for unmerged files
-f, --force force checkout (throw away local modifications)
-m, --merge perform a 3-way merge with the new branch
--overwrite-ignore update ignored files (default)
--conflict <style> conflict style (merge or diff3)
-p, --patch select hunks interactively
--ignore-skip-worktree-bits
do not limit pathspecs to sparse entries only
--ignore-other-worktrees
do not check if another worktree is holding the given ref
--progress force progress reporting
localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git checkout -- readme.txt
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ cat readme.txt
Now we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
如果 git commit -m “提交说明”
提交暂存区所有内容到HEAD
指向的分支,在没有推送到远程仓库前,查看历史版本并且版本回退到fix readme.txt
修改工作区前的一个版本。
localhost:learngit admin$ cat readme.txt
git checkout --readme.txtNow we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
Git tracks changes of files.localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git add .
localhost:learngit admin$ git commit -m "git checkout --readme.txt"
[master 5766648] git checkout --readme.txt
1 file changed, 1 insertion(+), 1 deletion(-)
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ git log
commit 5766648ce055e31ae88349bf06a81c80372f02aa
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 15:18:07 2017 +0800
git checkout --readme.txt
commit 9cf24f963fd2a5d809702b0ca0a3e49b3388c73f
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 14:45:47 2017 +0800
git tracks changes 2
commit b8134c7179b4d66f8fb0a836707895c4c83f6349
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 14:43:54 2017 +0800
git tracks changes
commit eefc07feb2351b50afb8728e3f028e36ad0f7029
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 11:02:09 2017 +0800
add git diff explain sec
commit ec56b0739ebfdc70aabf410364e6e21dec246237
Author: far <caosuyang@51yunche.com>
Date: Tue Feb 21 10:59:08 2017 +0800
add git diff explan
commit 73f71b3dd5beed2196900d164636c7523e11bd70
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:37:07 2017 +0800
submit a lot of files at a time
commit f04d8865cf9826ef1214790716ddec64458fcf6c
Author: far <caosuyang@51yunche.com>
Date: Fri Feb 17 12:30:01 2017 +0800
write a readme file
localhost:learngit admin$ git log --pretty=oneline
5766648ce055e31ae88349bf06a81c80372f02aa git checkout --readme.txt
9cf24f963fd2a5d809702b0ca0a3e49b3388c73f git tracks changes 2
b8134c7179b4d66f8fb0a836707895c4c83f6349 git tracks changes
eefc07feb2351b50afb8728e3f028e36ad0f7029 add git diff explain sec
ec56b0739ebfdc70aabf410364e6e21dec246237 add git diff explan
73f71b3dd5beed2196900d164636c7523e11bd70 submit a lot of files at a time
f04d8865cf9826ef1214790716ddec64458fcf6c write a readme file
localhost:learngit admin$ git relog
git: 'relog' is not a git command. See 'git --help'.
Did you mean this?
reflog
localhost:learngit admin$ git reflog
5766648 HEAD@{0}: commit: git checkout --readme.txt
9cf24f9 HEAD@{1}: commit: git tracks changes 2
b8134c7 HEAD@{2}: commit: git tracks changes
eefc07f HEAD@{3}: reset: moving to eefc07f
f04d886 HEAD@{4}: reset: moving to HEAD~3
eefc07f HEAD@{5}: reset: moving to eefc07feb23
f04d886 HEAD@{6}: reset: moving to HEAD^^
ec56b07 HEAD@{7}: reset: moving to HEAD^
eefc07f HEAD@{8}: commit: add git diff explain sec
ec56b07 HEAD@{9}: commit: add git diff explan
73f71b3 HEAD@{10}: commit: submit a lot of files at a time
f04d886 HEAD@{11}: commit (initial): write a readme file
localhost:learngit admin$ git reset --hard 5766648
HEAD is now at 5766648 git checkout --readme.txt
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
localhost:learngit admin$ cat readme.txt
git checkout --readme.txtNow we write a readme file, the file must be put in learngit directory or subdirectory.
If tell you git status files have been modified, use the git diff can view the changes.
—— fix readme txt ———
删除文件
rm 文件名
删除文件
localhost:learngit admin$ rm readme.docx
git checkout -- 文件名
用版本库版本替换工作区版本
localhost:learngit admin$ git checkout -- readme.docx
git rm 文件名
用于删除一个文件,如果文件被提交到当前分支,那么使用git rm 文件名
删除一个文件后,可以通过git checkout — 文件名
,用版本库提交过的版本替换工作区被删除文件的版本。
但是,替换工作区的版本是提交前一次最新的版本,而不是git add .
后最后一次提交的版本,因此,最新一次提交的版本将会丢失修改内容。
localhost:learngit admin$ rm readme.docx
localhost:learngit admin$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.docx
no changes added to commit (use "git add" and/or "git commit -a")
localhost:learngit admin$ git checkout -- readme.docx
localhost:learngit admin$ git status
On branch master
nothing to commit, working tree clean
结语
Git和SVN比,更加灵活轻便,在分支管理方面更加强大。对于分支管理,后续我会对其进行一次总结。
Git暂存区,也是不同于SVN等集中式版本控制系统,类似于“购物车”,它在版本穿梭里扮演极其重要的角色。