git 一个分布式版本控制系统,由Linus Torvalds(芬兰计算机科学家,linux之父呀)在2005创建,最初是为了帮助管理 Linux 内核的开发而设计

1. git基本工作流:

  • 克隆(Clone): 从远程仓库复制项目到本地,创建一个本地仓库的副本。

  • 添加(Add): 将修改后的文件添加到暂存区,准备提交。

  • 提交(Commit): 将暂存区的更改永久保存到本地仓库,并附上一条描述性的提交信息。

  • 推送(Push): 将本地仓库的提交推送到远程仓库,更新远程仓库的内容。

  • 拉取(Pull): 从远程仓库获取最新的更改,与本地仓库同步。

  • 分支管理(Branching): 创建、切换、合并和删除分支,以支持并行开发和功能独立性。

2. Git 的命令行操作

以下是一些常用的 Git 命令:

  • git clone <remote_repository_url>:克隆远程仓库到本地。
  • git add <file_path>:将指定文件添加到暂存区。
  • git commit -m "commit_message":提交暂存区的更改到本地仓库。
  • git push origin <branch_name>:将本地仓库的提交推送到远程仓库。
  • git pull origin <branch_name>:从远程仓库拉取最新更改到本地。
  • git branch:查看当前所有分支。
  • git checkout <branch_name>:切换到指定分支。
  • git merge <branch_name>:合并指定分支到当前分支。

3. 示例流程

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
➜  ~ mkdir beizi_blog
➜ ~ cd beizi_blog/
➜ ~/beizi_blog git init
已初始化空的 Git 仓库于 /Users/bz/beizi_blog/.git/
# 这会在当前目录下创建一个隐藏的 .git 文件夹,用来存储 Git 的版本控制相关信息。

➜ ~/beizi_blog git remote -v
beizi_blog git@github.com:beiziya/beizi_blog.git (fetch)
beizi_blog git@github.com:beiziya/beizi_blog.git (push)
origin git@github.com:beiziya/beizi_blog.git (fetch)
origin git@github.com:beiziya/beizi_blog.git (push)
# 之前已经添加过远程仓库,又添加了一个

➜ ~/beizi_blog touch test.txt
➜ ~/beizi_blog echo "this is a test" >test.txt
➜ ~/beizi_blog cat test.txt
this is a test
➜ ~/beizi_blog git add . # 添加当前目录所有文件到暂存区

➜ ~/beizi_blog git commit -m "first file"
[main(根提交) c4b8e0c] first file
1 file changed, 1 insertion(+)
create mode 100644 test.txt
➜ ~/beizi_blog git push -u beizi_blog main # 推送到远程仓库
枚举对象中: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 218 字节 | 218.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0(来自 0 个包)
To github.com:beiziya/beizi_blog.git
* [new branch] main -> main
分支 'main' 设置为跟踪 'beizi_blog/main'

-u 参数只需在第一次推送分支时使用。之后,可以直接使用 git push

如果已经设置过关联,也可以手动设置本地分支与远程分支的关联,例如:

1
git branch --set-upstream-to=origin/main main

前提:远程有beizi_blog仓库,并设置好ssh key或 gpg key 我已经设置好了。以下设置步骤

设置ssh key

1
2
3
4
5
6
7
8
9
➜  ~/beizi_blog ls ~/.ssh
beiziya_id_rsa config id_rsa.pub known_hosts.old
beiziya_id_rsa.pub id_rsa known_hosts
# 可以看到有我的公私钥,可通过ssh-keygen生成
# -t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa
# Specifies the type of key to create. The possible values are
# “dsa”, “ecdsa”, “ecdsa-sk”, “ed25519”, “ed25519-sk”, or “rsa”.
#可以指定不同加密算法

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
34
35
➜  ~/beizi_blog ssh-keygen -t rsa -b 4096 -C "test keys" #测试生成密钥对到当前目录
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/bz/.ssh/id_rsa): beizi_blog_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in beizi_blog_rsa
Your public key has been saved in beizi_blog_rsa.pub
The key fingerprint is:
SHA256:yWreqJG5jxl6c+cRcfiP9zMyP1s0haTP6htro26YyP4 test keys
The key's randomart image is:
+---[RSA 4096]----+
| . |
| . o . |
| o . . . .|
| . = o .|
| S . o..|
| o . . o . ..|
| =.o..o. = .|
| .oO+o+..o=++. |
| ..=*=+Eooo+*+= |
+----[SHA256]-----+
➜ ~/beizi_blog ls
beizi_blog_rsa beizi_blog_rsa.pub test.txt

# 注:千万不能把密钥放到项目目录中,这只是测试


# 将ssh key添加到ssh代理
eval "$(ssh-agent -s)"
ssh-add ./beizi_blog_rsa

# 将.pub中的内容复制到剪贴板
pbcopy < ./beizi_blog_rsa.pub # macos

#粘贴到git帐户ssh keys中

如果想让不同的密钥对访问不同主机,需要建立ssh config 文件

1
2
3
4
5
6
7
8
9
10
11
12
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa

# GitLab
Host gitlab.com
HostName gitlab.com
User your_username
IdentityFile ~/.ssh/gitlab_rsa