一些关于 Git 的使用笔记

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 文档&教程
# https://git-scm.com/
# https://github.com/
# https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository
# https://www.runoob.com/git/git-tutorial.html

# github cli 使用
# https://cli.github.com/manual/

# 使用 gh 登陆
gh auth login

# 创建新仓库,并推送至远程地址
echo "# your repo name" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/your-github-account-name/your-new-repo.git
git push -u origin main

# 已有 repo
git remote add origin https://github.com/your-github-account-name/your-new-repo.git
git branch -M main
git push -u origin main

# 在已有仓库创建新 branch
git checkout -b newBranch
git add *
git commit -m "init commit"
git push origin newBranch

# 查看已有分支
git branch -a

# 切换代码版本
git checkout "可以是tag、branch,也可以是当前分支下的 commit sha"

# 查看未提交的代码与原代码的差异
git diff

# 删除远程分支
git push origin -d remote_branch

# git 修改 commit
# https://segmentfault.com/a/1190000041122415
# https://juejin.cn/post/6844903806224826375
git rebase -i "你需要修改的 commit 的前一个 commit" (接着会跳出编辑器页面,把需要修改的 commit 的 "pick" 改成 "edit")
git commit --amend (还可以附带参数,如:--author="Author Name <[email protected]>")
# 如果你已经修改了 git config 中的用户名和邮箱,也可以使用:git commit --amend --reset-author --no-edit
git rebase --continue (移动到下个 commit 作为基准)
git push -f
......