• Git学习笔记2


    1. yum install -y git
    2. git --version
    3. [root@git-server ~]# git --help
    4. usage: git [--version] [--help] [-c name=value]
    5. [--exec-path[=]] [--html-path] [--man-path] [--info-path]
    6. [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
    7. [--git-dir=] [--work-tree=] [--namespace=]
    8. <command> []
    9. The most commonly used git commands are:
    10. add Add file contents to the index
    11. bisect Find by binary search the change that introduced a bug
    12. branch List, create, or delete branches
    13. checkout Checkout a branch or paths to the working tree
    14. clone Clone a repository into a new directory
    15. commit Record changes to the repository
    16. diff Show changes between commits, commit and working tree, etc
    17. fetch Download objects and refs from another repository
    18. grep Print lines matching a pattern
    19. init Create an empty Git repository or reinitialize an existing one
    20. log Show commit logs
    21. merge Join two or more development histories together
    22. mv Move or rename a file, a directory, or a symlink
    23. pull Fetch from and merge with another repository or a local branch
    24. push Update remote refs along with associated objects
    25. rebase Forward-port local commits to the updated upstream head
    26. reset Reset current HEAD to the specified state
    27. rm Remove files from the working tree and from the index
    28. show Show various types of objects
    29. status Show the working tree status
    30. tag Create, list, delete or verify a tag object signed with GPG
    31. 'git help -a' and 'git help -g' lists available subcommands and some
    32. concept guides. See 'git help ' or 'git help '
    33. to read about a specific subcommand or concept.

    git 应用:

    git身份设置:

    因为git是分布式版本控制系统,不同的人提交的代码需要分区,所以每个人都要设置一个身份标志。如果不设置的话,谁会知道你是谁呢?

    1. [root@git-server ~]# git config --global user.name "changchunhua"
    2. [root@git-server ~]# git config --global user.email "chang_chunhua@qq.com"
    3. [root@git-server ~]# git config --global color.ui true
    4. [root@git-server ~]#
    5. [root@git-server ~]# git config --list
    6. user.name=changchunhua
    7. user.email=chang_chunhua@qq.com
    8. color.ui=true

    创建本地仓库:

    工作目录,work directory,也叫工作区,是存放项目代码文件的一个目录。

    仓库(repository),也叫版本库,在git init命令初始化工作目录后会产生一个隐藏的子目录.git,可以将其理解为git的仓库或版本库。

    仓库分为本地仓库和远程仓库。

    创建本地仓库的步骤:

    1)创建工作目录:

    1. [root@git-server GitTest]# ls .git
    2. branches config description HEAD hooks info objects refs

    小结:mkdir创建一个目录后,然后cd进取,使用git init命令就创建好了本地仓库,开发者就可以在工作目录里开发项目代码文件了。

    暂存区:

    暂存区 index、stage,也叫缓存区。

    暂存区就看住是一个缓区区域,临时保存你的改动。

    如果在工作目录中创建了一个新文件,需要将新文件添加到暂存区。

    添加文件到暂存区:

    1、准备一个文件:

    1. [root@git-server GitTest]# cat 1.py
    2. print("hello world")
    3. [root@git-server GitTest]# git add 1.py
    4. [root@git-server GitTest]# ls .git
    5. branches config description HEAD hooks index info objects refs
    6. [root@git-server GitTest]# strings .git/index
    7. DIRC
    8. 1.py

     

    git版本控制

    1)提交版本(第1个版本):

    代码文件commit提交后才能纳入版本控制。

    1)可以使用git status查看工作目录里有哪些文件需要提交。

    1. [root@git-server GitTest]# git status
    2. # On branch master
    3. #
    4. # Initial commit
    5. #
    6. # Changes to be committed:
    7. # (use "git rm --cached ..." to unstage)
    8. #
    9. # new file: 1.py
    10. #

    2)使用git commit -m 后提交的说明信息:

    1. [root@git-server GitTest]# git commit -m "commit 1.py"
    2. [master (root-commit) 0eb70c9] commit 1.py
    3. 1 file changed, 1 insertion(+)
    4. create mode 100644 1.py
    5. [root@git-server GitTest]# git status
    6. # On branch master
    7. nothing to commit, working directory clean

    再查看git status,就没有文件可以提交的了。

    3)修改再提交(第二个版本):

    1. [root@git-server GitTest]# vim 1.py
    2. [root@git-server GitTest]#
    3. [root@git-server GitTest]#
    4. [root@git-server GitTest]# git status
    5. # On branch master
    6. # Changes not staged for commit:
    7. # (use "git add ..." to update what will be committed)
    8. # (use "git checkout -- ..." to discard changes in working directory)
    9. #
    10. # modified: 1.py
    11. #
    12. no changes added to commit (use "git add" and/or "git commit -a")
    13. [root@git-server GitTest]# git add 1.py
    14. [root@git-server GitTest]#
    15. [root@git-server GitTest]#
    16. [root@git-server GitTest]# git commit -m "Add a new line hello linux in 1.py"
    17. [master 32150cd] Add a new line hello linux in 1.py
    18. 1 file changed, 1 insertion(+)

    小结:

    工作目录中写好的代码文件需要git add 文件名添加到暂存区,再git commit 文件名提交,以后每次修改都要重复前面的步骤。

    git status是查看工作目录中的状态;

    git diff 文件名是查看修改了什么;

    查看提交历史:

    1、使用git log查看提交的历史信息:

    1. [root@git-server GitTest]# git log
    2. commit 32150cd35a9ccd888eff433068e54d776a24f7fb
    3. Author: changchunhua
    4. Date: Tue Sep 19 10:10:24 2023 +0800
    5. Add a new line hello linux in 1.py
    6. commit 22aba45892ccd930af92ae0ca12fdc261e035979
    7. Author: changchunhua
    8. Date: Tue Sep 19 10:09:26 2023 +0800
    9. Add a new line hello python in 1.py
    10. commit 0eb70c9e0fcc649cefab4577f2ee51f2146ccaca
    11. Author: changchunhua
    12. Date: Tue Sep 19 10:05:17 2023 +0800
    13. commit 1.py

    使用git log --pretty=oneline: 

    1. [root@git-server GitTest]# git log --pretty=oneline
    2. 32150cd35a9ccd888eff433068e54d776a24f7fb Add a new line hello linux in 1.py
    3. 22aba45892ccd930af92ae0ca12fdc261e035979 Add a new line hello python in 1.py
    4. 0eb70c9e0fcc649cefab4577f2ee51f2146ccaca commit 1.py

    版本回退和还原:

    1, 使用git reset --hard HEAD^回退到上一个版本(也就是第2个版本)

    1. [root@git-server GitTest]# git reset --hard HEAD^
    2. HEAD is now at 22aba45 Add a new line hello python in 1.py
    3. [root@git-server GitTest]#
    4. [root@git-server GitTest]#
    5. [root@git-server GitTest]# cat 1.py
    6. print("hello world")
    7. print("hello python")

    2、使用git reset --hard 第三个版本号还原到第三个版本:

    1. [root@git-server GitTest]# git reflog
    2. 22aba45 HEAD@{0}: reset: moving to HEAD^
    3. 32150cd HEAD@{1}: commit: Add a new line hello linux in 1.py
    4. 22aba45 HEAD@{2}: commit: Add a new line hello python in 1.py
    5. 0eb70c9 HEAD@{3}: commit (initial): commit 1.py
    6. [root@git-server GitTest]#
    7. [root@git-server GitTest]# git reset --hard 32150cd
    8. HEAD is now at 32150cd Add a new line hello linux in 1.py
    9. [root@git-server GitTest]# cat 1.py
    10. print("hello world")
    11. print("hello python")
    12. print("hello linux")

    另外:

    回退到上上一个版本, 也就是回退两个版本,使用git reset --hard HEAD^^

    回退三个版本,使用git reset --hard HEAD^^^, 以此类推。

    如果回退100个版本,那用100个^符号不方便,可以换成git reset --hard HEAD~100

    之前,学习的时候没有特别注意这块内容。还是需要加强。

    小结:

    • 提交后的代码文件,使用git log查看当前版本及以前的历史版本。

    • 使用git reset --hard HEAD^或者git reset --hard HEAD~100实现版本回退。

    • 使用git reflog查看提交的所有操作及版本号

    • 使用git reset --hard 版本号你可以自由的在不同版本之间来回切换。

     git工作流再次理解与应用拓展:

    工作目录中任何修改或增加的文件,都要git add到暂存区,让暂存区和工作目录的状态一致,这样才能提交一个版本。

    git commit提交的是在暂存区里的所有文件状态。也就是说整个工作目录里的状态保存为一个版本,而不是某一个文件。

    git版本控制不仅仅用于项目开发,你也可以用于一个软件包仓库的版本控制。

    撤销修改:

    开发者想撤销一段代码:

    1. [root@vm1 GitTest]# cat 1.py
    2. print("hello world")
    3. print("hello python")
    4. print("hello linux")
    5. print("hey,xxx is a gay") 这是写错的代码,需要反悔

    想要撤销有以下几种方法:

    1)直接把写错的代码删除;

    2)使用git checkout -- 文件名 就可以直接撤销修改了。

    1. [root@git-server GitTest]# git checkout -- 1.py
    2. [root@git-server GitTest]#
    3. [root@git-server GitTest]#
    4. [root@git-server GitTest]# cat 1.py
    5. print("hello world")
    6. print("hello python")
    7. print("hello linux")


    3)如果写乱了代码,添加到暂存区但是还没有commit提交,使用git reset HEAD 文件名取消暂存区添加,再git checkout -- 文件名 来撤销修改。

    4)如果写乱了代码,添加到暂存区并提交了,则使用版本回退。

    误删恢复:

    1, 只要文件git add到了暂存区, 无论有没有git commit提交。误删除后都可以使用 git checkout -- 文件名来恢复。

    1. [root@git-server GitTest]# git add 2.py
    2. [root@git-server GitTest]#
    3. [root@git-server GitTest]# rm -f 2.py
    4. [root@git-server GitTest]# ls
    5. 1.py
    6. [root@git-server GitTest]# git checkout -- 2.py
    7. [root@git-server GitTest]# ll
    8. total 8
    9. -rw-r--r-- 1 root root 64 Sep 19 11:19 1.py
    10. -rw-r--r-- 1 root root 19 Sep 19 11:25 2.py

    2、如果文件没有git add 到暂存区,误删除了也就没有了。

    文件删除:

    1、没有git add到暂存区的文件,直接rm删除就可以了。

    2、git add 添加到暂存区,但是还没有git commit提交的文件,需要rm删除本地,还要git rm 文件名删除。

    1. [root@git-server GitTest]# vim 3.py
    2. [root@git-server GitTest]#
    3. [root@git-server GitTest]# git add 3.py
    4. [root@git-server GitTest]#
    5. [root@git-server GitTest]# rm -f 3.py
    6. [root@git-server GitTest]#
    7. [root@git-server GitTest]# git rm 3.py
    8. rm '3.py'

    3、git add 添加到暂存区,并且已经git commit 提交的文件,需要rm 删除本地文件,在git rm文件名删除,最后再提交删除。

    1. [root@git-server GitTest]# touch 3.py
    2. [root@git-server GitTest]# git add 3.py
    3. [root@git-server GitTest]# git commit -m "committed 3.py"
    4. [master 99a16a7] committed 3.py
    5. 2 files changed, 2 insertions(+)
    6. create mode 100644 2.py
    7. create mode 100644 3.py
    8. [root@git-server GitTest]# git status
    9. # On branch master
    10. nothing to commit, working directory clean
    11. [root@git-server GitTest]# rm 3.py -f
    12. [root@git-server GitTest]#
    13. [root@git-server GitTest]# git rm 3.py
    14. rm '3.py'
    15. [root@git-server GitTest]# git commit -m "deleted 3.py"
    16. [master 8153d25] deleted 3.py
    17. 1 file changed, 1 deletion(-)
    18. delete mode 100644 3.py

    git版本控制小结:

  • 相关阅读:
    华为云之深入探究GaussDB如何助力企业公司打造金融核心数据
    Docker Hub 国内镜像源配置
    Linux 日期、时区
    根据消息信息如何快速定位程序
    先验概率 / 后验概率 / 条件概率 / 全概率公式 / 贝叶斯公式
    ubuntu22.04开机自启动Eureka服务
    go gin 多图片上传,并返回图片地址
    福彩双色球,中巨奖到底有多难? 试试 run 《仿真模拟双色球》代码就晓得了。
    【从跳板机ssh到内网目标服务器】配置vscode实现远程连接
    系列一、介绍
  • 原文地址:https://blog.csdn.net/chang_chunhua/article/details/133013341