git学习 之 实用方面

1、介绍几个概念
工作区:未git add之前,当前的本地
暂存区:git add 后,git commit 之前的期间
本地版本库:git commit 后
远程库:git push 后

2、介绍相关命令
1)git status //查看仓库的当前状态(可以查看工作区或暂存区的状态)

zx@zx:~/test/test_git$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	2.txt
nothing added to commit but untracked files present (use "git add" to track)

zx@zx:~/test/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	new file:   2.txt

2)git diff //查看修改的内容(比对的是已经被跟踪的文件的工作区和暂存区间的区别)
git diff – file (查看指定的修改)

zx@zx:~/test/test_git$ git diff
diff --git a/4.txt b/4.txt
index 8bb9320..666d423 100644
--- a/4.txt
+++ b/4.txt
@@ -1 +1,2 @@
 add 4.txt
+modify 4.txt
//演示被跟踪的(4.txt此时处在暂存区)
zx@zx:~/test/test_git$ vim 5.txt
zx@zx:~/test/test_git$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  readme.txt
zx@zx:~/test/test_git$ git diff
zx@zx:~/test/test_git$ git diff
zx@zx:~/test/test_git$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	5.txt

nothing added to commit but untracked files present (use "git add" to track)
//5.txt尚未被跟踪,此时git diff并没有内容,可通过git status查看当前的状态

3)git log、git reflog的使用(git log 查看历史修改的提交记录、git reflog查看历史的命令记录)

zx@zx:~/test/test_git$ git log
commit 0b4454c880a698890718d86bf4c215b441575370
Author: zx <465637045@qq.com>
Date:   Sun Sep 12 10:09:02 2021 +0800
    add 1.txt
commit 4e1e6dff5d0125951aa2866a4f0f3e7b3b253f65
Author: zx <465637045@qq.com>
Date:   Sun Sep 12 10:08:30 2021 +0800
zx@zx:~/test/test_git$ git log --pretty=oneline
0b4454c880a698890718d86bf4c215b441575370 add 1.txt
4e1e6dff5d0125951aa2866a4f0f3e7b3b253f65 add readme.txt
// git log 正常的显示比较详细,git log --pretty=oneline 显示一行

zx@zx:~/test/test_git$ git reflog
760fa98 HEAD@{0}: commit: add 5.txt
61b2156 HEAD@{1}: commit: add 4.txt
7cc008f HEAD@{2}: commit: add 3.txt
dd555d4 HEAD@{3}: reset: moving to dd555d4
0b4454c HEAD@{4}: reset: moving to HEAD^
dd555d4 HEAD@{5}: commit: add 2.txt
0b4454c HEAD@{6}: commit: add 1.txt
4e1e6df HEAD@{7}: commit (initial): add readme.txt

4)git checkout – file(恢复工作区(如果已经在暂存区中有,则恢复至暂存区的;如果暂存区中无,则从版本库中进行恢复)
//如果是git checkout – . 则是将工作区中所有的都恢复

zx@zx:~/test/test_git$ 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:   1.txt
	modified:   2.txt
no changes added to commit (use "git add" and/or "git commit -a")
zx@zx:~/test/test_git$ git checkout -- 1.txt
zx@zx:~/test/test_git$ 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:   2.txt
no changes added to commit (use "git add" and/or "git commit -a")

5)git reset HEAD file(将某个文件从暂存区中恢复至工作区,即还原暂存区中的修改)
//如果是git reset HEAD 则将暂存区中的所有的都恢复至工作区

zx@zx:~/test/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	modified:   1.txt
	
zx@zx:~/test/test_git$ git reset HEAD 1.txt
Unstaged changes after reset:
M	1.txt
zx@zx:~/test/test_git$ 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:   1.txt
no changes added to commit (use "git add" and/or "git commit -a")

小结:
特定文件情况下:如果是工作区中存在错误要修改可以使用git checkout – file将文件恢复至暂存区/版本中;如果是暂存区中存在错误,则可以使用git reset HEAD file。
全部文件情况下:想全部还原至版本库中,则可以使用git reset --hard HEAD
注:使用git clean -f -d可以清空工作区中未被跟踪的所有文件,git clean -f -d file(清掉特定文件),使用git rm 可以删除被跟踪的并且同时要commit 才能生效

6)git stash
使用场景:分支1的版本库中的某个文件被修改未被提交并且在分支2中未存在该文件,所以在从分支1切换到分支2时 要将分支1中的内容使用git stash 储藏起来,才能进行切换分支。

zx@zx:~/test/test_git$ vim 7.txt
zx@zx:~/test/test_git$ git add 7.txt
zx@zx:~/test/test_git$ git commit -m "add 7.txt"
[master 3498f38] add 7.txt
 1 file changed, 1 insertion(+)
 create mode 100644 7.txt
zx@zx:~/test/test_git$ vim 7.txt
zx@zx:~/test/test_git$ git checkout develop
error: Your local changes to the following files would be overwritten by checkout:
	7.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
//文件存在暂存区或本地仓库时才会出现该种情况
zx@zx:~/test/test_git$ 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:   7.txt
no changes added to commit (use "git add" and/or "git commit -a")
//7.txt只有master才拥有再次被修改后,
//此时想切换分支develop 就需要git stash 将7.txt的先储藏起来,
//后续切换回master 再使用git stash apply 提取出
zx@zx:~/test/test_git$ git stash 
Saved working directory and index state WIP on master: 3498f38 add 7.txt
HEAD is now at 3498f38 add 7.txt
zx@zx:~/test/test_git$ git checkout develop
Switched to branch 'develop'
zx@zx:~/test/test_git$ git checkout master
Switched to branch 'master'
zx@zx:~/test/test_git$ git stash list
stash@{0}: WIP on master: 3498f38 add 7.txt
stash@{1}: WIP on master: ac9bca7 add 6.txt
stash@{2}: WIP on master: ac9bca7 add 6.txt
//可以看出现在有多个stash,需要使用git stash apply stash@{0}来提取特定的
zx@zx:~/test/test_git$ git stash apply stash@{0}
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:   7.txt
no changes added to commit (use "git add" and/or "git commit -a")
zx@zx:~/test/test_git$ 

注:git stash apply 恢复只能在git stash 储藏时的特定节点上进行恢复。
git stash apply 恢复后,stash内容并不会删除,需要使用git stash drop来删除
git stash pop 在恢复的同时将stash 内容删掉


版权声明:本文为qq_33782617原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。