工作区和暂存区
工作区:项目目录即为工作区
版本库:项目目录中的隐藏文件夹.git,即为项目版本库
暂存区:使用 git add 命令,即将文件修改添加至暂存区
分支:使用 git commit 命令时,会将暂存区的文件修改提交到分支上。
创建版本库
$ git init
Initialized empty Git repository in /Users/guopanpan/panpan/gitTest/.git/
- 添加单个文件
$ git add text.txt
- 添加全部文件
$ git add .
$ 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: text.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff HEAD -- text.txt
diff --git a/text.txt b/text.txt
index 3e2a83e..b6f098b 100644
--- a/text.txt
+++ b/text.txt
@@ -1,2 +1,4 @@
git first commit
-git second commit
\ No newline at end of file
+git second commit
+git third commit
+git forth commit
\ No newline at end of file
git commit -m 'first commit '
[master (root-commit) 9be9026] first commit
1 file changed, 1 insertion(+)
create mode 100644 text.txt
版本回退
$ git log
commit 9605cfe3cfe0bcb2a17a0b95b6d6e7ea2744eb53 (HEAD -> master)
Author: panpan <183********@163.com>
Date: Mon Sep 7 14:18:21 2020 +0800
third commit
commit a5e8661b8bd47c8c062d1624caaab5c03b10185e
Author: panpan <183********@163.com>
Date: Mon Sep 7 14:18:02 2020 +0800
second commit
commit 9be9026aaee60232f043edc82bee8ee33e55bc0b
Author: panpan <183********@163.com>
Date: Mon Sep 7 14:14:23 2020 +0800
first commit
git log --pretty=oneline
9605cfe3cfe0bcb2a17a0b95b6d6e7ea2744eb53 (HEAD -> master) third commit
a5e8661b8bd47c8c062d1624caaab5c03b10185e second commit
9be9026aaee60232f043edc82bee8ee33e55bc0b first commit
head 表示当前版本,上一个版本就是HEAD^ ,上上一个版本就是HEAD^^ , 往上100个版本可以写作HEAD~100
$ git reset --hard head^
HEAD is now at a5e8661 second commit
git reflog
a5e8661 (HEAD -> master) HEAD@{0}: reset: moving to head^
9605cfe HEAD@{1}: commit: third commit
a5e8661 (HEAD -> master) HEAD@{2}: commit: second commit
9be9026 HEAD@{3}: commit (initial): first commit
撤销操作
$ git checkout -- text.txt
当text.txt自修改后还没有被放到暂存区时,撤销修改就回到和版本库一模一样的状态;
当text.txt已经添加到暂存区后,又作了修改时,撤销修改就回到添加到暂存区后的状态。
git checkout – file命令中的–很重要,没有–,就变成了“切换到另一个分支”的命令
$ git reset head test.txt
删除操作(从版本库中删除文件,不可被恢复)
$ git rm test.txt
rm 'test.txt'
$ git commit -m '删除test'
[master f6a6a00] 删除test
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
手动删除文件
git add 命令将改动添加到暂存区
git commit 命令将改动提交至版本库
添加远程库
$ git remote add origin https://github.com/yanaheng/gittest.git
$ git push -u origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 524 bytes | 524.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/yanaheng/gittest.git
da8f1ed..5aeac07 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
删除远程仓库
$ git remote -v
origin https://github.com/yangzongzhuan/RuoYi-Vue3.git (fetch)
origin https://github.com/yangzongzhuan/RuoYi-Vue3.git (push)
$ git remote rm origin
$ git remote -v
执行完再查看,发现当前项目已经没有远程仓库啦
分支操作
$ git branch dev
$ git checkout dev
Switched to branch 'dev'
$ git checkout -b dev
Switched to a new branch 'dev'
$ git branch
* dev
master
$ git branch -a
all
* dev
master
remotes/origin/master
$ git merge dev
Updating 5aeac07..d4d5f25
Fast-forward
text.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
$ git branch -d dev
Deleted branch dev (was d4d5f25).
把当前工作现场“储藏”起来,等以后恢复现场后继续工作
$ git stash
Saved working directory and index state WIP on dev: f52c633 add merge
查看stash中存储的工作现场
$ git stash list
stash@{0}: WIP on master: d4d5f25 dev first commit
恢复工作现场,同时删除stash内容
$ git stash pop
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: text.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f48898001e731d497a840a13454596d282389a38)
恢复指定stash
$ git stash apply stash@{0}
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: text.txt
no changes added to commit (use "git add" and/or "git commit -a")
删除stash内容
$ git stash drop
Dropped refs/stash@{0} (d6fca07ba8408c5a328e07d98874487196a93c89)
复制一个特定的提交到当前分支
$ git cherry-pick 4c805e2
[master 1d4b803] fix bug 101
1 file changed, 1 insertion(+), 1 deletion(-)
标签管理
直接创建, 打在当前分支的最新提交的commit上
$ git tag v1.0
按commit id 打标签
$ git tag v0.9 f52c633
带有说明的标签,用-a指定标签名,-m指定说明文字
$ git tag -a v0.1 -m "version 0.1 released" 1094adb
$ git tag
v0.9
v1.0
$ git show v0.9
commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:56:54 2018 +0800
add merge
diff --git a/readme.txt b/readme.txt
$ git tag -d v0.1
Deleted tag 'v0.1' (was f15b0dd)
$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
* [new tag] v1.0 -> v1.0
$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
* [new tag] v0.9 -> v0.9
先删除本地标签,再推送到远程
$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)
$ git push origin :refs/tags/v0.9
To github.com:michaelliao/learngit.git
- [deleted] v0.9
推荐阅读:
Git 底层数据结构和原理
Gitflow 工作流程