掌握創(chuàng)建版本庫(kù)后存皂,本篇咱們將著重討論Git版本穿梭晌坤。版本穿梭,包含版本追蹤回退艰垂、工作區(qū)與暫存區(qū) 泡仗、管理修改與撤銷修改、刪除文件四個(gè)內(nèi)容猜憎。大家可能有所疑問(wèn)娩怎,版本穿梭?什么是版本穿梭胰柑?為什么叫版本穿梭截亦?版本穿梭,顧名思義柬讨,使用git add <文件名>
崩瓤、git commit -m “提交說(shuō)明”
、git reset —-hard commit_id
等有效命令踩官,跟蹤并管理文件夾目錄工作區(qū)以及.git
版本庫(kù)歷史版本的修改却桶,而非文件。
版本追蹤回退
HEAD
指向是當(dāng)前版本蔗牡,git log
查看提交版本歷史颖系,git reflog
查看命令歷史,git reset —-hard commit_id
切換版本歷史辩越。
git status
查看工作區(qū)當(dāng)前狀態(tài)
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
查看工作區(qū)修改內(nèi)容
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
查看提交到倉(cāng)庫(kù)歷史記錄
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
查看提交到倉(cāng)庫(kù)歷史記錄
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^
回退到上個(gè)版本
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^^
回退到上上個(gè)版本
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
回到某個(gè)歷史版本
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個(gè)版本
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
工作區(qū)和暫存區(qū)
工作區(qū):本地創(chuàng)建的文件夾目錄嘁扼,就是一個(gè)工作區(qū)。工作區(qū)黔攒,用于修改文件趁啸。
版本庫(kù):工作區(qū)隱藏目錄.git
,就是Git的版本庫(kù)督惰。版本庫(kù)輕易不能動(dòng)不傅,不然就把這個(gè)目錄下的倉(cāng)庫(kù)破壞掉了。版本庫(kù)赏胚,包含暫存區(qū)以及HEAD
指向的分支(當(dāng)前分支)访娶。
暫存區(qū):版本庫(kù)中存有暫存區(qū),git add <文件名>
就是把工作區(qū)文件的修改添加到暫存區(qū)栅哀。而git commit -m “提交說(shuō)明”
把暫存區(qū)所有內(nèi)容添加到HEAD
指向的分支(當(dāng)前分支)震肮。
管理與撤銷修改
管理修改
Git跟蹤修改時(shí)称龙,多次使用git add <文件名>
,分別將多次工作區(qū)的修改添加到暫存區(qū)戳晌,或者git add .
一次性將工作區(qū)所有的修改添加到暫存區(qū)鲫尊。否則,git commit -m “一次性提交所有內(nèi)容”
不會(huì)將未添加到暫存區(qū)的修改提交到當(dāng)前分支沦偎。
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
撤銷修改
撤銷修改疫向,撤銷對(duì)工作區(qū)的修改。
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 .
到暫存區(qū)豪嚎,那么回退到git add .
到暫存區(qū)前的一個(gè)版本搔驼,然后再對(duì)工作區(qū)撤銷修改。
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 “提交說(shuō)明”
提交暫存區(qū)所有內(nèi)容到HEAD
指向的分支侈询,在沒(méi)有推送到遠(yuǎn)程倉(cāng)庫(kù)前舌涨,查看歷史版本并且版本回退到fix readme.txt
修改工作區(qū)前的一個(gè)版本。
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 -- 文件名
用版本庫(kù)版本替換工作區(qū)版本
localhost:learngit admin$ git checkout -- readme.docx
git rm 文件名
用于刪除一個(gè)文件扔字,如果文件被提交到當(dāng)前分支囊嘉,那么使用git rm 文件名
刪除一個(gè)文件后,可以通過(guò)git checkout — 文件名
革为,用版本庫(kù)提交過(guò)的版本替換工作區(qū)被刪除文件的版本扭粱。
但是,替換工作區(qū)的版本是提交前一次最新的版本震檩,而不是git add .
后最后一次提交的版本琢蛤,因此,最新一次提交的版本將會(huì)丟失修改內(nèi)容抛虏。
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
結(jié)語(yǔ)
Git和SVN比博其,更加靈活輕便,在分支管理方面更加強(qiáng)大嘉蕾。對(duì)于分支管理贺奠,后續(xù)我會(huì)對(duì)其進(jìn)行一次總結(jié)霜旧。
Git暫存區(qū)错忱,也是不同于SVN等集中式版本控制系統(tǒng),類似于“購(gòu)物車”挂据,它在版本穿梭里扮演極其重要的角色以清。