git基础教程与常用操作

本文最后更新于 2024年8月24日 上午

git

思维导图

git 资源下载

git:https://mirrors.tuna.tsinghua.edu.cn/github-release/git-for-windows/git/LatestRelease/
gitextension: https://gitextensions.github.io/

常用操作

创建仓库:git init 拉取远程到本地:git clone 添加文件:git add
[filename] 添加所有文件:git add . 忽略文件:.gitignore
提交到本地仓库:git commite -m “commit message” 查看状态:git status
对比工具:git diff (使用 bc 代替)
配置全局邮箱与用户名

1
2
3
4
git config --global user.name "zhang.san"
git config --global user.email "zhang.san@xxx.com"
git config --globa color.ui true
git config --global init.defaultBranch main

生成密钥步骤: 在 Windows Terminal 中输入:
ssh-keygen -t rsa -C “zhang.san@xxx.com
一直回车过去,在系统默认路径 C:\Users.ssh
下生成 id_rsa.pub 和 id_rsa 文件

常见问题

Q: 带模块的仓库,下载出错如何继续?
A: 进入仓库,执行git submodule update --init --recursive,或者执行git fetch [url]
A: 使用 hash 切换分支 git checkout <hash>

配置文件说明

:PROPERTIES:
:CUSTOM_ID: 配置文件说明
:END:
仓库优先级最高,个人其次,使用用户级别配置文件 仓库级别路径:.git\config
用户级别路径:C:\Users<user-name>.gitconfig
系统级别路径:C:\Program Files\Git\etc\config

配置文件命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[user]
name = name
email = name@xxx.com
[color]
ui = true
[alias]
br = branch
ci = commit
co = checkout
type = cat-file -t
dump = cat-file -p
st = status
unstage= reset HEAD --
last = log -1 HEAD
visual = !gitk
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
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
git add file_name
git commit -m "some comment"
git status
git cnfig --list
git clone [url]
git push origin HEAD:refs/for/master
//str: hash author_name author_date comment
git log --pretty=format:"%h - %an, %ar : %s" --graph
git log --since=2.weeks
git push [remote-name] [branch-name]
git config --global alias.ci commit //alias
git config --global alias.last 'log -1 HEAD' //alias
git checkout -b branch_name //创建分支并切换过去,等价于下面两行
git branch branch_name
git checkout branch_name
//创建修复分支并合并
git checkout -b hotfix
//做一些修复工作
git add .
git commit -m "做了一些修复工作"
git checkout master
git merge hotfix
git branch -d hotfix //master 移动到 hotfix 后,hotfix 就要删除掉
git branch -f main HEAD~3
git cherry-pick <提交号>
git rebase --interactive
//拉取远程仓库并合并到本地,然后上传
git pull --rebase
git push
//当使用 reset 后,使用 reflog 查看丢失的 log,可以根据哈希值回到 reset 之前
git reflog
1
2
3
4
5
git config --global user.name "name"
git config --global user.email "name@xxx.com"
git config --global core.editor vim
[gui]
encoding = utf-8
1
2
3
# gitlab 使用的时候,记得设置 ssh key 到网站上
# 在本机输入:
ssh-keygen
1
2
3
4
5
# 代码带子模块
git clone --recursive [address]
# 已经下载仓库
git submodule init
git submodule update

git 多账户配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 为公司生成一对秘钥 ssh key
ssh-keygen -t rsa -C 'yourEmail@xx.com' -f ~/.ssh/gitlab-rsa
# 为 github 生成一对秘钥 ssh key
ssh-keygen -t rsa -C 'yourEmail2@xx.com' -f ~/.ssh/github-rsa
# 在~/.ssh 目录下新建名称为 config 的文件(无后缀名)。用于配置多个不同的 host 使用不同的 ssh key,内容如下:
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id-rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id-rsa
# 配置文件参数说明
# Host : Host 可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和 ssh 文件
# HostName : 要登录主机的主机名
# User : 登录名
# IdentityFile : 指明上面 User 对应的 identityFile 路径
# 按照上面的步骤分别往 gitlab 和 github 上添加生成的公钥 gitlab_id-rsa.pub 和 github_id-rsa.pub
# OK,大功告成,再次执行 git 命令验证是不是已经不需要再次验证权限了。
# 再次查看~/.ssh 目录下的文件,会有 gitlab_id-rsa、gitlab_id-rsa.pub 和 github_id-rsa、github_id-rsa.pub 四个文件

git基础教程与常用操作
https://blog.zimablue.fun/2024/08/19/git基础教程与常用操作/
作者
zimablue1996
发布于
2024年8月19日
许可协议