Git 基础
# 安装 Git
# 使用安装包安装
(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
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
2
3
4
# 安装 git
scoop install git
1
# 使用 winget 安装
- 打开 Releases · microsoft/winget-cli (github.com) (opens new window) 下载 winget 安装包安装 winget。
- 使用
winget install --id Git.Git安装 git。
# 使用 Git 之前需要做的最小配置
设置你的用户名和邮件地址
$ git config --global user.name "Jaime" $ git config --global user.email Jaime@zxj.guru1
2配置作用域:
- --local:只对当前目录下的仓库有效
- --global:对当前用户下所有仓库有效
- --system:对系统所有登录用户有效
查看配置:
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
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 用户信息
创建 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
配置 local 用户信息
$ git config --local user.email "Raynjiena@gmail.com" $ git config --local user.name "zxja"1
2本地仓库配置中: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
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
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