美文网首页Git使用GitGit
课程一:操纵提交历史

课程一:操纵提交历史

作者: HsuJin | 来源:发表于2017-12-20 17:26 被阅读19次
    Lesson One: Navigating a Commit History
    • Discover what makes Git a great version control system for programmers.
    • Get practice using Git to view the history of an existing project.
    • See all the saved versions, checkout a previous version, and compare two different versions.

    课程一主要介绍了 Git 环境的搭建和 Git 版本控制系统的一些操作。


    Git 环境的搭建
    1. Git 的安装

    点击链接,按照说明选择 Windows 或 Mac 系统下载安装,完成后在 Windows 中打开 Git Bash 或在 Mac 中打开 Terminal 输入 git --version 来验证安装是否成功。

    Git 的基础知识
    Git Bash 的默认路径为系统当前用户的文件夹,如在 Windows 中 C:\Users\hsujin,在 Mac 中 /Users/hsujin,默认路径下有 .bash_profile(Windows & Mac)或
    .bashrc(Linux)、.bash_history.gitconfig 等文件。
    这些文件通常是隐藏的,在 Mac 中打开 Finder 并转到 Applications 页面,键入 Command+Shift+H 即可进入用户主页,键入Command+Shift+. 即可切换是否可见隐藏文件和文件夹。

    1. 设置从 Git Bash 或 Terminal 中启动文本编辑器
    • 在 Windows 中设置从 Git Bash 中启动 Notepad++

    (1)在 Git Bash 输入

    git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
    

    注意路径用 / 分隔,而不是 \ ,路径中的空格和括号无需添加转义字符 \ 。

    (2)在 .bash_profile 中添加 Notepad++ 的快捷方式

    alias notep="C:/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"
    

    注意路径用 / 分隔,而不是 \ ,路径中的空格和括号需要添加转义字符 \ 。

    (3)重启 Git Bash 后生效,输入 notep .bash_profile 即可在 Notepad++ 中打开文件 .bash_profile

    • 在 Mac 中设置从 Terminal 中启动 Sublime Text

    (1)在 Terminal 中输入

    git config --global core.editor "'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' -n -w"
    

    注意路径用 / 分隔,而不是 \ ,路径中的空格和括号无需添加转义字符 \ 。

    (2)在 .bash_profile 中添加 Sublime Text 的快捷方式

    alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
    

    注意路径用 / 分隔,而不是 \ ,路径中的空格和括号需要添加转义字符 \ 。

    (3)重启 Terminal 后生效,输入 subl .bash_profile 即可在 Sublime Text 中打开文件 .bash_profile

    1. Git 的一些使用优化

    (1)更改颜色:可以在选项中更改 Git Bash 的背景色,文字颜色等。

    在 Git Bash 输入 git config --global color.ui auto 可以设置彩色的文字输出,例如彩色的 diff 输出。

    (2)将课程提供的两个文件放到默认路径下,其中 git-completion.bash 提供了自动完成功能,git-prompt.sh 提供了提示符中的 Git 功能。在 Git Bash 或 Terminal 输入

    cd ~
    mv Downloads/git-completion.bash.txt git-completion.bash
    mv Downloads/git-prompt.sh.txt git-prompt.sh
    

    这里使用了 mv (移动)指令,以及修改了文件的后缀。

    (3)将课程提供的 .bash_profile 文件内容加入到已有文件中,添加的内容为
    a) 加载了刚刚加入的两个文件功能;
    b) 添加了内容变化的星号(*)提示;
    c) 定义了 Git Bash 的文字颜色(用户名紫色、checkout内容及其他 git 内容绿色、目录及 $ 蓝色、其他颜色默认)。

    (4)在 Git Bash 或 Terminal 输入课程提供的两条指令,在后续课程中会有用。

    git config --global push.default upstream
    git config --global merge.conflictstyle diff3`
    
    Git 版本控制系统
    Git 概念导图

    Part One
    Git 是一种版本控制系统(VCS, Version Control System),VCS 可以保存项目开发过程的所有版本,可用于还原先前版本,比较不同版本;同时 VCS 还能使项目很方便与他人协作;另外利用版本控制系统,可以让开发者更大胆地去探索和尝试修改和改进项目,思考项目中更深层次的架构概念,而不用担心毁掉整个项目。
    Git 属于 CVS(Concurrent Version System,并行版本系统)的一种,SVN (Subversion) 又是在 CVS 的基础改进而成的。

    Part Two
    Commit 手动提交是 Git 的一个基本构件 (part-of) ,每个提交代表一个时间点的内容版本,每次提交都需要提供提交信息,这样就能通过在 Git Bash 或 Terminal 中输入 git log (operates-on) 查看所有 commits 信息,包括 ID 、作者、时间、提交信息;输入 git log --stat 可以查看所有 commits 的统计信息,包括一次提交修改的文件数量、增加和删除数量。
    另外,在 Git Bash 或 Terminal 中输入 git diff old_commit_id new_commit_id (operates-on) 可以对比两次提交之间的差异(在 Windows 自带的 cmd 命令提示符 (Not Powershell) 中使用 FC (File Compare) 功能也能实现两个文件的对比),这里要求文件使用短行(一般一行不超过 80~120 个字符),否则只能显示两个文件不一样,而不是显示出详细的差异。
    log 和 diff 的退出键为 q 键。

    Part Three
    Repository 代码库是 Git 的文件集合概念 (part-of) ,通过 Repository 可以实现多文件提交 (Tracking Access Files) ,通过 git clone 可以克隆整个 Repository ,实现所有历史版本的代码拷贝,通过 git checkout 可以让 Repository 恢复之前的版本,注意 Git 的 checkout 概念与 SVN 的不同,执行 git checkout 可以让所有文件恢复到所选 commit 的状态 (Revert controls) ,用于 Debug 等。

    本课学习到的五个 Git 指令

    1.  git log
    2.  git log --stat
    3.  git diff old_commit_id new_commit_id
    4.  git clone URL
    5.  git checkout commit_id
    

    相关文章

      网友评论

        本文标题:课程一:操纵提交历史

        本文链接:https://www.haomeiwen.com/subject/meygwxtx.html