【Git】-- Git基本操作 文章目录2. Git基本操作2.1 创建本地仓库2.2 配置本地仓库方式1单独设置方式2全局设置2.3 Git工作原理2.4 文件操作2.4.1 添加文件2.4.2 修改文件查看当前仓库的状态查看当前工作区和暂存区的具体差异2.4.3 删除文件方式一方式二2.5 .git文件2.5.1 查看git目录2.5.2 git对象2.5.3 查看指针指向的分支2.5.4 查看分支2.5.5 查看commit id2.5.6 查看tree对象2.6 查看提交记录2.7 版本回退2.8 撤销修改情况一撤销还没有add的工作区中的内容情况二撤销已经add但还未commit的内容情况三撤销已经commit的内容(前提commit之后没有执行push操作)更多Git相关知识 Git专栏2. Git基本操作2.1 创建本地仓库创建一个目录。仓库需要在文件目录下进行创建。rootVM-0-3-ubuntu:~# mkdir gitcode/rootVM-0-3-ubuntu:~# cd gitcode初始化一个空的Git仓库rootVM-0-3-ubuntu:~/gitcode# git inithint: Usingmasteras the nameforthe initial branch. This default branch name hint: is subject to change. To configure the initial branch name to useinall hint: of your new repositories,whichwill suppress this warning, call: hint: hint:gitconfig--globalinit.defaultBranchnamehint: hint: Names commonly chosen instead ofmasteraremain,trunkand hint:development.The just-created branch can be renamed via this command: hint: hint:gitbranch-mnameInitialized empty Git repositoryin/root/gitcode/.git/ rootVM-0-3-ubuntu:~/gitcode# la.git此时会在当前目录下出现一个隐藏文件.git。查看.git 目录rootVM-0-3-ubuntu:~/gitcode# tree .git/.git/ ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── pre-merge-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ ├── pre-receive.sample │ ├── push-to-checkout.sample │ ├── sendemail-validate.sample │ └── update.sample ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags该git目录是追踪管理git仓库的如果对该目录进行修改的话会直接将该git仓库破坏掉。2.2 配置本地仓库方式1单独设置配置用户名和邮箱rootVM-0-3-ubuntu:~/gitcode# git config user.name cuckoorootVM-0-3-ubuntu:~/gitcode# git config user.email buxinyu163163.com查看配置rootVM-0-3-ubuntu:~/gitcode# git config -lcore.repositoryformatversion0core.filemodetruecore.barefalsecore.logallrefupdatestrueuser.namecuckoouser.emailbuxinyu163163.com删除配置rootVM-0-3-ubuntu:~/gitcode# git config --unset user.namerootVM-0-3-ubuntu:~/gitcode# git config --unset user.emailrootVM-0-3-ubuntu:~/gitcode# git config -lcore.repositoryformatversion0core.filemodetruecore.barefalsecore.logallrefupdatestrue方式2全局设置上面的设置方式是只在当前的本地仓库上生效。但是在一台服务器上可以创建多个本地仓库加上--global选项表示配置在当前机器上所有的本地仓库上。rootVM-0-3-ubuntu:~/gitcode# git config --global user.name cuckoorootVM-0-3-ubuntu:~/gitcode# git config --global user.email buxinyu163163.comrootVM-0-3-ubuntu:~/gitcode# git config -luser.namecuckoouser.emailbuxinyu163163.comcore.repositoryformatversion0core.filemodetruecore.barefalsecore.logallrefupdatestrue同样删除配置时也是只需要加上--global选项即可。2.3 Git工作原理工作区电脑上文件的目录。暂存区/索引 和 版本库 中存放的不是具体的内容而是一个个git对象的索引。每add一次在工作区中修改的内容会写入对象库中一个新的git对象中Git会为每个被修改/新添加的文件创建一个blob对象如果文件内容没有变化就会重用已有的blob对象同时暂存区中目录树的文件索引会被更新。git追踪管理的是修改并不是文件。在创建Git版本库时Git会自动创建一个唯一的master分支HEAD指针指向master分支。当执行commit命令时HEAD指向的分支会做出相应的更新。此时暂存区中的目录树才被真正写到版本库中。2.4 文件操作2.4.1 添加文件创建一个文件rootVM-0-3-ubuntu:~/gitcode# touch ReadMerootVM-0-3-ubuntu:~/gitcode# la.git ReadMe在文件中写入内容rootVM-0-3-ubuntu:~/gitcode# vim ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogit提交到暂存区中git add .表示将工作区中所有修改的文件提交到暂存区中。git add ReadMe表示指定ReadMe文件添加到暂存区中。如果想要一次指定多个文件进行add用空格隔开文件名即可。rootVM-0-3-ubuntu:~/gitcode# touch file1 file2 file3rootVM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# git add .rootVM-0-3-ubuntu:~/gitcode# git commit -m 新增3个文件[master 46fcecf]新增3个文件3files changed,0insertions(),0deletions(-)create mode100644file1 create mode100644file2 create mode100644file3提交到版本库中rootVM-0-3-ubuntu:~/gitcode# git commit -m 新增ReadMe文件[master(root-commit)77fde10]新增ReadMe文件1filechanged,1insertion()create mode100644ReadMe需要注意的是执行commit命令时只会将暂存区中的内容提交到版本库中工作区中未被add的内容不会被提交到版本库中。2.4.2 修改文件修改rootVM-0-3-ubuntu:~/gitcode# vim ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world查看当前仓库的状态rootVM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes not stagedforcommit:(usegit add file...to update what will be committed)(usegit restore file...to discard changesinworking directory)modified: ReadMe no changes added to commit(usegit addand/orgit commit -a)只记录哪些文件被修改了并不记录修改了哪些内容查看当前工作区和暂存区的具体差异rootVM-0-3-ubuntu:~/gitcode# git diffdiff--gita/ReadMe b/ReadMe index 8d0e412..05fe86c100644--- a/ReadMe b/ReadMe -11,2 hellogithello world提交到版本库中rootVM-0-3-ubuntu:~/gitcode# git add .rootVM-0-3-ubuntu:~/gitcode# git commit -m 修改ReadMe文件[master 97d8589]修改ReadMe文件1filechanged,1insertion()2.4.3 删除文件方式一rootVM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# rm file1rootVM-0-3-ubuntu:~/gitcode# lafile2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# git add .rootVM-0-3-ubuntu:~/gitcode# git commit -m delete file1[master 07344ec]delete file11filechanged,0insertions(),0deletions(-)delete mode100644file1方式二rootVM-0-3-ubuntu:~/gitcode# lafile2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# git rm file2rmfile2rootVM-0-3-ubuntu:~/gitcode# lafile3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(usegit restore --staged file...to unstage)deleted: file2git rm命令会直接删除工作区和暂存区中的file2。只需要再执行依次commit操作即可。rootVM-0-3-ubuntu:~/gitcode# git commit -m delete file2[master 5a01a0a]delete file21filechanged,0insertions(),0deletions(-)delete mode100644file2 rootVM-0-3-ubuntu:~/gitcode# git statusOn branch master nothing to commit, working tree clean2.5 .git文件2.5.1 查看git目录rootVM-0-3-ubuntu:~/gitcode# tree .git.git ├── branches ├── COMMIT_EDITMSG ├── config ├── description ├── HEAD ├── hooks │?? ├── applypatch-msg.sample │?? ├── commit-msg.sample │?? ├── fsmonitor-watchman.sample │?? ├── post-update.sample │?? ├── pre-applypatch.sample │?? ├── pre-commit.sample │?? ├── pre-merge-commit.sample │?? ├── prepare-commit-msg.sample │?? ├── pre-push.sample │?? ├── pre-rebase.sample │?? ├── pre-receive.sample │?? ├── push-to-checkout.sample │?? ├── sendemail-validate.sample │?? └── update.sample ├── index ├── info │?? └── exclude ├── logs │?? ├── HEAD │?? └── refs │?? └── heads │?? └── master ├── objects │?? ├── 0e │?? │?? └── 6b1780b73cd9220ec5073dc64b42f7ad4bd945 │?? ├──15│?? │?? └── a37e9ef171cca4a5d985fccd1fcf9414b2c7cf │?? ├──46│?? │?? └── fcecf10dd0b16e4b0a219b60c0f65aead13977 │?? ├──77│?? │?? └── fde109374766c4a3bcf52f069c16f05d9f0532 │?? ├── 8d │?? │?? └── 0e41234f24b6da002d962a26c2495ea16a425f │?? ├── e6 │?? │?? └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 │?? ├── info │?? └── pack └── refs ├── heads │?? └── master └── tags19directories,29files2.5.2 git对象git对象分为四种1. commit 对象2. blob对象3. tree对象4. tag对象2.5.3 查看指针指向的分支rootVM-0-3-ubuntu:~/gitcode# cat .git/HEADref: refs/heads/master2.5.4 查看分支master指向的就是最新的一次提交的commit id .rootVM-0-3-ubuntu:~/gitcode# cat .git/refs/heads/master46fcecf10dd0b16e4b0a219b60c0f65aead139772.5.5 查看commit idgitcat-file-pcommitId2.5.6 查看tree对象rootVM-0-3-ubuntu:~/gitcode# git cat-file -p 15a37e9ef171cca4a5d985fccd1fcf9414b2c7cf100644blob 8d0e41234f24b6da002d962a26c2495ea16a425f ReadMe100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file1100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file2100644blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file3 rootVM-0-3-ubuntu:~/gitcode# git cat-file -p 8d0e41234f24b6da002d962a26c2495ea16a425fhellogitcommitID指向的是一个tree对象的引用tree对象代表的是目录结构指向的是blob对象文件内容和其他的tree对象子目录。2.6 查看提交记录rootVM-0-3-ubuntu:~/gitcode# git logcommit 46fcecf10dd0b16e4b0a219b60c0f65aead13977(HEAD -master)Author: cuckoobuxinyu163163.comDate: Sat Jan1015:02:5620260800 新增3个文件 commit 77fde109374766c4a3bcf52f069c16f05d9f0532 Author: cuckoobuxinyu163163.comDate: Sat Jan1014:45:3820260800 新增ReadMe文件更加美观的打印方式rootVM-0-3-ubuntu:~/gitcode# git log --prettyoneline46fcecf10dd0b16e4b0a219b60c0f65aead13977(HEAD -master)新增3个文件 77fde109374766c4a3bcf52f069c16f05d9f0532 新增ReadMe文件2.7 版本回退版本回退其实git只需要修改对应分支指向的commit ID即可。版本回退reset命令的三个选项1.--soft回退版本库中的内容工作区和暂存区中的内容不进行回退。2.--mixed回退版本库和暂存区中的内容工作区中的内容不进行回退。3.--hard回退工作区、暂存区、版本库中的内容。默认是--mixed选项。这是我们现在的版本rootVM-0-3-ubuntu:~/gitcode# git log --prettyoneline97d8589fc0a517644a879826e165ee247020a9ce(HEAD -master)修改ReadMe文件 46fcecf10dd0b16e4b0a219b60c0f65aead13977 新增3个文件 77fde109374766c4a3bcf52f069c16f05d9f0532 新增ReadMe文件 rootVM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world回退到上一个版本rootVM-0-3-ubuntu:~/gitcode# git reset --hard 46fcecf10dd0b16e4b0a219b60c0f65aead13977HEAD is now at 46fcecf 新增3个文件 rootVM-0-3-ubuntu:~/gitcode# lafile1 file2 file3 .git ReadMe rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogit只要知道对应版本的commit id就可以实现回退。2.8 撤销修改情况一撤销还没有add的工作区中的内容修改前rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world修改后rootVM-0-3-ubuntu:~/gitcode# vim ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo撤销修改后rootVM-0-3-ubuntu:~/gitcode# git checkout -- ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world情况二撤销已经add但还未commit的内容修改前rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world修改后rootVM-0-3-ubuntu:~/gitcode# vim ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckooadd后rootVM-0-3-ubuntu:~/gitcode# git add ReadMerootVM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(usegit restore --staged file...to unstage)modified: ReadMe撤销修改后使用--hard可以使工作区、暂存区都回退到版本库的当前版本。rootVM-0-3-ubuntu:~/gitcode# git reset --hard HEADHEAD is now at 97d8589 修改ReadMe文件 rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world使用--mixed可以使暂存区中的内容回退到版本库的当前版本就变成了情况1。rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckoo rootVM-0-3-ubuntu:~/gitcode# git add ReadMerootVM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes to be committed:(usegit restore --staged file...to unstage)modified: ReadMe rootVM-0-3-ubuntu:~/gitcode# git reset --mixed HEAD ReadMewarning:--mixedwith paths is deprecated;usegit reset -- pathsinstead. Unstaged changes after reset: M ReadMe rootVM-0-3-ubuntu:~/gitcode# git statusOn branch master Changes not stagedforcommit:(usegit add file...to update what will be committed)(usegit restore file...to discard changesinworking directory)modified: ReadMe no changes added to commit(usegit addand/orgit commit -a)rootVM-0-3-ubuntu:~/gitcode# git checkout -- ReadMerootVM-0-3-ubuntu:~/gitcode# git statusOn branch master nothing to commit, working tree clean情况三撤销已经commit的内容(前提commit之后没有执行push操作)修改前rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world修改后rootVM-0-3-ubuntu:~/gitcode# vim ReadMerootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world hello cuckooadd commit 后rootVM-0-3-ubuntu:~/gitcode# git add ReadMerootVM-0-3-ubuntu:~/gitcode# git commit -m 修改ReadMe文件[master7963664]修改ReadMe文件1filechanged,1insertion()撤销后rootVM-0-3-ubuntu:~/gitcode# git reset --hard HEAD^HEAD is now at 97d8589 修改ReadMe文件 rootVM-0-3-ubuntu:~/gitcode# cat ReadMehellogithello world几个^就表示回退到前几个版本