git 学习手册

工作区和暂存区

工作区:项目目录即为工作区

版本库:项目目录中的隐藏文件夹.git,即为项目版本库

暂存区:使用 git add 命令,即将文件修改添加至暂存区

分支:使用 git commit 命令时,会将暂存区的文件修改提交到分支上。

创建版本库

  1. 初始化git仓库
$ git init
Initialized empty Git repository in /Users/guopanpan/panpan/gitTest/.git/
  1. 把文件修改添加到暂存区
  • 添加单个文件
$ git add text.txt
  • 添加全部文件
$ git add .
  1. 查看文件状态
$ 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")
  1. 查看文件在工作区和版本库里面最新版本的区别
$ 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
  1. 把暂存区的所有内容提交到版本库
git commit -m 'first commit '
[master (root-commit) 9be9026] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 text.txt

版本回退

  1. 查看提交历史
$ 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
  1. 回退版本

head 表示当前版本,上一个版本就是HEAD^ ,上上一个版本就是HEAD^^ , 往上100个版本可以写作HEAD~100

$ git reset --hard head^
HEAD is now at a5e8661 second commit
  1. 查看命令历史
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

撤销操作

  1. 撤销文件在工作区的修改(其实是用版本库里的版本替换工作区的版本)
$ git checkout -- text.txt

当text.txt自修改后还没有被放到暂存区时,撤销修改就回到和版本库一模一样的状态;
当text.txt已经添加到暂存区后,又作了修改时,撤销修改就回到添加到暂存区后的状态。

git checkout – file命令中的–很重要,没有–,就变成了“切换到另一个分支”的命令

  1. 撤销文件在暂存区的修改,重新放回工作区
$ git reset head test.txt

删除操作(从版本库中删除文件,不可被恢复)

  1. 方法一
$ git rm test.txt
rm 'test.txt'
$ git commit -m '删除test'
[master f6a6a00] 删除test
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
  1. 方法二

手动删除文件
git add 命令将改动添加到暂存区
git commit 命令将改动提交至版本库

添加远程库

  1. 新建远程仓库
  2. 将本地仓库与远程仓库关联
$ git remote add origin https://github.com/yanaheng/gittest.git
  1. 将本地库的所有内容推送到远程仓库
$ 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'.

删除远程仓库

  1. 查询项目当前远程仓库
$ git remote -v
origin  https://github.com/yangzongzhuan/RuoYi-Vue3.git (fetch)
origin  https://github.com/yangzongzhuan/RuoYi-Vue3.git (push)
  1. 删除仓库origin
$ git remote rm origin 
$ git remote -v

执行完再查看,发现当前项目已经没有远程仓库啦

分支操作

  1. 创建分支
$ git branch dev
  1. 切换分支
$ git checkout dev
Switched to branch 'dev'
  1. 创建并切换分支
$ git checkout -b dev
Switched to a new branch 'dev'
  1. 查看本地分支
$ git branch
* dev
  master
  1. 查看所有分支
$ git branch -a
  all
* dev
  master
  remotes/origin/master
  1. 将dev分支合并到master
$ git merge dev
Updating 5aeac07..d4d5f25
Fast-forward
 text.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  1. 删除分支
$ git branch -d dev
Deleted branch dev (was d4d5f25).
  1. stash功能

把当前工作现场“储藏”起来,等以后恢复现场后继续工作

$ 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(-)

标签管理

  1. 创建标签

直接创建, 打在当前分支的最新提交的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
  1. 查看标签
$ git tag
v0.9
v1.0
  1. 查看标签信息
$ 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
  1. 删除本地标签
$ git tag -d v0.1
Deleted tag 'v0.1' (was f15b0dd)
  1. 推送某个标签到远程
$ 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
  1. 推送所有标签到远程
$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
 * [new tag]         v0.9 -> v0.9
  1. 删除已推送到远程的标签

先删除本地标签,再推送到远程

$ 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 工作流程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值