$ vim test1.txt #对文本文件进行编辑,做出修改 $ cat test1.txt This is HeavyTiger! how are you? This is a new line which I insert on the second day! $ git diff test1.txt diff --git a/test1.txt b/test1.txt index 02e6d3d..6eac541 100644 --- a/test1.txt +++ b/test1.txt @@ -1,2 +1,2 @@ -Hello world! -This is HeavyTiger! +This is HeavyTiger! how are you? +This is a new line which I insert on the second day!
$ cat test.txt Hello world! this is a new line which I add the second time! And I deleted the raw second line. $ vim test.txt $ cat test.txt Hello world! this is a new line which I add the second time! And I deleted the raw second line. Add a line which I don't want to save. $ git checkout -- test.txt $ cat test.txt Hello world! this is a new line which I add the second time! And I deleted the raw second line.
如果此时的修改不止存在于工作区下,并且非常憨憨地使用了git add .将所有地修改全部添加到了暂存区,此时显然使用git checkout -- file已经不可能起作用,但是仍然可以进行撤销操作。使用git reset HEAD <file>可以将暂存区中的修改回退到工作区中,使用HEAD表示回退到最新版本。
$ git reset --hard HEAD^ HEAD is now at 4369702 The first commitment $ git log commit 4369702618f9fe107a9e2629446ce4e1d4b2a6a8 (HEAD -> master) Author: HeavyTiger <462857080@qq.com> Date: Mon Jun 14 16:19:26 2021 +0800
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git commit -m "the first dev modify" [dev a1d3fec] the first dev modify 1 file changed, 1 insertion(+) create mode 100644 hello.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ ls README.md hello.txt test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ ls README.md test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git status On branch dev Untracked files: (use "git add <file>..." to include in what will be committed) temp1.txt temp2.txt
nothing added to commit but untracked files present (use "git add" to track)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git stash Saved working directory and index state WIP on dev: a1d3fec the first dev modify
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git stash list stash@{0}: WIP on dev: a1d3fec the first dev modify
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ ls README.md test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ git checkout dev Switched to branch 'dev'
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ ls README.md hello.txt test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git stash pop On branch dev Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: temp1.txt new file: temp2.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ ls README.md hello.txt test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'.
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ ls README.md test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git log --pretty=oneline f5228b6a8d1b5c52d3672369b13227217d5ea339 (HEAD -> dev) 修改了hello.txt,对HelloWorld含义做出解释 a1d3fec8d4b76de78627db0c9c41a9d0e16d0769 (origin/dev, master) the first dev modify 0ccccf8dd1b842302270dac680fa01fccbe3e002 the first modify in branch dev 4369702618f9fe107a9e2629446ce4e1d4b2a6a8 (origin/master, origin/HEAD) The first commitment dcd812278c90f6c66a622ae2e6202054d1740f80 Initial commit
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (dev) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ ls README.md hello.txt test.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ vim hello.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ cat hello.txt Hello world, this is another way to modify. ni hao!
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ git log --pretty=oneline 42eede8aed5975b65ee866223ce642b3f8cc23e7 (HEAD -> master) 在主分支中修改了hello.txt,欲展示合并冲突处理过程 a1d3fec8d4b76de78627db0c9c41a9d0e16d0769 (origin/dev) the first dev modify 0ccccf8dd1b842302270dac680fa01fccbe3e002 the first modify in branch dev 4369702618f9fe107a9e2629446ce4e1d4b2a6a8 (origin/master, origin/HEAD) The first commitment dcd812278c90f6c66a622ae2e6202054d1740f80 Initial commit
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ git log --graph --pretty=oneline --abbrev-commit --all * 42eede8 (HEAD -> master) 在主分支中修改了hello.txt,欲展示合并冲突处理过程 | * f5228b6 (dev) 修改了hello.txt,对HelloWorld含义做出解释 |/ * a1d3fec (origin/dev) the first dev modify * 0ccccf8 the first modify in branch dev * 4369702 (origin/master, origin/HEAD) The first commitment * dcd8122 Initial commit
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master|MERGING) $ cat hello.txt <<<<<<< HEAD Hello world, this is another way to modify. ni hao! ======= Hello world means ni hao in chinese! I modified this file. >>>>>>> dev
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master|MERGING) $ vim hello.txt
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master|MERGING) $ cat hello.txt Hello world, this is another way to modify. ni hao!I modified this file.
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (develop) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ git merge --no-ff -m "进制Fast forward模式合并" develop Merge made by the 'recursive' strategy. noff.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
DCM@DESKTOP-GN199F8 MINGW64 /e/ProgramDemo/Learn_git (master) $ git log --graph --pretty=oneline --abbrev-commit --all * e0aa011 (HEAD -> master) 进制Fast forward模式合并 |\ | * d6bc5fc (develop) 尝试对develop分支进行修改,此次尝试不使用Fast forward合并 |/ * 20a7935 在develop分支中增加对noff.txt文件的修改 * 7373637 conflict fixed |\ | * f5228b6 修改了hello.txt,对HelloWorld含义做出解释 * | 42eede8 在主分支中修改了hello.txt,欲展示合并冲突处理过程 |/ * a1d3fec (origin/dev) the first dev modify * 0ccccf8 the first modify in branch dev * 4369702 (origin/master, origin/HEAD) The first commitment * dcd8122 Initial commit