Shell Shell
Home
Shell
Linux
Network
Git
Blog (opens new window)
GitHub (opens new window)
Home
Shell
Linux
Network
Git
Blog (opens new window)
GitHub (opens new window)
  • 玩转 Git 三剑客

    • 目录
    • Git 基础
      • 安装 Git
        • 使用安装包安装
        • 使用 Scoop 安装
        • 使用 winget 安装
      • 使用 Git 之前需要做的最小配置
      • 创建第一个仓库并配置 local 用户信息
      • 通过几次 commit 来认识工作区和暂存区
      • 给文件重命名的简便方法
      • 通过 git log 查看版本演变历史
      • gitk:通过图形界面工具来查看版本历史
      • 探密.git 目录
      • commit、tree 和 blob 三个对象之间的关系
      • 小练习:数一数 tree 的个数
      • 分离头指针情况下的注意事项
      • 进一步理解 HEAD 和 branch
  • git
  • 玩转 Git 三剑客
JaimeZeng
2022-03-16

Git 基础

提示

  • Git 官网:Git (git-scm.com) (opens new window)
  • Git 文档:Git - Book (git-scm.com) (opens new window)

# 安装 Git

文档:Git - 安装 Git (git-scm.com) (opens new window)

# 使用安装包安装

(Windows)打开 Git - Downloading Package (git-scm.com) (opens new window) 浏览器会自动下载 git 安装包。下载完成后双击安装包点击安装(安装过程中一直点击下一步即可)。

# 使用 Scoop 安装

# 打开 PowerShell 远程权限

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
1

# 自定义 Scoop 和 App 安装目录

Scoop 默认将 Scoop 和 App 安装在 $HOME/scoop 目录下。

$env:SCOOP='D:\Applications\Scoop'
[Environment]::SetEnvironmentVariable('SCOOP', $env:SCOOP, 'User')
1
2

# 安装 Scoop

Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')

# or shorter
iwr -useb get.scoop.sh | iex
1
2
3
4

# 安装 git

scoop install git
1

# 使用 winget 安装

  1. 打开 Releases · microsoft/winget-cli (github.com) (opens new window) 下载 winget 安装包安装 winget。
  2. 使用 winget install --id Git.Git 安装 git。

# 使用 Git 之前需要做的最小配置

  1. 设置你的用户名和邮件地址

    $ git config --global user.name "Jaime"
    $ git config --global user.email Jaime@zxj.guru
    
    1
    2
  2. 配置作用域:

    • --local:只对当前目录下的仓库有效
    • --global:对当前用户下所有仓库有效
    • --system:对系统所有登录用户有效
  3. 查看配置:git config —list [--local | --global | --system]

$ git config -l --global # 查看用户配置
includeif.gitdir:E:/Github/JaimeZeng/.path=~/.gitconfig-JaimeZeng
includeif.gitdir:E:/Github/Ryanjiena/.path=~/.gitconfig-Ryanjiena
credential.helper=manager-core
$ git config -l --local # 查看本地配置
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=git@gitcode.net:qq_365/note-assets.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/master
user.email=43134606+Ryanjiena@users.noreply.github.com
user.name=Ryanjiena
$ git config -l --system # 查看系统配置
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
diff.astextplain.textconv=astextplain
rebase.autosquash=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
credential.helper=helper-selector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 创建第一个仓库并配置 local 用户信息

  1. 创建 Git 仓库

    • 把已有的项目代码纳入 git 管理:很早之前本地生成了一个 git 的仓库,开发了一段时间,想把这个 git 仓库提交到公司 git 服务器新建的 project 中。

      $ cd project-directory # 进入到项目文件夹内
      $ git remote add origin git@your_git_server:your_group/your_project.git # 关联远程仓库
      $ git push -u origin --all # 推送所有分支
      $ git push -u origin --tags # 推送所有标签
      
      1
      2
      3
      4
    • 新建的项目直接使用 git 管理:本地有个项目代码写了一段时间了,但还没有用 git 管理起来,现在想用 git 在本地帮着记录变更的版本。

      $ cd project-directory # 进入到项目文件夹内
      $ git init # 初始化,会创建出 .git 目录
      $ git add . # 把项目文件加入到 git 的暂存区
      $ git commit -m "Initial commit" # 把项目文件加入到 git 的暂存区
      
      1
      2
      3
      4
  2. 配置 local 用户信息

    $ git config --local user.email "Raynjiena@gmail.com"
    $ git config --local user.name "zxja"
    
    1
    2
  3. 本地仓库配置中:local > global

➜ ~/VSCodeProject git init learning-git
Initialized empty Git repository in /home/jaime/VSCodeProject/learning-git/.git/
➜ ~/VSCodeProject cd learning-git
➜ learning-git main ✗ ls -la
drwxr-xr-x@ - jaime 15 Mar 13:19 .git
➜ learning-git main ✗ echo "## hello world" > README.md
➜ learning-git main ✗ git config --local user.email "Raynjiena@gmail.com"
➜ learning-git main ✗ git config --local user.name "zxja"
➜ learning-git main ✗ git config --list --global
user.email=43134606+Ryanjiena@users.noreply.github.com
user.name=Ryanjiena
init.defaultbranch=main
➜ learning-git main ✗ git config --list --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.email=Raynjiena@gmail.com
user.name=zxja
➜ learning-git main ✗ git add README.md
➜ learning-git main ✗ git commit -m "add README.md"
[main (root-commit) 89dc6ad] add README.md
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
➜ learning-git main ✓ git log --pretty="%h - %s(%ae)"
89dc6ad - add README.md(Raynjiena@gmail.com) # 这里邮箱为 local 里配置而非用户 global 里的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 通过几次 commit 来认识工作区和暂存区

  • add 将工作区添加进暂存区,跟踪新文件
    • git add -u:将文件的修改、文件的删除,添加到暂存区。
    • git add . | -A | --all:将文件的修改,文件的删除,文件的新建,添加到暂存区。
  • commit 提交更新
  • status 查看哪些文件处于什么状态
➜ learning-git main ✓ nvim working-add-staging.md
➜ learning-git main ✓ bat working-add-staging.md
zsh: command not found: bat
➜ learning-git main ✓ cat working-add-staging.md
## 通过几次 commit 来认识工作区和暂存区

- add 将工作区添加进暂存区
  - `git add -u`:将文件的修改、文件的删除,添加到暂存区。
  - `git add . | -A | --all`:将文件的修改,文件的删除,文件的新建,添加到暂存区。
- commit 提交

➜ learning-git main ✓ git add -u
➜ learning-git main ✓ git commit -m "add 05.working-add-staging"
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        working-add-staging.md

nothing added to commit but untracked files present (use "git add" to track)
➜ learning-git main ✓ git add --all
➜ learning-git main ✗ git commit -m "add 05.working-add-staging"
[main 3a387fc] add 05.working-add-staging
 1 file changed, 7 insertions(+)
 create mode 100644 working-add-staging.md
➜ learning-git main ✓ git log --pretty="%h - %s(%ae)"
3a387fc - add 05.working-add-staging(Raynjiena@gmail.com)
89dc6ad - add README.md(Raynjiena@gmail.com)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 给文件重命名的简便方法

# 通过 git log 查看版本演变历史

# gitk:通过图形界面工具来查看版本历史

# 探密.git 目录

# commit、tree 和 blob 三个对象之间的关系

# 小练习:数一数 tree 的个数

# 分离头指针情况下的注意事项

# 进一步理解 HEAD 和 branch

目录

← 目录

Theme by Vdoing | Copyright © 2020-2022 JaimeZeng | ❤️ | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式