Git入门 and installation

UnderstandGit basicconcepts, Learningsuch as何 in 不同operationsystem on installation and configurationGit

1. GitIntroduction

Git is a open-source distributedversion控制system, 用于agile high 效地processing任何 or small or big project. 它由Linus Torvalds于2005年 for Linux in 核Development而creation, 现 in 已成 for 世界 on 最流行 version控制system之一.

Git 主要特点including:

  • distributedDevelopment, 每个Development者都 has 完整 仓libraryreplica
  • 强 big branchmanagementfunctions, supportparallelDevelopment
  • 速度 fast , big many 数operation in 本地执行
  • dataintegrity high , usingSHA-1哈希algorithms
  • support非线性Development, flexible workflow

and 传统 集in式version控制system (such asSVN) 相比, Git distributedfeatures使它更适合团队协作 and open-sourceprojectDevelopment.

2. Gitinstallation

Gitsupport many 种operationsystem, includingWindows, macOS and Linux. under 面我们将分别介绍 in 不同system on installationGit method.

2.1 WindowssysteminstallationGit

  1. 访问Git官方网站 under 载页面: https://git-scm.com/download/win
  2. 选择适合你system installationpackage (32位 or 64位) , 点击 under 载
  3. 双击 under 载 installationpackage, 开始installation过程
  4. in installation向导in, 按照默认选项一路点击"Next"即可, or 者根据需要自定义installation选项
  5. installationcompletion after , 打开commands提示符 (CMD) or PowerShell, 输入以 under commandsverificationinstallation:
git --version

such as果installation成功, 会显示Git versioninformation, 例such as:

git version 2.40.1.windows.1

提示

in Windows on installationGit时, 建议选择"Use Git from Git Bash only"选项, 这样可以获得更 good commands行体验.

2.2 macOSsysteminstallationGit

in macOS on installationGit has many 种method:

method1: usingHomebrewinstallation

such as果你 macOS on 已经installation了Homebrew, 可以using以 under commandsinstallationGit:

brew install git

method2: usingXcode Command Line Toolsinstallation

macOSsystem自带了Xcode Command Line Tools, 其inpackage含Git. 你可以through以 under commandsinstallation:

xcode-select --install

method3: 直接 under 载installationpackage

访问Git官方网站 under 载页面: https://git-scm.com/download/mac, under 载并installationGit.

installationcompletion after , 打开终端, 输入以 under commandsverificationinstallation:

git --version

2.3 LinuxsysteminstallationGit

in Linuxsystem on , 你可以usingpackagemanagement器来installationGit.

Debian/Ubuntusystem

sudo apt update
sudo apt install git

Fedorasystem

sudo dnf install git

CentOS/RHELsystem

sudo yum install git

installationcompletion after , 输入以 under commandsverificationinstallation:

git --version

3. Gitconfiguration

installationcompletion after , 你需要 for Gitfor一些basicconfiguration, including设置user名 and 邮箱, 这些information会出现 in 你 submitting记录in.

3.1 basicconfiguration

using以 under commands设置你 user名 and 邮箱:

git config --global user.name "你 user名"
git config --global user.email "你 邮箱@example.com"

提示

这里 user名 and 邮箱应该 is 你 in GitHub, GitLab or otherGit平台 on using user名 and 邮箱, 这样你 submitting记录才能正确关联 to 你 账号.

3.2 查看configuration

using以 under commands可以查看Git configurationinformation:

git config --list

也可以查看specific configuration项:

git config user.name
git config user.email

3.3 configuration文本编辑器

Git默认usingsystem 默认编辑器, 你也可以configuration for 你喜欢 编辑器, 例such as:

git config --global core.editor "code --wait"  # usingVS Code
git config --global core.editor "nano"  # usingnano
git config --global core.editor "vim"  # usingvim

3.4 configurationdiff比较tool

你还可以configurationGitusing diff比较tool, 例such as:

git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"

4. GitworkflowIntroduction

in 开始usingGit之 before , Understandbasic Gitworkflow is 很 has helping . Git basicworkflow程including以 under 几个步骤:

  1. 初始化仓library: in projectTable of Contentsin初始化一个 new Git仓library, or 者 from 远程仓libraryclone一个现 has 仓library
  2. modifyfile: in 工作区inmodifyfile
  3. stagemodify: 将modify file添加 to stage区
  4. submittingmodify: 将stage区 modifysubmitting to 本地仓library
  5. pushmodify: 将本地仓library submittingpush to 远程仓library (such as果using远程仓library)

这个basicworkflow程将 in after 续 tutorialin详细介绍.

5. 实践case: creation你 第一个Git仓library

现 in , 让我们through一个 simple 实践case来Learningsuch as何usingGit:

  1. creation一个 new file夹, serving as你 Git仓library:
  2. mkdir my-first-git-repo
    cd my-first-git-repo
  3. 初始化Git仓library:
  4. git init

    执行这个commands after , Git会 in 当 before Table of Contentsincreation一个.gitTable of Contents, 这 is Git用来store仓library元data 地方.

  5. creation一个 new file:
  6. echo "Hello, Git!" > README.md
  7. 查看Gitstatus:
  8. git status

    你会看 to README.mdfile处于"Untracked files"status, 说明Git还没 has 开始跟踪这个file.

  9. 将file添加 to stage区:
  10. git add README.md
  11. 再次查看Gitstatus:
  12. git status

    现 in README.mdfile处于"Changes to be submittingted"status, 说明它已经被添加 to stage区.

  13. submittingfile to 本地仓library:
  14. git submitting -m "Initial submitting: Add README.md"

    这里 -m选项 after 面跟着 is submittinginformation, 用于describes这次submitting in 容.

  15. 查看submittinghistory:
  16. git log

    你会看 to 刚刚creation submitting记录, package含submitting哈希, 作者, 日期 and submittinginformation.

提示

submittinginformation应该简洁明了, able to准确describes这次submitting in 容. good submittinginformation has 助于你 and team membersunderstandingproject 演变history.

实践练习

请按照以 under 步骤completion练习:

  1. in 你 电脑 on installationGit (such as果还没 has installation)
  2. configurationGit user名 and 邮箱
  3. creation一个 new Git仓library
  4. in 仓libraryincreation一个 simple HTMLfile, in 容such as under :
  5. <!DOCTYPE html>
    <html>
    <head>
        <title>My First Git Project</title>
    </head>
    <body>
        <h1>Hello, Git!</h1>
        <p>This is my first Git project.</p>
    </body>
    </html>
  6. 将HTMLfile添加 to stage区
  7. submittingfile to 本地仓library, submittinginformation for "Add index.html file"
  8. 查看submittinghistory, 确认submitting成功