MIT-Missing Semester_Topic 6:Version Control (Git) 练习题

2024-02-07 02:12

本文主要是介绍MIT-Missing Semester_Topic 6:Version Control (Git) 练习题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 练习一
      • 练习二
      • 练习三
      • 练习四
      • 练习五
      • 练习六
      • 练习七

本 Topic 的 MIT 讲解网页(练习题未给解答)

练习一

若还没有 Git 的相关经验,阅读 Pro Git 的前几章或诸如 Learn Git Branching 的相关教程,并在学习的同时从 Git 的数据模型(data model)的角度思考各 Git 命令。

老师非常建议阅读 Pro Git。

练习二

clone 该课程(Missing Semester)网站的仓库,随后:

  1. 以图的形式展现其版本历史
  2. 运用 git log 加一个参数,找到修改 README.md 的最后一人
  3. 运用 git blamegit show 得到 _config.ymlcollections: 这一行最后一次修改的 commit 信息

第 1、3 小题在 lecture 中演示过,而第 2 小题据提示比较简单。

  1. git clone <URL> <folder>*(必备基础技能)*克隆课程仓库:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise$ git clone https://github.com/missing-semester/missing-semester missing-semester
    Cloning into 'missing-semester'...
    remote: Enumerating objects: 2344, done.
    remote: Counting objects: 100% (1487/1487), done.
    remote: Compressing objects: 100% (543/543), done.
    remote: Total 2344 (delta 980), reused 964 (delta 941), pack-reused 857
    Receiving objects: 100% (2344/2344), 15.61 MiB | 1.28 MiB/s, done.
    Resolving deltas: 100% (1392/1392), done.
    
  2. 不妨运用 Topic 5 所讲解的 别名 (alias) 知识,简化 git log 的相关图示法命令:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/test/demo$ alias gl='git log --all --graph --decorate'
    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/test/demo$ alias gl1='gl --oneline'
    

    **提示:**可以在配置文件 ~/.bashrc 中设定默认的 alias

    • gl:包含详细 commit 的信息

      cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ gl > gl_res.log
      cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ vim gl_res.log
      

      结果极长,共 4218 行。

    • gl1:缩略信息,每个 commit 仅一行

      *   d38f585 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'alexandr-gnrk/update-jupyter-binding'
      |\
      | * f600f32 Update Vim binding for Jupyter
      |/
      * 2253e4e Switch recommended app to Rectangle
      * 18ee93c Ignore Reddit URLs when checking links
      *   2ab1b78 Merge branch 'alkock/master'
      |\
      | * 98d90f8 added german translation
      |/
      * bd75a78 Fix broken links
      *   7330a25 Merge branch 'fipachu/git-aliases'
      |\
      | * 6fc734b Add more useful overview of Git aliases
      |/
      *   f45234f Merge branch 'Nikitaz1410/patch-2'
      ...
      

      部分较新的历史如上,共 885 行。

    **提示:**当 git log 等其它 git 命令的信息较长时会显示前一部分信息并自动进入某种 “交互” 模式,上下移动方法同 Vim,H 键显示帮助,Q 键退出。

  3. 考虑使用 Topic 2 提及的 tldr 工具查询所需用法:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ tldr git log...Show a history of commits.More information: https://git-scm.com/docs/git-log....- Show the history of a particular file or directory, including differences:git log -p path/to/file_or_directory...
    

    由此得知 git log -p + 文件/文件夹

    后来发现不应该如上加上 -p flag,其会显示所有 “differences”,直接加文件即可:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ git log README.md
    commit 9ef9db72211fefc00caaa7133b35dda4a99acccf
    Author: Anish Athalye <me@anishathalye.com>
    Date:   Thu Oct 27 20:28:41 2022 -0400Add Docker setup for easier development
    ...
    

    因此显然是授课老师 Anish Athalye 最后修改了 README.md

  4. lecture 中演示了。先 git blame + 文件并找到目标信息对应的 commit,随后 git show <commit> 得到其相关信息:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ git blame _config.yml
    ...
    a88b4eac (Anish Athalye 2020-01-17 15:26:30 -0500 18) collections:
    a88b4eac (Anish Athalye 2020-01-17 15:26:30 -0500 19)   '2019':
    ...
    

    ⇒ \Rightarrow commit 哈希值为 a88b4eac

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ git show a88b
    commit a88b4eac326483e29bdac5ee0a39b180948ae7fc
    Author: Anish Athalye <me@anishathalye.com>
    Date:   Fri Jan 17 15:26:30 2020 -0500Redo lectures as a collection
    ...
    

    所以对应 commit 的信息为 “Redo lectures as a collection”

练习三

为应对误将不应被 Git 管理的内容,如某大文件或敏感信息,commit 的情况,学习 Github 上的相关文章,随后模拟上述情形下删除目标文件。

学习文章知,可以使用两种工具(均需安装):

  • git filter-repo
  • BFG Repo-Cleaner

练习四

修改某仓库中的文件,随后依次 git stashgit log --all --onelinegit stash pop 巩固 git stash 的技能,并思考其可能适用于在何种情况。

stash vt. 藏匿; 隐藏; 存放; 贮藏(百度翻译)

lecture 中也演示过 git stash,其可 “隐藏” 所做的修改,并以 git stash pop 恢复。

  1. 修改 README.md 之后 git stash

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/test_git_stash$ echo '**REMOVED**' > README.md
    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/test_git_stash$ git status
    On branch master
    Your branch is up to date with 'origin/master'.Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   README.mdno changes added to commit (use "git add" and/or "git commit -a")
    
  2. git log

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/test_git_stash$ git log --all --oneline
    7fca419 (refs/stash) WIP on master: d38f585 Merge branch 'alexandr-gnrk/update-jupyter-binding'
    088c991 index on master: d38f585 Merge branch 'alexandr-gnrk/update-jupyter-binding'
    d38f585 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'alexandr-gnrk/update-jupyter-binding'`
    ...
    

    可以发现在 HEAD commit 之上多了两条 commit!且最后一条为 refs/stash

  3. git stash pop 恢复:

    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/test_git_stash$ git stash pop
    On branch master
    Your branch is up to date with 'origin/master'.Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   README.mdno changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (7fca419c92d8bff936036a4ca32b277d11c1849a)
    cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/test_git_stash$ git log --all --oneline | head -10
    d38f585 Merge branch 'alexandr-gnrk/update-jupyter-binding'
    ...
    

    直接跳出了 README.md 被更新的提示,且此时 git log 已无之前新增的 commit 记录。

git stash 应该在修改文件后又想看其之前的内容时非常有用。若无 git stash,恐怕只能先将文件存在别处,随后 git checkout -- <file> 丢弃修改。

练习五

在 Git 的配置文件 ~/.gitconfig 中创建 alias,以便使用 git graph 便能得到 git log --all --graph --decorate --oneline 的结果。

.gitconfig 文件中加入:

[alias]graph = log --all --graph --decorate --oneline

测试如下:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$ diff <(git graph) <(git log --all --graph --decorate --oneline)
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/6_Version Control(git)/exercise/missing-semester$

可见两者输出完全一样。

练习六

You can define global ignore patterns in ~/.gitignore_global after running git config --global core.excludesfile ~/.gitignore_global. Do this, and set up your global gitignore file to ignore OS-specific or editor-specific temporary files, like .DS_Store.

练习七

Fork the repository for the class website, find a typo or some other improvement you can make, and submit a pull request on GitHub (you may want to look at this). Please only submit PRs that are useful (don’t spam us, please!). If you can’t find an improvement to make, you can skip this exercise.
u may want to look at this). Please only submit PRs that are useful (don’t spam us, please!). If you can’t find an improvement to make, you can skip this exercise.

这篇关于MIT-Missing Semester_Topic 6:Version Control (Git) 练习题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/686233

相关文章

IDEA中Git版本回退的两种实现方案

《IDEA中Git版本回退的两种实现方案》作为开发者,代码版本回退是日常高频操作,IntelliJIDEA集成了强大的Git工具链,但面对reset和revert两种核心回退方案,许多开发者仍存在选择... 目录一、版本回退前置知识二、Reset方案:整体改写历史1、IDEA图形化操作(推荐)1.1、查看提

Git如何修改已提交人的用户名和邮箱

《Git如何修改已提交人的用户名和邮箱》文章介绍了如何修改Git已提交人的用户名和邮箱,包括注意事项和具体步骤,确保操作正确无误... 目录git修改已提交人的用户名和邮箱前言第一步第二步总结git修改已提交人的用户名和邮箱前言需注意以下两点内容:需要在顶层目录下(php就是 .git 文件夹所在的目

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push

Git中恢复已删除分支的几种方法

《Git中恢复已删除分支的几种方法》:本文主要介绍在Git中恢复已删除分支的几种方法,包括查找提交记录、恢复分支、推送恢复的分支等步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录1. 恢复本地删除的分支场景方法2. 恢复远程删除的分支场景方法3. 恢复未推送的本地删除分支场景方法4. 恢复

bat脚本启动git bash窗口,并执行命令方式

《bat脚本启动gitbash窗口,并执行命令方式》本文介绍了如何在Windows服务器上使用cmd启动jar包时出现乱码的问题,并提供了解决方法——使用GitBash窗口启动并设置编码,通过编写s... 目录一、简介二、使用说明2.1 start.BAT脚本2.2 参数说明2.3 效果总结一、简介某些情

提示:Decompiled.class file,bytecode version如何解决

《提示:Decompiled.classfile,bytecodeversion如何解决》在处理Decompiled.classfile和bytecodeversion问题时,通过修改Maven配... 目录问题原因总结问题1、提示:Decompiled .class file,China编程 bytecode

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

git ssh key相关

step1、进入.ssh文件夹   (windows下 下载git客户端)   cd ~/.ssh(windows mkdir ~/.ssh) step2、配置name和email git config --global user.name "你的名称"git config --global user.email "你的邮箱" step3、生成key ssh-keygen