配置

  1. # 配置清单
  2. git config -l
  3. # 系统配置
  4. git config --system -l
  5. # 用户配置
  6. git config --global -l

环境变量只是为了在全局使用应用

git理论

git本地有三个工作区

  • 工作目录(Working Directory)
    • 平时存放代码的地方
  • 暂存区(Stage/Index)
    • 用于临时存放改动,本质上是一个文件,保存即将提交的文件列表信息
  • 资源库(Repository或Git Directory)
    • 提交的所有版本数据,其中HEAD指向最新放入仓库的版本
  • ps:远程仓库(Remote Directory)
    • 托管代码的服务器

Git - 图1

文件操作

文件状态

  1. # 查看状态
  2. git status
  3. # 版本控制忽略文件 .gitignore
  • Untracked:未跟踪

    1. # 添加到暂存区,状态流转为Stage
    2. git add [filename]
  • Unmodify:未修改

    1. # 修改文件后,状态流转为Modified
    2. git rm [filename]
    3. # 移出版本库,状态流转为Untracked
  • Modified:文件已修改

    1. # 修改后再次add,状态流转为Stage
    2. git add [filename]
    3. # 丢弃修改,状态流转为Unmodify
    4. git checkout [filename]
  • Staged:暂存状态

    1. # 提交到本地仓库,状态流转为Unmodify
    2. git commit
    3. # 取消暂存,状态流转为Modified
    4. git reset HEAD [filename]

    git流转.png

    git分支

    ```bash

    查看分支列表

    git branch

删除分支

git branch -d [branchname]

强制删除分支

git branch -D [branchname]

新建分支

git branch [branchname]

更换分支

git checkout [branchname] git switch [branchname]

新建并更换分支

git branch -b [branchname] git switch -c [branchname]

合并代码

git merge [branchname]

  1. <a name="w5U1D"></a>
  2. ### Other
  3. <a name="hYmRD"></a>
  4. #### 检查/回退
  5. ```bash
  6. # 检查状态
  7. git status
  8. # 查看修改
  9. git diff

每当觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。

  1. # 查看历史记录
  2. git log
  3. # 查看历史记录(一行展示)
  4. git log --pretty=oneline
  5. # 回退版本
  6. git reset --hard HEAD^
  7. # HEAD^上一个版本,HEAD^^上两个版本,HEAD~100回退一百个版本
  8. # 可回到"未来"某个版本
  9. git reset --hard [commit_id(版本号)]

版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了

  1. # 记录了每一次命令
  2. git reflog

暂存区和工作区

Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

image.png

  1. # 一旦提交后,如果又没有对工作区做任何修改,那么工作区就是“干净”的
  2. $ git status
  3. On branch master
  4. nothing to commit, working tree clean

image.png

撤销

git commit —amend

此命令会重新提交,且保存后覆盖原有提交信息 例如:你提交后发现忘记了暂存某些需要的修改,可以像下面这样操作

  1. git commit -m 'initial commit'
  2. git add forgotten_file
  3. git commit --amend

取消对文件的修改/取消暂存的文件
  1. # 文件在工作区的修改全部撤销
  2. git checkout -- <filename>
  3. # 一种是<filename>自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态
  4. # 一种是<filename>已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态
  5. # 就是让这个文件回到最近一次git commit或git add时的状态
  6. # 可以把暂存区的修改撤销(unstage)
  7. git reset HEAD <filename>
  8. # git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区
  9. # 当我们用HEAD时,表示最新的版本

git checkout — file命令中的—很重要,没有—,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令

  1. # 放弃工作目录中的更改
  2. git restore <file>
  3. # 撤销暂存区的修改(unstage)
  4. git restore --staged <file>

分支管理

HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支。

git merge命令用于合并指定分支到当前分支。

  1. # 不使用Fast forward模式合并分支,就会在merge时生成一个新的commit
  2. # 这样,从分支历史上就可以看出分支信息
  3. git merge --no-ff -m <message> <branchname>

暂存工作区
  1. # 暂时"储藏"工作区
  2. git stash
  3. # 查看"储藏"列表
  4. git stash list
  5. # 恢复"储藏"
  6. git stash apply
  7. # 删除"储藏"
  8. git stash drop
  9. # 恢复并删除"储藏"
  10. git stash pop
  11. # 恢复指定stash
  12. git stash apply stash@{0}
  13. # 删除指定stash
  14. git stash drop stash@{0}
  15. # 将指定提交复制到当前分支
  16. git cherry-pick <commit>

标签管理

标签也是版本库的一个快照。

  1. # 打标签
  2. git tag <name>
  3. # 查看标签
  4. git tag
  5. # 指定commit打标签
  6. git tag <name> <commit_id,HASH字符串>
  7. # 查看提交的简略信息,每条数据单行呈现
  8. git log --pretty=oneline --abbrev-commit
  9. # 指定查看标签信息
  10. git show <tagname>
  11. # 创建带有说明的标签
  12. git tag -a <name> -m <message> <commit_id>
  13. # 删除标签
  14. git tag -d <tagname>
  15. # 推送标签到远程仓库
  16. git push origin <tagname>
  17. # 推送全部未推送的本地标签
  18. git push origin --tags
  19. # 删除远程标签,先删除本地标签
  20. git tag -d <tagname>
  21. git push origin :ref/tags/<tagname>

注意:标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。

git删除特定历史commit

  1. $ git reflog
  2. f8d9c75 (HEAD -> master) HEAD@{0}: commit: third commit
  3. 29dd461 HEAD@{1}: commit: second commit
  4. c470f1a HEAD@{2}: commit (initial): first commit
  1. 找到需要删除的commit之前一条提交记录

    例:需要删除 29dd461 HEAD@{1}: commit: second commit

  1. git rebase -i c470f1a
  1. 执行上述代码 ```bash pick 29dd461 second commit pick f8d9c75 third commit

Rebase c470f1a..f8d9c75 onto c470f1a (2 commands)

#

Commands:

p, pick = use commit

r, reword = use commit, but edit the commit message

e, edit = use commit, but stop for amending

s, squash = use commit, but meld into previous commit

f, fixup = like “squash”, but discard this commit’s log message

x, exec = run command (the rest of the line) using shell

b, break = stop here (continue rebase later with ‘git rebase —continue’)

d, drop = remove commit

l, label

t, reset

m, merge [-C | -c ]

. create a merge commit using the original merge commit’s

. message (or the oneline, if no original merge commit was

. specified). Use -c to reword the commit message.

#

These lines can be re-ordered; they are executed from top to bottom.

#

If you remove a line here THAT COMMIT WILL BE LOST.

#

However, if you remove everything, the rebase will be aborted.

#

  1. 3. 根据提示修改**pick**
  2. > "i":进入输入模式-- INSERT --
  3. > "Esc":回到命令模式
  4. > "**:**":进入编辑模式
  5. ```bash
  6. # 输入模式下修改pick->drop
  7. drop 29dd461 second commit
  8. pick f8d9c75 third commit
  9. # d, drop <commit> = remove commit
  10. # 退回命令模式
  11. # 进入编辑模式wq保存并退出
  12. :wq
  1. git log查看提交记录 ```bash $ git log commit e61c550ce7ee66244c21ae8ca8b930d79ce8b7e7 (HEAD -> master) Author: * Date: Wed Jun 22 17:23:02 2022 +0800

    third commit

commit c470f1a150a3ac00a7de2cf2ce5450e9fa3ae762 Author: * Date: Wed Jun 22 17:21:55 2022 +0800

  1. first commit

```